Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add more debug tools to handle objects properties and materials #1

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,10 @@
"typescript": "^5.3.3"
},
"dependencies": {
"@tweakpane/core": "^2.0.4",
"@vscode/vsce": "^2.24.0",
"three": "^0.161.0",
"tweakpane": "^4.0.3"
"tweakpane": "^4.0.3",
"tweakpane-plugin-media": "^1.0.3"
}
}
8 changes: 7 additions & 1 deletion src/app/main.mts
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,14 @@ import { FBXLoader } from "three/examples/jsm/loaders/FBXLoader.js";
import { DRACOLoader } from "three/examples/jsm/loaders/DRACOLoader.js";

import { Pane } from "tweakpane";
import * as TweakpanePluginMedia from 'tweakpane-plugin-media'

import type { WebviewApi } from "vscode-webview";
import { APPLICATION_CONSTANTS, MODEL_TYPES } from "../constants.js";

//@ts-ignore
import addObjectDebug from "./utils/addObjectDebug.js";

// @ts-ignore
const vscode: WebviewApi<unknown> = acquireVsCodeApi();

Expand Down Expand Up @@ -50,7 +54,8 @@ gltfLoader.setDRACOLoader(dracoLoader);

const fbxLoader = new FBXLoader(loadingManager);

const pane = new Pane() as any;
const pane = new Pane();
pane.registerPlugin(TweakpanePluginMedia);

