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