3
3
import {
4
4
ChangeDetectionStrategy ,
5
5
Component ,
6
+ computed ,
6
7
CUSTOM_ELEMENTS_SCHEMA ,
7
8
type ElementRef ,
8
9
input ,
@@ -49,19 +50,43 @@ export class MainSphere {
49
50
selector : 'app-sphere-instances' ,
50
51
standalone : true ,
51
52
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" />
53
57
@for (position of initialPositions; track $index) {
54
- <ngt-mesh #mesh [material]="material() " [position]="position">
58
+ <ngt-mesh #mesh [material]="distortMaterial. material" [position]="position">
55
59
<ngt-icosahedron-geometry *args="[1, 4]" />
56
60
</ngt-mesh>
57
61
}
58
62
` ,
59
63
schemas : [ CUSTOM_ELEMENTS_SCHEMA ] ,
60
64
changeDetection : ChangeDetectionStrategy . OnPush ,
61
- imports : [ MainSphere , NgtArgs ] ,
65
+ imports : [ MainSphere , NgtArgs , NgtsMeshDistortMaterial ] ,
62
66
} )
63
67
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
+ } ) ) ;
65
90
66
91
initialPositions = [
67
92
[ - 4 , 20 , - 12 ] ,
@@ -74,17 +99,17 @@ export class SphereInstances {
74
99
[ 8 , 10 , - 20 ] ,
75
100
] ;
76
101
77
- meshesRef = viewChildren < ElementRef < Mesh > > ( 'mesh' ) ;
102
+ private meshesRef = viewChildren < ElementRef < Mesh > > ( 'mesh' ) ;
78
103
79
104
constructor ( ) {
80
105
injectBeforeRender ( ( ) => {
81
106
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 ;
88
113
} ) ;
89
114
} ) ;
90
115
}
@@ -96,29 +121,7 @@ export class SphereInstances {
96
121
<ngt-color attach="background" *args="['#050505']" />
97
122
<ngt-fog attach="fog" *args="['#161616', 8, 30]" />
98
123
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 />
122
125
123
126
<ngtp-effect-composer [options]="{ multisampling: 0, disableNormalPass: true }">
124
127
<ngtp-depth-of-field [options]="{ focusDistance: 0, focalLength: 0.02, bokehScale: 2, height: 480 }" />
@@ -128,25 +131,9 @@ export class SphereInstances {
128
131
` ,
129
132
schemas : [ CUSTOM_ELEMENTS_SCHEMA ] ,
130
133
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 ] ,
140
135
} )
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 { }
150
137
151
138
@Component ( {
152
139
standalone : true ,
0 commit comments