3D engine that AI agents drive
JavaScript 3d3d-engineagentic-aiai-agentsblenderclaude-codegame-enginegaussian-splattinggltfjavascriptmcpmcp-servermodel-context-protocolpath-tracerpath-tracingpbrrenderingthreejs-alternativewebgl2webgpu
Use perceptual roughness for the MeshPhongMaterial shininess fit (#986)
Nguyen Xuan An9ac76845e157
diff --git a/docs/learn/if-you-know-three-js.md b/docs/learn/if-you-know-three-js.mdindex 3b31aa30..25d65118 100644--- a/docs/learn/if-you-know-three-js.md+++ b/docs/learn/if-you-know-three-js.md@@ -36,7 +36,7 @@ If you would rather write three.js-shaped code, the optional addon `packages/thr| --- | --- || `new THREE.MeshStandardMaterial({ color, metalness, roughness })` | `material: { color, metallic, roughness }`. `color` is three linear numbers from 0 to 1, not a hex number. || `new THREE.MeshLambertMaterial({ color })` | `material: { color, metallic: 0, roughness: 1 }`. |-| `new THREE.MeshPhongMaterial({ color, shininess })` | `material: { color, metallic: 0, roughness }`. The addon turns `shininess` into `roughness` (the square root of 2 divided by shininess plus 2) and ignores `specular`. |+| `new THREE.MeshPhongMaterial({ color, shininess })` | `material: { color, metallic: 0, roughness }`. The addon turns `shininess` into `roughness` (the fourth root of 2 divided by shininess plus 2, so the default shininess 30 gives 0.5) and ignores `specular`. || `emissive` and `emissiveIntensity` | `material.emission` and `material.emissiveColor`. || `map`, `normalMap` | `material.map` and `material.normalMap`, each `{ image, colorSpace }`. || `transparent` and `opacity` | `material.alphaMode` set to `'blend'`, and `material.opacity`. |diff --git a/packages/three-compat/index.mjs b/packages/three-compat/index.mjsindex 8c2f4c6b..eae95f7d 100644--- a/packages/three-compat/index.mjs+++ b/packages/three-compat/index.mjs@@ -464,12 +464,12 @@ export function createThreeCompat(core) {}}- // MeshPhongMaterial: shininess becomes roughness with the usual Blinn-Phong to GGX fit, roughness = sqrt(2 / (shininess + 2)).+ // MeshPhongMaterial: shininess becomes roughness with the usual Blinn-Phong to GGX fit, alpha = sqrt(2 / (shininess + 2)) and Crucible roughness is perceptual (alpha = roughness squared), so roughness = (2 / (shininess + 2)) ** 0.25.// The specular color has no Crucible field and is ignored.class MeshPhongMaterial extends MeshStandardMaterial {constructor(parameters = {}) {const shininess = parameters.shininess ?? 30;- super({ ...parameters, metalness: 0, roughness: Math.sqrt(2 / (shininess + 2)) });+ super({ ...parameters, metalness: 0, roughness: (2 / (shininess + 2)) ** 0.25 });this.shininess = shininess;}}diff --git a/tests/three-compat.test.js b/tests/three-compat.test.jsindex 6f3f96e1..f4800db5 100644--- a/tests/three-compat.test.js+++ b/tests/three-compat.test.js@@ -154,9 +154,9 @@ test('MeshLambertMaterial and MeshPhongMaterial build matte Crucible materials',assert.deepEqual(lambert.color, new THREE.Color(0xff0000).toArray());const defaultPhong = new THREE.MeshPhongMaterial({ color: 0x00ff00 }).toMaterial();assert.equal(defaultPhong.metallic, 0);- assert.ok(Math.abs(defaultPhong.roughness - Math.sqrt(2 / 32)) < 1e-9);+ assert.ok(Math.abs(defaultPhong.roughness - 0.5) < 1e-9);const shiny = new THREE.MeshPhongMaterial({ shininess: 198, specular: 0xffffff }).toMaterial();- assert.ok(Math.abs(shiny.roughness - 0.1) < 1e-9);+ assert.ok(Math.abs(shiny.roughness - 0.01 ** 0.25) < 1e-9);const mesh = new THREE.Mesh(new THREE.BoxGeometry(), new THREE.MeshPhongMaterial());assert.ok(mesh.material instanceof THREE.MeshStandardMaterial);assert.ok(!unsupportedNames.includes('MeshLambertMaterial') && !unsupportedNames.includes('MeshPhongMaterial'));