const environmentFolder = pane.addFolder({
title: "Environment",
Expand Down Expand Up @@ -193,6 +198,7 @@ window.addEventListener("message", async (event) => {
model = gltfModel.scene;
scene.add(model);
addAnimationsToFolder({ model: gltfModel, type });
addObjectDebug(modelFolder, model);
}
overlay.style.opacity = "0";
overlay.style.pointerEvents = "none";
Expand Down
246 changes: 246 additions & 0 deletions src/app/utils/addMaterialDebug.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,246 @@
import {
AdditiveBlending,
BackSide,
CustomBlending,
DoubleSide,
FrontSide,
Material,
MultiplyBlending,
NoBlending,
NormalBlending,
SubtractiveBlending,
Texture,
} from 'three';
import { FolderApi } from '@tweakpane/core';

const materialParams = {
wireframe: { type: 'boolean' },
flatShading: { type: 'boolean' },
color: { type: 'color', color: { type: 'float' } },
emissive: { type: 'color', color: { type: 'float' } },
roughness: { type: 'number', min: 0, max: 1, step: 0.01 },
metalness: { type: 'number', min: 0, max: 1, step: 0.01 },
transmission: { type: 'number', min: 0, max: 1, step: 0.01 },
thickness: { type: 'number', min: 0, max: 1, step: 0.01 },
clearcoat: { type: 'number', min: 0, max: 1, step: 0.01 },
clearcoatRoughness: { type: 'number', min: 0, max: 1, step: 0.01 },
ior: { type: 'number', min: 0, max: 2.333, step: 0.01 },
reflectivity: { type: 'number', min: 0, max: 1, step: 0.01 },
opacity: { type: 'number', min: 0, max: 1, step: 0.01 },
transparent: { type: 'boolean' },
side: {
type: 'list',
options: [
{ value: FrontSide.toString(), text: 'FrontSide' },
{ value: BackSide.toString(), text: 'BackSide' },
{ value: DoubleSide.toString(), text: 'DoubleSide' },
],
},
blending: {
type: 'list',
options: [
{ value: NoBlending.toString(), text: 'NoBlending' },
{ value: NormalBlending.toString(), text: 'NormalBlending' },
{ value: AdditiveBlending.toString(), text: 'AdditiveBlending' },
{
value: SubtractiveBlending.toString(),
text: 'SubtractiveBlending',
},
{ value: MultiplyBlending.toString(), text: 'MultiplyBlending' },
{ value: CustomBlending.toString(), text: 'CustomBlending' },
],
},
bumpScale: {
type: 'number',
min: 0,
max: 1,
step: 0.01,
condition: 'bumpMap',
},
displacementScale: {
type: 'number',
min: 0,
max: 1,
condition: 'displacementMap',
},
displacementBias: {
type: 'number',
min: 0,
max: 1,
condition: 'displacementMap',
},

map: { type: 'image' },
metalnessMap: { type: 'image' },
normalMap: { type: 'image' },
bumpMap: { type: 'image' },
roughnessMap: { type: 'image' },
alphaMap: { type: 'image' },
envMap: { type: 'image' },
displacementMap: { type: 'image' },
matcap: { type: 'image' },
aoMap: { type: 'image' },
emissiveMap: { type: 'image' },
lightMap: { type: 'image' },
aoMapIntensity: { type: 'number', condition: 'aoMap', min: 0, max: 2 },
envMapIntensity: { type: 'number', condition: 'envMap', min: 0, max: 2 },
lightMapIntensity: { type: 'number', condition: 'lightMap' },
emissiveIntensity: {
type: 'number',
condition: 'emissiveMap',
min: 0,
max: 2,
},
uniforms: { type: 'object' },
};

/**
* Adds debugging functionality to a given 3D material within a folder interface.
* @param {FolderApi} folder - Tweakpane folder
* @param {Material} material - 3D material
* @param {{ title?: string, expanded?: boolean }} [options] - Options
*/
export default function addMaterialDebug(folder, material, options = {}) {
const materialKeys = Object.keys(materialParams);
const title = options.title || material?.name || material?.uuid || 'Unknown material';

const gui = folder.addFolder({
title,
expanded: options.expanded || false,
});

materialKeys.forEach((key) => {
const keyValue = material[key];
const materialOption = materialParams[key];

if (!(keyValue == null || materialOption.condition)) {
switch (materialOption.type) {
case 'image': {
const bindImage = (image) => {
gui
.addBinding({ image }, 'image', {
view: 'image',
label: key,
})
.on('change', ({ value }) => {
const previousValue = material[key];
const texture = new Texture(value);
texture.colorSpace = previousValue.colorSpace;
texture.flipY = previousValue.flipY;
material[key] = texture;
material[key].needsUpdate = true;
});
};
let image = keyValue.image;
if (keyValue.image instanceof ImageBitmap) {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
const scaleFactor = 0.1;
canvas.width = keyValue.image.width * scaleFactor;
canvas.height = keyValue.image.height * scaleFactor;
ctx.drawImage(keyValue.image, 0, 0, canvas.width, canvas.height);

canvas.toBlob((blob) => {
const bitmapImageElement = new Image();
bitmapImageElement.src = URL.createObjectURL(blob);
image = bitmapImageElement;
canvas.remove();

bindImage(image);
});
}

if (!image.src) {return;}
bindImage(image);
break;
}
case 'list': {
const options =
typeof materialOption.options[0] === 'object'
? materialOption.options
: materialOption.options.map((v) => ({
value: v.toString(),
text: v.toString(),
}));

gui
.addBlade({
view: 'list',
label: materialOption.name || key,
options,
value: keyValue.toString(),
})
.on('change', ({ value }) => {
material[key] = parseInt(value);
material.needsUpdate = true;
});
break;
}
case 'object': {
//TODO: refactor
const uniformsNames = Object.keys(keyValue);
const uniformsValues = Object.values(keyValue);
uniformsNames.forEach((uniformName, index) => {
const uniformValue = uniformsValues[index].value;
if (uniformValue.isTexture) {
const bindImage = (image) => {
gui
.addBinding({ image }, 'image', {
view: 'image',
label: uniformName,
})
.on('change', ({ value }) => {
const previousValue = uniformsValues[index].value;
const texture = new Texture(value);
texture.colorSpace = previousValue.colorSpace;
texture.flipY = previousValue.flipY;
uniformsValues[index].value = texture;
uniformsValues[index].value.needsUpdate = true;
});
};
let image = uniformValue.image;
if (uniformValue.image instanceof ImageBitmap) {
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
const scaleFactor = 0.1;
canvas.width = uniformValue.image.width * scaleFactor;
canvas.height = uniformValue.image.height * scaleFactor;
ctx.drawImage(uniformValue.image, 0, 0, canvas.width, canvas.height);

canvas.toBlob((blob) => {
const bitmapImageElement = new Image();
bitmapImageElement.src = URL.createObjectURL(blob);
image = bitmapImageElement;
canvas.remove();

bindImage(image);
});
}

if (!image.src) {return;}
bindImage(image);
} else {
gui.addBinding(keyValue[uniformName], 'value', {
label: uniformName,
});
}
});

break;
}
default: {
gui
.addBinding(material, key, {
...materialOption,
label: materialOption.name || key,
})
.on('change', () => {
material.needsUpdate = true;
});
break;
}
}
}
});
return gui;
}
Loading