Skip to content

Official Codebase of 3DCodeVerse

Python

scene lib: every surface-gradient bump is shader.js astraBump, so the stream no longer shades NaN at a degenerate footprint

gaoypeng88f448e6427a
diff --git a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/accumulation.js b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/accumulation.js
index e5922806..d456f877 100644
--- a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/accumulation.js
+++ b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/accumulation.js
@@ -152,16 +152,7 @@ export function patchSnow(material, opts = {}) {
roughnessBody: 'roughnessFactor = mix(roughnessFactor, 0.86, snAmt);',
metalnessBody: 'metalnessFactor *= 1.0 - snAmt;',
normalBody: [
- 'vec3 snDx = dFdx(-vViewPosition);',
- 'vec3 snDy = dFdy(-vViewPosition);',
- 'vec3 snR1 = cross(snDy, normal);',
- 'vec3 snR2 = cross(normal, snDx);',
- 'float snDet = dot(snDx, snR1);',
- 'float snHeight = snAmt * snF * snFad * 0.0009;',
- 'vec3 snGradient = sign(snDet) *',
- ' (dFdx(snHeight) * snR1 + dFdy(snHeight) * snR2);',
- 'if (abs(snDet) > 1e-12)',
- ' normal = normalize(abs(snDet) * normal - snGradient);',
+ 'normal = astraBump(-vViewPosition, normal, snAmt * snF * snFad * 0.0009);',
].join('\n'),
fragmentBody: [
' vec3 snN = normalize(vAstraWorldN);',
diff --git a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/ice.js b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/ice.js
index a4277012..6c2daa87 100644
--- a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/ice.js
+++ b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/ice.js
@@ -134,14 +134,6 @@ function iceMaterial(opts, optical, maximumThickness) {
varying vec3 vIceLocal, vIceFace;
varying vec2 vIceUV;
varying float vIceEdge, vIcePath, vIceChip;
- vec3 iceSurfaceBump(vec3 eye, vec3 n, float height) {
- vec3 dx = dFdx(eye), dy = dFdy(eye);
- vec3 a = cross(dy, n), b = cross(n, dx);
- float determinant = dot(dx, a);
- if (abs(determinant) < 1e-12) return n;
- return normalize(abs(determinant) * n - sign(determinant)
- * (dFdx(height) * a + dFdy(height) * b));
- }
`,
fragmentBody: `
vec4 iceOptical = texture2D(uIceOptical, vIceUV);
@@ -166,7 +158,7 @@ function iceMaterial(opts, optical, maximumThickness) {
+ iceFine * 0.000025 + iceFracture * iceFaceGrain * 0.000015;
`,
roughnessBody: 'roughnessFactor = mix(roughnessFactor + iceFracture, 0.86, iceCover);',
- normalBody: 'normal = iceSurfaceBump(-vViewPosition, normal, iceHeight);',
+ normalBody: 'normal = astraBump(-vViewPosition, normal, iceHeight);',
transmissionBody: `
material.thickness = max(0.001, vIcePath);
material.transmission = transmission * (1.0 - iceCover * 0.96) * (1.0 - iceFracture);
diff --git a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/roadway.js b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/roadway.js
index e2e9d202..610e62fd 100644
--- a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/roadway.js
+++ b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/roadway.js
@@ -60,14 +60,6 @@ const ROAD_HEAD = [
' return 1.0 - smoothstep(0.30, 0.95,',
' length(fwidth(p)) / max(cycle, 1e-4));',
'}',
- 'vec3 astraRoadBump(vec3 eye, vec3 n, float height) {',
- ' vec3 dx = dFdx(eye), dy = dFdy(eye);',
- ' vec3 a = cross(dy, n), b = cross(n, dx);',
- ' float determinant = dot(dx, a);',
- ' if (abs(determinant) < 1e-12) return n;',
- ' return normalize(abs(determinant) * n - sign(determinant)',
- ' * (dFdx(height) * a + dFdy(height) * b));',
- '}',
].join('\n');
// `transformed` is still object-space after <begin_vertex>, so the
@@ -368,7 +360,7 @@ export function patchRoadSurface(material, opts = {}) {
' * (1.0 - roRut * 0.7) + roChip * 0.002 - roRut * 0.004',
' - roRim * 0.0008 + roSilt * 0.0008;',
].join('\n'),
- normalBody: 'normal = astraRoadBump(-vViewPosition, normal, roHeight);',
+ normalBody: 'normal = astraBump(-vViewPosition, normal, roHeight);',
roughnessBody: [
'roughnessFactor = mix(roughnessFactor, max(roughnessFactor, 0.94), uRoadAgg * 0.18);',
'roughnessFactor = mix(roughnessFactor, max(0.32, roughnessFactor * 0.78), roRut);',
@@ -485,7 +477,7 @@ export function patchSeamBand(material, opts = {}) {
' + (smC - 0.5) * 0.012 * smFc) - smJt * 0.004;',
].join('\n'),
roughnessBody: 'roughnessFactor = mix(roughnessFactor, max(roughnessFactor, 0.97), smK);',
- normalBody: 'normal = astraRoadBump(-vViewPosition, normal, smHeight);',
+ normalBody: 'normal = astraBump(-vViewPosition, normal, smHeight);',
});
}
@@ -618,6 +610,6 @@ export function patchTracks(material, opts = {}) {
' - tkTread * tkFd * 0.002 + tkRim * 0.006);',
].join('\n'),
roughnessBody: 'roughnessFactor = mix(roughnessFactor, max(0.12, roughnessFactor * 0.58), clamp(tkPress * uTrkDepth, 0.0, 1.0));',
- normalBody: 'normal = astraRoadBump(-vViewPosition, normal, tkHeight);',
+ normalBody: 'normal = astraBump(-vViewPosition, normal, tkHeight);',
});
}
diff --git a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/stream.js b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/stream.js
index 69e5763d..8974f296 100644
--- a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/stream.js
+++ b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/stream.js
@@ -392,7 +392,7 @@ export function makeStream(opts = {}) {
shader.fragmentShader = shader.fragmentShader
.replace(
'#include <common>',
- `#include <common>\nuniform mat3 normalMatrix; varying vec2 vStreamFlow; varying vec3 vStreamPosition; varying vec3 vStreamLongitudinal; varying vec3 vStreamAcross;\n${heightGLSL}\n${noiseGLSL}`
+ `#include <common>\nuniform mat3 normalMatrix; varying vec2 vStreamFlow; varying vec3 vStreamPosition; varying vec3 vStreamLongitudinal; varying vec3 vStreamAcross;\n${heightGLSL}\n#define ASTRA_FRAG\n${noiseGLSL}`
)
.replace(
'#include <color_fragment>',
@@ -424,11 +424,7 @@ export function makeStream(opts = {}) {
normal=normalize(normalMatrix*cross(vStreamLongitudinal+vec3(0,streamHs,0),vStreamAcross+vec3(0,streamHl,0)));
float microFilter=1.0-smoothstep(.035,.14,flowFootprint);
float streamMicro=(astraNoise2(advect*vec2(8.0,12.0))*.0012+astraNoise2(advect*vec2(19.0,24.0))*.0003)*microFilter;
- vec3 sx=dFdx(-vViewPosition),sy=dFdy(-vViewPosition);
- vec3 rx=cross(sy,normal),ry=cross(normal,sx);
- float det=dot(sx,rx);
- vec2 grad=vec2(dFdx(streamMicro),dFdy(streamMicro));
- normal=normalize(abs(det)*normal-sign(det)*(grad.x*rx+grad.y*ry));
+ normal=astraBump(-vViewPosition,normal,streamMicro);
`
)
.replace(
diff --git a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/terrain_shade.js b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/terrain_shade.js
index fba598f2..97704829 100644
--- a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/terrain_shade.js
+++ b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/terrain_shade.js
@@ -30,14 +30,6 @@ const FBM_UNIT = [
' }',
' return clamp(sum / max(weight, 0.25), 0.0, 1.0);',
'}',
- 'vec3 astraTerrainBump(vec3 eye, vec3 n, float height) {',
- ' vec3 dx = dFdx(eye), dy = dFdy(eye);',
- ' vec3 a = cross(dy, n), b = cross(n, dx);',
- ' float determinant = dot(dx, a);',
- ' if (abs(determinant) < 1e-12) return n;',
- ' return normalize(abs(determinant) * n - sign(determinant)',
- ' * (dFdx(height) * a + dFdy(height) * b));',
- '}',
'#endif',
].join('\n');
@@ -276,7 +268,7 @@ export function patchTriplanar(material, opts = {}) {
fragmentBody: TRI_BODY,
roughnessBody: 'roughnessFactor = clamp(uTriRoughness + (tpN - 0.5) * 0.10, 0.04, 1.0);',
metalnessBody: 'metalnessFactor = 0.0;',
- normalBody: 'normal = astraTerrainBump(-vViewPosition, normal, tpHeight);',
+ normalBody: 'normal = astraBump(-vViewPosition, normal, tpHeight);',
});
}
diff --git a/harness/tests/scene_runtime/lib/test_shader.py b/harness/tests/scene_runtime/lib/test_shader.py
index 3e1ca1ed..7e126eb2 100644
--- a/harness/tests/scene_runtime/lib/test_shader.py
+++ b/harness/tests/scene_runtime/lib/test_shader.py
@@ -461,3 +461,15 @@ def test_one_wind_is_read_the_same_way_by_every_effect(wind, spelling):
if spelling != "number": # windOf's own fallback direction is (1, 0.45)
assert w["windOf"] == pytest.approx(ref)
assert w["firefield"] == "ok" and math.isfinite(w["clouds"]) and w["clouds"] > 0
+
+
+def test_the_surface_gradient_bump_is_written_once():
+ """astraBump/astraBumpSlope return n at a degenerate footprint (det 0); five modules
+ kept their own copy after the helper landed, and stream.js's had no guard, so a
+ grazing pixel shaded NaN. Only tree.js's clamped variant and windows.js's
+ cotangent frame (a different job) build a screen-space frame of their own."""
+ import re
+ from _probe import LIB_DIR
+ frame = re.compile(r"cross\(\s*\w+\s*,\s*(normal|n)\s*\)")
+ own = sorted(p.name for p in LIB_DIR.glob("*.js") if frame.search(p.read_text(encoding="utf-8")))
+ assert own == ["shader.js", "tree.js", "windows.js"], own