webgl_buffergeometry_uint.html 5.3 KB

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