Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Vary roughness and tint per wear chip (#708)

Nguyen Xuan Anf063da2fccd9
diff --git a/changes/708-masks.size.md b/changes/708-masks.size.md
index 2896e426..c74dc9a3 100644
--- a/changes/708-masks.size.md
+++ b/changes/708-masks.size.md
@@ -12,3 +12,4 @@
- scene:gltf-box+lazy: the per-pixel wear blend (vertex shader decode, fragment blend, uniforms and the batch binding) is in the renderer and its shaders, about 730 bytes gzip on every bundle that carries the renderer; a mesh with no UVs cannot show metal on its edges or dirt in its cavities any other way, and a scene without wear draws byte for byte as before
- goal:lit-box: the same renderer growth; the box scenes carry the renderer and cannot leave it out
- goal:gltf-box: the same renderer growth; the box scenes carry the renderer and cannot leave it out
+- entry:core: bare metal varies its roughness and tint per chip from the wear noise (two multiplies in the fragment blend, a few bytes gzip); a flat chip of mirror metal reads as plastic flecks, and a scene without wear draws byte for byte as before
diff --git a/src/shaders.js b/src/shaders.js
index 78f4e735..4b4de86a 100644
--- a/src/shaders.js
+++ b/src/shaders.js
@@ -714,9 +714,9 @@ ${featureCode("surfaceHook")} float alpha = surfaceAlpha(textureAlpha);
// Bare metal and dirt have no clear coat over them.
float wearBreak = 0.65 * wearNoise(worldPosition * 48.0) + 0.35 * wearNoise(worldPosition * 110.0 + vec3(17.0));
float worn = smoothstep(wearShape.y - wearShape.z, wearShape.y + wearShape.z, vertexWear.x * (0.55 + 0.9 * wearBreak));
- color = mix(color, wearMetal.rgb, worn);
+ color = mix(color, wearMetal.rgb * (0.8 + 0.4 * wearBreak), worn);
metallic = mix(metallic, wearMetal.w, worn);
- roughness = mix(roughness, wearShape.x, worn);
+ roughness = mix(roughness, wearShape.x * (0.75 + 0.5 * wearBreak), worn);
coatKept = 1.0 - worn;
if (wearShape.w > 0.5) {
float dirtBreak = 0.65 * wearNoise(worldPosition * 30.0 + vec3(5.0)) + 0.35 * wearNoise(worldPosition * 75.0 + vec3(31.0));
diff --git a/tests/material-wear.test.js b/tests/material-wear.test.js
index 1e7c5b8e..2def9379 100644
--- a/tests/material-wear.test.js
+++ b/tests/material-wear.test.js
@@ -88,6 +88,8 @@ test('the shader breaks the wear edge with noise, darkens the layer under dirt a
assert.match(shader, /float wearBreak = 0\.65 \* wearNoise\(worldPosition \* 48\.0\)/);
assert.match(shader, /smoothstep\(wearShape\.y - wearShape\.z, wearShape\.y \+ wearShape\.z, vertexWear\.x \* \(0\.55 \+ 0\.9 \* wearBreak\)\)/);
assert.match(shader, /color = mix\(color, mix\(color \* 0\.35, wearDirt\.rgb, 0\.6\), dirty\);/);
+ assert.match(shader, /color = mix\(color, wearMetal\.rgb \* \(0\.8 \+ 0\.4 \* wearBreak\), worn\);/);
+ assert.match(shader, /roughness = mix\(roughness, wearShape\.x \* \(0\.75 \+ 0\.5 \* wearBreak\), worn\);/);
assert.match(shader, /metallic \*= 1\.0 - dirty;/);
assert.match(shader, /coatKept \*= 1\.0 - dirty;/);
assert.match(shader, /uniform vec4 wearDirt;/);