custom-buffergeometry-cube-indexed.html 5.1 KB

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