webgl_buffergeometry_instancing_lambert.html 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - instancing - lambert shader</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <style>
  8. body {
  9. color: #ffffff;
  10. font-family: Monospace;
  11. font-size: 13px;
  12. text-align: center;
  13. font-weight: bold;
  14. background-color: #000000;
  15. margin: 0px;
  16. overflow: hidden;
  17. }
  18. #info {
  19. position: absolute;
  20. top: 0px;
  21. width: 100%;
  22. padding: 5px;
  23. }
  24. a {
  25. color: #ffffff;
  26. }
  27. #notSupported {
  28. width: 50%;
  29. margin: auto;
  30. border: 2px red solid;
  31. margin-top: 20px;
  32. padding: 10px;
  33. }
  34. </style>
  35. </head>
  36. <body>
  37. <div id="container"></div>
  38. <div id="info">
  39. <a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> - instancing - lambert shader
  40. <div id="notSupported" style="display:none">Sorry your graphics card + browser does not support hardware instancing</div>
  41. </div>
  42. <script src="../build/three.js"></script>
  43. <script src="js/WebGL.js"></script>
  44. <script src="js/libs/stats.min.js"></script>
  45. <script src="js/controls/OrbitControls.js"></script>
  46. <script src="js/CurveExtras.js"></script>
  47. <script>
  48. if ( WEBGL.isWebGLAvailable() === false ) {
  49. document.body.appendChild( WEBGL.getWebGLErrorMessage() );
  50. }
  51. THREE.ShaderLib.customDepthRGBA = { // this is a cut-and-paste of the depth shader -- modified to accommodate instancing for this app
  52. uniforms: THREE.ShaderLib.depth.uniforms,
  53. vertexShader:
  54. `
  55. // instanced
  56. #ifdef INSTANCED
  57. attribute vec3 instanceOffset;
  58. attribute float instanceScale;
  59. #endif
  60. #include <common>
  61. #include <uv_pars_vertex>
  62. #include <displacementmap_pars_vertex>
  63. #include <morphtarget_pars_vertex>
  64. #include <skinning_pars_vertex>
  65. #include <logdepthbuf_pars_vertex>
  66. #include <clipping_planes_pars_vertex>
  67. void main() {
  68. #include <uv_vertex>
  69. #include <skinbase_vertex>
  70. #ifdef USE_DISPLACEMENTMAP
  71. #include <beginnormal_vertex>
  72. #include <morphnormal_vertex>
  73. #include <skinnormal_vertex>
  74. #endif
  75. #include <begin_vertex>
  76. // instanced
  77. #ifdef INSTANCED
  78. transformed *= instanceScale;
  79. transformed = transformed + instanceOffset;
  80. #endif
  81. #include <morphtarget_vertex>
  82. #include <skinning_vertex>
  83. #include <displacementmap_vertex>
  84. #include <project_vertex>
  85. #include <logdepthbuf_vertex>
  86. #include <clipping_planes_vertex>
  87. }
  88. `,
  89. fragmentShader: THREE.ShaderChunk.depth_frag
  90. };
  91. THREE.ShaderLib.lambert = { // this is a cut-and-paste of the lambert shader -- modified to accommodate instancing for this app
  92. uniforms: THREE.ShaderLib.lambert.uniforms,
  93. vertexShader:
  94. `
  95. #define LAMBERT
  96. #ifdef INSTANCED
  97. attribute vec3 instanceOffset;
  98. attribute vec3 instanceColor;
  99. attribute float instanceScale;
  100. #endif
  101. varying vec3 vLightFront;
  102. #ifdef DOUBLE_SIDED
  103. varying vec3 vLightBack;
  104. #endif
  105. #include <common>
  106. #include <uv_pars_vertex>
  107. #include <uv2_pars_vertex>
  108. #include <envmap_pars_vertex>
  109. #include <bsdfs>
  110. #include <lights_pars_begin>
  111. #include <color_pars_vertex>
  112. #include <fog_pars_vertex>
  113. #include <morphtarget_pars_vertex>
  114. #include <skinning_pars_vertex>
  115. #include <shadowmap_pars_vertex>
  116. #include <logdepthbuf_pars_vertex>
  117. #include <clipping_planes_pars_vertex>
  118. void main() {
  119. #include <uv_vertex>
  120. #include <uv2_vertex>
  121. #include <color_vertex>
  122. // vertex colors instanced
  123. #ifdef INSTANCED
  124. #ifdef USE_COLOR
  125. vColor.xyz = instanceColor.xyz;
  126. #endif
  127. #endif
  128. #include <beginnormal_vertex>
  129. #include <morphnormal_vertex>
  130. #include <skinbase_vertex>
  131. #include <skinnormal_vertex>
  132. #include <defaultnormal_vertex>
  133. #include <begin_vertex>
  134. // position instanced
  135. #ifdef INSTANCED
  136. transformed *= instanceScale;
  137. transformed = transformed + instanceOffset;
  138. #endif
  139. #include <morphtarget_vertex>
  140. #include <skinning_vertex>
  141. #include <project_vertex>
  142. #include <logdepthbuf_vertex>
  143. #include <clipping_planes_vertex>
  144. #include <worldpos_vertex>
  145. #include <envmap_vertex>
  146. #include <lights_lambert_vertex>
  147. #include <shadowmap_vertex>
  148. #include <fog_vertex>
  149. }
  150. `,
  151. fragmentShader: THREE.ShaderLib.lambert.fragmentShader
  152. };
  153. //
  154. var mesh, renderer, scene, camera, controls;
  155. var stats;
  156. init();
  157. animate();
  158. function init() {
  159. renderer = new THREE.WebGLRenderer( { antialias: true } );
  160. renderer.setSize( window.innerWidth, window.innerHeight );
  161. renderer.shadowMap.enabled = true;
  162. document.body.appendChild( renderer.domElement );
  163. renderer.gammaOutput = true;
  164. scene = new THREE.Scene();
  165. scene.fog = new THREE.FogExp2( 0x000000, 0.004 );
  166. renderer.setClearColor( scene.fog.color, 1 );
  167. camera = new THREE.PerspectiveCamera( 40, window.innerWidth / window.innerHeight, 1, 1000 );
  168. camera.position.set( 80, 40, 80 );
  169. scene.add( camera );
  170. controls = new THREE.OrbitControls( camera, renderer.domElement );
  171. controls.enableZoom = false;
  172. controls.maxPolarAngle = Math.PI / 2;
  173. scene.add( new THREE.AmbientLight( 0xffffff, 0.7 ) );
  174. var light = new THREE.DirectionalLight( 0xffffff, 0.4 );
  175. light.position.set( 50, 40, 0 );
  176. light.castShadow = true;
  177. light.shadow.camera.left = - 40;
  178. light.shadow.camera.right = 40;
  179. light.shadow.camera.top = 40;
  180. light.shadow.camera.bottom = - 40;
  181. light.shadow.camera.near = 10;
  182. light.shadow.camera.far = 180;
  183. light.shadow.bias = - 0.001;
  184. light.shadow.mapSize.width = 512;
  185. light.shadow.mapSize.height = 512;
  186. scene.add( light );
  187. // light shadow camera helper
  188. //light.shadowCameraHelper = new THREE.CameraHelper( light.shadow.camera );
  189. //scene.add( light.shadowCameraHelper );
  190. // instanced buffer geometry
  191. var geometry = new THREE.InstancedBufferGeometry();
  192. geometry.copy( new THREE.TorusBufferGeometry( 2, 0.5, 8, 128 ) );
  193. const INSTANCES = 256;
  194. var knot = new THREE.Curves.TorusKnot( 10 );
  195. var positions = knot.getSpacedPoints( INSTANCES );
  196. var offsets = new Float32Array( INSTANCES * 3 ); // xyz
  197. var colors = new Float32Array( INSTANCES * 3 ); // rgb
  198. var scales = new Float32Array( INSTANCES * 1 ); // s
  199. for ( var i = 0, l = INSTANCES; i < l; i ++ ) {
  200. var index = 3 * i;
  201. // per-instance position offset
  202. offsets[ index ] = positions[ i ].x;
  203. offsets[ index + 1 ] = positions[ i ].y;
  204. offsets[ index + 2 ] = positions[ i ].z;
  205. // per-instance color tint - optional
  206. colors[ index ] = 1;
  207. colors[ index + 1 ] = 1;
  208. colors[ index + 2 ] = 1;
  209. // per-instance scale variation
  210. scales[ i ] = 1 + 0.5 * Math.sin( 32 * Math.PI * i / INSTANCES );
  211. }
  212. geometry.addAttribute( 'instanceOffset', new THREE.InstancedBufferAttribute( offsets, 3 ) );
  213. geometry.addAttribute( 'instanceColor', new THREE.InstancedBufferAttribute( colors, 3 ) );
  214. geometry.addAttribute( 'instanceScale', new THREE.InstancedBufferAttribute( scales, 1 ) );
  215. // material
  216. var envMap = new THREE.TextureLoader().load( `textures/metal.jpg`, function ( texture ) {
  217. texture.mapping = THREE.SphericalReflectionMapping;
  218. texture.encoding = THREE.sRGBEncoding;
  219. if ( mesh ) mesh.material.needsUpdate = true;
  220. } );
  221. var material = new THREE.MeshLambertMaterial( {
  222. color: 0xffb54a,
  223. envMap: envMap,
  224. combine: THREE.MultiplyOperation,
  225. reflectivity: 0.8,
  226. vertexColors: THREE.VertexColors,
  227. fog: true
  228. } );
  229. material.defines = material.defines || {};
  230. material.defines[ 'INSTANCED'] = "";
  231. // custom depth material - required for instanced shadows
  232. var shader = THREE.ShaderLib[ 'customDepthRGBA' ];
  233. var uniforms = THREE.UniformsUtils.clone( shader.uniforms );
  234. var customDepthMaterial = new THREE.ShaderMaterial( {
  235. defines: {
  236. 'INSTANCED': "",
  237. 'DEPTH_PACKING': THREE.RGBADepthPacking
  238. },
  239. uniforms: uniforms,
  240. vertexShader: shader.vertexShader,
  241. fragmentShader: shader.fragmentShader
  242. } );
  243. //
  244. mesh = new THREE.Mesh( geometry, material );
  245. mesh.scale.set( 1, 1, 2 );
  246. mesh.castShadow = true;
  247. mesh.receiveShadow = true;
  248. mesh.customDepthMaterial = customDepthMaterial;
  249. mesh.frustumCulled = false;
  250. scene.add( mesh );
  251. //
  252. var ground = new THREE.Mesh(
  253. new THREE.PlaneBufferGeometry( 800, 800 ).rotateX( - Math.PI / 2 ),
  254. new THREE.MeshPhongMaterial( { color: 0x888888 } )
  255. );
  256. ground.position.set( 0, - 40, 0 );
  257. ground.receiveShadow = true;
  258. scene.add( ground );
  259. //
  260. stats = new Stats();
  261. document.body.appendChild( stats.dom );
  262. //
  263. window.addEventListener( 'resize', onWindowResize, false );
  264. }
  265. function onWindowResize( event ) {
  266. renderer.setSize( window.innerWidth, window.innerHeight );
  267. camera.aspect = window.innerWidth / window.innerHeight;
  268. camera.updateProjectionMatrix();
  269. }
  270. function animate() {
  271. requestAnimationFrame( animate );
  272. mesh.rotation.y += 0.005;
  273. stats.update();
  274. renderer.render( scene, camera );
  275. }
  276. </script>
  277. </body>
  278. </html>