Skip to content

Official Codebase of 3DCodeVerse

Python

scene lib: one ray-box and one volume depth in GLSL_UTIL (astraRayBox, astraClipDepth) for fire, fire field, smoke and clouds

gaoypeng5d2353315841
diff --git a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/cloudvolume.js b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/cloudvolume.js
index 7bbb9e48..f10c7505 100644
--- a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/cloudvolume.js
+++ b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/cloudvolume.js
@@ -116,12 +116,7 @@ uniform float uTime;
varying vec3 vLocal;
vec2 cloudBox(vec3 origin, vec3 direction) {
- vec3 safe = sign(direction) * max(abs(direction), vec3(1e-6));
- safe += vec3(equal(direction, vec3(0.0))) * 1e-6;
- vec3 a = (-uSize * .5 - origin) / safe;
- vec3 b = (uSize * .5 - origin) / safe;
- vec3 near = min(a, b), far = max(a, b);
- return vec2(max(max(near.x, near.y), near.z), min(min(far.x, far.y), far.z));
+ return astraRayBox(origin, direction, -uSize * .5, uSize * .5);
}
float cloudDensity(vec3 position) {
vec3 uv = position / uSize + .5;
@@ -263,8 +258,7 @@ export function makeCloudVolume(opts = {}) {
// The back face only bounds the integration ray. Test the visible
// density against opaque depth, not the often much farther proxy exit.
#ifndef USE_LOGDEPTHBUF
- vec4 densityClip = uLocalToClip * vec4(firstDensity, 1.0);
- gl_FragDepth = clamp(densityClip.z / densityClip.w * .5 + .5, 0.0, 1.0);
+ gl_FragDepth = astraClipDepth(uLocalToClip, firstDensity);
#endif
gl_FragColor = vec4(radiance / max(alpha, .001), alpha);
`,
diff --git a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/fire.js b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/fire.js
index db5407c6..acf0f08b 100644
--- a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/fire.js
+++ b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/fire.js
@@ -104,7 +104,7 @@ vec3 fireEmission(float heat) {
function fireMaterial({ radius, height, wind, intensity, candle, seed, steps, box }) {
const mat = makeShaderMaterial({
- name: 'FireVolume', util: false, fog: true,
+ name: 'FireVolume', fog: true,
uniforms: {
uRadius: { value: radius }, uHeight: { value: height },
uWind: { value: new THREE.Vector2(...wind) },
@@ -120,13 +120,8 @@ function fireMaterial({ radius, height, wind, intensity, candle, seed, steps, bo
fragmentMain: /* glsl */`
if (uIntensity <= 0.0) discard;
vec3 ray = normalize(vFireLocal - uCameraLocal);
- vec3 safeRay = mix(vec3(-1.0), vec3(1.0), step(vec3(0.0), ray))
- * max(abs(ray), vec3(0.000001));
- vec3 ta = (uBoxMin - uCameraLocal) / safeRay;
- vec3 tb = (uBoxMax - uCameraLocal) / safeRay;
- vec3 nearT = min(ta, tb), farT = max(ta, tb);
- float enter = max(0.0, max(nearT.x, max(nearT.y, nearT.z)));
- float leave = min(farT.x, min(farT.y, farT.z));
+ vec2 range = astraRayBox(uCameraLocal, ray, uBoxMin, uBoxMax);
+ float enter = max(0.0, range.x), leave = range.y;
if (leave <= enter) discard;
float ds = (leave - enter) / float(${steps});
// Static sub-pixel jitter avoids axial banding without temporal noise.
@@ -154,8 +149,7 @@ function fireMaterial({ radius, height, wind, intensity, candle, seed, steps, bo
// Depth from the luminous field, not its empty bounding cube. This also
// makes the volume visible to a camera inside its proxy geometry.
#ifndef USE_LOGDEPTHBUF
- vec4 clip = uLocalToClip * vec4(firstHit, 1.0);
- gl_FragDepth = clamp(clip.z / clip.w * 0.5 + 0.5, 0.0, 1.0);
+ gl_FragDepth = astraClipDepth(uLocalToClip, firstHit);
#endif
gl_FragColor = vec4(color / max(alpha, 0.001), alpha);
`,
diff --git a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/firefield.js b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/firefield.js
index 1eba7a91..542d2a1c 100644
--- a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/firefield.js
+++ b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/firefield.js
@@ -132,13 +132,7 @@ float fireSootCoarse(vec3 p) {
float n=texture(uFieldNoise,p*vec3(.32,.13,.32)/uCoarseScale-vec3(0.0,uTime*uRise*.13/uCoarseScale,0.0)+a.a*.31).r;
return a.b*pow(clamp(.68+(n-.5)*2.4,0.0,1.6),1.3);
}
-vec2 fireRange(vec3 origin,vec3 direction,vec3 boxMin,vec3 boxSize) {
- vec3 safe=mix(vec3(-1.0),vec3(1.0),step(vec3(0.0),direction))*max(abs(direction),vec3(1e-7));
- vec3 a=(boxMin-origin)/safe,b=(boxMin+boxSize-origin)/safe;
- vec3 near=min(a,b),far=max(a,b);
- return vec2(max(near.x,max(near.y,near.z)),min(far.x,min(far.y,far.z)));
-}
-vec2 fireBox(vec3 origin,vec3 direction){return fireRange(origin,direction,uBoxMin,uBoxSize);}
+vec2 fireBox(vec3 origin,vec3 direction){return astraRayBox(origin,direction,uBoxMin,uBoxMin+uBoxSize);}
vec3 fireRadiance(float heat) {
vec3 c=mix(vec3(1.8,.06,.002),vec3(4.0,.75,.04),smoothstep(.15,.58,heat));
return mix(c,vec3(6.0,2.8,.65),smoothstep(.55,.96,heat))*.72;
@@ -155,7 +149,7 @@ float fireSunTransmission(vec3 p,float jitter) {
`;
function fieldMaterial(uniforms, tier) {
- return makeShaderMaterial({name:'SharedFireSootVolume',util:false,fog:true,
+ return makeShaderMaterial({name:'SharedFireSootVolume',fog:true,
transparent:true,depthWrite:false,side:THREE.BackSide,uniforms,
defines:{FIELD_STEPS:tier.steps,FIELD_LIGHT_STEPS:tier.lightSteps},
varyings:'varying vec3 vFieldLocal;',vertexMain:'vFieldLocal=position;',fragmentHead:FIELD,
@@ -177,7 +171,7 @@ function fieldMaterial(uniforms, tier) {
}
if(leave<=enter||uIntensity<=0.0)discard;
float metric=length(uLocalToWorld*ray);
- vec2 hot=fireRange(origin,ray,uFlameBoxMin,uFlameBoxSize);
+ vec2 hot=astraRayBox(origin,ray,uFlameBoxMin,uFlameBoxMin+uFlameBoxSize);
hot=vec2(max(enter,hot.x),min(leave,hot.y));
int pre=0,post=0;
if(hot.y>hot.x){
@@ -213,8 +207,7 @@ function fieldMaterial(uniforms, tier) {
}
float alpha=1.0-transmittance;if(alpha<.005)discard;
#ifndef USE_LOGDEPTHBUF
- vec4 clip=uLocalToClip*vec4(first,1.0);
- gl_FragDepth=clamp(clip.z/clip.w*.5+.5,0.0,1.0);
+ gl_FragDepth=astraClipDepth(uLocalToClip,first);
#endif
gl_FragColor=vec4(radiance/max(alpha,.001),alpha);
`});
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 3188b161..83d3dc06 100644
--- a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/shader.js
+++ b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/shader.js
@@ -153,6 +153,24 @@ export const GLSL_UTIL = [
' return astraBumpSlope(eye, n, vec2(dFdx(h), dFdy(h)));',
'}',
'#endif',
+ // Bounded ray marches (fire, smoke, clouds). Entry and exit distances of
+ // origin + t * direction through an axis-aligned box; an axis-parallel
+ // ray keeps a finite slab. Miss when y <= x.
+ 'vec2 astraRayBox(vec3 origin, vec3 direction, vec3 boxMin, vec3 boxMax) {',
+ ' vec3 safe = mix(vec3(-1.0), vec3(1.0), step(vec3(0.0), direction))',
+ ' * max(abs(direction), vec3(1e-7));',
+ ' vec3 a = (boxMin - origin) / safe, b = (boxMax - origin) / safe;',
+ ' vec3 lo = min(a, b), hi = max(a, b);',
+ ' return vec2(max(lo.x, max(lo.y, lo.z)), min(hi.x, min(hi.y, hi.z)));',
+ '}',
+ // The depth a volume writes for its first visible density, so opaque
+ // geometry in front still hides it and its empty proxy box does not:
+ // `gl_FragDepth = astraClipDepth(uLocalToClip, firstHit);` (standard
+ // depth only — guard it with #ifndef USE_LOGDEPTHBUF).
+ 'float astraClipDepth(mat4 localToClip, vec3 p) {',
+ ' vec4 clip = localToClip * vec4(p, 1.0);',
+ ' return clamp(clip.z / clip.w * 0.5 + 0.5, 0.0, 1.0);',
+ '}',
// 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
// rib; fade by this.
diff --git a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/smoke.js b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/smoke.js
index 6f6f8c73..1b5c0409 100644
--- a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/smoke.js
+++ b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/smoke.js
@@ -58,11 +58,7 @@ uniform float uTime;
varying vec3 vPlumeLocal;
vec2 plumeBox(vec3 origin, vec3 direction) {
- vec3 safe = sign(direction) * max(abs(direction), vec3(1e-7));
- safe += vec3(equal(direction, vec3(0.0))) * 1e-7;
- vec3 a = (uBoundsMin - origin) / safe, b = (uBoundsMax - origin) / safe;
- vec3 lo = min(a,b), hi = max(a,b);
- return vec2(max(lo.x,max(lo.y,lo.z)), min(hi.x,min(hi.y,hi.z)));
+ return astraRayBox(origin, direction, uBoundsMin, uBoundsMax);
}
float plumeDensity(vec3 p) {
float y = p.y;
@@ -208,8 +204,7 @@ function makePlume(opts, steam) {
// The exit face is only a ray proxy, often behind nearby opaque objects.
// Depth-test the visible field instead of discarding its entire ray.
#ifndef USE_LOGDEPTHBUF
- vec4 densityClip=uLocalToClip*vec4(firstDensity,1.0);
- gl_FragDepth=clamp(densityClip.z/densityClip.w*.5+.5,0.0,1.0);
+ gl_FragDepth=astraClipDepth(uLocalToClip,firstDensity);
#endif
gl_FragColor=vec4(radiance/max(alpha,.001),alpha);
`,