webgl_interactive_buffergeometry.html 6.4 KB

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