|
| 1 | +import { |
| 2 | + engine, |
| 3 | + Entity, |
| 4 | + Transform, |
| 5 | + MeshRenderer, |
| 6 | + MeshCollider, |
| 7 | + Material, |
| 8 | + TextShape, |
| 9 | + VirtualCamera, |
| 10 | + MainCamera, |
| 11 | + InputModifier, |
| 12 | + PointerLock, |
| 13 | + PrimaryPointerInfo, |
| 14 | + pointerEventsSystem, |
| 15 | + InputAction, |
| 16 | + inputSystem, |
| 17 | + PointerEventType |
| 18 | +} from '@dcl/sdk/ecs' |
| 19 | +import { Vector3, Quaternion, Color4 } from '@dcl/sdk/math' |
| 20 | +import { setupUi } from './ui' |
| 21 | + |
| 22 | +const CENTER = Vector3.create(8, 1, 8) |
| 23 | +const RING_RADIUS = 6 |
| 24 | +const RING_BOX_COUNT = 10 |
| 25 | +// Degrees of camera rotation per pixel of PrimaryPointerInfo.screenDelta |
| 26 | +const SENSITIVITY = 0.15 |
| 27 | + |
| 28 | +let cameraEntity: Entity |
| 29 | +let cameraActive = false |
| 30 | +let yaw = 0 |
| 31 | +let pitch = 0 |
| 32 | + |
| 33 | +// Live values consumed by the HUD in ui.tsx. Updated from mouseLookSystem using getOrNull reads only, |
| 34 | +// so the HUD never triggers a CRDT re-sync by mutating components on every frame. |
| 35 | +export const state = { |
| 36 | + screenDelta: { x: 0, y: 0 }, |
| 37 | + isPointerLocked: false, |
| 38 | + cameraActive: false, |
| 39 | + yaw: 0, |
| 40 | + pitch: 0 |
| 41 | +} |
| 42 | + |
| 43 | +export function main() { |
| 44 | + createGround() |
| 45 | + createReferenceRing() |
| 46 | + cameraEntity = createVirtualCamera() |
| 47 | + createToggleBox() |
| 48 | + |
| 49 | + engine.addSystem(mouseLookSystem) |
| 50 | + engine.addSystem(exitSystem) |
| 51 | + |
| 52 | + setupUi() |
| 53 | +} |
| 54 | + |
| 55 | +function createGround() { |
| 56 | + const ground = engine.addEntity() |
| 57 | + Transform.create(ground, { |
| 58 | + position: Vector3.create(8, 0.01, 8), |
| 59 | + rotation: Quaternion.fromEulerDegrees(90, 0, 0), |
| 60 | + scale: Vector3.create(16, 16, 0.1) |
| 61 | + }) |
| 62 | + MeshRenderer.setPlane(ground) |
| 63 | + Material.setBasicMaterial(ground, { diffuseColor: Color4.Gray() }) |
| 64 | +} |
| 65 | + |
| 66 | +// A ring of colored boxes at varied heights plus cardinal N/E/S/W text markers, so that camera |
| 67 | +// rotation driven by mouse-look is easy to read unambiguously. |
| 68 | +function createReferenceRing() { |
| 69 | + for (let i = 0; i < RING_BOX_COUNT; i++) { |
| 70 | + const angle = (i / RING_BOX_COUNT) * Math.PI * 2 |
| 71 | + const x = CENTER.x + Math.cos(angle) * RING_RADIUS |
| 72 | + const z = CENTER.z + Math.sin(angle) * RING_RADIUS |
| 73 | + const height = 1 + (i % 4) * 0.75 |
| 74 | + |
| 75 | + const box = engine.addEntity() |
| 76 | + Transform.create(box, { |
| 77 | + position: Vector3.create(x, height / 2, z), |
| 78 | + scale: Vector3.create(0.8, height, 0.8) |
| 79 | + }) |
| 80 | + MeshRenderer.setBox(box) |
| 81 | + MeshCollider.setBox(box) |
| 82 | + Material.setPbrMaterial(box, { |
| 83 | + albedoColor: Color4.create((i * 0.37) % 1, (i * 0.61) % 1, (i * 0.83) % 1, 1) |
| 84 | + }) |
| 85 | + } |
| 86 | + |
| 87 | + createCardinalMarker('N', Vector3.create(CENTER.x, 3, CENTER.z - RING_RADIUS - 1), 180) |
| 88 | + createCardinalMarker('S', Vector3.create(CENTER.x, 3, CENTER.z + RING_RADIUS + 1), 0) |
| 89 | + createCardinalMarker('E', Vector3.create(CENTER.x + RING_RADIUS + 1, 3, CENTER.z), 270) |
| 90 | + createCardinalMarker('W', Vector3.create(CENTER.x - RING_RADIUS - 1, 3, CENTER.z), 90) |
| 91 | +} |
| 92 | + |
| 93 | +function createCardinalMarker(label: string, position: Vector3, facingYaw: number) { |
| 94 | + const marker = engine.addEntity() |
| 95 | + Transform.create(marker, { |
| 96 | + position, |
| 97 | + rotation: Quaternion.fromEulerDegrees(0, facingYaw, 0) |
| 98 | + }) |
| 99 | + TextShape.create(marker, { |
| 100 | + text: label, |
| 101 | + fontSize: 6, |
| 102 | + textColor: Color4.White() |
| 103 | + }) |
| 104 | +} |
| 105 | + |
| 106 | +function createVirtualCamera(): Entity { |
| 107 | + const cam = engine.addEntity() |
| 108 | + Transform.create(cam, { |
| 109 | + position: Vector3.create(CENTER.x, 3, CENTER.z) |
| 110 | + }) |
| 111 | + VirtualCamera.create(cam, { |
| 112 | + defaultTransition: { transitionMode: VirtualCamera.Transition.Time(0.5) } |
| 113 | + }) |
| 114 | + return cam |
| 115 | +} |
| 116 | + |
| 117 | +function createToggleBox() { |
| 118 | + const box = engine.addEntity() |
| 119 | + Transform.create(box, { |
| 120 | + position: Vector3.create(8, 1, 4) |
| 121 | + }) |
| 122 | + MeshRenderer.setBox(box) |
| 123 | + MeshCollider.setBox(box) |
| 124 | + Material.setBasicMaterial(box, { diffuseColor: Color4.Green() }) |
| 125 | + |
| 126 | + pointerEventsSystem.onPointerDown( |
| 127 | + { |
| 128 | + entity: box, |
| 129 | + opts: { button: InputAction.IA_POINTER, hoverText: 'Toggle mouse-look camera' } |
| 130 | + }, |
| 131 | + () => activateCamera(!cameraActive) |
| 132 | + ) |
| 133 | +} |
| 134 | + |
| 135 | +export function activateCamera(active: boolean) { |
| 136 | + cameraActive = active |
| 137 | + state.cameraActive = active |
| 138 | + |
| 139 | + MainCamera.createOrReplace(engine.CameraEntity, { |
| 140 | + virtualCameraEntity: active ? cameraEntity : undefined |
| 141 | + }) |
| 142 | + |
| 143 | + InputModifier.createOrReplace(engine.PlayerEntity, { |
| 144 | + mode: InputModifier.Mode.Standard({ disableAll: active }) |
| 145 | + }) |
| 146 | +} |
| 147 | + |
| 148 | +// THE SHOWCASE: PrimaryPointerInfo.screenDelta keeps reporting raw mouse delta while the pointer is |
| 149 | +// locked (renderer-side fix, shipped separately from this scene). We use it to drive virtual camera |
| 150 | +// rotation instead of the frozen screenCoordinates/worldRayDirection, which report the screen center |
| 151 | +// and center ray while locked. |
| 152 | +function mouseLookSystem() { |
| 153 | + const pointerInfo = PrimaryPointerInfo.getOrNull(engine.RootEntity) |
| 154 | + const pointerLock = PointerLock.getOrNull(engine.CameraEntity) |
| 155 | + |
| 156 | + const dx = pointerInfo?.screenDelta?.x ?? 0 |
| 157 | + const dy = pointerInfo?.screenDelta?.y ?? 0 |
| 158 | + const isLocked = pointerLock?.isPointerLocked ?? false |
| 159 | + |
| 160 | + state.screenDelta.x = dx |
| 161 | + state.screenDelta.y = dy |
| 162 | + state.isPointerLocked = isLocked |
| 163 | + |
| 164 | + if (!cameraActive || !isLocked) return |
| 165 | + |
| 166 | + yaw += dx * SENSITIVITY |
| 167 | + // NOTE: verify pitch sign in-engine — mouse up should look up, so a negative screenDelta.y |
| 168 | + // (cursor moving toward the top of the screen) must increase pitch. |
| 169 | + pitch = clamp(pitch - dy * SENSITIVITY, -85, 85) |
| 170 | + |
| 171 | + state.yaw = yaw |
| 172 | + state.pitch = pitch |
| 173 | + |
| 174 | + Transform.getMutable(cameraEntity).rotation = Quaternion.fromEulerDegrees(pitch, yaw, 0) |
| 175 | +} |
| 176 | + |
| 177 | +function exitSystem() { |
| 178 | + if (!cameraActive) return |
| 179 | + if (inputSystem.isTriggered(InputAction.IA_SECONDARY, PointerEventType.PET_DOWN)) { |
| 180 | + activateCamera(false) |
| 181 | + } |
| 182 | +} |
| 183 | + |
| 184 | +function clamp(value: number, min: number, max: number): number { |
| 185 | + return Math.min(max, Math.max(min, value)) |
| 186 | +} |
0 commit comments