webgl_interactive_buffergeometry.html 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - interactive - 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. <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 - interactive - buffergeometry</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 raycaster, pointer;
  26. let mesh, line;
  27. init();
  28. function init() {
  29. container = document.getElementById( 'container' );
  30. //
  31. camera = new THREE.PerspectiveCamera( 27, window.innerWidth / window.innerHeight, 1, 3500 );
  32. camera.position.z = 2750;
  33. scene = new THREE.Scene();
  34. scene.background = new THREE.Color( 0x050505 );
  35. scene.fog = new THREE.Fog( 0x050505, 2000, 3500 );
  36. //
  37. scene.add( new THREE.AmbientLight( 0x444444, 3 ) );
  38. const light1 = new THREE.DirectionalLight( 0xffffff, 1.5 );
  39. light1.position.set( 1, 1, 1 );
  40. scene.add( light1 );
  41. const light2 = new THREE.DirectionalLight( 0xffffff, 4.5 );
  42. light2.position.set( 0, - 1, 0 );
  43. scene.add( light2 );
  44. //
  45. const triangles = 5000;
  46. let geometry = new THREE.BufferGeometry();
  47. const positions = new Float32Array( triangles * 3 * 3 );
  48. const normals = new Float32Array( triangles * 3 * 3 );
  49. const colors = new Float32Array( triangles * 3 * 3 );
  50. const color = new THREE.Color();
  51. const n = 800, n2 = n / 2; // triangles spread in the cube
  52. const d = 120, d2 = d / 2; // individual triangle size
  53. const pA = new THREE.Vector3();
  54. const pB = new THREE.Vector3();
  55. const pC = new THREE.Vector3();
  56. const cb = new THREE.Vector3();
  57. const ab = new THREE.Vector3();
  58. for ( let i = 0; i < positions.length; i += 9 ) {
  59. // positions
  60. const x = Math.random() * n - n2;
  61. const y = Math.random() * n - n2;
  62. const z = Math.random() * n - n2;
  63. const ax = x + Math.random() * d - d2;
  64. const ay = y + Math.random() * d - d2;
  65. const az = z + Math.random() * d - d2;
  66. const bx = x + Math.random() * d - d2;
  67. const by = y + Math.random() * d - d2;
  68. const bz = z + Math.random() * d - d2;
  69. const cx = x + Math.random() * d - d2;
  70. const cy = y + Math.random() * d - d2;
  71. const cz = z + Math.random() * d - d2;
  72. positions[ i ] = ax;
  73. positions[ i + 1 ] = ay;
  74. positions[ i + 2 ] = az;
  75. positions[ i + 3 ] = bx;
  76. positions[ i + 4 ] = by;
  77. positions[ i + 5 ] = bz;
  78. positions[ i + 6 ] = cx;
  79. positions[ i + 7 ] = cy;
  80. positions[ i + 8 ] = cz;
  81. // flat face normals
  82. pA.set( ax, ay, az );
  83. pB.set( bx, by, bz );
  84. pC.set( cx, cy, cz );
  85. cb.subVectors( pC, pB );
  86. ab.subVectors( pA, pB );
  87. cb.cross( ab );
  88. cb.normalize();
  89. const nx = cb.x;
  90. const ny = cb.y;
  91. const nz = cb.z;
  92. normals[ i ] = nx;
  93. normals[ i + 1 ] = ny;
  94. normals[ i + 2 ] = nz;
  95. normals[ i + 3 ] = nx;
  96. normals[ i + 4 ] = ny;
  97. normals[ i + 5 ] = nz;
  98. normals[ i + 6 ] = nx;
  99. normals[ i + 7 ] = ny;
  100. normals[ i + 8 ] = nz;
  101. // colors
  102. const vx = ( x / n ) + 0.5;
  103. const vy = ( y / n ) + 0.5;
  104. const vz = ( z / n ) + 0.5;
  105. color.setRGB( vx, vy, vz );
  106. colors[ i ] = color.r;
  107. colors[ i + 1 ] = color.g;
  108. colors[ i + 2 ] = color.b;
  109. colors[ i + 3 ] = color.r;
  110. colors[ i + 4 ] = color.g;
  111. colors[ i + 5 ] = color.b;
  112. colors[ i + 6 ] = color.r;
  113. colors[ i + 7 ] = color.g;
  114. colors[ i + 8 ] = color.b;
  115. }
  116. geometry.setAttribute( 'position', new THREE.BufferAttribute( positions, 3 ) );
  117. geometry.setAttribute( 'normal', new THREE.BufferAttribute( normals, 3 ) );
  118. geometry.setAttribute( 'color', new THREE.BufferAttribute( colors, 3 ) );
  119. geometry.computeBoundingSphere();
  120. let material = new THREE.MeshPhongMaterial( {
  121. color: 0xaaaaaa, specular: 0xffffff, shininess: 250,
  122. side: THREE.DoubleSide, vertexColors: true
  123. } );
  124. mesh = new THREE.Mesh( geometry, material );
  125. scene.add( mesh );
  126. //
  127. raycaster = new THREE.Raycaster();
  128. pointer = new THREE.Vector2();
  129. geometry = new THREE.BufferGeometry();
  130. geometry.setAttribute( 'position', new THREE.BufferAttribute( new Float32Array( 4 * 3 ), 3 ) );
  131. material = new THREE.LineBasicMaterial( { color: 0xffffff, transparent: true } );
  132. line = new THREE.Line( geometry, material );
  133. scene.add( line );
  134. //
  135. renderer = new THREE.WebGLRenderer( { antialias: true } );
  136. renderer.setPixelRatio( window.devicePixelRatio );
  137. renderer.setSize( window.innerWidth, window.innerHeight );
  138. renderer.setAnimationLoop( animate );
  139. container.appendChild( renderer.domElement );
  140. //
  141. stats = new Stats();
  142. container.appendChild( stats.dom );
  143. //
  144. window.addEventListener( 'resize', onWindowResize );
  145. document.addEventListener( 'pointermove', onPointerMove );
  146. }
  147. function onWindowResize() {
  148. camera.aspect = window.innerWidth / window.innerHeight;
  149. camera.updateProjectionMatrix();
  150. renderer.setSize( window.innerWidth, window.innerHeight );
  151. }
  152. function onPointerMove( event ) {
  153. pointer.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  154. pointer.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  155. }
  156. //
  157. function animate() {
  158. render();
  159. stats.update();
  160. }
  161. function render() {
  162. const time = Date.now() * 0.001;
  163. mesh.rotation.x = time * 0.15;
  164. mesh.rotation.y = time * 0.25;
  165. raycaster.setFromCamera( pointer, camera );
  166. const intersects = raycaster.intersectObject( mesh );
  167. if ( intersects.length > 0 ) {
  168. const intersect = intersects[ 0 ];
  169. const face = intersect.face;
  170. const linePosition = line.geometry.attributes.position;
  171. const meshPosition = mesh.geometry.attributes.position;
  172. linePosition.copyAt( 0, meshPosition, face.a );
  173. linePosition.copyAt( 1, meshPosition, face.b );
  174. linePosition.copyAt( 2, meshPosition, face.c );
  175. linePosition.copyAt( 3, meshPosition, face.a );
  176. mesh.updateMatrix();
  177. line.geometry.applyMatrix4( mesh.matrix );
  178. line.visible = true;
  179. } else {
  180. line.visible = false;
  181. }
  182. renderer.render( scene, camera );
  183. }
  184. </script>
  185. </body>
  186. </html>