custom-buffergeometry-cube.html 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  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 - Cube</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. <!-- Import maps polyfill -->
  24. <!-- Remove this when import maps will be widely supported -->
  25. <script async src="https://unpkg.com/[email protected]/dist/es-module-shims.js"></script>
  26. <script type="importmap">
  27. {
  28. "imports": {
  29. "three": "../../build/three.module.js"
  30. }
  31. }
  32. </script>
  33. <script type="module">
  34. import * as THREE from 'three';
  35. function main() {
  36. const canvas = document.querySelector('#c');
  37. const renderer = new THREE.WebGLRenderer({antialias: true, canvas});
  38. const fov = 75;
  39. const aspect = 2; // the canvas default
  40. const near = 0.1;
  41. const far = 100;
  42. const camera = new THREE.PerspectiveCamera(fov, aspect, near, far);
  43. camera.position.z = 5;
  44. const scene = new THREE.Scene();
  45. {
  46. const color = 0xFFFFFF;
  47. const intensity = 1;
  48. const light = new THREE.DirectionalLight(color, intensity);
  49. light.position.set(-1, 2, 4);
  50. scene.add(light);
  51. }
  52. // NOT A GOOD EXAMPLE OF HOW TO MAKE A CUBE!
  53. // Only trying to make it clear most vertices are unique
  54. const vertices = [
  55. // front
  56. { pos: [-1, -1, 1], norm: [ 0, 0, 1], uv: [0, 0], },
  57. { pos: [ 1, -1, 1], norm: [ 0, 0, 1], uv: [1, 0], },
  58. { pos: [-1, 1, 1], norm: [ 0, 0, 1], uv: [0, 1], },
  59. { pos: [-1, 1, 1], norm: [ 0, 0, 1], uv: [0, 1], },
  60. { pos: [ 1, -1, 1], norm: [ 0, 0, 1], uv: [1, 0], },
  61. { pos: [ 1, 1, 1], norm: [ 0, 0, 1], uv: [1, 1], },
  62. // right
  63. { pos: [ 1, -1, 1], norm: [ 1, 0, 0], uv: [0, 0], },
  64. { pos: [ 1, -1, -1], norm: [ 1, 0, 0], uv: [1, 0], },
  65. { pos: [ 1, 1, 1], norm: [ 1, 0, 0], uv: [0, 1], },
  66. { pos: [ 1, 1, 1], norm: [ 1, 0, 0], uv: [0, 1], },
  67. { pos: [ 1, -1, -1], norm: [ 1, 0, 0], uv: [1, 0], },
  68. { pos: [ 1, 1, -1], norm: [ 1, 0, 0], uv: [1, 1], },
  69. // back
  70. { pos: [ 1, -1, -1], norm: [ 0, 0, -1], uv: [0, 0], },
  71. { pos: [-1, -1, -1], norm: [ 0, 0, -1], uv: [1, 0], },
  72. { pos: [ 1, 1, -1], norm: [ 0, 0, -1], uv: [0, 1], },
  73. { pos: [ 1, 1, -1], norm: [ 0, 0, -1], uv: [0, 1], },
  74. { pos: [-1, -1, -1], norm: [ 0, 0, -1], uv: [1, 0], },
  75. { pos: [-1, 1, -1], norm: [ 0, 0, -1], uv: [1, 1], },
  76. // left
  77. { pos: [-1, -1, -1], norm: [-1, 0, 0], uv: [0, 0], },
  78. { pos: [-1, -1, 1], norm: [-1, 0, 0], uv: [1, 0], },
  79. { pos: [-1, 1, -1], norm: [-1, 0, 0], uv: [0, 1], },
  80. { pos: [-1, 1, -1], norm: [-1, 0, 0], uv: [0, 1], },
  81. { pos: [-1, -1, 1], norm: [-1, 0, 0], uv: [1, 0], },
  82. { pos: [-1, 1, 1], norm: [-1, 0, 0], uv: [1, 1], },
  83. // top
  84. { pos: [ 1, 1, -1], norm: [ 0, 1, 0], uv: [0, 0], },
  85. { pos: [-1, 1, -1], norm: [ 0, 1, 0], uv: [1, 0], },
  86. { pos: [ 1, 1, 1], norm: [ 0, 1, 0], uv: [0, 1], },
  87. { pos: [ 1, 1, 1], norm: [ 0, 1, 0], uv: [0, 1], },
  88. { pos: [-1, 1, -1], norm: [ 0, 1, 0], uv: [1, 0], },
  89. { pos: [-1, 1, 1], norm: [ 0, 1, 0], uv: [1, 1], },
  90. // bottom
  91. { pos: [ 1, -1, 1], norm: [ 0, -1, 0], uv: [0, 0], },
  92. { pos: [-1, -1, 1], norm: [ 0, -1, 0], uv: [1, 0], },
  93. { pos: [ 1, -1, -1], norm: [ 0, -1, 0], uv: [0, 1], },
  94. { pos: [ 1, -1, -1], norm: [ 0, -1, 0], uv: [0, 1], },
  95. { pos: [-1, -1, 1], norm: [ 0, -1, 0], uv: [1, 0], },
  96. { pos: [-1, -1, -1], norm: [ 0, -1, 0], uv: [1, 1], },
  97. ];
  98. const positions = [];
  99. const normals = [];
  100. const uvs = [];
  101. for (const vertex of vertices) {
  102. positions.push(...vertex.pos);
  103. normals.push(...vertex.norm);
  104. uvs.push(...vertex.uv);
  105. }
  106. const geometry = new THREE.BufferGeometry();
  107. const positionNumComponents = 3;
  108. const normalNumComponents = 3;
  109. const uvNumComponents = 2;
  110. geometry.setAttribute(
  111. 'position',
  112. new THREE.BufferAttribute(new Float32Array(positions), positionNumComponents));
  113. geometry.setAttribute(
  114. 'normal',
  115. new THREE.BufferAttribute(new Float32Array(normals), normalNumComponents));
  116. geometry.setAttribute(
  117. 'uv',
  118. new THREE.BufferAttribute(new Float32Array(uvs), uvNumComponents));
  119. const loader = new THREE.TextureLoader();
  120. const texture = loader.load('resources/images/star.png');
  121. function makeInstance(geometry, color, x) {
  122. const material = new THREE.MeshPhongMaterial({color, map: texture});
  123. const cube = new THREE.Mesh(geometry, material);
  124. scene.add(cube);
  125. cube.position.x = x;
  126. return cube;
  127. }
  128. const cubes = [
  129. makeInstance(geometry, 0x88FF88, 0),
  130. makeInstance(geometry, 0x8888FF, -4),
  131. makeInstance(geometry, 0xFF8888, 4),
  132. ];
  133. function resizeRendererToDisplaySize(renderer) {
  134. const canvas = renderer.domElement;
  135. const width = canvas.clientWidth;
  136. const height = canvas.clientHeight;
  137. const needResize = canvas.width !== width || canvas.height !== height;
  138. if (needResize) {
  139. renderer.setSize(width, height, false);
  140. }
  141. return needResize;
  142. }
  143. function render(time) {
  144. time *= 0.001;
  145. if (resizeRendererToDisplaySize(renderer)) {
  146. const canvas = renderer.domElement;
  147. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  148. camera.updateProjectionMatrix();
  149. }
  150. cubes.forEach((cube, ndx) => {
  151. const speed = 1 + ndx * .1;
  152. const rot = time * speed;
  153. cube.rotation.x = rot;
  154. cube.rotation.y = rot;
  155. });
  156. renderer.render(scene, camera);
  157. requestAnimationFrame(render);
  158. }
  159. requestAnimationFrame(render);
  160. }
  161. main();
  162. </script>
  163. </html>