|
| 1 | +import { |
| 2 | + AxesHelper, |
| 3 | + DoubleSide, |
| 4 | + Mesh, |
| 5 | + MeshBasicMaterial, |
| 6 | + PerspectiveCamera, |
| 7 | + Scene, |
| 8 | + TorusGeometry, |
| 9 | + WebGLRenderer, |
| 10 | +} from 'three' |
| 11 | +import { Easing, Tween } from '@tweenjs/tween.js' |
| 12 | +import { OrbitControls } from 'three-stdlib' |
| 13 | +import type { RenderSettings } from 'src/components/Viewer/Scene' |
| 14 | +import type { UseViewportResult } from 'src/hooks/useViewport' |
| 15 | +import { CameraPreset } from 'src/state/camera.state' |
| 16 | +import { FrameCounter, RenderStats } from './gl/FrameCounter' |
| 17 | +import { makeCoordinatePlanes } from './gl/makePlane' |
| 18 | +import { UserInput } from './gl/UserInput' |
| 19 | + |
| 20 | +const CAMERA_POS_Z = 300 |
| 21 | + |
| 22 | +export async function initGeometry(mesh?: Mesh) { |
| 23 | + const geometry = new TorusGeometry(10, 3, 16, 100) |
| 24 | + |
| 25 | + const material = new MeshBasicMaterial({ color: 'lime', vertexColors: false, side: DoubleSide }) |
| 26 | + if (!mesh) { |
| 27 | + mesh = new Mesh(geometry, material) // eslint-disable-line no-param-reassign |
| 28 | + } |
| 29 | + |
| 30 | + // NOTE: Apply default mesh rotation, to make sure the default view is along negative Z axis. |
| 31 | + // NOTE: This prevents weird OrbitControls behavior. |
| 32 | + mesh.rotation.x = -Math.PI / 2 |
| 33 | + |
| 34 | + return mesh |
| 35 | +} |
| 36 | + |
| 37 | +export function getViewportSize(canvas: HTMLCanvasElement) { |
| 38 | + if (!canvas.parentElement) { |
| 39 | + throw new Error('Canvas element parent is invalid') |
| 40 | + } |
| 41 | + const rect = canvas.parentElement?.getBoundingClientRect() |
| 42 | + const { width, height } = rect |
| 43 | + return { width, height } |
| 44 | +} |
| 45 | + |
| 46 | +export class Renderer { |
| 47 | + private readonly renderer: WebGLRenderer |
| 48 | + private readonly scene: Scene |
| 49 | + private readonly camera: PerspectiveCamera |
| 50 | + private readonly controls: OrbitControls |
| 51 | + private readonly input: UserInput |
| 52 | + |
| 53 | + private cameraPresetTarget: CameraPreset | undefined |
| 54 | + private cameraTween: Tween<CameraPreset> | undefined |
| 55 | + |
| 56 | + private mesh: Mesh | undefined |
| 57 | + private material: MeshBasicMaterial | undefined |
| 58 | + |
| 59 | + private rafHandle: number | undefined |
| 60 | + private shouldRender = true |
| 61 | + |
| 62 | + public readonly frameCounter: FrameCounter |
| 63 | + |
| 64 | + public constructor( |
| 65 | + canvas: HTMLCanvasElement, |
| 66 | + renderSettings: RenderSettings, |
| 67 | + cameraPreset: CameraPreset, |
| 68 | + color: string, |
| 69 | + setRenderStats: (stats: RenderStats) => void, |
| 70 | + ) { |
| 71 | + if (!canvas) { |
| 72 | + throw new Error('Canvas element is invalid') |
| 73 | + } |
| 74 | + |
| 75 | + const { width, height } = getViewportSize(canvas) |
| 76 | + const aspect = width / height |
| 77 | + |
| 78 | + this.renderer = new WebGLRenderer({ |
| 79 | + canvas, |
| 80 | + antialias: true, |
| 81 | + precision: 'highp', |
| 82 | + powerPreference: 'high-performance', |
| 83 | + }) |
| 84 | + this.renderer.setClearColor(renderSettings.clearColor) |
| 85 | + this.renderer.setPixelRatio(window.devicePixelRatio) |
| 86 | + this.renderer.setSize(width, height) |
| 87 | + |
| 88 | + this.scene = new Scene() |
| 89 | + |
| 90 | + this.camera = new PerspectiveCamera(75, aspect, 0.1, 100_000) |
| 91 | + this.camera.position.z = CAMERA_POS_Z |
| 92 | + this.controls = new OrbitControls(this.camera, canvas) |
| 93 | + this.controls.minDistance = 10 |
| 94 | + this.controls.maxDistance = 500 |
| 95 | + this.controls.zoomSpeed = 2 |
| 96 | + this.onCameraPreset(cameraPreset, false) |
| 97 | + |
| 98 | + this.frameCounter = new FrameCounter(setRenderStats) |
| 99 | + |
| 100 | + this.input = new UserInput(canvas) |
| 101 | + |
| 102 | + void this.init() // eslint-disable-line no-void |
| 103 | + } |
| 104 | + |
| 105 | + public async init() { |
| 106 | + this.mesh = await initGeometry(this.mesh) |
| 107 | + this.mesh.geometry.computeBoundingSphere() |
| 108 | + this.scene.add(this.mesh) |
| 109 | + |
| 110 | + const axesHelper = new AxesHelper(1000) |
| 111 | + this.scene.add(axesHelper) |
| 112 | + |
| 113 | + const coordPlanes = makeCoordinatePlanes() |
| 114 | + this.scene.add(...coordPlanes) |
| 115 | + } |
| 116 | + |
| 117 | + public destroy() { |
| 118 | + if (this.rafHandle) { |
| 119 | + cancelAnimationFrame(this.rafHandle) |
| 120 | + } |
| 121 | + |
| 122 | + this.mesh?.geometry.dispose() |
| 123 | + this.material?.dispose() |
| 124 | + this.camera.clear() |
| 125 | + this.controls.dispose() |
| 126 | + this.scene.clear() |
| 127 | + this.scene.removeFromParent() |
| 128 | + } |
| 129 | + |
| 130 | + public onResize({ width, height }: UseViewportResult) { |
| 131 | + if (width === 0 || height === 0) { |
| 132 | + return |
| 133 | + } |
| 134 | + this.camera.aspect = width / height |
| 135 | + this.camera.updateProjectionMatrix() |
| 136 | + this.renderer.setSize(width, height) |
| 137 | + } |
| 138 | + |
| 139 | + public onCameraPreset(cameraPreset: CameraPreset, animated = true) { |
| 140 | + if (animated) { |
| 141 | + this.cameraTween = new Tween<CameraPreset>({ |
| 142 | + polar: this.controls.getPolarAngle(), |
| 143 | + azimuthal: this.controls.getAzimuthalAngle(), |
| 144 | + }) |
| 145 | + .to(cameraPreset, 250) |
| 146 | + .easing(Easing.Quadratic.Out) |
| 147 | + .onComplete((_) => { |
| 148 | + this.cameraPresetTarget = undefined |
| 149 | + }) |
| 150 | + .onUpdate((cameraPreset) => { |
| 151 | + this.cameraPresetTarget = cameraPreset |
| 152 | + }) |
| 153 | + .start() |
| 154 | + } else { |
| 155 | + const { polar, azimuthal } = cameraPreset |
| 156 | + this.controls.setPolarAngle(polar) |
| 157 | + this.controls.setAzimuthalAngle(azimuthal) |
| 158 | + } |
| 159 | + } |
| 160 | + |
| 161 | + public startRenderLoop(_time: number) { |
| 162 | + const { deltaMs } = this.frameCounter.update() |
| 163 | + this.cameraTween?.update() |
| 164 | + |
| 165 | + this.update(deltaMs) |
| 166 | + this.render(deltaMs) |
| 167 | + |
| 168 | + this.rafHandle = requestAnimationFrame((time) => this.startRenderLoop(time)) |
| 169 | + } |
| 170 | + |
| 171 | + public update(_deltaMs: number) { |
| 172 | + if (this.cameraPresetTarget) { |
| 173 | + this.controls.setPolarAngle(this.cameraPresetTarget.polar) |
| 174 | + this.controls.setAzimuthalAngle(this.cameraPresetTarget.azimuthal) |
| 175 | + } |
| 176 | + |
| 177 | + this.controls.update() |
| 178 | + this.camera.updateProjectionMatrix() |
| 179 | + |
| 180 | + if (this.input.isCtrlDown) { |
| 181 | + this.controls.enableRotate = false |
| 182 | + this.controls.enablePan = false |
| 183 | + } else { |
| 184 | + this.controls.enableRotate = true |
| 185 | + this.controls.enablePan = true |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + public render(_deltaMs: number) { |
| 190 | + if (this.shouldRender) { |
| 191 | + this.frameCounter.frame() |
| 192 | + this.renderer.clear() |
| 193 | + this.renderer.render(this.scene, this.camera) |
| 194 | + } |
| 195 | + } |
| 196 | +} |
0 commit comments