webgl_buffergeometry_uint.html 5.0 KB

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