Skip to content

Commit 8e2b07f

Browse files
committed
Update for v0.29.1-b
1 parent 7b3a228 commit 8e2b07f

16 files changed

+283
-151
lines changed

MMD.js/MMD_SA.js

Lines changed: 107 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// MMD for System Animator
2-
// (2024-10-31)
2+
// (2024-11-10)
33

44
var use_full_spectrum = true
55

@@ -15010,6 +15010,109 @@ this.visible = v;
1501015010
depth_effect: depth_effect,
1501115011

1501215012
init: async function () {
15013+
if (!transformers_worker) {
15014+
15015+
await new Promise((resolve)=>{
15016+
// MMD_SA_options._Wallpaper3D_status_ = '(🌐Loading Transformers.js...)';
15017+
transformers_worker = new Worker('js/transformers_worker.js', {type: 'module'});
15018+
15019+
transformers_worker.onmessage = (e)=>{
15020+
if (typeof e.data == 'string') {
15021+
if (e.data == 'OK') {
15022+
resolve();
15023+
}
15024+
else {
15025+
MMD_SA_options._Wallpaper3D_status_ = e.data;
15026+
}
15027+
}
15028+
else {
15029+
let img_raw = new Uint8ClampedArray(e.data.depth_rgba);
15030+
let image_data = new ImageData(e.data.depth_width, e.data.depth_height);
15031+
for (let i = 0, i_max = image_data.data.length/4; i < i_max; i++) {
15032+
image_data.data[i*4] = image_data.data[i*4+1] = image_data.data[i*4+2] = img_raw[i];
15033+
image_data.data[i*4+3] = 255;
15034+
}
15035+
//console.log(image_data);
15036+
/*
15037+
canvas_tex.depth_width = e.data.depth_width;
15038+
canvas_tex.depth_height = e.data.depth_height;
15039+
canvas_tex.getContext('2d').putImageData(image_data, 0,0);
15040+
this.mesh.material.map.needsUpdate = true;
15041+
*/
15042+
15043+
let ctx = canvas_tex.getContext('2d');
15044+
if (!e.data.upscaled_rgba) {
15045+
canvas_img.width = canvas_tex.width;
15046+
canvas_img.height = canvas_tex.height;
15047+
canvas_img.getContext('2d').drawImage(img, 0,0,canvas_tex.width,canvas_tex.height);
15048+
15049+
ctx.drawImage(canvas_img, 0,0);
15050+
}
15051+
else {
15052+
canvas_img.width = e.data.upscaled_width;
15053+
canvas_img.height = e.data.upscaled_height;
15054+
canvas_img.getContext('2d').putImageData(new ImageData(new Uint8ClampedArray(e.data.upscaled_rgba), e.data.upscaled_width,e.data.upscaled_height), 0,0);
15055+
System._browser.save_file(this.filename.replace(/\.\w+$/, '') + '_SR.png', canvas_img.toDataURL('image/png'), 'Data URL');
15056+
15057+
ctx.drawImage(canvas_img, 0,0,canvas_tex.width,canvas_tex.height);
15058+
15059+
canvas_img.width = canvas_tex.width;
15060+
canvas_img.height = canvas_tex.height;
15061+
canvas_img.getContext('2d').drawImage(canvas_tex, 0,0);
15062+
}
15063+
15064+
this.mesh.material.map.needsUpdate = true;
15065+
this.update_transform();
15066+
15067+
canvas_depth.width = e.data.depth_width;
15068+
canvas_depth.height = e.data.depth_height;
15069+
ctx = canvas_depth.getContext('2d');
15070+
ctx.putImageData(image_data, 0,0);
15071+
15072+
this.depth_map_ready = true;
15073+
this.mesh.visible = true;
15074+
this.update_mesh();
15075+
15076+
canvas_depth_effect.width = canvas_depth.width;
15077+
canvas_depth_effect.height = canvas_depth.height
15078+
ctx = canvas_depth_effect.getContext('2d');
15079+
ctx.filter = 'brightness(300%) invert(100%) brightness(75%)';
15080+
ctx.drawImage(canvas_depth, 0,0);
15081+
ctx.filter = 'none';
15082+
15083+
// CONV. STEP: move a component channel to alpha-channel
15084+
const idata = ctx.getImageData(0, 0, canvas_depth_effect.width, canvas_depth_effect.height);
15085+
const data32 = new Uint32Array(idata.data.buffer);
15086+
let i = 0, len = data32.length;
15087+
while(i < len) {
15088+
data32[i] = data32[i++] << 8; // shift blue channel into alpha (little-endian)
15089+
}
15090+
// update canvas
15091+
ctx.putImageData(idata, 0, 0);
15092+
15093+
depth_effect.needsUpdate = true;
15094+
15095+
System._browser.camera.display_floating = (MMD_SA_options.user_camera.display.floating_auto !== false);
15096+
15097+
e.data = e.data.depth_rgba = e.data.upscaled_rgba = img_raw = image_data = undefined;
15098+
15099+
this.busy = false;
15100+
15101+
if (resolve_loaded) {
15102+
resolve_loaded();
15103+
resolve_loaded = null;
15104+
}
15105+
15106+
if (!this.options.keeps_worker_thread) {
15107+
transformers_worker.terminate();
15108+
transformers_worker = null;
15109+
}
15110+
}
15111+
}
15112+
});
15113+
15114+
}
15115+
1501315116
if (this.mesh) return;
1501415117

1501515118
img = new Image();
@@ -15133,100 +15236,6 @@ if (!MMD_SA.THREEX.enabled) {
1513315236
if (MMD_SA.WebXR.ground_plane) MMD_SA.WebXR.ground_plane.visible = false;
1513415237
System._browser.camera.poseNet.ground_plane_visible = false;
1513515238
}
15136-
15137-
await new Promise((resolve)=>{
15138-
// MMD_SA_options._Wallpaper3D_status_ = '(🌐Loading Transformers.js...)';
15139-
transformers_worker = new Worker('js/transformers_worker.js', {type: 'module'});
15140-
15141-
transformers_worker.onmessage = (e)=>{
15142-
if (typeof e.data == 'string') {
15143-
if (e.data == 'OK') {
15144-
resolve();
15145-
}
15146-
else {
15147-
MMD_SA_options._Wallpaper3D_status_ = e.data;
15148-
}
15149-
}
15150-
else {
15151-
let img_raw = new Uint8ClampedArray(e.data.depth_rgba);
15152-
let image_data = new ImageData(e.data.depth_width, e.data.depth_height);
15153-
for (let i = 0, i_max = image_data.data.length/4; i < i_max; i++) {
15154-
image_data.data[i*4] = image_data.data[i*4+1] = image_data.data[i*4+2] = img_raw[i];
15155-
image_data.data[i*4+3] = 255;
15156-
}
15157-
//console.log(image_data);
15158-
/*
15159-
canvas_tex.depth_width = e.data.depth_width;
15160-
canvas_tex.depth_height = e.data.depth_height;
15161-
canvas_tex.getContext('2d').putImageData(image_data, 0,0);
15162-
this.mesh.material.map.needsUpdate = true;
15163-
*/
15164-
15165-
let ctx = canvas_tex.getContext('2d');
15166-
if (!e.data.upscaled_rgba) {
15167-
canvas_img.width = canvas_tex.width;
15168-
canvas_img.height = canvas_tex.height;
15169-
canvas_img.getContext('2d').drawImage(img, 0,0,canvas_tex.width,canvas_tex.height);
15170-
15171-
ctx.drawImage(canvas_img, 0,0);
15172-
}
15173-
else {
15174-
canvas_img.width = e.data.upscaled_width;
15175-
canvas_img.height = e.data.upscaled_height;
15176-
canvas_img.getContext('2d').putImageData(new ImageData(new Uint8ClampedArray(e.data.upscaled_rgba), e.data.upscaled_width,e.data.upscaled_height), 0,0);
15177-
System._browser.save_file(this.filename.replace(/\.\w+$/, '') + '_SR.png', canvas_img.toDataURL('image/png'), 'Data URL');
15178-
15179-
ctx.drawImage(canvas_img, 0,0,canvas_tex.width,canvas_tex.height);
15180-
15181-
canvas_img.width = canvas_tex.width;
15182-
canvas_img.height = canvas_tex.height;
15183-
canvas_img.getContext('2d').drawImage(canvas_tex, 0,0);
15184-
}
15185-
15186-
this.mesh.material.map.needsUpdate = true;
15187-
this.update_transform();
15188-
15189-
canvas_depth.width = e.data.depth_width;
15190-
canvas_depth.height = e.data.depth_height;
15191-
ctx = canvas_depth.getContext('2d');
15192-
ctx.putImageData(image_data, 0,0);
15193-
15194-
this.depth_map_ready = true;
15195-
this.mesh.visible = true;
15196-
this.update_mesh();
15197-
15198-
canvas_depth_effect.width = canvas_depth.width;
15199-
canvas_depth_effect.height = canvas_depth.height
15200-
ctx = canvas_depth_effect.getContext('2d');
15201-
ctx.filter = 'brightness(300%) invert(100%) brightness(75%)';
15202-
ctx.drawImage(canvas_depth, 0,0);
15203-
ctx.filter = 'none';
15204-
15205-
// CONV. STEP: move a component channel to alpha-channel
15206-
const idata = ctx.getImageData(0, 0, canvas_depth_effect.width, canvas_depth_effect.height);
15207-
const data32 = new Uint32Array(idata.data.buffer);
15208-
let i = 0, len = data32.length;
15209-
while(i < len) {
15210-
data32[i] = data32[i++] << 8; // shift blue channel into alpha (little-endian)
15211-
}
15212-
// update canvas
15213-
ctx.putImageData(idata, 0, 0);
15214-
15215-
depth_effect.needsUpdate = true;
15216-
15217-
System._browser.camera.display_floating = (MMD_SA_options.user_camera.display.floating_auto !== false);
15218-
15219-
e.data = e.data.depth_rgba = e.data.upscaled_rgba = img_raw = image_data = undefined;
15220-
15221-
this.busy = false;
15222-
15223-
if (resolve_loaded) {
15224-
resolve_loaded();
15225-
resolve_loaded = null;
15226-
}
15227-
}
15228-
}
15229-
});
1523015239
},
1523115240

1523215241
options_by_filename: {},
@@ -15258,9 +15267,11 @@ pos_x_offset_percent: 0,
1525815267
pos_y_offset_percent: 0,
1525915268
pos_z_offset_percent: 0,
1526015269

15270+
// general options
1526115271
depth_model: 'onnx-community/depth-anything-v2-small',
1526215272
SR_mode: 0,
1526315273
SR_model: 'Xenova/swin2SR-lightweight-x2-64',
15274+
keeps_worker_thread: false,
1526415275
};
1526515276

1526615277
const options_general = {};
@@ -15290,6 +15301,7 @@ switch (prop) {
1529015301
case 'depth_model':
1529115302
case 'SR_mode':
1529215303
case 'SR_model':
15304+
case 'keeps_worker_thread':
1529315305
options_general[prop] = value;
1529415306
return;
1529515307
}

0 commit comments

Comments
 (0)