webgl_buffergeometry_bufferDataTypes.html 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - buffergeometry - uint</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 - BufferAttributeTypes</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 = 500000;
  57. var geometry = new THREE.BufferGeometry();
  58. var indices = new Uint32Array( triangles * 3 );
  59. for ( var i = 0; i < indices.length; i ++ ) {
  60. indices[ i ] = i;
  61. }
  62. var positions = new Float32Array( triangles * 3 * 3 );
  63. var normals = new Int16Array( triangles * 3 * 3 );
  64. var colors = new Uint8Array( triangles * 3 * 3 );
  65. var color = new THREE.Color();
  66. var n = 800, n2 = n/2; // triangles spread in the cube
  67. var d = 12, d2 = d/2; // individual triangle size
  68. var pA = new THREE.Vector3();
  69. var pB = new THREE.Vector3();
  70. var pC = new THREE.Vector3();
  71. var cb = new THREE.Vector3();
  72. var ab = new THREE.Vector3();
  73. for ( var i = 0; i < positions.length; i += 9 ) {
  74. // positions
  75. var x = Math.random() * n - n2;
  76. var y = Math.random() * n - n2;
  77. var z = Math.random() * n - n2;
  78. var ax = x + Math.random() * d - d2;
  79. var ay = y + Math.random() * d - d2;
  80. var az = z + Math.random() * d - d2;
  81. var bx = x + Math.random() * d - d2;
  82. var by = y + Math.random() * d - d2;
  83. var bz = z + Math.random() * d - d2;
  84. var cx = x + Math.random() * d - d2;
  85. var cy = y + Math.random() * d - d2;
  86. var cz = z + Math.random() * d - d2;
  87. positions[ i ] = ax;
  88. positions[ i + 1 ] = ay;
  89. positions[ i + 2 ] = az;
  90. positions[ i + 3 ] = bx;
  91. positions[ i + 4 ] = by;
  92. positions[ i + 5 ] = bz;
  93. positions[ i + 6 ] = cx;
  94. positions[ i + 7 ] = cy;
  95. positions[ i + 8 ] = cz;
  96. // flat face normals
  97. pA.set( ax, ay, az );
  98. pB.set( bx, by, bz );
  99. pC.set( cx, cy, cz );
  100. cb.subVectors( pC, pB );
  101. ab.subVectors( pA, pB );
  102. cb.cross( ab );
  103. cb.normalize();
  104. var nx = cb.x;
  105. var ny = cb.y;
  106. var nz = cb.z;
  107. normals[ i ] = nx * 32767;
  108. normals[ i + 1 ] = ny * 32767;
  109. normals[ i + 2 ] = nz * 32767;
  110. normals[ i + 3 ] = nx * 32767;
  111. normals[ i + 4 ] = ny * 32767;
  112. normals[ i + 5 ] = nz * 32767;
  113. normals[ i + 6 ] = nx * 32767;
  114. normals[ i + 7 ] = ny * 32767;
  115. normals[ i + 8 ] = nz * 32767;
  116. // colors
  117. var vx = ( x / n ) + 0.5;
  118. var vy = ( y / n ) + 0.5;
  119. var vz = ( z / n ) + 0.5;
  120. color.setRGB( vx, vy, vz );
  121. colors[ i ] = color.r * 255;
  122. colors[ i + 1 ] = color.g * 255;
  123. colors[ i + 2 ] = color.b * 255;
  124. colors[ i + 3 ] = color.r * 255;
  125. colors[ i + 4 ] = color.g * 255;
  126. colors[ i + 5 ] = color.b * 255;
  127. colors[ i + 6 ] = color.r * 255;
  128. colors[ i + 7 ] = color.g * 255;
  129. colors[ i + 8 ] = color.b * 255;
  130. }
  131. geometry.setIndex( new THREE.BufferAttribute( indices, 1 ) );
  132. geometry.addAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
  133. geometry.addAttribute( 'normal', new THREE.BufferAttribute( normals, 3, true ) );
  134. geometry.addAttribute( 'color', new THREE.BufferAttribute( colors, 3, true ) );
  135. geometry.computeBoundingSphere();
  136. var material = new THREE.MeshPhongMaterial( {
  137. color: 0xaaaaaa, specular: 0xffffff, shininess: 250,
  138. side: THREE.DoubleSide, vertexColors: THREE.VertexColors
  139. } );
  140. mesh = new THREE.Mesh( geometry, material );
  141. scene.add( mesh );
  142. //
  143. renderer = new THREE.WebGLRenderer( { antialias: false } );
  144. renderer.setClearColor( scene.fog.color );
  145. renderer.setPixelRatio( window.devicePixelRatio );
  146. renderer.setSize( window.innerWidth, window.innerHeight );
  147. renderer.gammaInput = true;
  148. renderer.gammaOutput = true;
  149. container.appendChild( renderer.domElement );
  150. //
  151. stats = new Stats();
  152. stats.domElement.style.position = 'absolute';
  153. stats.domElement.style.top = '0px';
  154. container.appendChild( stats.domElement );
  155. //
  156. window.addEventListener( 'resize', onWindowResize, false );
  157. }
  158. function onWindowResize() {
  159. camera.aspect = window.innerWidth / window.innerHeight;
  160. camera.updateProjectionMatrix();
  161. renderer.setSize( window.innerWidth, window.innerHeight );
  162. }
  163. //
  164. function animate() {
  165. requestAnimationFrame( animate );
  166. render();
  167. stats.update();
  168. }
  169. function render() {
  170. var time = Date.now() * 0.001;
  171. mesh.rotation.x = time * 0.25;
  172. mesh.rotation.y = time * 0.5;
  173. renderer.render( scene, camera );
  174. }
  175. </script>
  176. </body>
  177. </html>