Skip to content

Official Codebase of 3DCodeVerse

Python

scene lib: one guarded surface-gradient bump (GLSL_UTIL astraBump / astraBumpSlope); sand samples through terrain.triangleValue

gaoypeng0514a853c08e
diff --git a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/aging.js b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/aging.js
index d775fd60..78b47fc0 100644
--- a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/aging.js
+++ b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/aging.js
@@ -54,17 +54,6 @@ const AGE_HEAD = [
// Positive curvature is convex (swept clean), negative the concave
// lee that holds.
glslCurv('astraAgeCurv'),
- // Surface-gradient bump in view space. Existing normal/bump maps have
- // already run when normalBody calls this, so their detail is retained.
- 'vec3 astraAgeBump(vec3 eye, vec3 n, float height) {',
- ' vec3 dx = dFdx(eye), dy = dFdy(eye);',
- ' vec3 r1 = cross(dy, n), r2 = cross(n, dx);',
- ' float determinant = dot(dx, r1);',
- ' if (abs(determinant) < 1e-12) return n;',
- ' vec3 gradient = sign(determinant)',
- ' * (dFdx(height) * r1 + dFdy(height) * r2);',
- ' return normalize(abs(determinant) * n - gradient);',
- '}',
].join('\n');
// One base for all three, on the world varyings every library shares;
@@ -352,7 +341,7 @@ export function patchRust(material, opts = {}) {
].join('\n'),
roughnessBody: 'roughnessFactor = mix(roughnessFactor, max(roughnessFactor, mix(0.82, 0.97, rsB)), rsAmt);',
metalnessBody: 'metalnessFactor *= 1.0 - rsAmt;',
- normalBody: 'normal = astraAgeBump(-vViewPosition, normal, rsAmt * (0.00035 + rsFlake * 0.0004 * rsFlakeFade + rsGrain * 0.00015 * rsGrainFade));',
+ normalBody: 'normal = astraBump(-vViewPosition, normal, rsAmt * (0.00035 + rsFlake * 0.0004 * rsFlakeFade + rsGrain * 0.00015 * rsGrainFade));',
});
}
diff --git a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/damp.js b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/damp.js
index ca206bb0..9cb84131 100644
--- a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/damp.js
+++ b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/damp.js
@@ -34,23 +34,6 @@ import {
worldBase,
} from './shader.js';
-/** Perturb the lit view normal from a local coating height in world metres. */
-function coatingNormal(prefix, height) {
- return [
- `vec3 ${prefix}Dx = dFdx(-vViewPosition);`,
- `vec3 ${prefix}Dy = dFdy(-vViewPosition);`,
- `vec3 ${prefix}R1 = cross(${prefix}Dy, normal);`,
- `vec3 ${prefix}R2 = cross(normal, ${prefix}Dx);`,
- `float ${prefix}Det = dot(${prefix}Dx, ${prefix}R1);`,
- `float ${prefix}Height = ${height};`,
- `vec3 ${prefix}Gradient = sign(${prefix}Det) *`,
- ` (dFdx(${prefix}Height) * ${prefix}R1`,
- ` + dFdy(${prefix}Height) * ${prefix}R2);`,
- `if (abs(${prefix}Det) > 1e-12)`,
- ` normal = normalize(abs(${prefix}Det) * normal - ${prefix}Gradient);`,
- ].join('\n');
-}
-
// All three patches read these, so they are declared once, in the base.
// astraDamp*, not a neighbour's spelling: patchStandard THROWS when two
// chained patches give one function name two different bodies.
@@ -271,9 +254,9 @@ export function patchMoss(material, opts = {}) {
].join('\n'),
roughnessBody: 'roughnessFactor = mix(roughnessFactor, 0.97, clamp((msK + msLiK) * msOn, 0.0, 1.0));',
metalnessBody: 'metalnessFactor *= 1.0 - clamp((msK + msLiK) * msOn, 0.0, 1.0);',
- normalBody: coatingNormal('msBump',
- 'msOn * (msK * (0.001 + 0.003 * msG) + msLiK * 0.0008)'
- + ' * (1.0 - smoothstep(0.03, 0.10, length(fwidth(vAstraWorld))))'),
+ normalBody: 'normal = astraBump(-vViewPosition, normal, '
+ + 'msOn * (msK * (0.001 + 0.003 * msG) + msLiK * 0.0008)'
+ + ' * (1.0 - smoothstep(0.03, 0.10, length(fwidth(vAstraWorld)))));',
fragmentBody: [
' vec3 msN = normalize(vAstraWorldN);',
' vec3 msUp = normalize(uMossUp);',
@@ -559,9 +542,9 @@ export function patchCrackedMud(material, opts = {}) {
].join('\n'),
roughnessBody: 'roughnessFactor = mix(roughnessFactor, mix(0.95, 0.28, uMudWet * (0.32 + 0.68 * mdCrk)), mdAmt);',
metalnessBody: 'metalnessFactor *= 1.0 - mdAmt;',
- normalBody: coatingNormal('mdBump',
- 'mdAmt * uMudDepth * uMudScale * (mdLip * 0.025 - mdCrk * 0.010)'
- + ' * (1.0 - smoothstep(0.025, 0.12, length(fwidth(vAstraWorld))))'),
+ normalBody: 'normal = astraBump(-vViewPosition, normal, '
+ + 'mdAmt * uMudDepth * uMudScale * (mdLip * 0.025 - mdCrk * 0.010)'
+ + ' * (1.0 - smoothstep(0.025, 0.12, length(fwidth(vAstraWorld)))));',
fragmentBody: [
' vec3 mdN = normalize(vAstraWorldN);',
// A bed is level: the cells are read from world XZ, and a face
diff --git a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/paving.js b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/paving.js
index a796191c..ecb63008 100644
--- a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/paving.js
+++ b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/paving.js
@@ -95,12 +95,6 @@ function surfaceMaterial(opts,bedHeight) {
fragmentHead:`
varying vec3 vPaveLocal;
uniform float uPaveMoisture, uPaveBed;
- vec3 paveBump(vec3 eye,vec3 n,float h) {
- vec3 dx=dFdx(eye),dy=dFdy(eye),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(h)*a+dFdy(h)*b));
- }
`,
fragmentBody:`
vec2 paveP=vPaveLocal.xz+vPaveLocal.y*vec2(.47,.81);
@@ -118,7 +112,7 @@ function surfaceMaterial(opts,bedHeight) {
roughnessFactor=clamp(mix(roughnessFactor+paveGrain*.10,.30,uPaveMoisture)
+paveContact*.09,.08,1.0);
`,
- normalBody:'normal=paveBump(-vViewPosition,normal,paveGrain*.0009+paveFine*.00025);',
+ normalBody:'normal=astraBump(-vViewPosition,normal,paveGrain*.0009+paveFine*.00025);',
});
return material;
}
diff --git a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/rock.js b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/rock.js
index 02fbc6cb..976a711d 100644
--- a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/rock.js
+++ b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/rock.js
@@ -279,13 +279,8 @@ function rockMaterial(opts, type, height) {
diffuseColor.rgb *= 1.0 - rockWet * 0.36;
`,
roughnessBody: 'roughnessFactor = clamp(roughnessFactor + (rockMedium - 0.5) * 0.16 - rockWet * 0.48, 0.24, 0.98);',
- normalBody: `
- vec3 rockDx = dFdx(-vViewPosition), rockDy = dFdy(-vViewPosition);
- vec3 rockR1 = cross(rockDy, normal), rockR2 = cross(normal, rockDx);
- float rockDet = dot(rockDx, rockR1);
- normal = normalize(abs(rockDet) * normal - sign(rockDet) *
- (dot(rockGradient, dFdx(vRockLocal)) * rockR1 + dot(rockGradient, dFdy(vRockLocal)) * rockR2));
- `,
+ normalBody: `normal = astraBumpSlope(-vViewPosition, normal,
+ vec2(dot(rockGradient, dFdx(vRockLocal)), dot(rockGradient, dFdy(vRockLocal))));`,
});
return mat;
}
diff --git a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/sand.js b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/sand.js
index e8330bb7..36f93d85 100644
--- a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/sand.js
+++ b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/sand.js
@@ -6,6 +6,7 @@ import * as THREE from 'three';
import { attachDisposal, snapshotResources } from './lifecycle.js';
import { fbm2, mulberry32 } from './noise.js';
import { patchStandard } from './shader.js';
+import { triangleValue } from './terrain.js';
const TAU = Math.PI * 2;
const clamp = (x, lo, hi) => Math.max(lo, Math.min(hi, x));
@@ -53,13 +54,7 @@ function sandMaterial(opts, wind, seed) {
roughnessBody: `
roughnessFactor = clamp(roughnessFactor - sandGrainFade * smoothstep(0.93, 1.0, sandGrain) * 0.32, 0.48, 0.98);
`,
- normalBody: `
- vec3 sandDx = dFdx(-vViewPosition), sandDy = dFdy(-vViewPosition);
- vec3 sandR1 = cross(sandDy, normal), sandR2 = cross(normal, sandDx);
- float sandDet = dot(sandDx, sandR1);
- normal = normalize(abs(sandDet) * normal - sign(sandDet) *
- (sandRippleDH.x * sandR1 + sandRippleDH.y * sandR2) * uSandDetail);
- `,
+ normalBody: 'normal = astraBumpSlope(-vViewPosition, normal, sandRippleDH * uSandDetail);',
});
return mat;
}
@@ -117,15 +112,7 @@ export function makeSandTerrain(opts = {}) {
group.userData.placement = 'free';
group.add(surface);
- const sampleHeight = (x, z) => {
- const gx = clamp((x / width + 0.5) * nx, 0, nx);
- const gz = clamp((z / depth + 0.5) * nz, 0, nz);
- const ix = Math.min(nx - 1, Math.floor(gx)), iz = Math.min(nz - 1, Math.floor(gz));
- const fx = gx - ix, fz = gz - iz, a = iz * (nx + 1) + ix;
- const h00 = p.getY(a), h10 = p.getY(a + 1), h01 = p.getY(a + nx + 1), h11 = p.getY(a + nx + 2);
- return fx + fz <= 1 ? h00 + fx * (h10 - h00) + fz * (h01 - h00)
- : h11 + (1 - fx) * (h01 - h11) + (1 - fz) * (h10 - h11);
- };
+ const sampleHeight = (x, z) => triangleValue(p, nx, nz, (x / width + 0.5) * nx, (z / depth + 0.5) * nz, 1);
// Sparse saltating grains: CPU updates cost a few hundred points, not a
// second terrain shader. Their vertical trajectory follows the same mesh.
diff --git a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/shader.js b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/shader.js
index 5d1b6574..8c57debf 100644
--- a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/shader.js
+++ b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/shader.js
@@ -137,6 +137,21 @@ export const GLSL_UTIL = [
' float m = 1.0 - smoothstep(max(w - aa, 0.0), w + aa, g);',
' return m * (1.0 - smoothstep(w * 1.5, w * 5.0, aa));',
'}',
+ // Surface-gradient bump (Mikkelsen) in view space: perturb the lit
+ // normal n by a height h (astraBump), or by h's screen derivatives dh
+ // (astraBumpSlope, for an analytic gradient). Existing
+ // normal/bump maps have already run, so their detail is kept. A
+ // degenerate pixel footprint (det 0) returns n, never NaN.
+ 'vec3 astraBumpSlope(vec3 eye, vec3 n, vec2 dh) {',
+ ' vec3 dx = dFdx(eye), dy = dFdy(eye);',
+ ' vec3 r1 = cross(dy, n), r2 = cross(n, dx);',
+ ' float det = dot(dx, r1);',
+ ' if (abs(det) < 1e-12) return n;',
+ ' return normalize(abs(det) * n - sign(det) * (dh.x * r1 + dh.y * r2));',
+ '}',
+ 'vec3 astraBump(vec3 eye, vec3 n, float h) {',
+ ' return astraBumpSlope(eye, n, vec2(dFdx(h), dFdy(h)));',
+ '}',
'#endif',
// 0 edge-on, 1 facing. A double-sided shell piles its front and back
// into the same pixels at the silhouette and stacks into a bright
diff --git a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/terrain.js b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/terrain.js
index 14846add..fc800054 100644
--- a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/terrain.js
+++ b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/terrain.js
@@ -13,7 +13,7 @@ import { clonePatchedMaterial } from './shader.js';
// PlaneGeometry splits each cell from its lower-left to upper-right vertex.
// Interpolating the original noise field (or bilinearly interpolating four
// corners) differs from that triangle surface between vertices.
-function triangleValue(position, nx, ny, gx, gy, component) {
+export function triangleValue(position, nx, ny, gx, gy, component) {
gx = Math.max(0, Math.min(nx, gx));
gy = Math.max(0, Math.min(ny, gy));
const ix = Math.min(nx - 1, Math.floor(gx));
diff --git a/harness/tests/scene_runtime/lib/test_library.py b/harness/tests/scene_runtime/lib/test_library.py
index 6e8b4c6e..ed6a62bb 100644
--- a/harness/tests/scene_runtime/lib/test_library.py
+++ b/harness/tests/scene_runtime/lib/test_library.py
@@ -329,7 +329,7 @@ const {{ {call.split('(')[0]} }} = M;
let error = null;
try {{ {call}; }} catch (e) {{ error = e instanceof RangeError ? e.message : 'not a RangeError: ' + e; }}
console.log(JSON.stringify({{ error }}));
-""", (module,))
+""", tuple(MODULES))
assert out["error"] and phrase in out["error"], out