Skip to content

Commit eba9277

Browse files
Chau TranChau Tran
Chau Tran
authored and
Chau Tran
committed
feat(cannon): migrate debug
1 parent b018754 commit eba9277

File tree

8 files changed

+137
-2
lines changed

8 files changed

+137
-2
lines changed

libs/cannon/debug/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# angular-three-cannon/debug
2+
3+
Secondary entry point of `angular-three-cannon`. It can be used by importing from `angular-three-cannon/debug`.

libs/cannon/debug/ng-package.json

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
{
2+
"lib": {
3+
"entryFile": "src/index.ts"
4+
}
5+
}

libs/cannon/debug/src/debug.ts

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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+
}

libs/cannon/debug/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from './debug';

libs/cannon/project.json

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,12 @@
4040
"executor": "@nx/linter:eslint",
4141
"outputs": ["{options.outputFile}"],
4242
"options": {
43-
"lintFilePatterns": ["libs/cannon/**/*.ts", "libs/cannon/**/*.html"]
43+
"lintFilePatterns": [
44+
"libs/cannon/**/*.ts",
45+
"libs/cannon/**/*.html",
46+
"libs/cannon/debug/**/*.ts",
47+
"libs/cannon/debug/**/*.html"
48+
]
4449
}
4550
}
4651
}

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@
8080
"@rx-angular/state": "^14.0.0",
8181
"@swc/helpers": "0.5.1",
8282
"cannon-es": "^0.20.0",
83+
"cannon-es-debugger": "^1.0.0",
8384
"ngx-resize": "^2.0.0",
8485
"nice-color-palettes": "^3.0.0",
8586
"postprocessing": "^6.31.0",

pnpm-lock.yaml

Lines changed: 18 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tsconfig.base.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
"angular-three": ["libs/angular-three/src/index.ts"],
2020
"angular-three-backup": ["libs/angular-three-backup/src/index.ts"],
2121
"angular-three-cannon": ["libs/cannon/src/index.ts"],
22+
"angular-three-cannon/debug": ["libs/cannon/debug/src/index.ts"],
2223
"angular-three-postprocessing": ["libs/postprocessing/src/index.ts"],
2324
"angular-three-postprocessing/effects": ["libs/postprocessing/effects/src/index.ts"],
2425
"angular-three-soba": ["libs/soba/src/index.ts"],

0 commit comments

Comments
 (0)