webgl_buffergeometry_uint.html 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  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. <link type="text/css" rel="stylesheet" href="main.css">
  8. </head>
  9. <body>
  10. <div id="container"></div>
  11. <div id="info"><a href="http://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - buffergeometry - uint</div>
  12. <script type="module">
  13. import {
  14. AmbientLight,
  15. BufferGeometry,
  16. Color,
  17. DirectionalLight,
  18. DoubleSide,
  19. Float32BufferAttribute,
  20. Fog,
  21. Int16BufferAttribute,
  22. Mesh,
  23. MeshPhongMaterial,
  24. PerspectiveCamera,
  25. Scene,
  26. Uint8BufferAttribute,
  27. Vector3,
  28. VertexColors,
  29. WebGLRenderer
  30. } from "../build/three.module.js";
  31. import Stats from './jsm/libs/stats.module.js';
  32. var container, stats;
  33. var camera, scene, renderer;
  34. var mesh;
  35. init();
  36. animate();
  37. function init() {
  38. container = document.getElementById( 'container' );
  39. //
  40. camera = new PerspectiveCamera( 27, window.innerWidth / window.innerHeight, 1, 3500 );
  41. camera.position.z = 2750;
  42. scene = new Scene();
  43. scene.background = new Color( 0x050505 );
  44. scene.fog = new Fog( 0x050505, 2000, 3500 );
  45. //
  46. scene.add( new AmbientLight( 0x444444 ) );
  47. var light1 = new DirectionalLight( 0xffffff, 0.5 );
  48. light1.position.set( 1, 1, 1 );
  49. scene.add( light1 );
  50. var light2 = new DirectionalLight( 0xffffff, 1.5 );
  51. light2.position.set( 0, - 1, 0 );
  52. scene.add( light2 );
  53. //
  54. var triangles = 500000;
  55. var geometry = new BufferGeometry();
  56. var positions = [];
  57. var normals = [];
  58. var colors = [];
  59. var color = new Color();
  60. var n = 800, n2 = n / 2; // triangles spread in the cube
  61. var d = 12, d2 = d / 2; // individual triangle size
  62. var pA = new Vector3();
  63. var pB = new Vector3();
  64. var pC = new Vector3();
  65. var cb = new Vector3();
  66. var ab = new Vector3();
  67. for ( var i = 0; i < triangles; i ++ ) {
  68. // positions
  69. var x = Math.random() * n - n2;
  70. var y = Math.random() * n - n2;
  71. var z = Math.random() * n - n2;
  72. var ax = x + Math.random() * d - d2;
  73. var ay = y + Math.random() * d - d2;
  74. var az = z + Math.random() * d - d2;
  75. var bx = x + Math.random() * d - d2;
  76. var by = y + Math.random() * d - d2;
  77. var bz = z + Math.random() * d - d2;
  78. var cx = x + Math.random() * d - d2;
  79. var cy = y + Math.random() * d - d2;
  80. var cz = z + Math.random() * d - d2;
  81. positions.push( ax, ay, az );
  82. positions.push( bx, by, bz );
  83. positions.push( cx, cy, cz );
  84. // flat face normals
  85. pA.set( ax, ay, az );
  86. pB.set( bx, by, bz );
  87. pC.set( cx, cy, cz );
  88. cb.subVectors( pC, pB );
  89. ab.subVectors( pA, pB );
  90. cb.cross( ab );
  91. cb.normalize();
  92. var nx = cb.x;
  93. var ny = cb.y;
  94. var nz = cb.z;
  95. normals.push( nx * 32767, ny * 32767, nz * 32767 );
  96. normals.push( nx * 32767, ny * 32767, nz * 32767 );
  97. normals.push( nx * 32767, ny * 32767, nz * 32767 );
  98. // colors
  99. var vx = ( x / n ) + 0.5;
  100. var vy = ( y / n ) + 0.5;
  101. var vz = ( z / n ) + 0.5;
  102. color.setRGB( vx, vy, vz );
  103. colors.push( color.r * 255, color.g * 255, color.b * 255 );
  104. colors.push( color.r * 255, color.g * 255, color.b * 255 );
  105. colors.push( color.r * 255, color.g * 255, color.b * 255 );
  106. }
  107. var positionAttribute = new Float32BufferAttribute( positions, 3 );
  108. var normalAttribute = new Int16BufferAttribute( normals, 3 );
  109. var colorAttribute = new Uint8BufferAttribute( colors, 3 );
  110. normalAttribute.normalized = true; // this will map the buffer values to 0.0f - +1.0f in the shader
  111. colorAttribute.normalized = true;
  112. geometry.addAttribute( 'position', positionAttribute );
  113. geometry.addAttribute( 'normal', normalAttribute );
  114. geometry.addAttribute( 'color', colorAttribute );
  115. geometry.computeBoundingSphere();
  116. var material = new MeshPhongMaterial( {
  117. color: 0xaaaaaa, specular: 0xffffff, shininess: 250,
  118. side: DoubleSide, vertexColors: VertexColors
  119. } );
  120. mesh = new Mesh( geometry, material );
  121. scene.add( mesh );
  122. //
  123. renderer = new WebGLRenderer();
  124. renderer.setPixelRatio( window.devicePixelRatio );
  125. renderer.setSize( window.innerWidth, window.innerHeight );
  126. renderer.gammaInput = true;
  127. renderer.gammaOutput = true;
  128. container.appendChild( renderer.domElement );
  129. //
  130. stats = new Stats();
  131. container.appendChild( stats.dom );
  132. //
  133. window.addEventListener( 'resize', onWindowResize, false );
  134. }
  135. function onWindowResize() {
  136. camera.aspect = window.innerWidth / window.innerHeight;
  137. camera.updateProjectionMatrix();
  138. renderer.setSize( window.innerWidth, window.innerHeight );
  139. }
  140. //
  141. function animate() {
  142. requestAnimationFrame( animate );
  143. render();
  144. stats.update();
  145. }
  146. function render() {
  147. var time = Date.now() * 0.001;
  148. mesh.rotation.x = time * 0.25;
  149. mesh.rotation.y = time * 0.5;
  150. renderer.render( scene, camera );
  151. }
  152. </script>
  153. </body>
  154. </html>