1
0

custom-buffergeometry-cube.html 5.6 KB

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