|
| 1 | +import { |
| 2 | + Component, |
| 3 | + computed, |
| 4 | + CUSTOM_ELEMENTS_SCHEMA, |
| 5 | + inject, |
| 6 | + InjectionToken, |
| 7 | + Input, |
| 8 | + OnInit, |
| 9 | + Signal, |
| 10 | +} from '@angular/core'; |
| 11 | +import { BodyProps, BodyShapeType, propsToBody } from '@pmndrs/cannon-worker-api'; |
| 12 | +import { injectBeforeRender, NgtArgs } from 'angular-three'; |
| 13 | +import { NGTC_PHYSICS_API } from 'angular-three-cannon'; |
| 14 | +import { Body, Quaternion as CQuarternion, Vec3, World } from 'cannon-es'; |
| 15 | +import CannonDebugger from 'cannon-es-debugger'; |
| 16 | +import * as THREE from 'three'; |
| 17 | + |
| 18 | +const q = new THREE.Quaternion(); |
| 19 | +const s = new THREE.Vector3(1, 1, 1); |
| 20 | +const v = new THREE.Vector3(); |
| 21 | +const m = new THREE.Matrix4(); |
| 22 | + |
| 23 | +function getMatrix(o: THREE.Object3D): THREE.Matrix4 { |
| 24 | + if (o instanceof THREE.InstancedMesh) { |
| 25 | + o.getMatrixAt(parseInt(o.uuid.split('/')[1]), m); |
| 26 | + return m; |
| 27 | + } |
| 28 | + return o.matrix; |
| 29 | +} |
| 30 | + |
| 31 | +export interface NgtcDebugApi { |
| 32 | + add(id: string, props: BodyProps, type: BodyShapeType): void; |
| 33 | + remove(id: string): void; |
| 34 | +} |
| 35 | + |
| 36 | +export const NGTC_DEBUG_API = new InjectionToken<Signal<NgtcDebugApi>>('NgtcDebug API'); |
| 37 | + |
| 38 | +@Component({ |
| 39 | + selector: 'ngtc-debug', |
| 40 | + standalone: true, |
| 41 | + template: ` |
| 42 | + <ngt-primitive *args="[scene]" /> |
| 43 | + <ng-content /> |
| 44 | + `, |
| 45 | + providers: [{ provide: NGTC_DEBUG_API, useFactory: (debug: NgtcDebug) => debug.api, deps: [NgtcDebug] }], |
| 46 | + imports: [NgtArgs], |
| 47 | + schemas: [CUSTOM_ELEMENTS_SCHEMA], |
| 48 | +}) |
| 49 | +export class NgtcDebug implements OnInit { |
| 50 | + @Input() color = 'black'; |
| 51 | + @Input() scale = 1; |
| 52 | + @Input() impl = CannonDebugger; |
| 53 | + @Input() disabled = false; |
| 54 | + |
| 55 | + readonly #bodies: Body[] = []; |
| 56 | + readonly #bodyMap: Record<string, Body> = {}; |
| 57 | + readonly #scene = new THREE.Scene(); |
| 58 | + |
| 59 | + readonly #physicsApi = inject(NGTC_PHYSICS_API); |
| 60 | + |
| 61 | + #cannonDebugger!: ReturnType<typeof CannonDebugger>; |
| 62 | + |
| 63 | + readonly api = computed(() => ({ |
| 64 | + add: (uuid: string, props: BodyProps, type: BodyShapeType) => { |
| 65 | + const body = propsToBody({ uuid, props, type }); |
| 66 | + this.#bodies.push(body); |
| 67 | + this.#bodyMap[uuid] = body; |
| 68 | + }, |
| 69 | + remove: (id: string) => { |
| 70 | + const debugBodyIndex = this.#bodies.indexOf(this.#bodyMap[id]); |
| 71 | + if (debugBodyIndex > -1) this.#bodies.splice(debugBodyIndex, 1); |
| 72 | + delete this.#bodyMap[id]; |
| 73 | + }, |
| 74 | + })); |
| 75 | + |
| 76 | + constructor() { |
| 77 | + injectBeforeRender(() => { |
| 78 | + if (!this.#cannonDebugger) return; |
| 79 | + const refs = this.#physicsApi().refs; |
| 80 | + for (const uuid in this.#bodyMap) { |
| 81 | + getMatrix(refs[uuid]).decompose(v, q, s); |
| 82 | + this.#bodyMap[uuid].position.copy(v as unknown as Vec3); |
| 83 | + this.#bodyMap[uuid].quaternion.copy(q as unknown as CQuarternion); |
| 84 | + } |
| 85 | + |
| 86 | + for (const child of this.#scene.children) { |
| 87 | + child.visible = !this.disabled; |
| 88 | + } |
| 89 | + |
| 90 | + if (!this.disabled) { |
| 91 | + this.#cannonDebugger.update(); |
| 92 | + } |
| 93 | + }); |
| 94 | + } |
| 95 | + |
| 96 | + ngOnInit() { |
| 97 | + this.#cannonDebugger = this.impl(this.#scene, { bodies: this.#bodies } as World, { |
| 98 | + color: this.color, |
| 99 | + scale: this.scale, |
| 100 | + }); |
| 101 | + } |
| 102 | +} |
0 commit comments