Skip to content

Commit 3e92fa3

Browse files
Chau TranChau Tran
Chau Tran
authored and
Chau Tran
committed
fix(core): clean up cdr call
1 parent 70348d7 commit 3e92fa3

File tree

8 files changed

+49
-60
lines changed

8 files changed

+49
-60
lines changed

libs/angular-three/src/lib/renderer/renderer.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import { NgtStore } from '../stores/store';
1818
import type { NgtAnyRecord } from '../types';
1919
import { getLocalState, prepare } from '../utils/instance';
2020
import { is } from '../utils/is';
21-
import { safeDetectChanges } from '../utils/safe-detect-changes';
2221
import { createSignal } from '../utils/signal';
2322
import { NGT_COMPOUND_PREFIXES } from './di';
2423
import { NgtRendererClassId } from './enums';
@@ -245,7 +244,6 @@ export class NgtRenderer implements Renderer2 {
245244
if (getLocalState(newChild).parent && untracked(getLocalState(newChild).parent)) return;
246245
// attach THREE child
247246
attachThreeChild(parent, newChild);
248-
safeDetectChanges(this.cdr);
249247
// here, we handle the special case of if the parent has a compoundParent, which means this child is part of a compound parent template
250248
if (!cRS[NgtRendererClassId.compound]) return;
251249
const closestGrandparentWithCompound = this.store.getClosestParentWithCompound(parent);
@@ -397,7 +395,13 @@ export class NgtRenderer implements Renderer2 {
397395
) {
398396
const instance = rS[NgtRendererClassId.compounded] || target;
399397
const priority = getLocalState(target).priority;
400-
return processThreeEvent(instance, priority || 0, eventName, callback, this.zone, this.cdr);
398+
const targetCdr =
399+
rS[NgtRendererClassId.injectorFactory]?.().get(ChangeDetectorRef, null) ||
400+
rS[NgtRendererClassId.parent]?.__ngt_renderer__?.[NgtRendererClassId.injectorFactory]?.().get(
401+
ChangeDetectorRef,
402+
null
403+
);
404+
return processThreeEvent(instance, priority || 0, eventName, callback, this.zone, this.cdr, targetCdr);
401405
}
402406

403407
if (rS[NgtRendererClassId.type] === 'compound' && !rS[NgtRendererClassId.compounded]) {

libs/angular-three/src/lib/renderer/utils.ts

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,8 @@ export function processThreeEvent(
145145
eventName: string,
146146
callback: (event: any) => void,
147147
zone: NgZone,
148-
cdr: ChangeDetectorRef
148+
cdr: ChangeDetectorRef,
149+
targetCdr?: ChangeDetectorRef | null
149150
): () => void {
150151
const lS = getLocalState(instance);
151152
if (eventName === SPECIAL_EVENTS.BEFORE_RENDER) {
@@ -170,12 +171,12 @@ export function processThreeEvent(
170171
if (previousHandler) previousHandler(event);
171172
zone.run(() => {
172173
callback(event);
174+
safeDetectChanges(targetCdr);
173175
safeDetectChanges(cdr);
174-
// cdr.detectChanges();
175176
});
176177
};
177178

178-
Object.assign(lS.handlers, { [eventName]: eventToHandler(updatedCallback) });
179+
Object.assign(lS.handlers, { [eventName]: updatedCallback });
179180

180181
// increment the count everytime
181182
lS.eventCount += 1;

libs/angular-three/src/lib/utils/safe-detect-changes.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@ import type { NgtAnyRecord } from '../types';
44
export function safeDetectChanges(cdr: ChangeDetectorRef | undefined | null) {
55
if (!cdr) return;
66
try {
7-
if ((cdr as NgtAnyRecord)['_attachedToViewContainer']) {
7+
// dynamic created component with ViewContainerRef#createComponent does not have Context
8+
// but it has _attachedToViewContainer
9+
if ((cdr as NgtAnyRecord)['_attachedToViewContainer'] || !!(cdr as NgtAnyRecord)['context']) {
810
cdr.detectChanges();
911
}
1012
} catch (e) {

libs/soba/abstractions/src/gizmo-helper/gizmo-viewcube/gizmo-viewcube-edge.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ChangeDetectorRef, Component, CUSTOM_ELEMENTS_SCHEMA, inject, Input, signal } from '@angular/core';
1+
import { Component, CUSTOM_ELEMENTS_SCHEMA, inject, Input, signal } from '@angular/core';
22
import { extend, NgtArgs, NgtSignalStore, NgtThreeEvent } from 'angular-three';
33
import { BoxGeometry, Mesh, MeshBasicMaterial, Vector3 } from 'three';
44
import { NGTS_GIZMO_HELPER_API } from '../gizmo-helper';
@@ -35,7 +35,6 @@ export class NgtsGizmoViewcubeEdgeCube extends NgtSignalStore<{
3535
dimensions: [number, number, number];
3636
}> {
3737
readonly #gizmoHelperApi = inject(NGTS_GIZMO_HELPER_API);
38-
readonly #cdr = inject(ChangeDetectorRef);
3938

4039
protected readonly viewcubeInputs = inject(NgtsGizmoViewcubeInputs);
4140

@@ -60,13 +59,11 @@ export class NgtsGizmoViewcubeEdgeCube extends NgtSignalStore<{
6059
onPointerMove(event: NgtThreeEvent<PointerEvent>) {
6160
event.stopPropagation();
6261
this.hover.set(true);
63-
this.#cdr.detectChanges();
6462
}
6563

6664
onPointerOut(event: NgtThreeEvent<PointerEvent>) {
6765
event.stopPropagation();
6866
this.hover.set(false);
69-
this.#cdr.detectChanges();
7067
}
7168

7269
onClick(event: NgtThreeEvent<MouseEvent>) {

libs/soba/abstractions/src/gizmo-helper/gizmo-viewcube/gizmo-viewcube-face.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { DOCUMENT } from '@angular/common';
2-
import { ChangeDetectorRef, Component, computed, CUSTOM_ELEMENTS_SCHEMA, inject, Input, signal } from '@angular/core';
2+
import { Component, computed, CUSTOM_ELEMENTS_SCHEMA, inject, Input, signal } from '@angular/core';
33
import { extend, NgtRepeat, NgtSignalStore, NgtStore, NgtThreeEvent } from 'angular-three';
44
import { BoxGeometry, CanvasTexture, Mesh, MeshLambertMaterial } from 'three';
55
import { NGTS_GIZMO_HELPER_API } from '../gizmo-helper';
@@ -101,7 +101,6 @@ export class NgtsGizmoViewcubeFaceMaterial extends NgtSignalStore<{
101101
})
102102
export class NgtsGizmoViewcubeFaceCube {
103103
readonly #gizmoHelperApi = inject(NGTS_GIZMO_HELPER_API);
104-
readonly #cdr = inject(ChangeDetectorRef);
105104

106105
protected readonly viewcubeInputs = inject(NgtsGizmoViewcubeInputs);
107106

@@ -110,13 +109,11 @@ export class NgtsGizmoViewcubeFaceCube {
110109
onPointerMove(event: NgtThreeEvent<PointerEvent>) {
111110
event.stopPropagation();
112111
this.hover.set(Math.floor(event.faceIndex! / 2));
113-
this.#cdr.detectChanges();
114112
}
115113

116114
onPointerOut(event: NgtThreeEvent<PointerEvent>) {
117115
event.stopPropagation();
118116
this.hover.set(-1);
119-
this.#cdr.detectChanges();
120117
}
121118

122119
onClick(event: NgtThreeEvent<MouseEvent>) {

libs/soba/abstractions/src/gizmo-helper/gizmo-viewport/gizmo-viewport-axis.ts

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,5 @@
11
import { DOCUMENT } from '@angular/common';
2-
import {
3-
ChangeDetectorRef,
4-
Component,
5-
computed,
6-
CUSTOM_ELEMENTS_SCHEMA,
7-
EventEmitter,
8-
inject,
9-
Input,
10-
signal,
11-
} from '@angular/core';
2+
import { Component, computed, CUSTOM_ELEMENTS_SCHEMA, EventEmitter, inject, Input, signal } from '@angular/core';
123
import { extend, NgtArgs, NgtSignalStore, NgtStore, NgtThreeEvent } from 'angular-three';
134
import { BoxGeometry, CanvasTexture, Group, Mesh, MeshBasicMaterial, Sprite, SpriteMaterial } from 'three';
145

@@ -88,7 +79,6 @@ export class NgtsGizmoViewportAxisHead extends NgtSignalStore<{
8879
clickEmitter: EventEmitter<NgtThreeEvent<MouseEvent>>;
8980
}> {
9081
readonly #document = inject(DOCUMENT);
91-
readonly #cdr = inject(ChangeDetectorRef);
9282
readonly #store = inject(NgtStore);
9383
readonly gl = this.#store.select('gl');
9484

@@ -170,7 +160,6 @@ export class NgtsGizmoViewportAxisHead extends NgtSignalStore<{
170160
if (!this.get('disabled')) {
171161
event.stopPropagation();
172162
this.active.set(true);
173-
this.#cdr.detectChanges();
174163
}
175164
}
176165

@@ -181,7 +170,6 @@ export class NgtsGizmoViewportAxisHead extends NgtSignalStore<{
181170
} else {
182171
event.stopPropagation();
183172
this.active.set(false);
184-
this.#cdr.detectChanges();
185173
}
186174
}
187175
}

libs/soba/src/shaders/mesh-refraction-material.stories.ts

Lines changed: 30 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,36 +20,35 @@ import { makeStoryFunction, StorybookSetup } from '../setup-canvas';
2020
standalone: true,
2121
template: `
2222
<ngts-cube-camera [resolution]="256" [frames]="1" [envMap]="cameraTexture()">
23-
<ng-template [ngtsCameraContent]="true" let-fbo="fbo">
24-
<ngts-caustics
25-
color="white"
26-
[backside]="true"
27-
[position]="[0, -0.5, 0]"
28-
[lightSource]="[5, 5, -10]"
29-
[worldRadius]="0.1"
30-
[ior]="1.8"
31-
[backsideIOR]="1.1"
32-
[intensity]="0.1"
23+
<ngts-caustics
24+
*ngtsCameraContent="true; fbo as fbo"
25+
color="white"
26+
[backside]="true"
27+
[position]="[0, -0.5, 0]"
28+
[lightSource]="[5, 5, -10]"
29+
[worldRadius]="0.1"
30+
[ior]="1.8"
31+
[backsideIOR]="1.1"
32+
[intensity]="0.1"
33+
>
34+
<ngt-mesh
35+
*ngIf="diamondGeometry() as diamondGeometry"
36+
[castShadow]="true"
37+
[geometry]="diamondGeometry"
38+
[rotation]="[0, 0, 0.715]"
39+
[position]="[0, -0.175 + 0.5, 0]"
3340
>
34-
<ngt-mesh
35-
*ngIf="diamondGeometry() as diamondGeometry"
36-
[castShadow]="true"
37-
[geometry]="diamondGeometry"
38-
[rotation]="[0, 0, 0.715]"
39-
[position]="[0, -0.175 + 0.5, 0]"
40-
>
41-
<ngts-mesh-refraction-material
42-
[envMap]="fbo.texture"
43-
[bounces]="3"
44-
[aberrationStrength]="0.01"
45-
[ior]="2.75"
46-
[fresnel]="1"
47-
[fastChroma]="true"
48-
[toneMapped]="false"
49-
/>
50-
</ngt-mesh>
51-
</ngts-caustics>
52-
</ng-template>
41+
<ngts-mesh-refraction-material
42+
[envMap]="fbo.texture"
43+
[bounces]="3"
44+
[aberrationStrength]="0.01"
45+
[ior]="2.75"
46+
[fresnel]="1"
47+
[fastChroma]="true"
48+
[toneMapped]="false"
49+
/>
50+
</ngt-mesh>
51+
</ngts-caustics>
5352
</ngts-cube-camera>
5453
`,
5554
schemas: [CUSTOM_ELEMENTS_SCHEMA],
@@ -136,6 +135,7 @@ class Diamond {
136135
`,
137136
imports: [
138137
Diamond,
138+
NgIf,
139139
NgtArgs,
140140
NgtsMeshTranmissionMaterial,
141141
NgtsAccumulativeShadows,
@@ -156,5 +156,5 @@ export default {
156156
} as Meta;
157157

158158
export const Default = makeStoryFunction(DefaultMeshRefractionMaterialStory, {
159-
camera: { fov: 45, position: [-5, 0.5, 5] },
159+
camera: { fov: 45, position: [-5, 0.5, 0] },
160160
});

libs/soba/staging/src/stage/stage.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
OnChanges,
1212
Output,
1313
} from '@angular/core';
14-
import { createSignal, extend, NgtArgs, NgtSignalStore } from 'angular-three';
14+
import { createSignal, extend, NgtArgs, NgtSignalStore, safeDetectChanges } from 'angular-three';
1515
import { AmbientLight, Group, PointLight, SpotLight, Vector2 } from 'three';
1616
import { NgtsAccumulativeShadows } from '../accumulative-shadows/accumulative-shadows';
1717
import { NgtsRandomizedLights } from '../accumulative-shadows/randomized-lights';
@@ -312,7 +312,7 @@ export class NgtsStage extends NgtSignalStore<NgtsStageProps> {
312312
}) {
313313
const { boundingSphere, width, height, depth } = props;
314314
this.boundingState.set({ radius: boundingSphere.radius, width, height, depth });
315-
this.#cdr.detectChanges();
315+
safeDetectChanges(this.#cdr);
316316
if (this.centered.observed) this.centered.emit(props);
317317
}
318318
}

0 commit comments

Comments
 (0)