Skip to content

Commit 7b7230a

Browse files
committed
docs: simplify postprocessing example
1 parent 0bbea62 commit 7b7230a

File tree

1 file changed

+39
-52
lines changed
  • apps/astro-docs/src/components/postprocessing

1 file changed

+39
-52
lines changed

apps/astro-docs/src/components/postprocessing/sample.ts

Lines changed: 39 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import {
44
ChangeDetectionStrategy,
55
Component,
6+
computed,
67
CUSTOM_ELEMENTS_SCHEMA,
78
type ElementRef,
89
input,
@@ -49,19 +50,43 @@ export class MainSphere {
4950
selector: 'app-sphere-instances',
5051
standalone: true,
5152
template: `
52-
<app-main-sphere [material]="material()" />
53+
<!-- we render the material with attach="none" so we can share it between instances -->
54+
<ngts-mesh-distort-material #distortMaterial attach="none" [options]="materialOptions()" />
55+
56+
<app-main-sphere [material]="distortMaterial.material" />
5357
@for (position of initialPositions; track $index) {
54-
<ngt-mesh #mesh [material]="material()" [position]="position">
58+
<ngt-mesh #mesh [material]="distortMaterial.material" [position]="position">
5559
<ngt-icosahedron-geometry *args="[1, 4]" />
5660
</ngt-mesh>
5761
}
5862
`,
5963
schemas: [CUSTOM_ELEMENTS_SCHEMA],
6064
changeDetection: ChangeDetectionStrategy.OnPush,
61-
imports: [MainSphere, NgtArgs],
65+
imports: [MainSphere, NgtArgs, NgtsMeshDistortMaterial],
6266
})
6367
export class SphereInstances {
64-
material = input.required<Material>();
68+
private envMap = injectLoader(
69+
// @ts-expect-error - CubeTextureLoader is ok
70+
() => CubeTextureLoader,
71+
() => [['px.png', 'nx.png', 'py.png', 'ny.png', 'pz.png', 'nz.png']],
72+
{ extensions: (loader) => loader.setPath('/cube/') },
73+
);
74+
private bumpMap = injectTexture(() => '/bump.jpg');
75+
76+
materialOptions = computed(() => ({
77+
envMap: this.envMap()?.[0],
78+
bumpMap: this.bumpMap(),
79+
emissive: '#010101',
80+
emissiveIntensity: 2,
81+
roughness: 0.1,
82+
metalness: 1,
83+
bumpScale: 0.005,
84+
clearcoat: 1,
85+
clearcoatRoughness: 1,
86+
radius: 1,
87+
distort: 0.4,
88+
toneMapped: false,
89+
}));
6590

6691
initialPositions = [
6792
[-4, 20, -12],
@@ -74,17 +99,17 @@ export class SphereInstances {
7499
[8, 10, -20],
75100
];
76101

77-
meshesRef = viewChildren<ElementRef<Mesh>>('mesh');
102+
private meshesRef = viewChildren<ElementRef<Mesh>>('mesh');
78103

79104
constructor() {
80105
injectBeforeRender(() => {
81106
const meshes = this.meshesRef();
82-
meshes.forEach((mesh) => {
83-
mesh.nativeElement.position.y += 0.02;
84-
if (mesh.nativeElement.position.y > 19) mesh.nativeElement.position.y = -18;
85-
mesh.nativeElement.rotation.x += 0.06;
86-
mesh.nativeElement.rotation.y += 0.06;
87-
mesh.nativeElement.rotation.z += 0.02;
107+
meshes.forEach(({ nativeElement: mesh }) => {
108+
mesh.position.y += 0.02;
109+
if (mesh.position.y > 19) mesh.position.y = -18;
110+
mesh.rotation.x += 0.06;
111+
mesh.rotation.y += 0.06;
112+
mesh.rotation.z += 0.02;
88113
});
89114
});
90115
}
@@ -96,29 +121,7 @@ export class SphereInstances {
96121
<ngt-color attach="background" *args="['#050505']" />
97122
<ngt-fog attach="fog" *args="['#161616', 8, 30]" />
98123
99-
<!-- we render the material with attach="none" so we can share it between instances -->
100-
<ngts-mesh-distort-material
101-
#distortMaterial
102-
attach="none"
103-
[options]="{
104-
envMap: envMap()?.[0],
105-
bumpMap: bumpMap(),
106-
emissive: '#010101',
107-
emissiveIntensity: 2,
108-
roughness: 0.1,
109-
metalness: 1,
110-
bumpScale: 0.005,
111-
clearcoat: 1,
112-
clearcoatRoughness: 1,
113-
radius: 1,
114-
distort: 0.4,
115-
toneMapped: false,
116-
}"
117-
/>
118-
119-
@if (distortMaterial.material) {
120-
<app-sphere-instances [material]="distortMaterial.material" />
121-
}
124+
<app-sphere-instances />
122125
123126
<ngtp-effect-composer [options]="{ multisampling: 0, disableNormalPass: true }">
124127
<ngtp-depth-of-field [options]="{ focusDistance: 0, focalLength: 0.02, bokehScale: 2, height: 480 }" />
@@ -128,25 +131,9 @@ export class SphereInstances {
128131
`,
129132
schemas: [CUSTOM_ELEMENTS_SCHEMA],
130133
changeDetection: ChangeDetectionStrategy.OnPush,
131-
imports: [
132-
NgtsMeshDistortMaterial,
133-
SphereInstances,
134-
NgtArgs,
135-
NgtpEffectComposer,
136-
NgtpDepthOfField,
137-
NgtpBloom,
138-
NgtpVignette,
139-
],
134+
imports: [SphereInstances, NgtArgs, NgtpEffectComposer, NgtpDepthOfField, NgtpBloom, NgtpVignette],
140135
})
141-
export class SceneGraph {
142-
envMap = injectLoader(
143-
// @ts-expect-error - CubeTextureLoader is ok
144-
() => CubeTextureLoader,
145-
() => [['px.png', 'nx.png', 'py.png', 'ny.png', 'pz.png', 'nz.png']],
146-
{ extensions: (loader) => loader.setPath('/cube/') },
147-
);
148-
bumpMap = injectTexture(() => '/bump.jpg');
149-
}
136+
export class SceneGraph {}
150137

151138
@Component({
152139
standalone: true,

0 commit comments

Comments
 (0)