threejs-custom-buffergeometry-dynamic.html 5.2 KB

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