planeBuilder.js 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  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 for a Plane
  6. * @param options an object used to set the following optional parameters for the plane, required but can be empty
  7. * * size sets the width and height of the plane to the value of size, optional default 1
  8. * * width sets the width (x direction) of the plane, overwrites the width set by size, optional, default size
  9. * * height sets the height (y direction) of the plane, overwrites the height set by size, optional, default size
  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. export function CreatePlaneVertexData(options) {
  16. const indices = [];
  17. const positions = [];
  18. const normals = [];
  19. const uvs = [];
  20. const width = options.width || options.size || 1;
  21. const height = options.height || options.size || 1;
  22. const sideOrientation = options.sideOrientation === 0 ? 0 : options.sideOrientation || VertexData.DEFAULTSIDE;
  23. // Vertices
  24. const halfWidth = width / 2.0;
  25. const halfHeight = height / 2.0;
  26. positions.push(-halfWidth, -halfHeight, 0);
  27. normals.push(0, 0, -1.0);
  28. uvs.push(0.0, CompatibilityOptions.UseOpenGLOrientationForUV ? 1.0 : 0.0);
  29. positions.push(halfWidth, -halfHeight, 0);
  30. normals.push(0, 0, -1.0);
  31. uvs.push(1.0, CompatibilityOptions.UseOpenGLOrientationForUV ? 1.0 : 0.0);
  32. positions.push(halfWidth, halfHeight, 0);
  33. normals.push(0, 0, -1.0);
  34. uvs.push(1.0, CompatibilityOptions.UseOpenGLOrientationForUV ? 0.0 : 1.0);
  35. positions.push(-halfWidth, halfHeight, 0);
  36. normals.push(0, 0, -1.0);
  37. uvs.push(0.0, CompatibilityOptions.UseOpenGLOrientationForUV ? 0.0 : 1.0);
  38. // Indices
  39. indices.push(0);
  40. indices.push(1);
  41. indices.push(2);
  42. indices.push(0);
  43. indices.push(2);
  44. indices.push(3);
  45. // Sides
  46. VertexData._ComputeSides(sideOrientation, positions, indices, normals, uvs, options.frontUVs, options.backUVs);
  47. // Result
  48. const vertexData = new VertexData();
  49. vertexData.indices = indices;
  50. vertexData.positions = positions;
  51. vertexData.normals = normals;
  52. vertexData.uvs = uvs;
  53. return vertexData;
  54. }
  55. /**
  56. * Creates a plane mesh
  57. * * The parameter `size` sets the size (float) of both sides of the plane at once (default 1)
  58. * * You can set some different plane dimensions by using the parameters `width` and `height` (both by default have the same value of `size`)
  59. * * The parameter `sourcePlane` is a Plane instance. It builds a mesh plane from a Math plane
  60. * * You can also set the mesh side orientation with the values : BABYLON.Mesh.FRONTSIDE (default), BABYLON.Mesh.BACKSIDE or BABYLON.Mesh.DOUBLESIDE
  61. * * 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
  62. * * The mesh can be set to updatable with the boolean parameter `updatable` (default false) if its internal geometry is supposed to change once created
  63. * @param name defines the name of the mesh
  64. * @param options defines the options used to create the mesh
  65. * @param scene defines the hosting scene
  66. * @returns the plane mesh
  67. * @see https://doc.babylonjs.com/features/featuresDeepDive/mesh/creation/set#plane
  68. */
  69. export function CreatePlane(name, options = {}, scene = null) {
  70. const plane = new Mesh(name, scene);
  71. options.sideOrientation = Mesh._GetDefaultSideOrientation(options.sideOrientation);
  72. plane._originalBuilderSideOrientation = options.sideOrientation;
  73. const vertexData = CreatePlaneVertexData(options);
  74. vertexData.applyToMesh(plane, options.updatable);
  75. if (options.sourcePlane) {
  76. plane.translate(options.sourcePlane.normal, -options.sourcePlane.d);
  77. plane.setDirection(options.sourcePlane.normal.scale(-1));
  78. }
  79. return plane;
  80. }
  81. /**
  82. * Class containing static functions to help procedurally build meshes
  83. * @deprecated use the function directly from the module
  84. */
  85. export const PlaneBuilder = {
  86. // eslint-disable-next-line @typescript-eslint/naming-convention
  87. CreatePlane,
  88. };
  89. VertexData.CreatePlane = CreatePlaneVertexData;
  90. Mesh.CreatePlane = (name, size, scene, updatable, sideOrientation) => {
  91. const options = {
  92. size,
  93. width: size,
  94. height: size,
  95. sideOrientation,
  96. updatable,
  97. };
  98. return CreatePlane(name, options, scene);
  99. };
  100. //# sourceMappingURL=planeBuilder.js.map