Browse Source

Merge pull request #17073 from higharc/use-onbeforecompile

Use onBeforeCompile to customize shader for instancing
WestLangley 6 years ago
parent
commit
23b9a85196
1 changed files with 106 additions and 136 deletions
  1. 106 136
      examples/webgl_buffergeometry_instancing_lambert.html

+ 106 - 136
examples/webgl_buffergeometry_instancing_lambert.html

@@ -34,147 +34,120 @@
 		import { OrbitControls } from './jsm/controls/OrbitControls.js';
 		import { Curves } from './jsm/curves/CurveExtras.js';
 
-		THREE.ShaderLib.customDepthRGBA = { // this is a cut-and-paste of the depth shader -- modified to accommodate instancing for this app
+		// this is a cut-and-paste of the depth shader -- modified to accommodate instancing for this app
+		var customDepthVertexShader = 
+			`
+			// instanced
+			attribute vec3 instanceOffset;
+			attribute float instanceScale;
 
-			uniforms: THREE.ShaderLib.depth.uniforms,
+			#include <common>
+			#include <uv_pars_vertex>
+			#include <displacementmap_pars_vertex>
+			#include <morphtarget_pars_vertex>
+			#include <skinning_pars_vertex>
+			#include <logdepthbuf_pars_vertex>
+			#include <clipping_planes_pars_vertex>
 
-			vertexShader:
-				`
-				// instanced
-				#ifdef INSTANCED
-
-					attribute vec3 instanceOffset;
-					attribute float instanceScale;
-
-				#endif
-
-				#include <common>
-				#include <uv_pars_vertex>
-				#include <displacementmap_pars_vertex>
-				#include <morphtarget_pars_vertex>
-				#include <skinning_pars_vertex>
-				#include <logdepthbuf_pars_vertex>
-				#include <clipping_planes_pars_vertex>
-
-				void main() {
-
-					#include <uv_vertex>
-
-					#include <skinbase_vertex>
-
-					#ifdef USE_DISPLACEMENTMAP
-
-						#include <beginnormal_vertex>
-						#include <morphnormal_vertex>
-						#include <skinnormal_vertex>
-
-					#endif
-
-					#include <begin_vertex>
-
-					// instanced
-					#ifdef INSTANCED
-
-						transformed *= instanceScale;
-						transformed = transformed + instanceOffset;
-
-					#endif
-
-					#include <morphtarget_vertex>
-					#include <skinning_vertex>
-					#include <displacementmap_vertex>
-					#include <project_vertex>
-					#include <logdepthbuf_vertex>
-					#include <clipping_planes_vertex>
-
-				}
-			`,
+			void main() {
 
-			fragmentShader: THREE.ShaderChunk.depth_frag
+				#include <uv_vertex>
 
-		};
+				#include <skinbase_vertex>
 
-		THREE.ShaderLib.lambert = { // this is a cut-and-paste of the lambert shader -- modified to accommodate instancing for this app
+				#ifdef USE_DISPLACEMENTMAP
 
-			uniforms: THREE.ShaderLib.lambert.uniforms,
-
-			vertexShader:
-				`
-				#define LAMBERT
+					#include <beginnormal_vertex>
+					#include <morphnormal_vertex>
+					#include <skinnormal_vertex>
 
-				#ifdef INSTANCED
-					attribute vec3 instanceOffset;
-					attribute vec3 instanceColor;
-					attribute float instanceScale;
 				#endif
 
-				varying vec3 vLightFront;
-				varying vec3 vIndirectFront;
+				#include <begin_vertex>
 
-				#ifdef DOUBLE_SIDED
-					varying vec3 vLightBack;
-					varying vec3 vIndirectBack;
-				#endif
-
-				#include <common>
-				#include <uv_pars_vertex>
-				#include <uv2_pars_vertex>
-				#include <envmap_pars_vertex>
-				#include <bsdfs>
-				#include <lights_pars_begin>
-				#include <color_pars_vertex>
-				#include <fog_pars_vertex>
-				#include <morphtarget_pars_vertex>
-				#include <skinning_pars_vertex>
-				#include <shadowmap_pars_vertex>
-				#include <logdepthbuf_pars_vertex>
-				#include <clipping_planes_pars_vertex>
-
-				void main() {
-
-					#include <uv_vertex>
-					#include <uv2_vertex>
-					#include <color_vertex>
-
-					// vertex colors instanced
-					#ifdef INSTANCED
-						#ifdef USE_COLOR
-							vColor.xyz = instanceColor.xyz;
-						#endif
-					#endif
+				// instanced
+				transformed *= instanceScale;
+				transformed = transformed + instanceOffset;
 
-					#include <beginnormal_vertex>
-					#include <morphnormal_vertex>
-					#include <skinbase_vertex>
-					#include <skinnormal_vertex>
-					#include <defaultnormal_vertex>
+				#include <morphtarget_vertex>
+				#include <skinning_vertex>
+				#include <displacementmap_vertex>
+				#include <project_vertex>
+				#include <logdepthbuf_vertex>
+				#include <clipping_planes_vertex>
 
-					#include <begin_vertex>
+			}
+			`;
+
+		// this is a cut-and-paste of the lambert shader -- modified to accommodate instancing for this app
+		var customLambertVertexShader =
+			`
+			#define LAMBERT
+
+			// instanced
+			attribute vec3 instanceOffset;
+			attribute vec3 instanceColor;
+			attribute float instanceScale;
+
+			varying vec3 vLightFront;
+			varying vec3 vIndirectFront;
+
+			#ifdef DOUBLE_SIDED
+				varying vec3 vLightBack;
+				varying vec3 vIndirectBack;
+			#endif
+
+			#include <common>
+			#include <uv_pars_vertex>
+			#include <uv2_pars_vertex>
+			#include <envmap_pars_vertex>
+			#include <bsdfs>
+			#include <lights_pars_begin>
+			#include <color_pars_vertex>
+			#include <fog_pars_vertex>
+			#include <morphtarget_pars_vertex>
+			#include <skinning_pars_vertex>
+			#include <shadowmap_pars_vertex>
+			#include <logdepthbuf_pars_vertex>
+			#include <clipping_planes_pars_vertex>
+
+			void main() {
+
+				#include <uv_vertex>
+				#include <uv2_vertex>
+				#include <color_vertex>
+
+				// vertex colors instanced
+				#ifdef USE_COLOR
+					vColor.xyz = instanceColor.xyz;
+				#endif
 
-					// position instanced
-					#ifdef INSTANCED
-						transformed *= instanceScale;
-						transformed = transformed + instanceOffset;
-					#endif
+				#include <beginnormal_vertex>
+				#include <morphnormal_vertex>
+				#include <skinbase_vertex>
+				#include <skinnormal_vertex>
+				#include <defaultnormal_vertex>
 
-					#include <morphtarget_vertex>
-					#include <skinning_vertex>
-					#include <project_vertex>
-					#include <logdepthbuf_vertex>
-					#include <clipping_planes_vertex>
+				#include <begin_vertex>
 
-					#include <worldpos_vertex>
-					#include <envmap_vertex>
-					#include <lights_lambert_vertex>
-					#include <shadowmap_vertex>
-					#include <fog_vertex>
+				// position instanced
+				transformed *= instanceScale;
+				transformed = transformed + instanceOffset;
 
-				}
-				`,
+				#include <morphtarget_vertex>
+				#include <skinning_vertex>
+				#include <project_vertex>
+				#include <logdepthbuf_vertex>
+				#include <clipping_planes_vertex>
 
-			fragmentShader: THREE.ShaderLib.lambert.fragmentShader
+				#include <worldpos_vertex>
+				#include <envmap_vertex>
+				#include <lights_lambert_vertex>
+				#include <shadowmap_vertex>
+				#include <fog_vertex>
 
-		};
+			}
+			`;
 
 
 		//
@@ -292,27 +265,24 @@
 
 			} );
 
-			material.defines = material.defines || {};
-			material.defines[ 'INSTANCED' ] = "";
+			material.onBeforeCompile = function( shader ) {
+
+				shader.vertexShader = customLambertVertexShader;
+
+			};
 
 
 			// custom depth material - required for instanced shadows
 
-			var shader = THREE.ShaderLib[ 'customDepthRGBA' ];
+			var customDepthMaterial = new THREE.MeshDepthMaterial();
 
-			var uniforms = THREE.UniformsUtils.clone( shader.uniforms );
+			customDepthMaterial.onBeforeCompile = function( shader ) {
 
-			var customDepthMaterial = new THREE.ShaderMaterial( {
+				shader.vertexShader = customDepthVertexShader;
 
-				defines: {
-					'INSTANCED': "",
-					'DEPTH_PACKING': THREE.RGBADepthPacking
-				},
-				uniforms: uniforms,
-				vertexShader: shader.vertexShader,
-				fragmentShader: shader.fragmentShader
+			};
 
-			} );
+			customDepthMaterial.depthPacking = THREE.RGBADepthPacking;
 
 			//