webgl_buffergeometry_instancing_lambert.html 8.7 KB

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