webgl_buffergeometry.html 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - buffergeometry</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. <style>
  8. body {
  9. color: #cccccc;
  10. font-family:Monospace;
  11. font-size:13px;
  12. text-align:center;
  13. background-color: #050505;
  14. margin: 0px;
  15. overflow: hidden;
  16. }
  17. #info {
  18. position: absolute;
  19. top: 0px; width: 100%;
  20. padding: 5px;
  21. }
  22. a {
  23. color: #0080ff;
  24. }
  25. </style>
  26. </head>
  27. <body>
  28. <div id="container"></div>
  29. <div id="info"><a href="http://threejs.org" target="_blank">three.js</a> webgl - buffergeometry</div>
  30. <script src="../build/three.min.js"></script>
  31. <script src="js/Detector.js"></script>
  32. <script src="js/libs/stats.min.js"></script>
  33. <script>
  34. if ( ! Detector.webgl ) Detector.addGetWebGLMessage();
  35. var container, stats;
  36. var camera, scene, renderer;
  37. var mesh;
  38. init();
  39. animate();
  40. function init() {
  41. container = document.getElementById( 'container' );
  42. //
  43. camera = new THREE.PerspectiveCamera( 27, window.innerWidth / window.innerHeight, 1, 3500 );
  44. camera.position.z = 2750;
  45. scene = new THREE.Scene();
  46. scene.fog = new THREE.Fog( 0x050505, 2000, 3500 );
  47. //
  48. scene.add( new THREE.AmbientLight( 0x444444 ) );
  49. var light1 = new THREE.DirectionalLight( 0xffffff, 0.5 );
  50. light1.position.set( 1, 1, 1 );
  51. scene.add( light1 );
  52. var light2 = new THREE.DirectionalLight( 0xffffff, 1.5 );
  53. light2.position.set( 0, -1, 0 );
  54. scene.add( light2 );
  55. //
  56. var triangles = 160000;
  57. var geometry = new THREE.BufferGeometry();
  58. geometry.attributes = {
  59. index: {
  60. itemSize: 1,
  61. array: new Uint16Array( triangles * 3 )
  62. },
  63. position: {
  64. itemSize: 3,
  65. array: new Float32Array( triangles * 3 * 3 )
  66. },
  67. normal: {
  68. itemSize: 3,
  69. array: new Float32Array( triangles * 3 * 3 )
  70. },
  71. color: {
  72. itemSize: 3,
  73. array: new Float32Array( triangles * 3 * 3 )
  74. }
  75. }
  76. // break geometry into
  77. // chunks of 21,845 triangles (3 unique vertices per triangle)
  78. // for indices to fit into 16 bit integer number
  79. // floor(2^16 / 3) = 21845
  80. var chunkSize = 21845;
  81. var indices = geometry.attributes.index.array;
  82. for ( var i = 0; i < indices.length; i ++ ) {
  83. indices[ i ] = i % ( 3 * chunkSize );
  84. }
  85. var positions = geometry.attributes.position.array;
  86. var normals = geometry.attributes.normal.array;
  87. var colors = geometry.attributes.color.array;
  88. var color = new THREE.Color();
  89. var n = 800, n2 = n/2; // triangles spread in the cube
  90. var d = 12, d2 = d/2; // individual triangle size
  91. var pA = new THREE.Vector3();
  92. var pB = new THREE.Vector3();
  93. var pC = new THREE.Vector3();
  94. var cb = new THREE.Vector3();
  95. var ab = new THREE.Vector3();
  96. for ( var i = 0; i < positions.length; i += 9 ) {
  97. // positions
  98. var x = Math.random() * n - n2;
  99. var y = Math.random() * n - n2;
  100. var z = Math.random() * n - n2;
  101. var ax = x + Math.random() * d - d2;
  102. var ay = y + Math.random() * d - d2;
  103. var az = z + Math.random() * d - d2;
  104. var bx = x + Math.random() * d - d2;
  105. var by = y + Math.random() * d - d2;
  106. var bz = z + Math.random() * d - d2;
  107. var cx = x + Math.random() * d - d2;
  108. var cy = y + Math.random() * d - d2;
  109. var cz = z + Math.random() * d - d2;
  110. positions[ i ] = ax;
  111. positions[ i + 1 ] = ay;
  112. positions[ i + 2 ] = az;
  113. positions[ i + 3 ] = bx;
  114. positions[ i + 4 ] = by;
  115. positions[ i + 5 ] = bz;
  116. positions[ i + 6 ] = cx;
  117. positions[ i + 7 ] = cy;
  118. positions[ i + 8 ] = cz;
  119. // flat face normals
  120. pA.set( ax, ay, az );
  121. pB.set( bx, by, bz );
  122. pC.set( cx, cy, cz );
  123. cb.subVectors( pC, pB );
  124. ab.subVectors( pA, pB );
  125. cb.cross( ab );
  126. cb.normalize();
  127. var nx = cb.x;
  128. var ny = cb.y;
  129. var nz = cb.z;
  130. normals[ i ] = nx;
  131. normals[ i + 1 ] = ny;
  132. normals[ i + 2 ] = nz;
  133. normals[ i + 3 ] = nx;
  134. normals[ i + 4 ] = ny;
  135. normals[ i + 5 ] = nz;
  136. normals[ i + 6 ] = nx;
  137. normals[ i + 7 ] = ny;
  138. normals[ i + 8 ] = nz;
  139. // colors
  140. var vx = ( x / n ) + 0.5;
  141. var vy = ( y / n ) + 0.5;
  142. var vz = ( z / n ) + 0.5;
  143. color.setRGB( vx, vy, vz );
  144. colors[ i ] = color.r;
  145. colors[ i + 1 ] = color.g;
  146. colors[ i + 2 ] = color.b;
  147. colors[ i + 3 ] = color.r;
  148. colors[ i + 4 ] = color.g;
  149. colors[ i + 5 ] = color.b;
  150. colors[ i + 6 ] = color.r;
  151. colors[ i + 7 ] = color.g;
  152. colors[ i + 8 ] = color.b;
  153. }
  154. geometry.offsets = [];
  155. var offsets = triangles / chunkSize;
  156. for ( var i = 0; i < offsets; i ++ ) {
  157. var offset = {
  158. start: i * chunkSize * 3,
  159. index: i * chunkSize * 3,
  160. count: Math.min( triangles - ( i * chunkSize ), chunkSize ) * 3
  161. };
  162. geometry.offsets.push( offset );
  163. }
  164. geometry.computeBoundingSphere();
  165. var material = new THREE.MeshPhongMaterial( {
  166. color: 0xaaaaaa, ambient: 0xaaaaaa, specular: 0xffffff, shininess: 250,
  167. side: THREE.DoubleSide, vertexColors: THREE.VertexColors
  168. } );
  169. mesh = new THREE.Mesh( geometry, material );
  170. scene.add( mesh );
  171. //
  172. renderer = new THREE.WebGLRenderer( { antialias: false, clearColor: 0x333333, clearAlpha: 1, alpha: false } );
  173. renderer.setSize( window.innerWidth, window.innerHeight );
  174. renderer.setClearColor( scene.fog.color, 1 );
  175. renderer.gammaInput = true;
  176. renderer.gammaOutput = true;
  177. renderer.physicallyBasedShading = true;
  178. container.appendChild( renderer.domElement );
  179. //
  180. stats = new Stats();
  181. stats.domElement.style.position = 'absolute';
  182. stats.domElement.style.top = '0px';
  183. container.appendChild( stats.domElement );
  184. //
  185. window.addEventListener( 'resize', onWindowResize, false );
  186. }
  187. function onWindowResize() {
  188. camera.aspect = window.innerWidth / window.innerHeight;
  189. camera.updateProjectionMatrix();
  190. renderer.setSize( window.innerWidth, window.innerHeight );
  191. }
  192. //
  193. function animate() {
  194. requestAnimationFrame( animate );
  195. render();
  196. stats.update();
  197. }
  198. function render() {
  199. var time = Date.now() * 0.001;
  200. mesh.rotation.x = time * 0.25;
  201. mesh.rotation.y = time * 0.5;
  202. renderer.render( scene, camera );
  203. }
  204. </script>
  205. </body>
  206. </html>