webgl_buffergeometry_instancing_lambert.html 8.8 KB

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