threejs-custom-buffergeometry.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. 'use strict';
  2. /* global THREE, threejsLessonUtils */
  3. {
  4. const loader = new THREE.TextureLoader();
  5. const texture = loader.load('../resources/images/star-light.png');
  6. texture.wrapS = THREE.RepeatWrapping;
  7. texture.wrapT = THREE.RepeatWrapping;
  8. texture.repeat.set(3, 1);
  9. function makeMesh(geometry) {
  10. const material = new THREE.MeshPhongMaterial({
  11. color: 'hsl(300,50%,50%)',
  12. side: THREE.DoubleSide,
  13. map: texture,
  14. });
  15. return new THREE.Mesh(geometry, material);
  16. }
  17. threejsLessonUtils.addDiagrams({
  18. geometryCylinder: {
  19. create() {
  20. const numSegments = 24;
  21. const geometry = new THREE.Geometry();
  22. const wrap = ndx => ndx % (numSegments * 2);
  23. for (let s = 0; s < numSegments; ++s) {
  24. const u = s / numSegments;
  25. const a = u * Math.PI * 2;
  26. const x = Math.sin(a);
  27. const z = Math.cos(a);
  28. geometry.vertices.push(new THREE.Vector3(x, -1, z));
  29. geometry.vertices.push(new THREE.Vector3(x, 1, z));
  30. // share the start and end positions
  31. const ndx = s * 2;
  32. geometry.faces.push(new THREE.Face3(ndx, wrap(ndx + 2), ndx + 1));
  33. geometry.faces.push(new THREE.Face3(ndx + 1, wrap(ndx + 2), wrap(ndx + 3)));
  34. const u2 = (s + 1) / numSegments;
  35. geometry.faceVertexUvs[0].push([
  36. new THREE.Vector2(u, 0),
  37. new THREE.Vector2(u2, 0),
  38. new THREE.Vector2(u, 1),
  39. ]);
  40. geometry.faceVertexUvs[0].push([
  41. new THREE.Vector2(u, 1),
  42. new THREE.Vector2(u2, 0),
  43. new THREE.Vector2(u2, 1),
  44. ]);
  45. }
  46. geometry.computeVertexNormals();
  47. geometry.scale(5, 5, 5);
  48. return makeMesh(geometry);
  49. },
  50. },
  51. bufferGeometryCylinder: {
  52. create() {
  53. const numSegments = 24;
  54. const positions = [];
  55. const uvs = [];
  56. for (let s = 0; s <= numSegments; ++s) {
  57. const u = s / numSegments;
  58. const a = u * Math.PI * 2;
  59. const x = Math.sin(a);
  60. const z = Math.cos(a);
  61. positions.push(x, -1, z);
  62. positions.push(x, 1, z);
  63. uvs.push(u, 0);
  64. uvs.push(u, 1);
  65. }
  66. const indices = [];
  67. for (let s = 0; s < numSegments; ++s) {
  68. const ndx = s * 2;
  69. indices.push(
  70. ndx, ndx + 2, ndx + 1,
  71. ndx + 1, ndx + 2, ndx + 3,
  72. );
  73. }
  74. const positionNumComponents = 3;
  75. const uvNumComponents = 2;
  76. const geometry = new THREE.BufferGeometry();
  77. geometry.addAttribute(
  78. 'position',
  79. new THREE.BufferAttribute(new Float32Array(positions), positionNumComponents));
  80. geometry.addAttribute(
  81. 'uv',
  82. new THREE.BufferAttribute(new Float32Array(uvs), uvNumComponents));
  83. geometry.setIndex(indices);
  84. geometry.computeVertexNormals();
  85. geometry.scale(5, 5, 5);
  86. return makeMesh(geometry);
  87. },
  88. },
  89. });
  90. }