Files
cad-world/frontend/tests/browser/particle-morph.entry.ts
T
2026-09-22 10:36:28 +08:00

114 lines
5.4 KiB
TypeScript

import * as THREE from 'three'
import { WebGPURenderer } from 'three/webgpu'
import { ParticleMorphEffect } from '../../src/modules/infrastructure/render/scene-render/effects/ParticleMorphEffect'
import { PARTICLE_DURATIONS } from '../../src/modules/assembly-animation/application/duration'
// Real renderer fixture: exercises the same effect used by the workbench on
// WebGPU, WebGPURenderer's WebGL2 fallback, and legacy WebGL.
async function main() {
const backend = new URLSearchParams(location.search).get('backend') ?? 'webgpu'
const renderer = backend === 'webgl'
? new THREE.WebGLRenderer({ antialias: true })
: new WebGPURenderer({ antialias: true, forceWebGL: backend === 'fallback' })
if (backend !== 'webgl') await renderer.init()
const width = Math.min(innerWidth, 1000)
const height = Math.min(innerHeight - 85, 720)
renderer.setSize(width, height)
renderer.setPixelRatio(1)
document.body.style.cssText = 'margin:0;background:#111827;color:white;font:16px sans-serif'
document.body.appendChild(renderer.domElement)
const scene = new THREE.Scene()
scene.background = new THREE.Color('#111827')
scene.add(new THREE.HemisphereLight(0xffffff, 0x435573, 3))
const camera = new THREE.PerspectiveCamera(45, width / height, 0.01, 100)
camera.position.set(2.4, 1.5, 5)
camera.lookAt(0, 0.4, 0)
let effect: ParticleMorphEffect | null = null
let solid: THREE.Group | null = null
let playing = false
let startedAt = 0
let duration = PARTICLE_DURATIONS.generate as number
document.documentElement.dataset.theme = 'dark'
const status = document.createElement('div')
document.body.appendChild(status)
const box = (size: number[], position: number[]) => {
const mesh = new THREE.Mesh(new THREE.BoxGeometry(...size), new THREE.MeshStandardMaterial({ color: 0xb5cad9, roughness: 0.4 }))
mesh.position.set(position[0], position[1], position[2])
return mesh
}
const hand = () => {
const group = new THREE.Group()
group.add(box([0.25, 0.32, 0.15], [0.85, 0.2, 0]))
for (let i = 0; i < 4; i++) group.add(box([0.045, 0.2, 0.07], [0.75 + i * 0.065, -0.06, 0]))
return group
}
const claw = () => {
const group = new THREE.Group()
group.add(box([0.34, 0.18, 0.22], [0.85, 0.24, 0]))
group.add(box([0.07, 0.38, 0.16], [0.64, -0.03, 0]), box([0.07, 0.38, 0.16], [1.06, -0.03, 0]))
group.add(box([0.15, 0.07, 0.16], [0.69, -0.22, 0]), box([0.15, 0.07, 0.16], [1.01, -0.22, 0]))
group.traverse((object) => { if (object instanceof THREE.Mesh) (object.material as THREE.MeshStandardMaterial).color.setHex(0xf0a020) })
return group
}
const body = () => {
const group = new THREE.Group()
group.add(box([0.7, 0.9, 0.4], [0, 0.5, 0]), box([0.42, 0.42, 0.4], [0, 1.25, 0]))
for (const side of [-1, 1]) group.add(box([0.22, 0.85, 0.25], [side * 0.7, 0.6, 0]), box([0.23, 0.8, 0.3], [side * 0.22, -0.45, 0]))
;(group.children[0] as THREE.Mesh<THREE.BufferGeometry, THREE.MeshStandardMaterial>).material.color.setHex(0x327fcf)
return group
}
const start = (mode: 'generate' | 'replace') => {
duration = mode === 'generate' ? PARTICLE_DURATIONS.generate : PARTICLE_DURATIONS.change
effect?.dispose()
if (solid) {
scene.remove(solid)
solid.traverse((obj) => {
if (obj instanceof THREE.Mesh) { obj.geometry.dispose(); (obj.material as THREE.Material).dispose() }
})
}
solid = mode === 'replace' ? body() : new THREE.Group()
scene.add(solid)
const to = mode === 'generate' ? body() : claw()
if (mode === 'generate') to.add(hand())
effect = new ParticleMorphEffect({ mode, fromObjects: mode === 'replace' ? [hand()] : [], toObjects: [to], pixelRatio: 1, webGPU: backend !== 'webgl' })
scene.add(effect.root)
playing = false
status.textContent = `${backend}: ${mode} ready`
}
for (const mode of ['generate', 'replace'] as const) {
const button = document.createElement('button')
button.textContent = mode
button.onclick = () => start(mode)
document.body.appendChild(button)
}
const slider = document.createElement('input')
slider.type = 'range'; slider.min = '0'; slider.max = '100'; slider.value = '0'
slider.setAttribute('aria-label', 'Progress')
slider.oninput = () => { playing = false; effect?.update(Number(slider.value) / 100); status.textContent = `${backend}: progress ${slider.value}` }
document.body.appendChild(slider)
const play = document.createElement('button')
play.textContent = 'Play'
play.onclick = () => { playing = true; startedAt = performance.now() }
document.body.appendChild(play)
const themeButton = document.createElement('button')
themeButton.textContent = 'Toggle theme'
themeButton.onclick = () => {
const light = document.documentElement.dataset.theme !== 'light'
document.documentElement.dataset.theme = light ? 'light' : 'dark'
scene.background = new THREE.Color(light ? '#ffffff' : '#111827')
effect?.update(Number(slider.value) / 100)
}
document.body.appendChild(themeButton)
start('generate')
renderer.setAnimationLoop(() => {
if (playing) {
const progress = Math.min((performance.now() - startedAt) / duration, 1)
effect?.update(progress)
slider.value = String(Math.round(progress * 100))
if (progress === 1) playing = false
}
renderer.render(scene, camera)
})
}
main().catch((error) => { document.body.textContent = String(error); console.error(error) })