webgl_buffergeometry_instancing_lambert.html 9.0 KB

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