Skip to content

Commit b73211d

Browse files
committed
refactor(cannon): update cannon with new renderer
1 parent 0964536 commit b73211d

File tree

6 files changed

+60
-55
lines changed

6 files changed

+60
-55
lines changed

libs/cannon/body/src/lib/body.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ import {
1010
signal,
1111
} from '@angular/core';
1212
import { BodyShapeType } from '@pmndrs/cannon-worker-api';
13-
import { resolveRef } from 'angular-three';
13+
import { is, resolveRef } from 'angular-three';
1414
import { NgtcPhysics } from 'angular-three-cannon';
1515
import { NgtcDebug } from 'angular-three-cannon/debug';
1616
import { assertInjector } from 'ngxtension/assert-injector';
17-
import { DynamicDrawUsage, InstancedMesh, Object3D } from 'three';
17+
import * as THREE from 'three';
1818
import { NgtcArgFn, NgtcBodyPropsMap, NgtcBodyPublicApi, NgtcGetByIndex } from './types';
1919
import { defaultTransformArgs, makeBodyApi, prepare, setupCollision } from './utils';
2020

@@ -24,14 +24,14 @@ export interface NgtcBodyOptions<TShape extends BodyShapeType> {
2424
}
2525

2626
function createInjectBody<TShape extends BodyShapeType>(type: TShape) {
27-
return <TObject extends Object3D>(
27+
return <TObject extends THREE.Object3D>(
2828
getPropFn: NgtcGetByIndex<NgtcBodyPropsMap[TShape]>,
2929
ref: ElementRef<TObject> | TObject | Signal<ElementRef<TObject> | TObject | undefined>,
3030
options?: NgtcBodyOptions<TShape>,
3131
) => injectBody<TShape, TObject>(type, getPropFn, ref, options);
3232
}
3333

34-
function injectBody<TShape extends BodyShapeType, TObject extends Object3D>(
34+
function injectBody<TShape extends BodyShapeType, TObject extends THREE.Object3D>(
3535
type: TShape,
3636
getPropFn: NgtcGetByIndex<NgtcBodyPropsMap[TShape]>,
3737
ref: ElementRef<TObject> | TObject | Signal<ElementRef<TObject> | TObject | undefined>,
@@ -77,11 +77,11 @@ function injectBody<TShape extends BodyShapeType, TObject extends Object3D>(
7777

7878
const [uuid, props] = (() => {
7979
let uuids: string[] = [];
80-
let temp: Object3D;
81-
if (object instanceof InstancedMesh) {
82-
object.instanceMatrix.setUsage(DynamicDrawUsage);
80+
let temp: THREE.Object3D;
81+
if (is.three<THREE.InstancedMesh>(object, 'isInstancedMesh')) {
82+
object.instanceMatrix.setUsage(THREE.DynamicDrawUsage);
8383
uuids = new Array(object.count).fill(0).map((_, i) => `${object.uuid}/${i}`);
84-
temp = new Object3D();
84+
temp = new THREE.Object3D();
8585
} else {
8686
uuids = [object.uuid];
8787
}
@@ -91,8 +91,8 @@ function injectBody<TShape extends BodyShapeType, TObject extends Object3D>(
9191
const props = getPropFn(index);
9292
if (temp) {
9393
prepare(temp, props);
94-
(object as unknown as InstancedMesh).setMatrixAt(index, temp.matrix);
95-
(object as unknown as InstancedMesh).instanceMatrix.needsUpdate = true;
94+
(object as unknown as THREE.InstancedMesh).setMatrixAt(index, temp.matrix);
95+
(object as unknown as THREE.InstancedMesh).instanceMatrix.needsUpdate = true;
9696
} else {
9797
prepare(object, props);
9898
}

libs/cannon/body/src/lib/types.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import {
1+
import type {
22
AtomicName,
33
AtomicProps,
44
BodyProps,
@@ -15,21 +15,21 @@ import {
1515
Triplet,
1616
VectorName,
1717
} from '@pmndrs/cannon-worker-api';
18-
import { Euler, Quaternion, Vector3 } from 'three';
18+
import type * as THREE from 'three';
1919

2020
export interface NgtcAtomicApi<K extends AtomicName> {
2121
set: (value: AtomicProps[K]) => void;
2222
subscribe: (callback: (value: AtomicProps[K]) => void) => () => void;
2323
}
2424

2525
export interface NgtcQuaternionApi {
26-
copy: ({ w, x, y, z }: Quaternion) => void;
26+
copy: ({ w, x, y, z }: THREE.Quaternion) => void;
2727
set: (x: number, y: number, z: number, w: number) => void;
2828
subscribe: (callback: (value: Quad) => void) => () => void;
2929
}
3030

3131
export interface NgtcVectorApi {
32-
copy: ({ x, y, z }: Vector3 | Euler) => void;
32+
copy: ({ x, y, z }: THREE.Vector3 | THREE.Euler) => void;
3333
set: (x: number, y: number, z: number) => void;
3434
subscribe: (callback: (value: Triplet) => void) => () => void;
3535
}

libs/cannon/body/src/lib/utils.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,30 +20,31 @@ import {
2020
Triplet,
2121
VectorName,
2222
} from '@pmndrs/cannon-worker-api';
23+
import { is } from 'angular-three';
2324
import { NgtcCannonEvents, NgtcPhysics } from 'angular-three-cannon';
24-
import { Euler, Object3D, Quaternion, Vector3 } from 'three';
25+
import * as THREE from 'three';
2526
import { NgtcWorkerApi } from './types';
2627

2728
function capitalize<T extends string>(str: T): Capitalize<T> {
2829
return (str.charAt(0).toUpperCase() + str.slice(1)) as Capitalize<T>;
2930
}
3031

31-
function getUUID(body: Object3D, index?: number) {
32+
function getUUID(body: THREE.Object3D, index?: number) {
3233
const suffix = index === undefined ? '' : `/${index}`;
3334
if (typeof body === 'function') return null;
3435
return body && body && `${body.uuid}${suffix}`;
3536
}
3637

37-
const e = new Euler();
38-
const q = new Quaternion();
38+
const e = new THREE.Euler();
39+
const q = new THREE.Quaternion();
3940

4041
function quaternionToRotation(callback: (v: Triplet) => void) {
4142
return (v: Quad) => callback(e.setFromQuaternion(q.fromArray(v)).toArray() as Triplet);
4243
}
4344

4445
let incrementingId = 0;
4546
export function createSubscribe<T extends SubscriptionName>(
46-
body: Object3D,
47+
body: THREE.Object3D,
4748
worker: CannonWorkerAPI,
4849
subscriptions: Subscriptions,
4950
type: T,
@@ -62,11 +63,14 @@ export function createSubscribe<T extends SubscriptionName>(
6263
};
6364
}
6465

65-
function makeTriplet(v: Vector3 | Triplet): Triplet {
66-
return v instanceof Vector3 ? [v.x, v.y, v.z] : v;
66+
function makeTriplet(v: THREE.Vector3 | Triplet): Triplet {
67+
return is.three<THREE.Vector3>(v, 'isVector3') ? [v.x, v.y, v.z] : v;
6768
}
6869

69-
export function prepare(object: Object3D, { position = [0, 0, 0], rotation = [0, 0, 0], userData = {} }: BodyProps) {
70+
export function prepare(
71+
object: THREE.Object3D,
72+
{ position = [0, 0, 0], rotation = [0, 0, 0], userData = {} }: BodyProps,
73+
) {
7074
object.userData = userData;
7175
object.position.set(...position);
7276
object.rotation.set(...rotation);
@@ -82,7 +86,7 @@ export function setupCollision(
8286
}
8387

8488
export function makeBodyApi(
85-
body: Object3D,
89+
body: THREE.Object3D,
8690
worker: CannonWorkerAPI,
8791
{ subscriptions, scaleOverrides }: Pick<NgtcPhysics, 'subscriptions' | 'scaleOverrides'>,
8892
) {
@@ -101,7 +105,7 @@ export function makeBodyApi(
101105
const makeQuaternion = (index?: number) => {
102106
const type = 'quaternion';
103107
return {
104-
copy: ({ w, x, y, z }: Quaternion) => {
108+
copy: ({ w, x, y, z }: THREE.Quaternion) => {
105109
const uuid = getUUID(body, index);
106110
uuid && worker.setQuaternion({ props: [x, y, z, w], uuid });
107111
},
@@ -115,7 +119,7 @@ export function makeBodyApi(
115119

116120
const makeRotation = (index?: number) => {
117121
return {
118-
copy: ({ x, y, z }: Vector3 | Euler) => {
122+
copy: ({ x, y, z }: THREE.Vector3 | THREE.Euler) => {
119123
const uuid = getUUID(body, index);
120124
uuid && worker.setRotation({ props: [x, y, z], uuid });
121125
},
@@ -140,7 +144,7 @@ export function makeBodyApi(
140144
const makeVec = (type: VectorName, index?: number) => {
141145
const op: SetOpName<VectorName> = `set${capitalize(type)}`;
142146
return {
143-
copy: ({ x, y, z }: Vector3 | Euler) => {
147+
copy: ({ x, y, z }: THREE.Vector3 | THREE.Euler) => {
144148
const uuid = getUUID(body, index);
145149
uuid && worker[op]({ props: [x, y, z], uuid });
146150
},
@@ -199,7 +203,7 @@ export function makeBodyApi(
199203
rotation: makeRotation(index),
200204
scaleOverride(scale) {
201205
const uuid = getUUID(body, index);
202-
if (uuid) scaleOverrides[uuid] = new Vector3(...scale);
206+
if (uuid) scaleOverrides[uuid] = new THREE.Vector3(...scale);
203207
},
204208
sleep() {
205209
const uuid = getUUID(body, index);

libs/cannon/constraint/src/lib/constraint.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import {
1010
import { makeId, resolveRef } from 'angular-three';
1111
import { NgtcPhysics } from 'angular-three-cannon';
1212
import { assertInjector } from 'ngxtension/assert-injector';
13-
import { Object3D } from 'three';
13+
import * as THREE from 'three';
1414

1515
export interface NgtcConstraintApi {
1616
disable: () => void;
@@ -44,7 +44,7 @@ export type NgtcConstraintOptions<TConstraintType extends 'Hinge' | ConstraintTy
4444
};
4545

4646
function createInjectConstraint<TConstraint extends ConstraintTypes | 'Hinge'>(type: TConstraint) {
47-
return <A extends Object3D = Object3D, B extends Object3D = Object3D>(
47+
return <A extends THREE.Object3D = THREE.Object3D, B extends THREE.Object3D = THREE.Object3D>(
4848
bodyA: ElementRef<A> | A | Signal<ElementRef<A> | A | undefined>,
4949
bodyB: ElementRef<B> | B | Signal<ElementRef<B> | B | undefined>,
5050
options?: NgtcConstraintOptions<TConstraint>,
@@ -53,8 +53,8 @@ function createInjectConstraint<TConstraint extends ConstraintTypes | 'Hinge'>(t
5353

5454
function injectConstraint<
5555
TConstraint extends ConstraintTypes | 'Hinge',
56-
A extends Object3D = Object3D,
57-
B extends Object3D = Object3D,
56+
A extends THREE.Object3D = THREE.Object3D,
57+
B extends THREE.Object3D = THREE.Object3D,
5858
>(
5959
type: TConstraint,
6060
bodyA: ElementRef<A> | A | Signal<ElementRef<A> | A | undefined>,

libs/cannon/debug/src/lib/debug.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
import { Directive, afterNextRender, inject, input } from '@angular/core';
22
import { BodyProps, BodyShapeType, propsToBody } from '@pmndrs/cannon-worker-api';
3-
import { injectBeforeRender, injectStore } from 'angular-three';
3+
import { injectBeforeRender, injectStore, is } from 'angular-three';
44
import { NgtcPhysics } from 'angular-three-cannon';
55
import { Body, Quaternion as CQuarternion, Vec3, World } from 'cannon-es';
66
import CannonDebugger from 'cannon-es-debugger';
77
import { mergeInputs } from 'ngxtension/inject-inputs';
8-
import { InstancedMesh, Matrix4, Object3D, Quaternion, Scene, Vector3 } from 'three';
8+
import * as THREE from 'three';
99

10-
const q = new Quaternion();
11-
const s = new Vector3(1, 1, 1);
12-
const v = new Vector3();
13-
const m = new Matrix4();
14-
function getMatrix(o: Object3D) {
15-
if (o instanceof InstancedMesh) {
10+
const q = new THREE.Quaternion();
11+
const s = new THREE.Vector3(1, 1, 1);
12+
const v = new THREE.Vector3();
13+
const m = new THREE.Matrix4();
14+
15+
function getMatrix(o: THREE.Object3D) {
16+
if (is.three<THREE.InstancedMesh>(o, 'isInstancedMesh')) {
1617
o.getMatrixAt(parseInt(o.uuid.split('/')[1]), m);
1718
return m;
1819
}
@@ -35,14 +36,14 @@ const defaultOptions: NgtcDebugInputs = {
3536

3637
@Directive({ selector: 'ngtc-physics[debug]' })
3738
export class NgtcDebug {
38-
private store = injectStore();
39-
private physics = inject(NgtcPhysics);
40-
4139
debug = input(defaultOptions, { transform: mergeInputs(defaultOptions) });
4240

43-
private defaultScene = this.store.select('scene');
41+
private physics = inject(NgtcPhysics);
42+
43+
private store = injectStore();
44+
private defaultScene = this.store.scene;
4445

45-
private debuggerScene = new Scene();
46+
private debuggerScene = new THREE.Scene();
4647
private bodies: Body[] = [];
4748
private bodyMap: Record<string, Body> = {};
4849

libs/cannon/src/lib/physics.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ import {
1414
WorkerFrameMessage,
1515
WorkerRayhitEvent,
1616
} from '@pmndrs/cannon-worker-api';
17-
import { injectBeforeRender, injectStore, pick } from 'angular-three';
17+
import { injectBeforeRender, injectStore, is, pick } from 'angular-three';
1818
import { mergeInputs } from 'ngxtension/inject-inputs';
19-
import { InstancedMesh, Matrix4, Object3D, Quaternion, QuaternionTuple, Vector3 } from 'three';
19+
import * as THREE from 'three';
2020

2121
export interface NgtcCannonWorkerEvents {
2222
collide: WorkerCollideEvent;
@@ -31,20 +31,20 @@ export type NgtcCannonWorker = CannonWorkerAPI & {
3131
removeAllListeners: () => void;
3232
};
3333

34-
const v = new Vector3();
35-
const s = new Vector3(1, 1, 1);
36-
const q = new Quaternion();
37-
const m = new Matrix4();
34+
const v = new THREE.Vector3();
35+
const s = new THREE.Vector3(1, 1, 1);
36+
const q = new THREE.Quaternion();
37+
const m = new THREE.Matrix4();
3838

3939
function apply(
4040
index: number,
4141
positions: ArrayLike<number>,
4242
quaternions: ArrayLike<number>,
4343
scale = s,
44-
object?: Object3D,
44+
object?: THREE.Object3D,
4545
) {
4646
if (index !== undefined) {
47-
m.compose(v.fromArray(positions, index * 3), q.fromArray(quaternions as QuaternionTuple, index * 4), scale);
47+
m.compose(v.fromArray(positions, index * 3), q.fromArray(quaternions as THREE.QuaternionTuple, index * 4), scale);
4848
if (object) {
4949
object.matrixAutoUpdate = false;
5050
object.matrix.copy(m);
@@ -66,7 +66,7 @@ type NgtcCallbackByType<T extends { type: string }> = {
6666

6767
export type NgtcCannonEvents = Record<string, Partial<NgtcCallbackByType<NgtcCannonEvent>>>;
6868

69-
export type ScaleOverrides = Record<string, Vector3>;
69+
export type ScaleOverrides = Record<string, THREE.Vector3>;
7070

7171
export interface NgtcPhysicsOptions extends CannonWorkerProps {
7272
isPaused?: boolean;
@@ -111,7 +111,7 @@ export class NgtcPhysics {
111111
private iterations = pick(this.options, 'iterations');
112112
private tolerance = pick(this.options, 'tolerance');
113113

114-
private invalidate = this.store.select('invalidate');
114+
private invalidate = this.store.invalidate;
115115
// @ts-expect-error - worker is not nullable, and we don't want to use ! operator.
116116
private cannonWorker = signal<CannonWorkerAPI>(null);
117117

@@ -237,7 +237,7 @@ export class NgtcPhysics {
237237
});
238238
if (!active) return;
239239
for (const ref of Object.values(refs).filter(unique())) {
240-
if (ref instanceof InstancedMesh) {
240+
if (is.three<THREE.InstancedMesh>(ref, 'isInstancedMesh')) {
241241
for (let i = 0; i < ref.count; i++) {
242242
const uuid = `${ref.uuid}/${i}`;
243243
const index = bodies[uuid];

0 commit comments

Comments
 (0)