threejs-custom-buffergeometry-cube.html 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. 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], },
  48. { pos: [ 1, -1, 1], norm: [ 0, 0, 1], uv: [1, 1], },
  49. { pos: [-1, 1, 1], norm: [ 0, 0, 1], uv: [0, 0], },
  50. { pos: [-1, 1, 1], norm: [ 0, 0, 1], uv: [0, 0], },
  51. { pos: [ 1, -1, 1], norm: [ 0, 0, 1], uv: [1, 1], },
  52. { pos: [ 1, 1, 1], norm: [ 0, 0, 1], uv: [1, 0], },
  53. // right
  54. { pos: [ 1, -1, 1], norm: [ 1, 0, 0], uv: [0, 1], },
  55. { pos: [ 1, -1, -1], norm: [ 1, 0, 0], uv: [1, 1], },
  56. { pos: [ 1, 1, 1], norm: [ 1, 0, 0], uv: [0, 0], },
  57. { pos: [ 1, 1, 1], norm: [ 1, 0, 0], uv: [0, 0], },
  58. { pos: [ 1, -1, -1], norm: [ 1, 0, 0], uv: [1, 1], },
  59. { pos: [ 1, 1, -1], norm: [ 1, 0, 0], uv: [1, 0], },
  60. // back
  61. { pos: [ 1, -1, -1], norm: [ 0, 0, -1], uv: [0, 1], },
  62. { pos: [-1, -1, -1], norm: [ 0, 0, -1], uv: [1, 1], },
  63. { pos: [ 1, 1, -1], norm: [ 0, 0, -1], uv: [0, 0], },
  64. { pos: [ 1, 1, -1], norm: [ 0, 0, -1], uv: [0, 0], },
  65. { pos: [-1, -1, -1], norm: [ 0, 0, -1], uv: [1, 1], },
  66. { pos: [-1, 1, -1], norm: [ 0, 0, -1], uv: [1, 0], },
  67. // left
  68. { pos: [-1, -1, -1], norm: [-1, 0, 0], uv: [0, 1], },
  69. { pos: [-1, -1, 1], norm: [-1, 0, 0], uv: [1, 1], },
  70. { pos: [-1, 1, -1], norm: [-1, 0, 0], uv: [0, 0], },
  71. { pos: [-1, 1, -1], norm: [-1, 0, 0], uv: [0, 0], },
  72. { pos: [-1, -1, 1], norm: [-1, 0, 0], uv: [1, 1], },
  73. { pos: [-1, 1, 1], norm: [-1, 0, 0], uv: [1, 0], },
  74. // top
  75. { pos: [ 1, 1, -1], norm: [ 0, 1, 0], uv: [0, 1], },
  76. { pos: [-1, 1, -1], norm: [ 0, 1, 0], uv: [1, 1], },
  77. { pos: [ 1, 1, 1], norm: [ 0, 1, 0], uv: [0, 0], },
  78. { pos: [ 1, 1, 1], norm: [ 0, 1, 0], uv: [0, 0], },
  79. { pos: [-1, 1, -1], norm: [ 0, 1, 0], uv: [1, 1], },
  80. { pos: [-1, 1, 1], norm: [ 0, 1, 0], uv: [1, 0], },
  81. // bottom
  82. { pos: [ 1, -1, 1], norm: [ 0, -1, 0], uv: [0, 1], },
  83. { pos: [-1, -1, 1], norm: [ 0, -1, 0], uv: [1, 1], },
  84. { pos: [ 1, -1, -1], norm: [ 0, -1, 0], uv: [0, 0], },
  85. { pos: [ 1, -1, -1], norm: [ 0, -1, 0], uv: [0, 0], },
  86. { pos: [-1, -1, 1], norm: [ 0, -1, 0], uv: [1, 1], },
  87. { pos: [-1, -1, -1], norm: [ 0, -1, 0], uv: [1, 0], },
  88. ];
  89. const positions = [];
  90. const normals = [];
  91. const uvs = [];
  92. for (const vertex of vertices) {
  93. positions.push(...vertex.pos);
  94. normals.push(...vertex.norm);
  95. uvs.push(...vertex.uv);
  96. }
  97. const geometry = new THREE.BufferGeometry();
  98. const positionNumComponents = 3;
  99. const normalNumComponents = 3;
  100. const uvNumComponents = 2;
  101. geometry.addAttribute(
  102. 'position',
  103. new THREE.BufferAttribute(new Float32Array(positions), positionNumComponents));
  104. geometry.addAttribute(
  105. 'normal',
  106. new THREE.BufferAttribute(new Float32Array(normals), normalNumComponents));
  107. geometry.addAttribute(
  108. 'uv',
  109. new THREE.BufferAttribute(new Float32Array(uvs), uvNumComponents));
  110. const loader = new THREE.TextureLoader();
  111. const texture = loader.load('resources/images/star.png');
  112. function makeInstance(geometry, color, x) {
  113. const material = new THREE.MeshPhongMaterial({color, map: texture});
  114. const cube = new THREE.Mesh(geometry, material);
  115. scene.add(cube);
  116. cube.position.x = x;
  117. return cube;
  118. }
  119. const cubes = [
  120. makeInstance(geometry, 0x88FF88, 0),
  121. makeInstance(geometry, 0x8888FF, -4),
  122. makeInstance(geometry, 0xFF8888, 4),
  123. ];
  124. function resizeRendererToDisplaySize(renderer) {
  125. const canvas = renderer.domElement;
  126. const width = canvas.clientWidth;
  127. const height = canvas.clientHeight;
  128. const needResize = canvas.width !== width || canvas.height !== height;
  129. if (needResize) {
  130. renderer.setSize(width, height, false);
  131. }
  132. return needResize;
  133. }
  134. function render(time) {
  135. time *= 0.001;
  136. if (resizeRendererToDisplaySize(renderer)) {
  137. const canvas = renderer.domElement;
  138. camera.aspect = canvas.clientWidth / canvas.clientHeight;
  139. camera.updateProjectionMatrix();
  140. }
  141. cubes.forEach((cube, ndx) => {
  142. const speed = 1 + ndx * .1;
  143. const rot = time * speed;
  144. cube.rotation.x = rot;
  145. cube.rotation.y = rot;
  146. });
  147. renderer.render(scene, camera);
  148. requestAnimationFrame(render);
  149. }
  150. requestAnimationFrame(render);
  151. }
  152. main();
  153. </script>
  154. </html>