|
| 1 | +/* |
| 2 | +Toolbox Aid |
| 3 | +David Quesenberry |
| 4 | +04/16/2026 |
| 5 | +LightingDemo3DScene.js |
| 6 | +*/ |
| 7 | +import { Scene } from '/src/engine/scene/index.js'; |
| 8 | +import { Theme, ThemeTokens } from '/src/engine/theme/index.js'; |
| 9 | +import { drawFrame, drawPanel } from '/src/engine/debug/index.js'; |
| 10 | +import { |
| 11 | + applyPhase16CameraMode, |
| 12 | + createPhase16ViewState, |
| 13 | + createProjectionViewport, |
| 14 | + drawDepthBackdrop, |
| 15 | + drawGroundGrid, |
| 16 | + drawPhase16DebugOverlay, |
| 17 | + drawWireBox, |
| 18 | + stepPhase16ViewToggles, |
| 19 | +} from '../shared/threeDWireframe.js'; |
| 20 | + |
| 21 | +const theme = new Theme(ThemeTokens); |
| 22 | + |
| 23 | +function clamp(value, min, max) { |
| 24 | + return Math.max(min, Math.min(max, value)); |
| 25 | +} |
| 26 | + |
| 27 | +function parseHexColor(color) { |
| 28 | + if (typeof color !== 'string' || !color.startsWith('#') || color.length !== 7) { |
| 29 | + return null; |
| 30 | + } |
| 31 | + const r = Number.parseInt(color.slice(1, 3), 16); |
| 32 | + const g = Number.parseInt(color.slice(3, 5), 16); |
| 33 | + const b = Number.parseInt(color.slice(5, 7), 16); |
| 34 | + if (Number.isNaN(r) || Number.isNaN(g) || Number.isNaN(b)) { |
| 35 | + return null; |
| 36 | + } |
| 37 | + return { r, g, b }; |
| 38 | +} |
| 39 | + |
| 40 | +function shadeColor(color, brightness) { |
| 41 | + const rgb = parseHexColor(color); |
| 42 | + if (!rgb) { |
| 43 | + return color; |
| 44 | + } |
| 45 | + const scale = clamp(brightness, 0.2, 1.2); |
| 46 | + const r = Math.round(clamp(rgb.r * scale, 0, 255)); |
| 47 | + const g = Math.round(clamp(rgb.g * scale, 0, 255)); |
| 48 | + const b = Math.round(clamp(rgb.b * scale, 0, 255)); |
| 49 | + return `rgb(${r}, ${g}, ${b})`; |
| 50 | +} |
| 51 | + |
| 52 | +export default class LightingDemo3DScene extends Scene { |
| 53 | + constructor() { |
| 54 | + super(); |
| 55 | + this.viewport = { |
| 56 | + x: 40, |
| 57 | + y: 170, |
| 58 | + width: 860, |
| 59 | + height: 320, |
| 60 | + focalLength: 460, |
| 61 | + }; |
| 62 | + this.viewState = createPhase16ViewState(); |
| 63 | + this.lightAngle = 0; |
| 64 | + this.lightHeight = 1.6; |
| 65 | + this.lightSpeed = 1.8; |
| 66 | + this.lightPulse = 0; |
| 67 | + this.lastInputLabel = 'idle'; |
| 68 | + this.cameraYaw = 0; |
| 69 | + this.cameraPitch = -0.36; |
| 70 | + this.cameraDistance = 16; |
| 71 | + this.cameraHeight = 7; |
| 72 | + |
| 73 | + this.cubes = [ |
| 74 | + { transform3D: { x: -6.8, y: -0.2, z: 8.0 }, size3D: { width: 3.2, height: 3.2, depth: 3.2 }, color: '#60a5fa' }, |
| 75 | + { transform3D: { x: -1.6, y: -0.2, z: 12.8 }, size3D: { width: 3.0, height: 4.2, depth: 3.0 }, color: '#f59e0b' }, |
| 76 | + { transform3D: { x: 3.0, y: -0.2, z: 9.8 }, size3D: { width: 4.0, height: 2.5, depth: 4.0 }, color: '#22c55e' }, |
| 77 | + { transform3D: { x: 6.6, y: -0.2, z: 15.0 }, size3D: { width: 2.6, height: 3.6, depth: 2.6 }, color: '#f472b6' }, |
| 78 | + ]; |
| 79 | + } |
| 80 | + |
| 81 | + setCamera3D(camera3D) { |
| 82 | + this.camera3D = camera3D; |
| 83 | + this.syncCamera(); |
| 84 | + } |
| 85 | + |
| 86 | + getLightPosition() { |
| 87 | + return { |
| 88 | + x: Math.cos(this.lightAngle) * 8.5, |
| 89 | + y: this.lightHeight + Math.sin(this.lightPulse) * 0.5 + 4.0, |
| 90 | + z: 11 + Math.sin(this.lightAngle) * 7.5, |
| 91 | + }; |
| 92 | + } |
| 93 | + |
| 94 | + syncCamera() { |
| 95 | + if (!this.camera3D) { |
| 96 | + return; |
| 97 | + } |
| 98 | + const focusPoint = { x: 0, y: 1.5, z: 12 }; |
| 99 | + const basePose = { |
| 100 | + position: { |
| 101 | + x: focusPoint.x + Math.sin(this.cameraYaw) * this.cameraDistance, |
| 102 | + y: this.cameraHeight + Math.sin(this.cameraPitch) * 1.4, |
| 103 | + z: focusPoint.z - Math.cos(this.cameraYaw) * this.cameraDistance, |
| 104 | + }, |
| 105 | + rotation: { |
| 106 | + x: this.cameraPitch, |
| 107 | + y: this.cameraYaw, |
| 108 | + z: 0, |
| 109 | + }, |
| 110 | + }; |
| 111 | + applyPhase16CameraMode(this.camera3D, this.viewState, basePose, focusPoint); |
| 112 | + } |
| 113 | + |
| 114 | + step3DPhysics(dt, engine) { |
| 115 | + const input = engine.input; |
| 116 | + stepPhase16ViewToggles(this.viewState, input); |
| 117 | + |
| 118 | + const left = input?.isDown('ArrowLeft') || input?.isDown('KeyA'); |
| 119 | + const right = input?.isDown('ArrowRight') || input?.isDown('KeyD'); |
| 120 | + const up = input?.isDown('ArrowUp') || input?.isDown('KeyW'); |
| 121 | + const down = input?.isDown('ArrowDown') || input?.isDown('KeyS'); |
| 122 | + |
| 123 | + if (left) this.lightAngle -= this.lightSpeed * dt; |
| 124 | + if (right) this.lightAngle += this.lightSpeed * dt; |
| 125 | + if (up) this.lightHeight = clamp(this.lightHeight + 3.4 * dt, 0.5, 4.8); |
| 126 | + if (down) this.lightHeight = clamp(this.lightHeight - 3.4 * dt, 0.5, 4.8); |
| 127 | + if (input?.isDown('KeyQ')) this.cameraYaw -= 1.1 * dt; |
| 128 | + if (input?.isDown('KeyE')) this.cameraYaw += 1.1 * dt; |
| 129 | + |
| 130 | + this.lastInputLabel = [left ? 'left' : '', right ? 'right' : '', up ? 'up' : '', down ? 'down' : ''] |
| 131 | + .filter(Boolean) |
| 132 | + .join('+') || 'idle'; |
| 133 | + |
| 134 | + this.lightPulse += dt * 1.8; |
| 135 | + this.syncCamera(); |
| 136 | + } |
| 137 | + |
| 138 | + getLightBrightness(target, lightPosition) { |
| 139 | + const center = { |
| 140 | + x: target.transform3D.x + target.size3D.width * 0.5, |
| 141 | + y: target.transform3D.y + target.size3D.height * 0.5, |
| 142 | + z: target.transform3D.z + target.size3D.depth * 0.5, |
| 143 | + }; |
| 144 | + const dx = center.x - lightPosition.x; |
| 145 | + const dy = center.y - lightPosition.y; |
| 146 | + const dz = center.z - lightPosition.z; |
| 147 | + const distance = Math.hypot(dx, dy, dz); |
| 148 | + const attenuation = clamp(1 - distance / 26, 0.2, 1); |
| 149 | + const verticalBias = clamp((lightPosition.y - center.y + 2.5) / 6, 0.35, 1.05); |
| 150 | + return attenuation * verticalBias; |
| 151 | + } |
| 152 | + |
| 153 | + render(renderer) { |
| 154 | + drawFrame(renderer, theme, [ |
| 155 | + 'Sample 1609 - 3D Lighting Demo', |
| 156 | + 'Demonstrates dynamic light position affecting wireframe shading.', |
| 157 | + 'Light angle: Left/Right | Light height: Up/Down | Orbit camera: Q/E | Camera: C | Debug: V', |
| 158 | + ]); |
| 159 | + |
| 160 | + renderer.strokeRect(this.viewport.x, this.viewport.y, this.viewport.width, this.viewport.height, '#d8d5ff', 2); |
| 161 | + drawDepthBackdrop(renderer, this.viewport); |
| 162 | + |
| 163 | + const cameraState = this.camera3D?.getState?.() ?? { |
| 164 | + position: { x: 0, y: 7, z: -4 }, |
| 165 | + rotation: { x: -0.36, y: 0, z: 0 }, |
| 166 | + }; |
| 167 | + const projectionViewport = createProjectionViewport(this.viewport); |
| 168 | + drawGroundGrid(renderer, { minX: -10, maxX: 10, minZ: 4, maxZ: 20, y: -0.2, step: 2 }, cameraState, projectionViewport); |
| 169 | + |
| 170 | + const lightPosition = this.getLightPosition(); |
| 171 | + const sortedCubes = [...this.cubes].sort((left, right) => right.transform3D.z - left.transform3D.z); |
| 172 | + sortedCubes.forEach((cube) => { |
| 173 | + const brightness = this.getLightBrightness(cube, lightPosition); |
| 174 | + drawWireBox( |
| 175 | + renderer, |
| 176 | + cube.transform3D, |
| 177 | + cube.size3D, |
| 178 | + cameraState, |
| 179 | + projectionViewport, |
| 180 | + shadeColor(cube.color, brightness + 0.25), |
| 181 | + { lineWidth: 2.1, depthCueEnabled: true } |
| 182 | + ); |
| 183 | + }); |
| 184 | + drawWireBox( |
| 185 | + renderer, |
| 186 | + { |
| 187 | + x: lightPosition.x - 0.35, |
| 188 | + y: lightPosition.y - 0.35, |
| 189 | + z: lightPosition.z - 0.35, |
| 190 | + }, |
| 191 | + { width: 0.7, height: 0.7, depth: 0.7 }, |
| 192 | + cameraState, |
| 193 | + projectionViewport, |
| 194 | + '#fef08a', |
| 195 | + { lineWidth: 2.5, depthCueEnabled: false } |
| 196 | + ); |
| 197 | + |
| 198 | + drawPanel(renderer, 620, 34, 300, 170, 'Lighting Runtime', [ |
| 199 | + `Light position: x=${lightPosition.x.toFixed(2)} y=${lightPosition.y.toFixed(2)} z=${lightPosition.z.toFixed(2)}`, |
| 200 | + `Light angle: ${this.lightAngle.toFixed(2)} rad`, |
| 201 | + `Light height offset: ${this.lightHeight.toFixed(2)}`, |
| 202 | + `Light input: ${this.lastInputLabel}`, |
| 203 | + `Camera yaw: ${this.cameraYaw.toFixed(2)}`, |
| 204 | + `Camera mode: ${this.viewState.cameraMode}`, |
| 205 | + ]); |
| 206 | + |
| 207 | + drawPhase16DebugOverlay(renderer, this.viewport, this.viewState, [ |
| 208 | + `Light pulse: ${Math.sin(this.lightPulse).toFixed(2)}`, |
| 209 | + 'Shading model: distance attenuation + vertical bias', |
| 210 | + ]); |
| 211 | + } |
| 212 | +} |
0 commit comments