custom-buffergeometry-dynamic.html 5.1 KB

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