Skip to content

armgit / 67.js

Public Imported from GitHub
0

3D engine that AI agents drive

Dirt pulls the layer below toward the dirt colour; tests for the wear shader and stratified occlusion rays (#708)

Nguyen Xuan And02326c7b28c
diff --git a/src/shaders.js b/src/shaders.js
index 1704e1da..78f4e735 100644
--- a/src/shaders.js
+++ b/src/shaders.js
@@ -721,8 +721,8 @@ ${featureCode("surfaceHook")} float alpha = surfaceAlpha(textureAlpha);
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));
float dirty = smoothstep(wearDirtShape.x - wearDirtShape.y, wearDirtShape.x + wearDirtShape.y, vertexWear.y * (0.7 + 0.6 * dirtBreak));
- // Dirt darkens and tints the layer below instead of replacing it, so paint and metal stay readable through it.
- color = mix(color, color * wearDirt.rgb * 4.0 + wearDirt.rgb * 0.5, dirty);
+ // Dirt darkens the layer below and pulls it toward the dirt colour, so paint and metal stay readable through it.
+ color = mix(color, mix(color * 0.35, wearDirt.rgb, 0.6), dirty);
metallic *= 1.0 - dirty;
roughness = mix(roughness, max(roughness, wearDirt.w), dirty);
coatKept *= 1.0 - dirty;
diff --git a/tests/material-wear.test.js b/tests/material-wear.test.js
index 7d92defc..1e7c5b8e 100644
--- a/tests/material-wear.test.js
+++ b/tests/material-wear.test.js
@@ -82,3 +82,13 @@ test('the shader carries the wear term only when the feature is on', () => {
assert.match(createFragmentShader(withWear), /#define USES_SURFACE_WEAR \(wearEnabled > 0\.5\)/);
assert.match(createFragmentShader(['vertexColors']), /#define USES_SURFACE_WEAR false/);
});
+
+test('the shader breaks the wear edge with noise, darkens the layer under dirt and keeps no clear coat on worn or dirty pixels', () => {
+ const shader = createFragmentShader(['vertexColors', 'surfaceWear']);
+ 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, /metallic \*= 1\.0 - dirty;/);
+ assert.match(shader, /coatKept \*= 1\.0 - dirty;/);
+ assert.match(shader, /uniform vec4 wearDirt;/);
+});
diff --git a/tests/mesh-masks.test.js b/tests/mesh-masks.test.js
index 5b2a97e8..68f68ee8 100644
--- a/tests/mesh-masks.test.js
+++ b/tests/mesh-masks.test.js
@@ -158,3 +158,23 @@ test('a vertex_color group gradient names the fix for an unknown group or a bad
assert.throws(run({ group: 'wear', colors: [[0, 0, 0]] }), /two colors/);
assert.throws(run({ group: 'wear', axis: 'y', colors: [[0, 0, 0], [1, 1, 1]] }), /group gradient fields/);
});
+
+test('stratified occlusion rays give the same bytes on every run, in whole shares of the rays, and 0 on an open face', () => {
+ const edit = { measure: 'occlusion', rays: 64, distance: 1, seed: 3 };
+ const first = measured(edit, grooved());
+ const second = measured(edit, grooved());
+ assert.ok(sameBytes(first.weights, second.weights));
+ for (const weight of first.weights) {
+ assert.ok(Math.abs(weight * 64 - Math.round(weight * 64)) < 1e-4, `${weight} is not a whole share of 64 rays`);
+ }
+ let topWeights = 0;
+ let topCount = 0;
+ for (let vertex = 0; vertex < first.mesh.vertexCount; vertex++) {
+ if (first.mesh.positions[vertex * 3 + 1] > 0.49) {
+ topWeights += first.weights[vertex];
+ topCount++;
+ }
+ }
+ assert.ok(topCount > 0);
+ assert.equal(topWeights, 0, 'the open top is not occluded');
+});