webgl_interactive_buffergeometry.html 6.8 KB

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