custom-buffergeometry-dynamic.html 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. <!-- Licensed under a BSD license. See license.html for license -->
  2. <!DOCTYPE html>
  3. <html>
  4. <head>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes">
  7. <title>Three.js - Custom BufferGeometry - Dynamic</title>
  8. <style>
  9. html, body {
  10. height: 100%;
  11. margin: 0;
  12. }
  13. #c {
  14. width: 100%;
  15. height: 100%;
  16. display: block;
  17. }
  18. </style>
  19. </head>
  20. <body>
  21. <canvas id="c"></canvas>
  22. </body>
  23. <!-- Import maps polyfill -->
  24. <!-- Remove this when import maps will be widely supported -->
  25. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  26. <script type="importmap">
  27. {
  28. "imports": {
  29. "three": "../../build/three.module.js"
  30. }
  31. }
  32. </script>
  33. <script type="module">
  34. import * as THREE from 'three';
  35. function main() {
  36. const canvas = document.querySelector( '#c' );
  37. const renderer = new THREE.WebGLRenderer( { antialias: true, canvas } );
  38. renderer.useLegacyLights = false;
  39. const fov = 75;
  40. const aspect = 2; // the canvas default
  41. const near = 0.1;
  42. const far = 100;
  43. const camera = new THREE.PerspectiveCamera( fov, aspect, near, far );
  44. camera.position.z = 3;
  45. const scene = new THREE.Scene();
  46. function addLight( ...pos ) {
  47. const color = 0xFFFFFF;
  48. const intensity = 3;
  49. const light = new THREE.DirectionalLight( color, intensity );
  50. light.position.set( ...pos );
  51. scene.add( light );
  52. }
  53. addLight( - 1, 2, 4 );
  54. addLight( 2, - 2, 3 );
  55. function makeSpherePositions( segmentsAround, segmentsDown ) {
  56. const numVertices = segmentsAround * segmentsDown * 6;
  57. const numComponents = 3;
  58. const positions = new Float32Array( numVertices * numComponents );
  59. const indices = [];
  60. const longHelper = new THREE.Object3D();
  61. const latHelper = new THREE.Object3D();
  62. const pointHelper = new THREE.Object3D();
  63. longHelper.add( latHelper );
  64. latHelper.add( pointHelper );
  65. pointHelper.position.z = 1;
  66. const temp = new THREE.Vector3();
  67. function getPoint( lat, long ) {
  68. latHelper.rotation.x = lat;
  69. longHelper.rotation.y = long;
  70. longHelper.updateMatrixWorld( true );
  71. return pointHelper.getWorldPosition( temp ).toArray();
  72. }
  73. let posNdx = 0;
  74. let ndx = 0;
  75. for ( let down = 0; down < segmentsDown; ++ down ) {
  76. const v0 = down / segmentsDown;
  77. const v1 = ( down + 1 ) / segmentsDown;
  78. const lat0 = ( v0 - 0.5 ) * Math.PI;
  79. const lat1 = ( v1 - 0.5 ) * Math.PI;
  80. for ( let across = 0; across < segmentsAround; ++ across ) {
  81. const u0 = across / segmentsAround;
  82. const u1 = ( across + 1 ) / segmentsAround;
  83. const long0 = u0 * Math.PI * 2;
  84. const long1 = u1 * Math.PI * 2;
  85. positions.set( getPoint( lat0, long0 ), posNdx ); posNdx += numComponents;
  86. positions.set( getPoint( lat1, long0 ), posNdx ); posNdx += numComponents;
  87. positions.set( getPoint( lat0, long1 ), posNdx ); posNdx += numComponents;
  88. positions.set( getPoint( lat1, long1 ), posNdx ); posNdx += numComponents;
  89. indices.push(
  90. ndx, ndx + 1, ndx + 2,
  91. ndx + 2, ndx + 1, ndx + 3,
  92. );
  93. ndx += 4;
  94. }
  95. }
  96. return { positions, indices };
  97. }
  98. const segmentsAround = 24;
  99. const segmentsDown = 16;
  100. const { positions, indices } = makeSpherePositions( segmentsAround, segmentsDown );
  101. const normals = positions.slice();
  102. const geometry = new THREE.BufferGeometry();
  103. const positionNumComponents = 3;
  104. const normalNumComponents = 3;
  105. const positionAttribute = new THREE.BufferAttribute( positions, positionNumComponents );
  106. positionAttribute.setUsage( THREE.DynamicDrawUsage );
  107. geometry.setAttribute(
  108. 'position',
  109. positionAttribute );
  110. geometry.setAttribute(
  111. 'normal',
  112. new THREE.BufferAttribute( normals, normalNumComponents ) );
  113. geometry.setIndex( indices );
  114. function makeInstance( geometry, color, x ) {
  115. const material = new THREE.MeshPhongMaterial( {
  116. color,
  117. side: THREE.DoubleSide,
  118. shininess: 100,
  119. } );
  120. const cube = new THREE.Mesh( geometry, material );
  121. scene.add( cube );
  122. cube.position.x = x;
  123. return cube;
  124. }
  125. const cubes = [
  126. makeInstance( geometry, 0xFF0000, 0 ),
  127. ];
  128. function resizeRendererToDisplaySize( renderer ) {
  129. const canvas = renderer.domElement;
  130. const width = canvas.clientWidth;
  131. const height = canvas.clientHeight;
  132. const needResize = canvas.width !== width || canvas.height !== height;
  133. if ( needResize ) {
  134. renderer.setSize( width, height, false );
  135. }
  136. return needResize;
  137. }
  138. const temp = new THREE.Vector3();
  139. function render( time ) {
  140. time *= 0.001;
  141. if ( resizeRendererToDisplaySize( renderer ) ) {
  142. const canvas = renderer.domElement;
  143. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  144. camera.updateProjectionMatrix();
  145. }
  146. for ( let i = 0; i < positions.length; i += 3 ) {
  147. const quad = ( i / 12 | 0 );
  148. const ringId = quad / segmentsAround | 0;
  149. const ringQuadId = quad % segmentsAround;
  150. const ringU = ringQuadId / segmentsAround;
  151. const angle = ringU * Math.PI * 2;
  152. temp.fromArray( normals, i );
  153. temp.multiplyScalar( THREE.MathUtils.lerp( 1, 1.4, Math.sin( time + ringId + angle ) * .5 + .5 ) );
  154. temp.toArray( positions, i );
  155. }
  156. positionAttribute.needsUpdate = true;
  157. cubes.forEach( ( cube, ndx ) => {
  158. const speed = - 0.2 + ndx * .1;
  159. const rot = time * speed;
  160. cube.rotation.y = rot;
  161. } );
  162. renderer.render( scene, camera );
  163. requestAnimationFrame( render );
  164. }
  165. requestAnimationFrame( render );
  166. }
  167. main();
  168. </script>
  169. </html>