webgl_buffergeometry_instancing_lambert.html 8.5 KB

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