Skip to content

Commit cd97b90

Browse files
authored
Merge pull request #14069 from WestLangley/dev-instancing_lambert2
Lambert instancing example: replace ShaderMaterial with modified MeshLambertMaterial
2 parents e026cef + b7e9000 commit cd97b90

File tree

1 file changed

+115
-136
lines changed

1 file changed

+115
-136
lines changed

examples/webgl_buffergeometry_instancing_lambert.html

Lines changed: 115 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -56,153 +56,150 @@
5656

5757
if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
5858

59-
THREE.CustomShaderLib = {
59+
THREE.ShaderLib.customDepthRGBA = { // this is a cut-and-paste of the depth shader -- modified to accommodate instancing for this app
6060

61-
customDepthRGBA: { // this is a cut-and-paste of the depth shader -- modified to accommodate instancing for this app
61+
uniforms: THREE.ShaderLib.depth.uniforms,
6262

63-
uniforms: THREE.ShaderLib.depth.uniforms,
63+
vertexShader:
64+
`
65+
// instanced
66+
#ifdef INSTANCED
6467
65-
vertexShader:
66-
`
67-
// instanced
68-
#ifdef INSTANCED
68+
attribute vec3 instanceOffset;
69+
attribute float instanceScale;
6970
70-
attribute vec3 instanceOffset;
71-
attribute float instanceScale;
71+
#endif
7272
73-
#endif
73+
#include <common>
74+
#include <uv_pars_vertex>
75+
#include <displacementmap_pars_vertex>
76+
#include <morphtarget_pars_vertex>
77+
#include <skinning_pars_vertex>
78+
#include <logdepthbuf_pars_vertex>
79+
#include <clipping_planes_pars_vertex>
7480
75-
#include <common>
76-
#include <uv_pars_vertex>
77-
#include <displacementmap_pars_vertex>
78-
#include <morphtarget_pars_vertex>
79-
#include <skinning_pars_vertex>
80-
#include <logdepthbuf_pars_vertex>
81-
#include <clipping_planes_pars_vertex>
81+
void main() {
8282
83-
void main() {
83+
#include <uv_vertex>
8484
85-
#include <uv_vertex>
85+
#include <skinbase_vertex>
8686
87-
#include <skinbase_vertex>
87+
#ifdef USE_DISPLACEMENTMAP
8888
89-
#ifdef USE_DISPLACEMENTMAP
89+
#include <beginnormal_vertex>
90+
#include <morphnormal_vertex>
91+
#include <skinnormal_vertex>
9092
91-
#include <beginnormal_vertex>
92-
#include <morphnormal_vertex>
93-
#include <skinnormal_vertex>
93+
#endif
9494
95-
#endif
95+
#include <begin_vertex>
9696
97-
#include <begin_vertex>
97+
// instanced
98+
#ifdef INSTANCED
9899
99-
// instanced
100-
#ifdef INSTANCED
100+
transformed *= instanceScale;
101+
transformed = transformed + instanceOffset;
101102
102-
transformed *= instanceScale;
103-
transformed = transformed + instanceOffset;
103+
#endif
104104
105-
#endif
105+
#include <morphtarget_vertex>
106+
#include <skinning_vertex>
107+
#include <displacementmap_vertex>
108+
#include <project_vertex>
109+
#include <logdepthbuf_vertex>
110+
#include <clipping_planes_vertex>
106111
107-
#include <morphtarget_vertex>
108-
#include <skinning_vertex>
109-
#include <displacementmap_vertex>
110-
#include <project_vertex>
111-
#include <logdepthbuf_vertex>
112-
#include <clipping_planes_vertex>
112+
}
113+
`,
113114

114-
}
115-
`,
115+
fragmentShader: THREE.ShaderChunk.depth_frag
116116

117-
fragmentShader: THREE.ShaderChunk.depth_frag
117+
};
118118

119-
},
119+
THREE.ShaderLib.lambert = { // this is a cut-and-paste of the lambert shader -- modified to accommodate instancing for this app
120120

121-
lambert: { // this is a cut-and-paste of the lambert shader -- modified to accommodate instancing for this app
121+
uniforms: THREE.ShaderLib.lambert.uniforms,
122122

123-
uniforms: THREE.ShaderLib.lambert.uniforms,
123+
vertexShader:
124+
`
125+
#define LAMBERT
124126
125-
vertexShader:
126-
`
127-
#define LAMBERT
127+
#ifdef INSTANCED
128+
attribute vec3 instanceOffset;
129+
attribute vec3 instanceColor;
130+
attribute float instanceScale;
131+
#endif
128132
129-
#ifdef INSTANCED
130-
attribute vec3 instanceOffset;
131-
attribute vec3 instanceColor;
132-
attribute float instanceScale;
133-
#endif
133+
varying vec3 vLightFront;
134134
135-
varying vec3 vLightFront;
135+
#ifdef DOUBLE_SIDED
136136
137-
#ifdef DOUBLE_SIDED
137+
varying vec3 vLightBack;
138138
139-
varying vec3 vLightBack;
139+
#endif
140140
141-
#endif
141+
#include <common>
142+
#include <uv_pars_vertex>
143+
#include <uv2_pars_vertex>
144+
#include <envmap_pars_vertex>
145+
#include <bsdfs>
146+
#include <lights_pars_begin>
147+
#include <lights_pars_maps>
148+
#include <color_pars_vertex>
149+
#include <fog_pars_vertex>
150+
#include <morphtarget_pars_vertex>
151+
#include <skinning_pars_vertex>
152+
#include <shadowmap_pars_vertex>
153+
#include <logdepthbuf_pars_vertex>
154+
#include <clipping_planes_pars_vertex>
142155
143-
#include <common>
144-
#include <uv_pars_vertex>
145-
#include <uv2_pars_vertex>
146-
#include <envmap_pars_vertex>
147-
#include <bsdfs>
148-
#include <lights_pars_begin>
149-
#include <lights_pars_maps>
150-
#include <color_pars_vertex>
151-
#include <fog_pars_vertex>
152-
#include <morphtarget_pars_vertex>
153-
#include <skinning_pars_vertex>
154-
#include <shadowmap_pars_vertex>
155-
#include <logdepthbuf_pars_vertex>
156-
#include <clipping_planes_pars_vertex>
157-
158-
void main() {
159-
160-
#include <uv_vertex>
161-
#include <uv2_vertex>
162-
#include <color_vertex>
163-
164-
// vertex colors instanced
165-
#ifdef INSTANCED
166-
#ifdef USE_COLOR
167-
vColor.xyz = instanceColor.xyz;
168-
#endif
169-
#endif
156+
void main() {
170157
171-
#include <beginnormal_vertex>
172-
#include <morphnormal_vertex>
173-
#include <skinbase_vertex>
174-
#include <skinnormal_vertex>
175-
#include <defaultnormal_vertex>
158+
#include <uv_vertex>
159+
#include <uv2_vertex>
160+
#include <color_vertex>
176161
177-
#include <begin_vertex>
178-
179-
// position instanced
180-
#ifdef INSTANCED
181-
transformed *= instanceScale;
182-
transformed = transformed + instanceOffset;
162+
// vertex colors instanced
163+
#ifdef INSTANCED
164+
#ifdef USE_COLOR
165+
vColor.xyz = instanceColor.xyz;
183166
#endif
167+
#endif
168+
169+
#include <beginnormal_vertex>
170+
#include <morphnormal_vertex>
171+
#include <skinbase_vertex>
172+
#include <skinnormal_vertex>
173+
#include <defaultnormal_vertex>
174+
175+
#include <begin_vertex>
184176
185-
#include <morphtarget_vertex>
186-
#include <skinning_vertex>
187-
#include <project_vertex>
188-
#include <logdepthbuf_vertex>
189-
#include <clipping_planes_vertex>
177+
// position instanced
178+
#ifdef INSTANCED
179+
transformed *= instanceScale;
180+
transformed = transformed + instanceOffset;
181+
#endif
190182
191-
#include <worldpos_vertex>
192-
#include <envmap_vertex>
193-
#include <lights_lambert_vertex>
194-
#include <shadowmap_vertex>
195-
#include <fog_vertex>
183+
#include <morphtarget_vertex>
184+
#include <skinning_vertex>
185+
#include <project_vertex>
186+
#include <logdepthbuf_vertex>
187+
#include <clipping_planes_vertex>
196188
197-
}
198-
`,
189+
#include <worldpos_vertex>
190+
#include <envmap_vertex>
191+
#include <lights_lambert_vertex>
192+
#include <shadowmap_vertex>
193+
#include <fog_vertex>
199194
200-
fragmentShader: THREE.ShaderLib.lambert.fragmentShader
195+
}
196+
`,
201197

202-
}
198+
fragmentShader: THREE.ShaderLib.lambert.fragmentShader
203199

204200
};
205201

202+
206203
//
207204

208205
var mesh, renderer, scene, camera, controls;
@@ -303,48 +300,30 @@
303300
var envMap = new THREE.TextureLoader().load( `textures/metal.jpg`, function ( texture ) {
304301

305302
texture.mapping = THREE.SphericalReflectionMapping;
303+
texture.encoding = THREE.sRGBEncoding;
304+
if ( mesh ) mesh.material.needsUpdate = true;
306305

307306
} );
308307

309-
var shader = THREE.CustomShaderLib[ 'lambert' ];
308+
var material = new THREE.MeshLambertMaterial( {
310309

311-
var uniforms = THREE.UniformsUtils.clone( shader.uniforms );
312-
313-
uniforms[ "diffuse" ].value.set( 0xffb54a );
314-
uniforms[ "envMap" ].value = envMap;
315-
uniforms[ "reflectivity" ].value = 1;
316-
317-
// defines - Since we are reusing the ShaderChunks, we must specify the required defines.
318-
// The renderer does not set these defines for ShaderMaterial
319-
320-
var defines = {
321-
'INSTANCED': "",
322-
'USE_ENVMAP': "",
323-
'ENVMAP_TYPE_SPHERE': "",
324-
'ENVMAP_MODE_REFLECTION': "",
325-
'ENVMAP_BLENDING_MULTIPLY': "",
326-
};
327-
328-
var material = new THREE.ShaderMaterial( {
329-
330-
// Material and ShaderMaterial properties can be set here -- except opacity
331-
// the Lambert-properties must be set in the uniforms or defines
332-
333-
defines: defines,
334-
uniforms: uniforms,
335-
vertexShader: shader.vertexShader,
336-
fragmentShader: shader.fragmentShader,
310+
color: 0xffb54a,
311+
envMap: envMap,
312+
combine: THREE.MultiplyOperation,
313+
reflectivity: 0.8,
337314

338315
vertexColors: THREE.VertexColors,
339-
lights: true,
340316
fog: true
341317

342318
} );
343319

320+
material.defines = material.defines || {};
321+
material.defines[ 'INSTANCED'] = "";
322+
344323

345324
// custom depth material - required for instanced shadows
346325

347-
var shader = THREE.CustomShaderLib[ 'customDepthRGBA' ];
326+
var shader = THREE.ShaderLib[ 'customDepthRGBA' ];
348327

349328
var uniforms = THREE.UniformsUtils.clone( shader.uniforms );
350329

0 commit comments

Comments
 (0)