webgl_buffergeometry_instancing_lambert.html 8.3 KB

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