webgl_buffergeometry.html 5.6 KB

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