Skip to content

Commit 88fbae4

Browse files
committed
fix(soba): clean up sampler even more
1 parent a8e2966 commit 88fbae4

File tree

2 files changed

+30
-44
lines changed

2 files changed

+30
-44
lines changed

apps/astro-docs/src/components/soba/scene-graph.ts

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -53,15 +53,6 @@ export class SceneGraph {
5353
afterNextRender(() => {
5454
untracked(() => {
5555
ref = this.anchor().createComponent(this.sobaContent());
56-
57-
// const componentInputs = this.storyMirror.inputs.map((input) => input.propName);
58-
// autoEffect(() => {
59-
// const storyOptions = this.storyOptions();
60-
// for (const key of componentInputs) {
61-
// ref.setInput(key, storyOptions[key]);
62-
// }
63-
// });
64-
6556
ref.changeDetectorRef.detectChanges();
6657
});
6758
});

libs/soba/misc/src/lib/sampler.ts

Lines changed: 30 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import {
2-
afterNextRender,
32
ChangeDetectionStrategy,
43
Component,
54
computed,
65
CUSTOM_ELEMENTS_SCHEMA,
76
effect,
87
ElementRef,
9-
inject,
108
Injector,
119
input,
1210
viewChild,
@@ -178,43 +176,40 @@ export class NgtsSampler {
178176
options = input(defaultOptions, { transform: mergeInputs(defaultOptions) });
179177
parameters = omit(this.options, ['weight', 'transform', 'count']);
180178

181-
groupRef = viewChild.required<ElementRef<Group>>('group');
182-
183-
private sampleState = computed(() => {
184-
const group = this.groupRef().nativeElement;
185-
const localState = getLocalState(group);
186-
if (!localState) return { mesh: null, instanced: null };
187-
188-
const [mesh, instances] = [resolveRef(this.mesh()), resolveRef(this.instances())];
189-
const objects = localState.objects();
190-
191-
return {
192-
mesh: mesh ?? (objects.find((c) => c.type === 'Mesh') as Mesh),
193-
instanced:
194-
instances ?? (objects.find((c) => !!Object.getOwnPropertyDescriptor(c, 'instanceMatrix')) as InstancedMesh),
195-
};
196-
});
179+
// NOTE: this could have been a viewChild.required, but we need to _try_ to consume
180+
// this Signal earlier than when a viewChild.required would resolve.
181+
groupRef = viewChild<ElementRef<Group>>('group');
197182

198183
constructor() {
199184
extend({ Group });
200-
const injector = inject(Injector);
201-
202-
afterNextRender(() => {
203-
const meshToSample = pick(this.sampleState, 'mesh');
204-
const instancedToSample = pick(this.sampleState, 'instanced');
205-
206-
const sampler = injectSurfaceSampler(
207-
meshToSample,
208-
() => ({
209-
count: this.options().count,
210-
transform: this.options().transform,
211-
weight: this.options().weight,
212-
instanceMesh: instancedToSample(),
213-
}),
214-
{ injector },
215-
);
216185

217-
effect(sampler, { injector });
186+
const sampleState = computed(() => {
187+
const group = this.groupRef()?.nativeElement;
188+
const localState = getLocalState(group);
189+
if (!localState) return { mesh: null, instanced: null };
190+
191+
const [mesh, instances] = [resolveRef(this.mesh()), resolveRef(this.instances())];
192+
const objects = localState.objects();
193+
194+
return {
195+
mesh: mesh ?? (objects.find((c) => c.type === 'Mesh') as Mesh),
196+
instanced:
197+
instances ?? (objects.find((c) => !!Object.getOwnPropertyDescriptor(c, 'instanceMatrix')) as InstancedMesh),
198+
};
218199
});
200+
201+
const meshToSample = pick(sampleState, 'mesh');
202+
const instancedToSample = pick(sampleState, 'instanced');
203+
204+
// NOTE: because injectSurfaceSampler returns a computed, we need to consume
205+
// this computed in a Reactive Context (an effect) to ensure the inner logic of
206+
// injectSurfaceSampler is run properly.
207+
const sampler = injectSurfaceSampler(meshToSample, () => ({
208+
count: this.options().count,
209+
transform: this.options().transform,
210+
weight: this.options().weight,
211+
instanceMesh: instancedToSample(),
212+
}));
213+
effect(sampler);
219214
}
220215
}

0 commit comments

Comments
 (0)