webgl_interactive_buffergeometry.html 6.8 KB

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