webgl_buffergeometry_instancing_lambert.html 8.2 KB

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