Official Codebase of 3DCodeVerse
Python
scene lib: one scene-light reader (shader.makeLightProbe) for smoke, fire field, cloud volume and ocean
gaoypeng74a402590360
diff --git a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/cloudvolume.js b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/cloudvolume.jsindex b4414e34..7bbb9e48 100644--- a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/cloudvolume.js+++ b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/cloudvolume.js@@ -15,7 +15,7 @@* reconstruction or external textures are required.*/import * as THREE from 'three';-import { makeShaderMaterial, keepOutOfDepthPasses } from './shader.js';+import { makeShaderMaterial, keepOutOfDepthPasses, makeLightProbe } from './shader.js';import { attachDisposal, snapshotResources } from './lifecycle.js';import { bakeFbm3, dataTexture3D, mulberry32, sampleGrid3 } from './noise.js';@@ -203,9 +203,7 @@ export function makeCloudVolume(opts = {}) {const sun = new THREE.Vector3(...direction).normalize();const sunTint = new THREE.Color(opts.sunColor ?? 0xfff3dc);const skyTint = new THREE.Color(opts.skyColor ?? 0x9eb9da);- const lightPosition = new THREE.Vector3(), lightTarget = new THREE.Vector3();- const skyLight = new THREE.Color(0, 0, 0);- const keyColor = new THREE.Color(0, 0, 0), scratch = new THREE.Color();+ const readLights = makeLightProbe();const material = makeShaderMaterial({name: 'CloudVolumeMaterial', transparent: true, depthWrite: false,side: THREE.BackSide, fog: false,@@ -279,33 +277,17 @@ export function makeCloudVolume(opts = {}) {mesh.onBeforeRender = (...args) => {beforeRender.apply(mesh, args);const camera = args[2], scene = args[1];- let key = null;- skyLight.setRGB(0, 0, 0);- scene.traverseVisible((light) => {- if (!light.isLight || !(light.intensity > 0)) return;- if (light.isDirectionalLight && (!key || light.intensity > key.intensity)) key = light;- if (light.isAmbientLight) skyLight.add(scratch.copy(light.color).multiplyScalar(light.intensity));- if (light.isHemisphereLight) {- skyLight.add(scratch.copy(light.color).multiplyScalar(light.intensity * .75));- skyLight.add(scratch.copy(light.groundColor).multiplyScalar(light.intensity * .25));- }- });- keyColor.setRGB(0, 0, 0);+ const { sun: key, sunDirection, ambient, sky, ground, environment } = readLights(scene);+ const uniforms = material.uniforms;+ uniforms.uSunColor.value.setRGB(0, 0, 0);if (key) {- keyColor.copy(key.color).multiply(sunTint).multiplyScalar(key.intensity / 5.4 * intensity);- if (!sunPinned) {- key.getWorldPosition(lightPosition);key.target.getWorldPosition(lightTarget);- sun.copy(lightPosition).sub(lightTarget).normalize();- }+ uniforms.uSunColor.value.copy(key.color).multiply(sunTint).multiplyScalar(key.intensity / 5.4 * intensity);+ if (!sunPinned) sun.copy(sunDirection);}// The scene environment is an independent source of diffuse sky light;// removing a directional light does not remove that authored environment.- if (scene.environment) {- const environmentLight = .35 * (scene.environmentIntensity ?? 1);- skyLight.r += environmentLight;skyLight.g += environmentLight;skyLight.b += environmentLight;- }- material.uniforms.uSunColor.value.copy(keyColor);- material.uniforms.uSkyColor.value.copy(skyTint).multiply(skyLight);+ const fill = (c) => ambient[c] + sky[c] * .75 + ground[c] * .25 + environment * .35;+ uniforms.uSkyColor.value.setRGB(fill('r'), fill('g'), fill('b')).multiply(skyTint);inverse.copy(mesh.matrixWorld).invert();camera.getWorldPosition(material.uniforms.uCameraLocal.value).applyMatrix4(inverse);material.uniforms.uSunLocal.value.copy(sun).transformDirection(inverse);diff --git a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/firefield.js b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/firefield.jsindex 655c2475..1eba7a91 100644--- a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/firefield.js+++ b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/firefield.js@@ -29,7 +29,7 @@*/import * as THREE from 'three';import { bakeFbm3, dataTexture3D, mulberry32, sampleGrid3 } from './noise.js';-import { makeShaderMaterial, keepOutOfDepthPasses, withRendererState } from './shader.js';+import { makeShaderMaterial, keepOutOfDepthPasses, makeLightProbe, withRendererState } from './shader.js';import { snapshotResources, attachDisposal } from './lifecycle.js';const TIERS = {@@ -374,7 +374,7 @@ export function makeFireField(opts = {}) {const material=fieldMaterial(uniforms,tier);material.userData.bloom=true;const volume=new THREE.Mesh(geometry,material);volume.name='SharedFlameSootVolume';volume.renderOrder=2;volume.visible=activeEmitters.length>0;keepOutOfDepthPasses(volume);group.add(volume);- const inverse=new THREE.Matrix4(),sunPosition=new THREE.Vector3(),sunTarget=new THREE.Vector3(),sunDirection=new THREE.Vector3(),ambientColor=new THREE.Color();+ const inverse=new THREE.Matrix4(),readLights=makeLightProbe();const guard=volume.onBeforeRender;volume.onBeforeRender=function(renderer,scene,camera,geo,mat,...rest){guard.call(this,renderer,scene,camera,geo,mat,...rest);@@ -385,14 +385,10 @@ export function makeFireField(opts = {}) {uniforms.uLocalToClip.value.copy(camera.projectionMatrix).multiply(camera.matrixWorldInverse).multiply(volume.matrixWorld);uniforms.uLocalToWorld.value.setFromMatrix4(volume.matrixWorld);renderer.getCurrentViewport(uniforms.uViewport.value);- uniforms.uSunColor.value.setRGB(0,0,0);uniforms.uAmbient.value.setRGB(0,0,0);let strongest=0;- scene.traverseVisible(o=>{- if(o.isDirectionalLight&&o.intensity>strongest){strongest=o.intensity;- o.getWorldPosition(sunPosition);o.target.getWorldPosition(sunTarget);sunDirection.copy(sunPosition).sub(sunTarget).normalize();- uniforms.uSunLocal.value.copy(sunDirection).transformDirection(inverse);- uniforms.uSunColor.value.copy(o.color).multiplyScalar(o.intensity*.20);- }else if(o.isAmbientLight||o.isHemisphereLight)uniforms.uAmbient.value.add(ambientColor.copy(o.color).multiplyScalar(o.intensity*.15));- });+ const lights=readLights(scene);uniforms.uSunColor.value.setRGB(0,0,0);+ if(lights.sun){uniforms.uSunLocal.value.copy(lights.sunDirection).transformDirection(inverse);+ uniforms.uSunColor.value.copy(lights.sun.color).multiplyScalar(lights.sun.intensity*.20);}+ uniforms.uAmbient.value.copy(lights.ambient).add(lights.sky).multiplyScalar(.15);if(scene.environment)uniforms.uAmbient.value.addScalar(.035);if(depth){if(renderer.capabilities.logarithmicDepthBuffer)throw new Error('makeFireField: occluder depth requires non-logarithmic depth');diff --git a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/ocean.js b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/ocean.jsindex 35558a6c..fc3021c7 100644--- a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/ocean.js+++ b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/ocean.js@@ -16,7 +16,7 @@import * as THREE from 'three';import { attachDisposal, snapshotResources } from './lifecycle.js';import { mulberry32 } from './noise.js';-import { GLSL_UTIL, planarCapture } from './shader.js';+import { GLSL_UTIL, makeLightProbe, planarCapture } from './shader.js';import { Reflector } from 'three/addons/objects/Reflector.js';const TAU = Math.PI * 2;@@ -459,10 +459,7 @@ export function makeOceanSurface(opts = {}) {geometry.boundingSphere = geometry.boundingBox.getBoundingSphere(new THREE.Sphere());const u = sea.material.uniforms,base = sea.onBeforeRender;- const lp = new THREE.Vector3(),- tp = new THREE.Vector3(),- ambientContribution = new THREE.Color(),- environmentRotation = new THREE.Matrix4();+ const readLights = makeLightProbe(), environmentRotation = new THREE.Matrix4();// One guarded colour-pass capture: never in override (GTAO/depth) passes or// nested captures, renderer state restored, rigid mirror frame under scale.planarCapture(sea, (renderer, scene, camera, toRigid) => {@@ -479,23 +476,12 @@ export function makeOceanSurface(opts = {}) {environmentRotation.makeRotationFromEuler(scene.environmentRotation).invert());}- let key = null;+ const { sun: key, sunDirection, ambient, sky } = readLights(scene);u.seaKey.value.setRGB(0, 0, 0);u.seaSunColor.value.setRGB(0, 0, 0);- u.seaAmbient.value.setRGB(0, 0, 0);- scene.traverseVisible((o) => {- if (o.isDirectionalLight && (!key || o.intensity > key.intensity)) key = o;- if (o.isHemisphereLight || o.isAmbientLight) {- ambientContribution.copy(o.color).multiplyScalar(o.intensity / Math.PI);- u.seaAmbient.value.add(ambientContribution);- }- });+ u.seaAmbient.value.copy(ambient).add(sky).multiplyScalar(1 / Math.PI);if (key) {- if (opts.sunDirection === undefined) {- key.getWorldPosition(lp);- key.target.getWorldPosition(tp);- u.seaSun.value.copy(lp.sub(tp).normalize());- }+ if (opts.sunDirection === undefined) u.seaSun.value.copy(sunDirection);u.seaSunColor.value.copy(key.color);u.seaKey.value.copy(key.color).multiplyScalar(key.intensity / Math.PI);}diff --git a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/shader.js b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/shader.jsindex 8c57debf..3188b161 100644--- a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/shader.js+++ b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/shader.js@@ -1434,3 +1434,48 @@ export function planarCapture(mesh, capture, {};return mesh;}++/**+ * A reader for the lights an effect shades itself with, reused per draw:+ * `const readLights = makeLightProbe(); const l = readLights(scene, near);`+ * Over the VISIBLE lights with intensity > 0 it returns (one reused object):+ * `sun` the strongest directional light and `sunDirection` the world unit+ * vector toward it (position minus target; +Y without a sun); `ambient`,+ * `sky` and `ground` the summed ambient colour and hemisphere sky/ground+ * colours, each times intensity (linear); `environment` the scene+ * environment's intensity (0 without one); and, when `near` (a world+ * Vector3) is given, `point` the point light with the most+ * intensity / distance² there. Each effect applies its own calibration.+ */+export function makeLightProbe() {+ const probe = {+ sun: null, sunDirection: new THREE.Vector3(0, 1, 0), point: null, environment: 0,+ ambient: new THREE.Color(), sky: new THREE.Color(), ground: new THREE.Color(),+ };+ const at = new THREE.Vector3(), target = new THREE.Vector3(), scratch = new THREE.Color();+ return (scene, near = null) => {+ let score = -1;+ probe.sun = probe.point = null;+ probe.ambient.setRGB(0, 0, 0); probe.sky.setRGB(0, 0, 0); probe.ground.setRGB(0, 0, 0);+ scene.traverseVisible((light) => {+ if (!light.isLight || !(light.intensity > 0)) return;+ const i = light.intensity;+ if (light.isDirectionalLight && (!probe.sun || i > probe.sun.intensity)) probe.sun = light;+ else if (light.isAmbientLight) probe.ambient.add(scratch.copy(light.color).multiplyScalar(i));+ else if (light.isHemisphereLight) {+ probe.sky.add(scratch.copy(light.color).multiplyScalar(i));+ probe.ground.add(scratch.copy(light.groundColor).multiplyScalar(i));+ } else if (near && light.isPointLight) {+ const s = i / Math.max(.01, light.getWorldPosition(at).distanceToSquared(near));+ if (s > score) { score = s; probe.point = light; }+ }+ });+ probe.sunDirection.set(0, 1, 0);+ if (probe.sun) {+ probe.sun.getWorldPosition(at); probe.sun.target.getWorldPosition(target);+ if (at.sub(target).lengthSq() > 1e-12) probe.sunDirection.copy(at).normalize();+ }+ probe.environment = scene.environment ? (scene.environmentIntensity ?? 1) : 0;+ return probe;+ };+}diff --git a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/smoke.js b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/smoke.jsindex faa8bc36..6f6f8c73 100644--- a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/smoke.js+++ b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/smoke.js@@ -13,7 +13,7 @@* Overlapping transparent objects use ordinary Three.js object sorting.*/import * as THREE from 'three';-import { makeShaderMaterial, keepOutOfDepthPasses } from './shader.js';+import { makeShaderMaterial, keepOutOfDepthPasses, makeLightProbe } from './shader.js';import { attachDisposal, snapshotResources } from './lifecycle.js';import { bakeFbm3, dataTexture3D, mulberry32, sampleGrid3 } from './noise.js';@@ -220,7 +220,7 @@ function makePlume(opts, steam) {mesh.name = opts.name ?? (steam ? 'Steam' : 'Smoke');keepOutOfDepthPasses(mesh);const before = mesh.onBeforeRender, inverse = new THREE.Matrix4();- const position = new THREE.Vector3(), target = new THREE.Vector3(), lightPosition = new THREE.Vector3();+ const position = new THREE.Vector3(), readLights = makeLightProbe();const u = material.uniforms;mesh.onBeforeRender = (...args) => {before.apply(mesh,args);@@ -229,30 +229,13 @@ function makePlume(opts, steam) {camera.getWorldPosition(u.uCameraLocal.value).applyMatrix4(inverse);u.uLocalToWorld.value.setFromMatrix4(mesh.matrixWorld);u.uLocalToClip.value.copy(camera.projectionMatrix).multiply(camera.matrixWorldInverse).multiply(mesh.matrixWorld);- mesh.getWorldPosition(position);- let sun = null, point = null, best = -1;- u.uAmbient.value.setRGB(0,0,0);u.uSunColor.value.setRGB(0,0,0);u.uPointColor.value.setRGB(0,0,0);- scene.traverseVisible(light => {- if (!light.isLight || !(light.intensity > 0)) return;- if (light.isDirectionalLight && (!sun || light.intensity > sun.intensity)) sun = light;- if (light.isPointLight) {- light.getWorldPosition(lightPosition);- const score = light.intensity / Math.max(.01,lightPosition.distanceToSquared(position));- if (score > best) { point = light;best = score; }- }- const amount = light.isAmbientLight ? light.intensity * .28 : light.isHemisphereLight ? light.intensity * .22 : 0;- u.uAmbient.value.r += light.color.r * amount;- u.uAmbient.value.g += light.color.g * amount;- u.uAmbient.value.b += light.color.b * amount;- });- if (scene.environment) {- const level = .12 * (scene.environmentIntensity ?? 1);- u.uAmbient.value.r += level;u.uAmbient.value.g += level;u.uAmbient.value.b += level;- }+ const { sun, point, ambient, sky, environment, sunDirection } = readLights(scene, mesh.getWorldPosition(position));+ u.uSunColor.value.setRGB(0,0,0);u.uPointColor.value.setRGB(0,0,0);+ const fill = (c) => ambient[c] * .28 + sky[c] * .22 + environment * .12;+ u.uAmbient.value.setRGB(fill('r'), fill('g'), fill('b'));if (sun) {- sun.getWorldPosition(lightPosition);sun.target.getWorldPosition(target);- u.uSunWorld.value.copy(lightPosition).sub(target).normalize();- u.uSunLocal.value.copy(u.uSunWorld.value).transformDirection(inverse);+ u.uSunWorld.value.copy(sunDirection);+ u.uSunLocal.value.copy(sunDirection).transformDirection(inverse);u.uSunColor.value.copy(sun.color).multiplyScalar(sun.intensity);}if (point) {