latheBuilder.js 4.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. import { Vector3 } from "../../Maths/math.vector.js";
  2. import { Mesh } from "../mesh.js";
  3. import { CreateRibbon } from "./ribbonBuilder.js";
  4. /**
  5. * Creates lathe mesh.
  6. * The lathe is a shape with a symmetry axis : a 2D model shape is rotated around this axis to design the lathe
  7. * * The parameter `shape` is a required array of successive Vector3. This array depicts the shape to be rotated in its local space : the shape must be designed in the xOy plane and will be rotated around the Y axis. It's usually a 2D shape, so the Vector3 z coordinates are often set to zero
  8. * * The parameter `radius` (positive float, default 1) is the radius value of the lathe
  9. * * The parameter `tessellation` (positive integer, default 64) is the side number of the lathe
  10. * * The parameter `clip` (positive integer, default 0) is the number of sides to not create without effecting the general shape of the sides
  11. * * The parameter `arc` (positive float, default 1) is the ratio of the lathe. 0.5 builds for instance half a lathe, so an opened shape
  12. * * The parameter `closed` (boolean, default true) opens/closes the lathe circumference. This should be set to false when used with the parameter "arc"
  13. * * The parameter `cap` sets the way the extruded shape is capped. Possible values : BABYLON.Mesh.NO_CAP (default), BABYLON.Mesh.CAP_START, BABYLON.Mesh.CAP_END, BABYLON.Mesh.CAP_ALL
  14. * * You can also set the mesh side orientation with the values : BABYLON.Mesh.FRONTSIDE (default), BABYLON.Mesh.BACKSIDE or BABYLON.Mesh.DOUBLESIDE
  15. * * 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
  16. * * The optional parameter `invertUV` (boolean, default false) swaps in the geometry the U and V coordinates to apply a texture
  17. * * The mesh can be set to updatable with the boolean parameter `updatable` (default false) if its internal geometry is supposed to change once created
  18. * @param name defines the name of the mesh
  19. * @param options defines the options used to create the mesh
  20. * @param scene defines the hosting scene
  21. * @returns the lathe mesh
  22. * @see https://doc.babylonjs.com/features/featuresDeepDive/mesh/creation/param#lathe
  23. */
  24. export function CreateLathe(name, options, scene = null) {
  25. const arc = options.arc ? (options.arc <= 0 || options.arc > 1 ? 1.0 : options.arc) : 1.0;
  26. const closed = options.closed === undefined ? true : options.closed;
  27. const shape = options.shape;
  28. const radius = options.radius || 1;
  29. const tessellation = options.tessellation || 64;
  30. const clip = options.clip || 0;
  31. const updatable = options.updatable;
  32. const sideOrientation = Mesh._GetDefaultSideOrientation(options.sideOrientation);
  33. const cap = options.cap || Mesh.NO_CAP;
  34. const pi2 = Math.PI * 2;
  35. const paths = [];
  36. const invertUV = options.invertUV || false;
  37. let i = 0;
  38. let p = 0;
  39. const step = (pi2 / tessellation) * arc;
  40. let rotated;
  41. let path;
  42. for (i = 0; i <= tessellation - clip; i++) {
  43. path = [];
  44. if (cap == Mesh.CAP_START || cap == Mesh.CAP_ALL) {
  45. path.push(new Vector3(0, shape[0].y, 0));
  46. path.push(new Vector3(Math.cos(i * step) * shape[0].x * radius, shape[0].y, Math.sin(i * step) * shape[0].x * radius));
  47. }
  48. for (p = 0; p < shape.length; p++) {
  49. rotated = new Vector3(Math.cos(i * step) * shape[p].x * radius, shape[p].y, Math.sin(i * step) * shape[p].x * radius);
  50. path.push(rotated);
  51. }
  52. if (cap == Mesh.CAP_END || cap == Mesh.CAP_ALL) {
  53. path.push(new Vector3(Math.cos(i * step) * shape[shape.length - 1].x * radius, shape[shape.length - 1].y, Math.sin(i * step) * shape[shape.length - 1].x * radius));
  54. path.push(new Vector3(0, shape[shape.length - 1].y, 0));
  55. }
  56. paths.push(path);
  57. }
  58. // lathe ribbon
  59. const lathe = CreateRibbon(name, { pathArray: paths, closeArray: closed, sideOrientation: sideOrientation, updatable: updatable, invertUV: invertUV, frontUVs: options.frontUVs, backUVs: options.backUVs }, scene);
  60. return lathe;
  61. }
  62. /**
  63. * Class containing static functions to help procedurally build meshes
  64. * @deprecated use the function direction from the module
  65. */
  66. export const LatheBuilder = {
  67. // eslint-disable-next-line @typescript-eslint/naming-convention
  68. CreateLathe,
  69. };
  70. Mesh.CreateLathe = (name, shape, radius, tessellation, scene, updatable, sideOrientation) => {
  71. const options = {
  72. shape: shape,
  73. radius: radius,
  74. tessellation: tessellation,
  75. sideOrientation: sideOrientation,
  76. updatable: updatable,
  77. };
  78. return CreateLathe(name, options, scene);
  79. };
  80. //# sourceMappingURL=latheBuilder.js.map