threejs-custom-buffergeometry-cube-indexed.html 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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 - Indexed</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 = 5;
  35. const scene = new THREE.Scene();
  36. {
  37. const color = 0xFFFFFF;
  38. const intensity = 1;
  39. const light = new THREE.DirectionalLight(color, intensity);
  40. light.position.set(-1, 2, 4);
  41. scene.add(light);
  42. }
  43. // NOT A GOOD EXAMPLE OF HOW TO MAKE A CUBE!
  44. // Only trying to make it clear most vertices are unique
  45. const vertices = [
  46. // front
  47. { pos: [-1, -1, 1], norm: [ 0, 0, 1], uv: [0, 1], }, // 0
  48. { pos: [ 1, -1, 1], norm: [ 0, 0, 1], uv: [1, 1], }, // 1
  49. { pos: [-1, 1, 1], norm: [ 0, 0, 1], uv: [0, 0], }, // 2
  50. { pos: [ 1, 1, 1], norm: [ 0, 0, 1], uv: [1, 0], }, // 3
  51. // right
  52. { pos: [ 1, -1, 1], norm: [ 1, 0, 0], uv: [0, 1], }, // 4
  53. { pos: [ 1, -1, -1], norm: [ 1, 0, 0], uv: [1, 1], }, // 5
  54. { pos: [ 1, 1, 1], norm: [ 1, 0, 0], uv: [0, 0], }, // 6
  55. { pos: [ 1, 1, -1], norm: [ 1, 0, 0], uv: [1, 0], }, // 7
  56. // back
  57. { pos: [ 1, -1, -1], norm: [ 0, 0, -1], uv: [0, 1], }, // 8
  58. { pos: [-1, -1, -1], norm: [ 0, 0, -1], uv: [1, 1], }, // 9
  59. { pos: [ 1, 1, -1], norm: [ 0, 0, -1], uv: [0, 0], }, // 10
  60. { pos: [-1, 1, -1], norm: [ 0, 0, -1], uv: [1, 0], }, // 11
  61. // left
  62. { pos: [-1, -1, -1], norm: [-1, 0, 0], uv: [0, 1], }, // 12
  63. { pos: [-1, -1, 1], norm: [-1, 0, 0], uv: [1, 1], }, // 13
  64. { pos: [-1, 1, -1], norm: [-1, 0, 0], uv: [0, 0], }, // 14
  65. { pos: [-1, 1, 1], norm: [-1, 0, 0], uv: [1, 0], }, // 15
  66. // top
  67. { pos: [ 1, 1, -1], norm: [ 0, 1, 0], uv: [0, 1], }, // 16
  68. { pos: [-1, 1, -1], norm: [ 0, 1, 0], uv: [1, 1], }, // 17
  69. { pos: [ 1, 1, 1], norm: [ 0, 1, 0], uv: [0, 0], }, // 18
  70. { pos: [-1, 1, 1], norm: [ 0, 1, 0], uv: [1, 0], }, // 19
  71. // bottom
  72. { pos: [ 1, -1, 1], norm: [ 0, -1, 0], uv: [0, 1], }, // 20
  73. { pos: [-1, -1, 1], norm: [ 0, -1, 0], uv: [1, 1], }, // 21
  74. { pos: [ 1, -1, -1], norm: [ 0, -1, 0], uv: [0, 0], }, // 22
  75. { pos: [-1, -1, -1], norm: [ 0, -1, 0], uv: [1, 0], }, // 23
  76. ];
  77. const positions = [];
  78. const normals = [];
  79. const uvs = [];
  80. for (const vertex of vertices) {
  81. positions.push(...vertex.pos);
  82. normals.push(...vertex.norm);
  83. uvs.push(...vertex.uv);
  84. }
  85. const geometry = new THREE.BufferGeometry();
  86. const positionNumComponents = 3;
  87. const normalNumComponents = 3;
  88. const uvNumComponents = 2;
  89. geometry.addAttribute(
  90. 'position',
  91. new THREE.BufferAttribute(new Float32Array(positions), positionNumComponents));
  92. geometry.addAttribute(
  93. 'normal',
  94. new THREE.BufferAttribute(new Float32Array(normals), normalNumComponents));
  95. geometry.addAttribute(
  96. 'uv',
  97. new THREE.BufferAttribute(new Float32Array(uvs), uvNumComponents));
  98. geometry.setIndex([
  99. 0, 1, 2, 2, 1, 3,
  100. 4, 5, 6, 6, 5, 7,
  101. 8, 9, 10, 10, 9, 11,
  102. 12, 13, 14, 14, 13, 15,
  103. 16, 17, 18, 18, 17, 19,
  104. 20, 21, 22, 22, 21, 23,
  105. ]);
  106. const loader = new THREE.TextureLoader();
  107. const texture = loader.load('resources/images/star.png');
  108. function makeInstance(geometry, color, x) {
  109. const material = new THREE.MeshPhongMaterial({color, map: texture});
  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, 0x88FF88, 0),
  117. makeInstance(geometry, 0x8888FF, -4),
  118. makeInstance(geometry, 0xFF8888, 4),
  119. ];
  120. function resizeRendererToDisplaySize(renderer) {
  121. const canvas = renderer.domElement;
  122. const width = canvas.clientWidth;
  123. const height = canvas.clientHeight;
  124. const needResize = canvas.width !== width || canvas.height !== height;
  125. if (needResize) {
  126. renderer.setSize(width, height, false);
  127. }
  128. return needResize;
  129. }
  130. function render(time) {
  131. time *= 0.001;
  132. if (resizeRendererToDisplaySize(renderer)) {
  133. const canvas = renderer.domElement;
  134. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  135. camera.updateProjectionMatrix();
  136. }
  137. cubes.forEach((cube, ndx) => {
  138. const speed = 1 + ndx * .1;
  139. const rot = time * speed;
  140. cube.rotation.x = rot;
  141. cube.rotation.y = rot;
  142. });
  143. renderer.render(scene, camera);
  144. requestAnimationFrame(render);
  145. }
  146. requestAnimationFrame(render);
  147. }
  148. main();
  149. </script>
  150. </html>