Skip to content

Commit affed6c

Browse files
committed
feat(soba): materials
1 parent b4690fc commit affed6c

File tree

5 files changed

+33
-22
lines changed

5 files changed

+33
-22
lines changed

libs/soba/materials/src/lib/custom-shader-material.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export class NgtsCustomShaderMaterial {
2929
options = input({} as Omit<ConstructorParameters<typeof CustomShaderMaterial>[0], 'baseMaterial'>);
3030
parameters = omit(this.options, ['fragmentShader', 'vertexShader', 'uniforms', 'cacheKey']);
3131

32-
base = computed(() => {
32+
private base = computed(() => {
3333
const baseMaterial = this.baseMaterial();
3434
return is.ref(baseMaterial) ? baseMaterial.nativeElement : baseMaterial;
3535
});

libs/soba/materials/src/lib/mesh-distort-material.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ChangeDetectionStrategy, Component, CUSTOM_ELEMENTS_SCHEMA, input } from '@angular/core';
1+
import { ChangeDetectionStrategy, Component, CUSTOM_ELEMENTS_SCHEMA, DestroyRef, inject, input } from '@angular/core';
22
import { injectBeforeRender, NgtArgs, NgtAttachable, NgtMeshPhysicalMaterial, omit, pick } from 'angular-three';
33
import { MeshDistortMaterial, MeshDistortMaterialParameters } from 'angular-three-soba/vanilla-exports';
44
import { mergeInputs } from 'ngxtension/inject-inputs';
@@ -36,6 +36,8 @@ export class NgtsMeshDistortMaterial {
3636
private speed = pick(this.options, 'speed');
3737

3838
constructor() {
39+
inject(DestroyRef).onDestroy(() => this.material.dispose());
40+
3941
injectBeforeRender(({ clock }) => {
4042
this.material.time = clock.getElapsedTime() * this.speed();
4143
});

libs/soba/materials/src/lib/mesh-portal-material.ts

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { NgTemplateOutlet } from '@angular/common';
22
import {
3-
afterNextRender,
43
ChangeDetectionStrategy,
54
Component,
65
computed,
@@ -92,17 +91,19 @@ export class ManagePortalScene {
9291
return [quad, blend] as const;
9392
});
9493

94+
effect(() => {
95+
portalScene().matrixAutoUpdate = false;
96+
});
97+
9598
effect(() => {
9699
const [events, setEvents] = [this.events(), portalSetEvents()];
97100
if (!events) return;
98101
setEvents({ enabled: events });
99102
});
100103

101-
afterNextRender(() => {
102-
portalScene().matrixAutoUpdate = false;
103-
});
104-
105104
effect((onCleanup) => {
105+
const priority = this.priority();
106+
106107
// we start the before render in effect because we need the priority input to be resolved
107108
const sub = injectBeforeRender(
108109
({ gl, camera }) => {
@@ -115,9 +116,8 @@ export class ManagePortalScene {
115116
if (!parent) return;
116117

117118
const materialBlend = 'blend' in material && typeof material.blend === 'number' ? material.blend : 0;
118-
const [worldUnits, priority, rootScene, scene, [quad, blend]] = [
119+
const [worldUnits, rootScene, scene, [quad, blend]] = [
119120
this.worldUnits(),
120-
this.priority(),
121121
this.rootScene(),
122122
portalScene(),
123123
fullScreenQuad(),
@@ -150,7 +150,7 @@ export class ManagePortalScene {
150150
}
151151
}
152152
},
153-
{ injector, priority: this.priority() },
153+
{ injector, priority },
154154
);
155155
onCleanup(() => sub());
156156
});
@@ -285,19 +285,22 @@ export class NgtsMeshPortalMaterial {
285285
constructor() {
286286
extend({ MeshPortalMaterial });
287287

288-
afterNextRender(() => {
289-
const material = this.materialRef().nativeElement;
288+
effect(
289+
() => {
290+
const material = this.materialRef().nativeElement;
290291

291-
const localState = getLocalState(material);
292-
if (!localState) return;
292+
const localState = getLocalState(material);
293+
if (!localState) return;
293294

294-
const materialParent = localState.parent();
295-
if (!materialParent || !(materialParent instanceof Mesh)) return;
295+
const materialParent = localState.parent();
296+
if (!materialParent || !(materialParent instanceof Mesh)) return;
296297

297-
// Since the ref above is not tied to a mesh directly (we're inside a material),
298-
// it has to be tied to the parent mesh here
299-
this.parent.set(materialParent);
300-
});
298+
// Since the ref above is not tied to a mesh directly (we're inside a material),
299+
// it has to be tied to the parent mesh here
300+
this.parent.set(materialParent);
301+
},
302+
{ allowSignalWrites: true },
303+
);
301304

302305
effect(() => {
303306
const events = this.events();

libs/soba/materials/src/lib/mesh-wobble-material.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ChangeDetectionStrategy, Component, CUSTOM_ELEMENTS_SCHEMA, input } from '@angular/core';
1+
import { ChangeDetectionStrategy, Component, CUSTOM_ELEMENTS_SCHEMA, DestroyRef, inject, input } from '@angular/core';
22
import { injectBeforeRender, NgtArgs, NgtAttachable, NgtMeshStandardMaterial, omit } from 'angular-three';
33
import { MeshWobbleMaterial, MeshWobbleMaterialParameters } from 'angular-three-soba/vanilla-exports';
44
import { mergeInputs } from 'ngxtension/inject-inputs';
@@ -31,6 +31,8 @@ export class NgtsMeshWobbleMaterial {
3131
material = new MeshWobbleMaterial();
3232

3333
constructor() {
34+
inject(DestroyRef).onDestroy(() => this.material.dispose());
35+
3436
injectBeforeRender(({ clock }) => {
3537
const material = this.material;
3638
material.time = clock.elapsedTime * this.options().speed;

libs/soba/materials/src/lib/point-material.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ChangeDetectionStrategy, Component, CUSTOM_ELEMENTS_SCHEMA, input } from '@angular/core';
1+
import { ChangeDetectionStrategy, Component, CUSTOM_ELEMENTS_SCHEMA, DestroyRef, inject, input } from '@angular/core';
22
import { NgtArgs, NgtAttachable } from 'angular-three';
33
import { PointMaterial } from 'angular-three-soba/shaders';
44
import { PointsMaterialParameters } from 'three';
@@ -20,4 +20,8 @@ export class NgtsPointMaterial {
2020
options = input({} as PointsMaterialParameters);
2121

2222
material = new PointMaterial(this.options());
23+
24+
constructor() {
25+
inject(DestroyRef).onDestroy(() => this.material.dispose());
26+
}
2327
}

0 commit comments

Comments
 (0)