custom-buffergeometry-dynamic.html 5.2 KB

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