discBuilder.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import { Mesh } from "../mesh.js";
  2. import { VertexData } from "../mesh.vertexData.js";
  3. import { CompatibilityOptions } from "../../Compat/compatibilityOptions.js";
  4. /**
  5. * Creates the VertexData of the Disc or regular Polygon
  6. * @param options an object used to set the following optional parameters for the disc, required but can be empty
  7. * * radius the radius of the disc, optional default 0.5
  8. * * tessellation the number of polygon sides, optional, default 64
  9. * * arc a number from 0 to 1, to create an unclosed polygon based on the fraction of the circumference given by the arc value, optional, default 1
  10. * * sideOrientation optional and takes the values : Mesh.FRONTSIDE (default), Mesh.BACKSIDE or Mesh.DOUBLESIDE
  11. * * frontUvs only usable when you create a double-sided mesh, used to choose what parts of the texture image to crop and apply on the front side, optional, default vector4 (0, 0, 1, 1)
  12. * * backUVs only usable when you create a double-sided mesh, used to choose what parts of the texture image to crop and apply on the back side, optional, default vector4 (0, 0, 1, 1)
  13. * @returns the VertexData of the box
  14. */
  15. // eslint-disable-next-line @typescript-eslint/naming-convention
  16. export function CreateDiscVertexData(options) {
  17. const positions = [];
  18. const indices = [];
  19. const normals = [];
  20. const uvs = [];
  21. const radius = options.radius || 0.5;
  22. const tessellation = options.tessellation || 64;
  23. const arc = options.arc && (options.arc <= 0 || options.arc > 1) ? 1.0 : options.arc || 1.0;
  24. const sideOrientation = options.sideOrientation === 0 ? 0 : options.sideOrientation || VertexData.DEFAULTSIDE;
  25. // positions and uvs
  26. positions.push(0, 0, 0); // disc center first
  27. uvs.push(0.5, 0.5);
  28. const theta = Math.PI * 2 * arc;
  29. const step = arc === 1 ? theta / tessellation : theta / (tessellation - 1);
  30. let a = 0;
  31. for (let t = 0; t < tessellation; t++) {
  32. const x = Math.cos(a);
  33. const y = Math.sin(a);
  34. const u = (x + 1) / 2;
  35. const v = (1 - y) / 2;
  36. positions.push(radius * x, radius * y, 0);
  37. uvs.push(u, CompatibilityOptions.UseOpenGLOrientationForUV ? 1 - v : v);
  38. a += step;
  39. }
  40. if (arc === 1) {
  41. positions.push(positions[3], positions[4], positions[5]); // close the circle
  42. uvs.push(uvs[2], CompatibilityOptions.UseOpenGLOrientationForUV ? 1 - uvs[3] : uvs[3]);
  43. }
  44. //indices
  45. const vertexNb = positions.length / 3;
  46. for (let i = 1; i < vertexNb - 1; i++) {
  47. indices.push(i + 1, 0, i);
  48. }
  49. // result
  50. VertexData.ComputeNormals(positions, indices, normals);
  51. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs, options.frontUVs, options.backUVs);
  52. const vertexData = new VertexData();
  53. vertexData.indices = indices;
  54. vertexData.positions = positions;
  55. vertexData.normals = normals;
  56. vertexData.uvs = uvs;
  57. return vertexData;
  58. }
  59. /**
  60. * Creates a plane polygonal mesh. By default, this is a disc
  61. * * The parameter `radius` sets the radius size (float) of the polygon (default 0.5)
  62. * * The parameter `tessellation` sets the number of polygon sides (positive integer, default 64). So a tessellation valued to 3 will build a triangle, to 4 a square, etc
  63. * * You can create an unclosed polygon with the parameter `arc` (positive float, default 1), valued between 0 and 1, what is the ratio of the circumference : 2 x PI x ratio
  64. * * You can also set the mesh side orientation with the values : BABYLON.Mesh.FRONTSIDE (default), BABYLON.Mesh.BACKSIDE or BABYLON.Mesh.DOUBLESIDE
  65. * * If you create a double-sided mesh, you can choose what parts of the texture image to crop and stick respectively on the front and the back sides with the parameters `frontUVs` and `backUVs` (Vector4). Detail here : https://doc.babylonjs.com/features/featuresDeepDive/mesh/creation/set#side-orientation
  66. * * The mesh can be set to updatable with the boolean parameter `updatable` (default false) if its internal geometry is supposed to change once created
  67. * @param name defines the name of the mesh
  68. * @param options defines the options used to create the mesh
  69. * @param scene defines the hosting scene
  70. * @returns the plane polygonal mesh
  71. * @see https://doc.babylonjs.com/features/featuresDeepDive/mesh/creation/set#disc-or-regular-polygon
  72. */
  73. export function CreateDisc(name, options = {}, scene = null) {
  74. const disc = new Mesh(name, scene);
  75. options.sideOrientation = Mesh._GetDefaultSideOrientation(options.sideOrientation);
  76. disc._originalBuilderSideOrientation = options.sideOrientation;
  77. const vertexData = CreateDiscVertexData(options);
  78. vertexData.applyToMesh(disc, options.updatable);
  79. return disc;
  80. }
  81. /**
  82. * Class containing static functions to help procedurally build meshes
  83. * @deprecated please use CreateDisc directly
  84. */
  85. export const DiscBuilder = {
  86. // eslint-disable-next-line @typescript-eslint/naming-convention
  87. CreateDisc,
  88. };
  89. VertexData.CreateDisc = CreateDiscVertexData;
  90. Mesh.CreateDisc = (name, radius, tessellation, scene = null, updatable, sideOrientation) => {
  91. const options = {
  92. radius,
  93. tessellation,
  94. sideOrientation,
  95. updatable,
  96. };
  97. return CreateDisc(name, options, scene);
  98. };
  99. //# sourceMappingURL=discBuilder.js.map