|
| 1 | +/* |
| 2 | +Toolbox Aid |
| 3 | +David Quesenberry |
| 4 | +04/16/2026 |
| 5 | +EntityComposition3DScene.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 rotateY(point, yaw) { |
| 24 | + const cos = Math.cos(yaw); |
| 25 | + const sin = Math.sin(yaw); |
| 26 | + return { |
| 27 | + x: point.x * cos - point.z * sin, |
| 28 | + y: point.y, |
| 29 | + z: point.x * sin + point.z * cos, |
| 30 | + }; |
| 31 | +} |
| 32 | + |
| 33 | +export default class EntityComposition3DScene extends Scene { |
| 34 | + constructor() { |
| 35 | + super(); |
| 36 | + this.viewState = createPhase16ViewState(); |
| 37 | + this.viewport = { |
| 38 | + x: 40, |
| 39 | + y: 170, |
| 40 | + width: 860, |
| 41 | + height: 320, |
| 42 | + focalLength: 460, |
| 43 | + }; |
| 44 | + this.root = { x: 0, y: 0, z: 13 }; |
| 45 | + this.assemblyYaw = 0.4; |
| 46 | + this.autoSpinSpeed = 0.45; |
| 47 | + this.partSpacing = 1; |
| 48 | + this.spinPaused = false; |
| 49 | + this.pauseLatch = false; |
| 50 | + this.parts = this.createParts(); |
| 51 | + } |
| 52 | + |
| 53 | + createParts() { |
| 54 | + return [ |
| 55 | + { name: 'core', offset: { x: 0, y: 0.9, z: 0 }, size: { width: 1.8, height: 1.5, depth: 1.8 }, color: '#38bdf8' }, |
| 56 | + { name: 'head', offset: { x: 0.2, y: 2.7, z: 0 }, size: { width: 1.2, height: 1.1, depth: 1.2 }, color: '#a78bfa' }, |
| 57 | + { name: 'arm-left', offset: { x: -1.7, y: 1.2, z: 0 }, size: { width: 0.65, height: 1.5, depth: 0.65 }, color: '#34d399' }, |
| 58 | + { name: 'arm-right', offset: { x: 2.2, y: 1.2, z: 0 }, size: { width: 0.65, height: 1.5, depth: 0.65 }, color: '#34d399' }, |
| 59 | + { name: 'pod-front', offset: { x: 0.25, y: 0.2, z: -1.55 }, size: { width: 1.3, height: 0.7, depth: 0.7 }, color: '#f59e0b' }, |
| 60 | + { name: 'leg-left', offset: { x: -0.65, y: -1.05, z: 0.1 }, size: { width: 0.65, height: 1.2, depth: 0.65 }, color: '#93c5fd' }, |
| 61 | + { name: 'leg-right', offset: { x: 1.1, y: -1.05, z: 0.1 }, size: { width: 0.65, height: 1.2, depth: 0.65 }, color: '#93c5fd' }, |
| 62 | + ]; |
| 63 | + } |
| 64 | + |
| 65 | + setCamera3D(camera3D) { |
| 66 | + this.camera3D = camera3D; |
| 67 | + this.syncCamera(); |
| 68 | + } |
| 69 | + |
| 70 | + syncCamera() { |
| 71 | + if (!this.camera3D) { |
| 72 | + return; |
| 73 | + } |
| 74 | + const focusPoint = { |
| 75 | + x: this.root.x + 0.4, |
| 76 | + y: this.root.y + 1.45, |
| 77 | + z: this.root.z + 0.2, |
| 78 | + }; |
| 79 | + const basePose = { |
| 80 | + position: { |
| 81 | + x: focusPoint.x + Math.sin(this.assemblyYaw) * 10.8, |
| 82 | + y: focusPoint.y + 6.5, |
| 83 | + z: focusPoint.z - Math.cos(this.assemblyYaw) * 10.8, |
| 84 | + }, |
| 85 | + rotation: { |
| 86 | + x: -0.52, |
| 87 | + y: this.assemblyYaw, |
| 88 | + z: 0, |
| 89 | + }, |
| 90 | + }; |
| 91 | + applyPhase16CameraMode(this.camera3D, this.viewState, basePose, focusPoint); |
| 92 | + } |
| 93 | + |
| 94 | + step3DPhysics(dt, engine) { |
| 95 | + const input = engine.input; |
| 96 | + stepPhase16ViewToggles(this.viewState, input); |
| 97 | + |
| 98 | + if (input?.isDown('KeyQ')) this.assemblyYaw -= 0.95 * dt; |
| 99 | + if (input?.isDown('KeyE')) this.assemblyYaw += 0.95 * dt; |
| 100 | + if (input?.isDown('ArrowUp')) this.partSpacing = Math.min(1.65, this.partSpacing + 0.95 * dt); |
| 101 | + if (input?.isDown('ArrowDown')) this.partSpacing = Math.max(0.7, this.partSpacing - 0.95 * dt); |
| 102 | + |
| 103 | + const pausePressed = input?.isDown('Space') === true; |
| 104 | + if (pausePressed && !this.pauseLatch) { |
| 105 | + this.spinPaused = !this.spinPaused; |
| 106 | + } |
| 107 | + this.pauseLatch = pausePressed; |
| 108 | + |
| 109 | + if (!this.spinPaused) { |
| 110 | + this.assemblyYaw += this.autoSpinSpeed * dt; |
| 111 | + } |
| 112 | + |
| 113 | + this.syncCamera(); |
| 114 | + } |
| 115 | + |
| 116 | + render(renderer) { |
| 117 | + drawFrame(renderer, theme, [ |
| 118 | + 'Sample 1615 - 3D Entity Composition', |
| 119 | + 'Builds one actor from modular parts with controllable spacing and orbit view.', |
| 120 | + 'Orbit: Q/E | Part spacing: Up/Down | Pause spin: Space | Camera: C | Debug: V', |
| 121 | + ]); |
| 122 | + |
| 123 | + renderer.strokeRect(this.viewport.x, this.viewport.y, this.viewport.width, this.viewport.height, '#d8d5ff', 2); |
| 124 | + drawDepthBackdrop(renderer, this.viewport); |
| 125 | + |
| 126 | + const cameraState = this.camera3D?.getState?.() ?? { |
| 127 | + position: { x: 7, y: 8, z: 1 }, |
| 128 | + rotation: { x: -0.52, y: this.assemblyYaw, z: 0 }, |
| 129 | + }; |
| 130 | + const projectionViewport = createProjectionViewport(this.viewport); |
| 131 | + drawGroundGrid(renderer, { minX: -9, maxX: 9, minZ: 7, maxZ: 21, y: -1.4, step: 2 }, cameraState, projectionViewport); |
| 132 | + |
| 133 | + for (let i = 0; i < this.parts.length; i += 1) { |
| 134 | + const part = this.parts[i]; |
| 135 | + const rotated = rotateY( |
| 136 | + { |
| 137 | + x: part.offset.x * this.partSpacing, |
| 138 | + y: part.offset.y, |
| 139 | + z: part.offset.z * this.partSpacing, |
| 140 | + }, |
| 141 | + this.assemblyYaw |
| 142 | + ); |
| 143 | + drawWireBox( |
| 144 | + renderer, |
| 145 | + { |
| 146 | + x: this.root.x + rotated.x, |
| 147 | + y: this.root.y + rotated.y, |
| 148 | + z: this.root.z + rotated.z, |
| 149 | + }, |
| 150 | + part.size, |
| 151 | + cameraState, |
| 152 | + projectionViewport, |
| 153 | + part.color, |
| 154 | + { lineWidth: 2, depthCueEnabled: true } |
| 155 | + ); |
| 156 | + } |
| 157 | + |
| 158 | + drawPanel(renderer, 620, 34, 300, 188, 'Composition Runtime', [ |
| 159 | + `Parts: ${this.parts.length}`, |
| 160 | + `Part spacing: ${this.partSpacing.toFixed(2)}`, |
| 161 | + `Assembly yaw: ${this.assemblyYaw.toFixed(2)}`, |
| 162 | + `Auto spin: ${this.spinPaused ? 'paused' : `${this.autoSpinSpeed.toFixed(2)} rad/s`}`, |
| 163 | + `Camera mode: ${this.viewState.cameraMode}`, |
| 164 | + 'Composition: root + head + limbs + pods', |
| 165 | + ]); |
| 166 | + |
| 167 | + drawPhase16DebugOverlay(renderer, this.viewport, this.viewState, [ |
| 168 | + 'Hierarchy demo: local offsets transformed around one root', |
| 169 | + 'Spacing control applies to lateral and depth offsets', |
| 170 | + ]); |
| 171 | + } |
| 172 | +} |
0 commit comments