shapeBuilder.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261
  1. import { Vector3, TmpVectors, Matrix } from "../../Maths/math.vector.js";
  2. import { Mesh } from "../mesh.js";
  3. import { CreateRibbon } from "./ribbonBuilder.js";
  4. import { Path3D } from "../../Maths/math.path.js";
  5. /**
  6. * Creates an extruded shape mesh. The extrusion is a parametric shape. It has no predefined shape. Its final shape will depend on the input parameters.
  7. * * The parameter `shape` is a required array of successive Vector3. This array depicts the shape to be extruded in its local space : the shape must be designed in the xOy plane and will be extruded along the Z axis.
  8. * * The parameter `path` is a required array of successive Vector3. This is the axis curve the shape is extruded along.
  9. * * The parameter `rotation` (float, default 0 radians) is the angle value to rotate the shape each step (each path point), from the former step (so rotation added each step) along the curve.
  10. * * The parameter `scale` (float, default 1) is the value to scale the shape.
  11. * * The parameter `closeShape` (boolean, default false) closes the shape when true, since v5.0.0.
  12. * * The parameter `closePath` (boolean, default false) closes the path when true and no caps, since v5.0.0.
  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. * * The optional parameter `instance` is an instance of an existing ExtrudedShape object to be updated with the passed `shape`, `path`, `scale` or `rotation` parameters : https://doc.babylonjs.com/features/featuresDeepDive/mesh/dynamicMeshMorph#extruded-shape
  15. * * Remember you can only change the shape or path point positions, not their number when updating an extruded shape.
  16. * * You can also set the mesh side orientation with the values : BABYLON.Mesh.FRONTSIDE (default), BABYLON.Mesh.BACKSIDE or BABYLON.Mesh.DOUBLESIDE
  17. * * 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
  18. * * The optional parameter `invertUV` (boolean, default false) swaps in the geometry the U and V coordinates to apply a texture.
  19. * * The mesh can be set to updatable with the boolean parameter `updatable` (default false) if its internal geometry is supposed to change once created.
  20. * * The optional parameter `firstNormal` (Vector3) defines the direction of the first normal of the supplied path. Consider using this for any path that is straight, and particular for paths in the xy plane.
  21. * * The optional `adjustFrame` (boolean, default false) will cause the internally generated Path3D tangents, normals, and binormals to be adjusted so that a) they are always well-defined, and b) they do not reverse from one path point to the next. This prevents the extruded shape from being flipped and/or rotated with resulting mesh self-intersections. This is primarily useful for straight paths that can reverse direction.
  22. * @param name defines the name of the mesh
  23. * @param options defines the options used to create the mesh
  24. * @param scene defines the hosting scene
  25. * @returns the extruded shape mesh
  26. * @see https://doc.babylonjs.com/features/featuresDeepDive/mesh/creation/param
  27. * @see https://doc.babylonjs.com/features/featuresDeepDive/mesh/creation/param#extruded-shapes
  28. */
  29. export function ExtrudeShape(name, options, scene = null) {
  30. const path = options.path;
  31. const shape = options.shape;
  32. const scale = options.scale || 1;
  33. const rotation = options.rotation || 0;
  34. const cap = options.cap === 0 ? 0 : options.cap || Mesh.NO_CAP;
  35. const updatable = options.updatable;
  36. const sideOrientation = Mesh._GetDefaultSideOrientation(options.sideOrientation);
  37. const instance = options.instance || null;
  38. const invertUV = options.invertUV || false;
  39. const closeShape = options.closeShape || false;
  40. const closePath = options.closePath || false;
  41. return _ExtrudeShapeGeneric(name, shape, path, scale, rotation, null, null, closePath, closeShape, cap, false, scene, updatable ? true : false, sideOrientation, instance, invertUV, options.frontUVs || null, options.backUVs || null, options.firstNormal || null, options.adjustFrame ? true : false);
  42. }
  43. /**
  44. * Creates an custom extruded shape mesh.
  45. * The custom extrusion is a parametric shape. It has no predefined shape. Its final shape will depend on the input parameters.
  46. * * The parameter `shape` is a required array of successive Vector3. This array depicts the shape to be extruded in its local space : the shape must be designed in the xOy plane and will be extruded along the Z axis.
  47. * * The parameter `path` is a required array of successive Vector3. This is the axis curve the shape is extruded along.
  48. * * The parameter `rotationFunction` (JS function) is a custom Javascript function called on each path point. This function is passed the position i of the point in the path and the distance of this point from the beginning of the path
  49. * * It must returns a float value that will be the rotation in radians applied to the shape on each path point.
  50. * * The parameter `scaleFunction` (JS function) is a custom Javascript function called on each path point. This function is passed the position i of the point in the path and the distance of this point from the beginning of the path
  51. * * It must returns a float value that will be the scale value applied to the shape on each path point
  52. * * The parameter `closeShape` (boolean, default false) closes the shape when true, since v5.0.0.
  53. * * The parameter `closePath` (boolean, default false) closes the path when true and no caps, since v5.0.0.
  54. * * The parameter `ribbonClosePath` (boolean, default false) forces the extrusion underlying ribbon to close all the paths in its `pathArray` - depreciated in favor of closeShape
  55. * * The parameter `ribbonCloseArray` (boolean, default false) forces the extrusion underlying ribbon to close its `pathArray` - depreciated in favor of closePath
  56. * * 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
  57. * * The optional parameter `instance` is an instance of an existing ExtrudedShape object to be updated with the passed `shape`, `path`, `scale` or `rotation` parameters : https://doc.babylonjs.com/features/featuresDeepDive/mesh/dynamicMeshMorph#extruded-shape
  58. * * Remember you can only change the shape or path point positions, not their number when updating an extruded shape
  59. * * You can also set the mesh side orientation with the values : BABYLON.Mesh.FRONTSIDE (default), BABYLON.Mesh.BACKSIDE or BABYLON.Mesh.DOUBLESIDE
  60. * * 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
  61. * * The optional parameter `invertUV` (boolean, default false) swaps in the geometry the U and V coordinates to apply a texture
  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. * * The optional parameter `firstNormal` (Vector3) defines the direction of the first normal of the supplied path. It should be supplied when the path is in the xy plane, and particularly if these sections are straight, because the underlying Path3D object will pick a normal in the xy plane that causes the extrusion to be collapsed into the plane. This should be used for any path that is straight.
  64. * * The optional `adjustFrame` (boolean, default false) will cause the internally generated Path3D tangents, normals, and binormals to be adjusted so that a) they are always well-defined, and b) they do not reverse from one path point to the next. This prevents the extruded shape from being flipped and/or rotated with resulting mesh self-intersections. This is primarily useful for straight paths that can reverse direction.
  65. * @param name defines the name of the mesh
  66. * @param options defines the options used to create the mesh
  67. * @param scene defines the hosting scene
  68. * @returns the custom extruded shape mesh
  69. * @see https://doc.babylonjs.com/features/featuresDeepDive/mesh/creation/param#custom-extruded-shapes
  70. * @see https://doc.babylonjs.com/features/featuresDeepDive/mesh/creation/param
  71. * @see https://doc.babylonjs.com/features/featuresDeepDive/mesh/creation/param#extruded-shapes
  72. */
  73. export function ExtrudeShapeCustom(name, options, scene = null) {
  74. const path = options.path;
  75. const shape = options.shape;
  76. const scaleFunction = options.scaleFunction ||
  77. (() => {
  78. return 1;
  79. });
  80. const rotationFunction = options.rotationFunction ||
  81. (() => {
  82. return 0;
  83. });
  84. const ribbonCloseArray = options.closePath || options.ribbonCloseArray || false;
  85. const ribbonClosePath = options.closeShape || options.ribbonClosePath || false;
  86. const cap = options.cap === 0 ? 0 : options.cap || Mesh.NO_CAP;
  87. const updatable = options.updatable;
  88. const firstNormal = options.firstNormal || null;
  89. const adjustFrame = options.adjustFrame || false;
  90. const sideOrientation = Mesh._GetDefaultSideOrientation(options.sideOrientation);
  91. const instance = options.instance;
  92. const invertUV = options.invertUV || false;
  93. return _ExtrudeShapeGeneric(name, shape, path, null, null, scaleFunction, rotationFunction, ribbonCloseArray, ribbonClosePath, cap, true, scene, updatable ? true : false, sideOrientation, instance || null, invertUV, options.frontUVs || null, options.backUVs || null, firstNormal, adjustFrame);
  94. }
  95. function _ExtrudeShapeGeneric(name, shape, curve, scale, rotation, scaleFunction, rotateFunction, rbCA, rbCP, cap, custom, scene, updtbl, side, instance, invertUV, frontUVs, backUVs, firstNormal, adjustFrame) {
  96. // extrusion geometry
  97. const extrusionPathArray = (shape, curve, path3D, shapePaths, scale, rotation, scaleFunction, rotateFunction, cap, custom, adjustFrame) => {
  98. const tangents = path3D.getTangents();
  99. const normals = path3D.getNormals();
  100. const binormals = path3D.getBinormals();
  101. const distances = path3D.getDistances();
  102. if (adjustFrame) {
  103. /* fix tangents,normals, binormals */
  104. for (let i = 0; i < tangents.length; i++) {
  105. if (tangents[i].x == 0 && tangents[i].y == 0 && tangents[i].z == 0) {
  106. tangents[i].copyFrom(tangents[i - 1]);
  107. }
  108. if (normals[i].x == 0 && normals[i].y == 0 && normals[i].z == 0) {
  109. normals[i].copyFrom(normals[i - 1]);
  110. }
  111. if (binormals[i].x == 0 && binormals[i].y == 0 && binormals[i].z == 0) {
  112. binormals[i].copyFrom(binormals[i - 1]);
  113. }
  114. if (i > 0) {
  115. let v = tangents[i - 1];
  116. if (Vector3.Dot(v, tangents[i]) < 0) {
  117. tangents[i].scaleInPlace(-1);
  118. }
  119. v = normals[i - 1];
  120. if (Vector3.Dot(v, normals[i]) < 0) {
  121. normals[i].scaleInPlace(-1);
  122. }
  123. v = binormals[i - 1];
  124. if (Vector3.Dot(v, binormals[i]) < 0) {
  125. binormals[i].scaleInPlace(-1);
  126. }
  127. }
  128. }
  129. }
  130. let angle = 0;
  131. const returnScale = () => {
  132. return scale !== null ? scale : 1;
  133. };
  134. const returnRotation = () => {
  135. return rotation !== null ? rotation : 0;
  136. };
  137. const rotate = custom && rotateFunction ? rotateFunction : returnRotation;
  138. const scl = custom && scaleFunction ? scaleFunction : returnScale;
  139. let index = cap === Mesh.NO_CAP || cap === Mesh.CAP_END ? 0 : 2;
  140. const rotationMatrix = TmpVectors.Matrix[0];
  141. for (let i = 0; i < curve.length; i++) {
  142. const shapePath = [];
  143. const angleStep = rotate(i, distances[i]);
  144. const scaleRatio = scl(i, distances[i]);
  145. Matrix.RotationAxisToRef(tangents[i], angle, rotationMatrix);
  146. for (let p = 0; p < shape.length; p++) {
  147. const planed = tangents[i].scale(shape[p].z).add(normals[i].scale(shape[p].x)).add(binormals[i].scale(shape[p].y));
  148. const rotated = Vector3.Zero();
  149. Vector3.TransformCoordinatesToRef(planed, rotationMatrix, rotated);
  150. rotated.scaleInPlace(scaleRatio).addInPlace(curve[i]);
  151. shapePath[p] = rotated;
  152. }
  153. shapePaths[index] = shapePath;
  154. angle += angleStep;
  155. index++;
  156. }
  157. // cap
  158. const capPath = (shapePath) => {
  159. const pointCap = Array();
  160. const barycenter = Vector3.Zero();
  161. let i;
  162. for (i = 0; i < shapePath.length; i++) {
  163. barycenter.addInPlace(shapePath[i]);
  164. }
  165. barycenter.scaleInPlace(1.0 / shapePath.length);
  166. for (i = 0; i < shapePath.length; i++) {
  167. pointCap.push(barycenter);
  168. }
  169. return pointCap;
  170. };
  171. switch (cap) {
  172. case Mesh.NO_CAP:
  173. break;
  174. case Mesh.CAP_START:
  175. shapePaths[0] = capPath(shapePaths[2]);
  176. shapePaths[1] = shapePaths[2];
  177. break;
  178. case Mesh.CAP_END:
  179. shapePaths[index] = shapePaths[index - 1];
  180. shapePaths[index + 1] = capPath(shapePaths[index - 1]);
  181. break;
  182. case Mesh.CAP_ALL:
  183. shapePaths[0] = capPath(shapePaths[2]);
  184. shapePaths[1] = shapePaths[2];
  185. shapePaths[index] = shapePaths[index - 1];
  186. shapePaths[index + 1] = capPath(shapePaths[index - 1]);
  187. break;
  188. default:
  189. break;
  190. }
  191. return shapePaths;
  192. };
  193. let path3D;
  194. let pathArray;
  195. if (instance) {
  196. // instance update
  197. const storage = instance._creationDataStorage;
  198. path3D = firstNormal ? storage.path3D.update(curve, firstNormal) : storage.path3D.update(curve);
  199. pathArray = extrusionPathArray(shape, curve, storage.path3D, storage.pathArray, scale, rotation, scaleFunction, rotateFunction, storage.cap, custom, adjustFrame);
  200. instance = CreateRibbon("", { pathArray, closeArray: false, closePath: false, offset: 0, updatable: false, sideOrientation: 0, instance }, scene || undefined);
  201. return instance;
  202. }
  203. // extruded shape creation
  204. path3D = firstNormal ? new Path3D(curve, firstNormal) : new Path3D(curve);
  205. const newShapePaths = new Array();
  206. cap = cap < 0 || cap > 3 ? 0 : cap;
  207. pathArray = extrusionPathArray(shape, curve, path3D, newShapePaths, scale, rotation, scaleFunction, rotateFunction, cap, custom, adjustFrame);
  208. const extrudedGeneric = CreateRibbon(name, {
  209. pathArray: pathArray,
  210. closeArray: rbCA,
  211. closePath: rbCP,
  212. updatable: updtbl,
  213. sideOrientation: side,
  214. invertUV: invertUV,
  215. frontUVs: frontUVs || undefined,
  216. backUVs: backUVs || undefined,
  217. }, scene);
  218. extrudedGeneric._creationDataStorage.pathArray = pathArray;
  219. extrudedGeneric._creationDataStorage.path3D = path3D;
  220. extrudedGeneric._creationDataStorage.cap = cap;
  221. return extrudedGeneric;
  222. }
  223. /**
  224. * Class containing static functions to help procedurally build meshes
  225. * @deprecated please use the functions directly from the module
  226. */
  227. export const ShapeBuilder = {
  228. // eslint-disable-next-line @typescript-eslint/naming-convention
  229. ExtrudeShape,
  230. // eslint-disable-next-line @typescript-eslint/naming-convention
  231. ExtrudeShapeCustom,
  232. };
  233. Mesh.ExtrudeShape = (name, shape, path, scale, rotation, cap, scene = null, updatable, sideOrientation, instance) => {
  234. const options = {
  235. shape: shape,
  236. path: path,
  237. scale: scale,
  238. rotation: rotation,
  239. cap: cap === 0 ? 0 : cap || Mesh.NO_CAP,
  240. sideOrientation: sideOrientation,
  241. instance: instance,
  242. updatable: updatable,
  243. };
  244. return ExtrudeShape(name, options, scene);
  245. };
  246. Mesh.ExtrudeShapeCustom = (name, shape, path, scaleFunction, rotationFunction, ribbonCloseArray, ribbonClosePath, cap, scene, updatable, sideOrientation, instance) => {
  247. const options = {
  248. shape: shape,
  249. path: path,
  250. scaleFunction: scaleFunction,
  251. rotationFunction: rotationFunction,
  252. ribbonCloseArray: ribbonCloseArray,
  253. ribbonClosePath: ribbonClosePath,
  254. cap: cap === 0 ? 0 : cap || Mesh.NO_CAP,
  255. sideOrientation: sideOrientation,
  256. instance: instance,
  257. updatable: updatable,
  258. };
  259. return ExtrudeShapeCustom(name, options, scene);
  260. };
  261. //# sourceMappingURL=shapeBuilder.js.map