Official Codebase of 3DCodeVerse
Python
scene lib: one sunVector turns an azimuth/elevation into a direction, and env.js states the convention sunRig reads
gaoypeng7e7e1eb52f4a
diff --git a/harness/codeverse3d/languages/scene_threejs/starter/src/env.js b/harness/codeverse3d/languages/scene_threejs/starter/src/env.jsindex 426afc8d..7c93f02c 100644--- a/harness/codeverse3d/languages/scene_threejs/starter/src/env.js+++ b/harness/codeverse3d/languages/scene_threejs/starter/src/env.js@@ -16,7 +16,7 @@ import { makeOutskirts, roomShell, sunRig, worldShell } from './lib/environment.import { mulberry32 } from './lib/noise.js';import { ground } from './lib/terrain.js';-export const SUN_AZIMUTH_DEG = 60; // where the sun is (0 = +Z front, CCW from above); cameras on the sun side are front-lit+export const SUN_AZIMUTH_DEG = 60; // where the sun is, as sunRig reads it (0 = +X, 90 = +Z front); cameras on the sun side are front-litexport const SUN_ELEVATION_DEG = 38;export const MOOD = 'day'; // day | golden | night | overcast — the rig, the shell and the fog agree on itexport const GROUND_SIZE = 160; // ground plane extent (m); the outskirts start at its edgediff --git a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/clouds.js b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/clouds.jsindex 9e38b3d1..f719f7f7 100644--- a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/clouds.js+++ b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/clouds.js@@ -4,7 +4,7 @@import * as THREE from 'three';import { attachDisposal, snapshotResources } from './lifecycle.js';import { mulberry32, fbm2 } from './noise.js';-import { makeShaderMaterial, keepOutOfDepthPasses } from './shader.js';+import { makeShaderMaterial, keepOutOfDepthPasses, sunVector } from './shader.js';// Cumulus/cirrus mood presets, graded for THIS renderer: ACES filmic at// exposure 1.0, sRGB out, and nothing after that rescues a dim top or@@ -45,12 +45,8 @@ function lightVector(v) {const d = v.clone().normalize();if (d.y >= -0.02) return d;const el = Math.asin(-d.y); // how deep the sun is- const moonEl = Math.max(30, Math.min(55, 25 + el * 180 / Math.PI))- * Math.PI / 180;- const az = Math.atan2(-d.z, -d.x); // opposite azimuth- return new THREE.Vector3(- Math.cos(moonEl) * Math.cos(az), Math.sin(moonEl),- Math.cos(moonEl) * Math.sin(az));+ const az = Math.atan2(-d.z, -d.x) * 180 / Math.PI; // opposite azimuth+ return sunVector(az, Math.max(30, Math.min(55, 25 + el * 180 / Math.PI)));}/** Seeded billowy puff: RGB stores a shading normal, alpha optical coverage.diff --git a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/dapple.js b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/dapple.jsindex d0424f3f..276e29b6 100644--- a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/dapple.js+++ b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/dapple.js@@ -19,15 +19,11 @@*/import * as THREE from 'three';-import { patchStandard, composeRoughness } from './shader.js';+import { patchStandard, composeRoughness, sunVector } from './shader.js';// The day rig's sun (azimuth 35, elevation 48), matching water.js,// so a surface that was not told where the sun is agrees.-const _AZ = 35 * Math.PI / 180;-const _EL = 48 * Math.PI / 180;-const DAY_SUN = new THREE.Vector3(- Math.cos(_EL) * Math.cos(_AZ), Math.sin(_EL),- Math.cos(_EL) * Math.sin(_AZ)).normalize();+const DAY_SUN = sunVector(35, 48);// Half the sun's angular diameter, in radians (0.53 deg across). This is// the ONE number that sets how soft a sunfleck's edge is: the penumbra adiff --git a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/environment.js b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/environment.jsindex c59d0a1e..710551fe 100644--- a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/environment.js+++ b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/environment.js@@ -9,7 +9,7 @@import * as THREE from 'three';import { fbm2, mulberry32 } from './noise.js';-import { patchStandard } from './shader.js';+import { patchStandard, sunVector } from './shader.js';import { skyRadiance } from './sky.js';import { snapshotResources, attachDisposal } from './lifecycle.js';@@ -600,14 +600,11 @@ export function sunRig(opts = {}) {opts.zenith === undefined ? mood.zenith : opts.zenith);const horizon = new THREE.Color(opts.horizon === undefined ? mood.horizon : opts.horizon);- const toDir = (az, el) => new THREE.Vector3(- Math.cos(el) * Math.cos(az), Math.sin(el), Math.cos(el) * Math.sin(az));- const dir = toDir(azDeg * Math.PI / 180, elDeg * Math.PI / 180);+ const dir = sunVector(azDeg, elDeg);// A set sun lights nothing from under the ground: the moon rises// opposite it, 30-55 deg up (deeper sun, higher moon).const moonDir = belowHorizon- ? toDir((azDeg + 180) * Math.PI / 180,- Math.max(30, Math.min(55, 25 - elDeg)) * Math.PI / 180)+ ? sunVector(azDeg + 180, Math.max(30, Math.min(55, 25 - elDeg))): (night ? dir.clone() : null);const lightDir = belowHorizon ? moonDir.clone() : dir.clone();diff --git a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/place.js b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/place.jsindex 6f95a631..705e8e34 100644--- a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/place.js+++ b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/place.js@@ -8,6 +8,7 @@*/import * as THREE from 'three';+import { sunVector } from './shader.js';const _ray = new THREE.Raycaster();const _down = new THREE.Vector3(0, -1, 0);@@ -324,12 +325,6 @@ export function establishingShot(name, heroObj, opts = {}) {}return d;};- const eyeDir = (aDeg, eDeg) => {- const a = aDeg * DEG;- const e = eDeg * DEG;- return new THREE.Vector3(Math.cos(e) * Math.cos(a), Math.sin(e),- Math.cos(e) * Math.sin(a));- };// MOVE ON THE FIT SPHERE, NEVER BACKWARDS. `shot()` retreats from a// blocker, which is right for a street camera and wrong here: backing// away shrinks the hero the establishing shot exists to show. Look@@ -346,7 +341,7 @@ export function establishingShot(name, heroObj, opts = {}) {for (const [dAz, dEl] of arcs) {const a = dAz === 0 ? azDeg: awayFromSun(azDeg + dAz + 180, opts.sunAzDeg) - 180;- const dir = eyeDir(a, Math.min(70, elDeg + dEl));+ const dir = sunVector(a, Math.min(70, elDeg + dEl));const p = center.clone().addScaledVector(dir, fit(dir));liftAbove(p, targets, 1.6);const v = probeView(p, center, targets);diff --git a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/shader.js b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/shader.jsindex 83d3dc06..195ef480 100644--- a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/shader.js+++ b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/shader.js@@ -226,6 +226,19 @@ export function seedVec3(s, a, b, c, k) {.multiplyScalar(k);}+/**+ * The library's ONE azimuth/elevation -> direction rule: azimuth in degrees+ * from +X toward +Z (0 = +X, 90 = +Z), elevation in degrees above the+ * horizon. sunRig, makeSky, the moon, establishingShot's eye and every+ * default sun use it, so an angle means the same thing to all of them.+ */+export function sunVector(azDeg, elDeg) {+ const az = azDeg * Math.PI / 180;+ const el = elDeg * Math.PI / 180;+ return new THREE.Vector3(Math.cos(el) * Math.cos(az), Math.sin(el),+ Math.cos(el) * Math.sin(az));+}+/** Read a Vector3, an array or an {x,y,z} into a new Vector3. */export function readVec3(p, fx = 0, fy = 0, fz = 0) {if (!p) return new THREE.Vector3(fx, fy, fz);diff --git a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/sky.js b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/sky.jsindex f8472e7d..c9aa3868 100644--- a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/sky.js+++ b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/sky.js@@ -21,6 +21,7 @@import * as THREE from 'three';import { attachDisposal, snapshotResources } from './lifecycle.js';+import { sunVector } from './shader.js';import { Sky } from 'three/addons/objects/Sky.js';/**@@ -178,14 +179,8 @@ export function makeSky(scene, opts = {}) {} else if (opts.sunDir) {sunDir = opts.sunDir.clone().normalize();} else {- const el = (opts.elevationDeg === undefined ? 15- : opts.elevationDeg) *- Math.PI / 180;- const az = (opts.azimuthDeg === undefined ? 135 : opts.azimuthDeg) *- Math.PI / 180;- sunDir = new THREE.Vector3(- Math.cos(el) * Math.cos(az), Math.sin(el),- Math.cos(el) * Math.sin(az));+ sunDir = sunVector(opts.azimuthDeg === undefined ? 135 : opts.azimuthDeg,+ opts.elevationDeg === undefined ? 15 : opts.elevationDeg);}u.sunPosition.value.copy(sunDir);// r184 Sky ships value-noise clouds ON by default — grid artifactsdiff --git a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/water.js b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/water.jsindex ed4b4ac6..51ba30a5 100644--- a/harness/codeverse3d/languages/scene_threejs/starter/src/lib/water.js+++ b/harness/codeverse3d/languages/scene_threejs/starter/src/lib/water.js@@ -16,7 +16,7 @@import * as THREE from 'three';import { attachDisposal, snapshotResources } from './lifecycle.js';import { Water } from 'three/addons/objects/Water.js';-import { planarCapture } from './shader.js';+import { planarCapture, sunVector } from './shader.js';// Battery-graded freeze point: mid-phase, waves clearly formed.const FROZEN_TIME = 7.3;@@ -24,11 +24,7 @@ const FROZEN_TIME = 7.3;// Default sun matches the day rig in lib/environment.js (azimuth 35,// elevation 48, same axis convention), so a scene that forgot to pass// sunDir still gets specular broadly agreeing with the shipped light.-const _AZ = 35 * Math.PI / 180;-const _EL = 48 * Math.PI / 180;-const _DAY_SUN = new THREE.Vector3(- Math.cos(_EL) * Math.cos(_AZ), Math.sin(_EL),- Math.cos(_EL) * Math.sin(_AZ));+const _DAY_SUN = sunVector(35, 48);/**diff --git a/harness/tests/scene_runtime/lib/test_environment.py b/harness/tests/scene_runtime/lib/test_environment.pyindex e69ec8dc..47f47cf4 100644--- a/harness/tests/scene_runtime/lib/test_environment.py+++ b/harness/tests/scene_runtime/lib/test_environment.py@@ -124,6 +124,9 @@ out.set = { night: set.night, mood: set.mood, sunY: set.sunDir.y, moonY: moon.y,opposite: Math.atan2(moon.z, moon.x) * 180 / Math.PI, discName: set.sunDisc.name, discOnMoon: set.sunDisc.position.clone().normalize().dot(moon),lightDirIsMoon: set.lightDir.dot(moon) > 0.999, envZenith: sample(set, new THREE.Vector3(0, 1, 0)), envMoon: lum(sample(set, moon)),envAway: lum(sample(set, moon.clone().multiply(new THREE.Vector3(-1, 1, -1)))), sunIntensity: set.sun.intensity };+// The azimuth convention env.js's SUN_AZIMUTH_DEG comment states (0 = +X, 90 = +Z):+// the harness's camera fitter converts from it, so it must not drift.+out.axes = [0, 90].map((a) => sunRig({ azimuth: a, elevation: 0 }).sunDir.toArray().map((v) => Math.round(v * 1e6) / 1e6));console.log(JSON.stringify(out));"""@@ -135,6 +138,10 @@ def sun() -> dict:return _measure(_SUN)+def test_azimuth_zero_is_plus_x_and_ninety_is_plus_z(sun):+ assert sun["axes"] == [[1, 0, 0], [0, 0, 1]]++def test_the_env_texture_is_a_linear_half_float_equirect(sun):for mood in _MOODS:m = sun[mood]