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