custom-buffergeometry-dynamic.html 5.4 KB

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