webgl_buffergeometry.html 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - 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. <meta property="og:title" content="three.js webgl - buffergeometry">
  8. <meta property="og:type" content="website">
  9. <meta property="og:url" content="https://threejs.org/examples/webgl_buffergeometry.html">
  10. <meta property="og:image" content="https://threejs.org/examples/screenshots/webgl_buffergeometry.jpg">
  11. <link type="text/css" rel="stylesheet" href="main.css">
  12. </head>
  13. <body>
  14. <div id="container"></div>
  15. <div id="info"><a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - buffergeometry</div>
  16. <script type="importmap">
  17. {
  18. "imports": {
  19. "three": "../build/three.module.js",
  20. "three/addons/": "./jsm/"
  21. }
  22. }
  23. </script>
  24. <script type="module">
  25. import * as THREE from 'three';
  26. import Stats from 'three/addons/libs/stats.module.js';
  27. let container, stats;
  28. let camera, scene, renderer;
  29. let mesh;
  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( 0xcccccc ) );
  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 = 160000;
  50. const geometry = new THREE.BufferGeometry();
  51. const positions = [];
  52. const normals = [];
  53. const colors = [];
  54. const color = new THREE.Color();
  55. const n = 800, n2 = n / 2; // triangles spread in the cube
  56. const d = 12, 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 < triangles; i ++ ) {
  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.push( ax, ay, az );
  77. positions.push( bx, by, bz );
  78. positions.push( cx, cy, cz );
  79. // flat face normals
  80. pA.set( ax, ay, az );
  81. pB.set( bx, by, bz );
  82. pC.set( cx, cy, cz );
  83. cb.subVectors( pC, pB );
  84. ab.subVectors( pA, pB );
  85. cb.cross( ab );
  86. cb.normalize();
  87. const nx = cb.x;
  88. const ny = cb.y;
  89. const nz = cb.z;
  90. normals.push( nx, ny, nz );
  91. normals.push( nx, ny, nz );
  92. normals.push( nx, ny, nz );
  93. // colors
  94. const vx = ( x / n ) + 0.5;
  95. const vy = ( y / n ) + 0.5;
  96. const vz = ( z / n ) + 0.5;
  97. color.setRGB( vx, vy, vz );
  98. const alpha = Math.random();
  99. colors.push( color.r, color.g, color.b, alpha );
  100. colors.push( color.r, color.g, color.b, alpha );
  101. colors.push( color.r, color.g, color.b, alpha );
  102. }
  103. function disposeArray() {
  104. this.array = null;
  105. }
  106. geometry.setAttribute( 'position', new THREE.Float32BufferAttribute( positions, 3 ).onUpload( disposeArray ) );
  107. geometry.setAttribute( 'normal', new THREE.Float32BufferAttribute( normals, 3 ).onUpload( disposeArray ) );
  108. geometry.setAttribute( 'color', new THREE.Float32BufferAttribute( colors, 4 ).onUpload( disposeArray ) );
  109. geometry.computeBoundingSphere();
  110. const material = new THREE.MeshPhongMaterial( {
  111. color: 0xd5d5d5, specular: 0xffffff, shininess: 250,
  112. side: THREE.DoubleSide, vertexColors: true, transparent: true
  113. } );
  114. mesh = new THREE.Mesh( geometry, material );
  115. scene.add( mesh );
  116. //
  117. renderer = new THREE.WebGLRenderer( { antialias: true } );
  118. renderer.setPixelRatio( window.devicePixelRatio );
  119. renderer.setSize( window.innerWidth, window.innerHeight );
  120. renderer.setAnimationLoop( animate );
  121. container.appendChild( renderer.domElement );
  122. //
  123. stats = new Stats();
  124. container.appendChild( stats.dom );
  125. //
  126. window.addEventListener( 'resize', onWindowResize );
  127. }
  128. function onWindowResize() {
  129. camera.aspect = window.innerWidth / window.innerHeight;
  130. camera.updateProjectionMatrix();
  131. renderer.setSize( window.innerWidth, window.innerHeight );
  132. }
  133. //
  134. function animate() {
  135. const time = Date.now() * 0.001;
  136. mesh.rotation.x = time * 0.25;
  137. mesh.rotation.y = time * 0.5;
  138. renderer.render( scene, camera );
  139. stats.update();
  140. }
  141. </script>
  142. </body>
  143. </html>
粤ICP备19079148号