csg.d.ts 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. import type { Nullable } from "../types";
  2. import type { Scene } from "../scene";
  3. import { Quaternion, Matrix, Vector3, Vector2 } from "../Maths/math.vector";
  4. import { Mesh } from "../Meshes/mesh";
  5. import type { Material } from "../Materials/material";
  6. import { Color4 } from "../Maths/math.color";
  7. import { VertexData } from "./mesh.vertexData";
  8. /**
  9. * Represents a vertex of a polygon. Use your own vertex class instead of this
  10. * one to provide additional features like texture coordinates and vertex
  11. * colors. Custom vertex classes need to provide a `pos` property and `clone()`,
  12. * `flip()`, and `interpolate()` methods that behave analogous to the ones
  13. * defined by `BABYLON.CSG.Vertex`. This class provides `normal` so convenience
  14. * functions like `BABYLON.CSG.sphere()` can return a smooth vertex normal, but `normal`
  15. * is not used anywhere else.
  16. * Same goes for uv, it allows to keep the original vertex uv coordinates of the 2 meshes
  17. */
  18. declare class Vertex {
  19. /**
  20. * The position of the vertex
  21. */
  22. pos: Vector3;
  23. /**
  24. * The normal of the vertex
  25. */
  26. normal: Vector3;
  27. /**
  28. * The texture coordinate of the vertex
  29. */
  30. uv?: Vector2 | undefined;
  31. /**
  32. * The texture coordinate of the vertex
  33. */
  34. vertColor?: Color4 | undefined;
  35. /**
  36. * Initializes the vertex
  37. * @param pos The position of the vertex
  38. * @param normal The normal of the vertex
  39. * @param uv The texture coordinate of the vertex
  40. * @param vertColor The RGBA color of the vertex
  41. */
  42. constructor(
  43. /**
  44. * The position of the vertex
  45. */
  46. pos: Vector3,
  47. /**
  48. * The normal of the vertex
  49. */
  50. normal: Vector3,
  51. /**
  52. * The texture coordinate of the vertex
  53. */
  54. uv?: Vector2 | undefined,
  55. /**
  56. * The texture coordinate of the vertex
  57. */
  58. vertColor?: Color4 | undefined);
  59. /**
  60. * Make a clone, or deep copy, of the vertex
  61. * @returns A new Vertex
  62. */
  63. clone(): Vertex;
  64. /**
  65. * Invert all orientation-specific data (e.g. vertex normal). Called when the
  66. * orientation of a polygon is flipped.
  67. */
  68. flip(): void;
  69. /**
  70. * Create a new vertex between this vertex and `other` by linearly
  71. * interpolating all properties using a parameter of `t`. Subclasses should
  72. * override this to interpolate additional properties.
  73. * @param other the vertex to interpolate against
  74. * @param t The factor used to linearly interpolate between the vertices
  75. * @returns The new interpolated vertex
  76. */
  77. interpolate(other: Vertex, t: number): Vertex;
  78. }
  79. /**
  80. * Represents a plane in 3D space.
  81. */
  82. declare class CSGPlane {
  83. normal: Vector3;
  84. w: number;
  85. /**
  86. * Initializes the plane
  87. * @param normal The normal for the plane
  88. * @param w
  89. */
  90. constructor(normal: Vector3, w: number);
  91. /**
  92. * `CSG.Plane.EPSILON` is the tolerance used by `splitPolygon()` to decide if a
  93. * point is on the plane
  94. */
  95. static EPSILON: number;
  96. /**
  97. * Construct a plane from three points
  98. * @param a Point a
  99. * @param b Point b
  100. * @param c Point c
  101. * @returns A new plane
  102. */
  103. static FromPoints(a: Vector3, b: Vector3, c: Vector3): Nullable<CSGPlane>;
  104. /**
  105. * Clone, or make a deep copy of the plane
  106. * @returns a new Plane
  107. */
  108. clone(): CSGPlane;
  109. /**
  110. * Flip the face of the plane
  111. */
  112. flip(): void;
  113. /**
  114. * Split `polygon` by this plane if needed, then put the polygon or polygon
  115. * fragments in the appropriate lists. Coplanar polygons go into either
  116. `* coplanarFront` or `coplanarBack` depending on their orientation with
  117. * respect to this plane. Polygons in front or in back of this plane go into
  118. * either `front` or `back`
  119. * @param polygon The polygon to be split
  120. * @param coplanarFront Will contain polygons coplanar with the plane that are oriented to the front of the plane
  121. * @param coplanarBack Will contain polygons coplanar with the plane that are oriented to the back of the plane
  122. * @param front Will contain the polygons in front of the plane
  123. * @param back Will contain the polygons begind the plane
  124. */
  125. splitPolygon(polygon: CSGPolygon, coplanarFront: CSGPolygon[], coplanarBack: CSGPolygon[], front: CSGPolygon[], back: CSGPolygon[]): void;
  126. }
  127. /**
  128. * Represents a convex polygon. The vertices used to initialize a polygon must
  129. * be coplanar and form a convex loop.
  130. *
  131. * Each convex polygon has a `shared` property, which is shared between all
  132. * polygons that are clones of each other or were split from the same polygon.
  133. * This can be used to define per-polygon properties (such as surface color)
  134. */
  135. declare class CSGPolygon {
  136. /**
  137. * Vertices of the polygon
  138. */
  139. vertices: Vertex[];
  140. /**
  141. * Properties that are shared across all polygons
  142. */
  143. shared: any;
  144. /**
  145. * A plane formed from the vertices of the polygon
  146. */
  147. plane: CSGPlane;
  148. /**
  149. * Initializes the polygon
  150. * @param vertices The vertices of the polygon
  151. * @param shared The properties shared across all polygons
  152. */
  153. constructor(vertices: Vertex[], shared: any);
  154. /**
  155. * Clones, or makes a deep copy, or the polygon
  156. * @returns A new CSGPolygon
  157. */
  158. clone(): CSGPolygon;
  159. /**
  160. * Flips the faces of the polygon
  161. */
  162. flip(): void;
  163. }
  164. /**
  165. * Class for building Constructive Solid Geometry
  166. */
  167. export declare class CSG {
  168. private _polygons;
  169. /**
  170. * The world matrix
  171. */
  172. matrix: Matrix;
  173. /**
  174. * Stores the position
  175. */
  176. position: Vector3;
  177. /**
  178. * Stores the rotation
  179. */
  180. rotation: Vector3;
  181. /**
  182. * Stores the rotation quaternion
  183. */
  184. rotationQuaternion: Nullable<Quaternion>;
  185. /**
  186. * Stores the scaling vector
  187. */
  188. scaling: Vector3;
  189. /**
  190. * Convert a VertexData to CSG
  191. * @param data defines the VertexData to convert to CSG
  192. * @returns the new CSG
  193. */
  194. static FromVertexData(data: VertexData): CSG;
  195. /**
  196. * Convert the Mesh to CSG
  197. * @param mesh The Mesh to convert to CSG
  198. * @param absolute If true, the final (local) matrix transformation is set to the identity and not to that of `mesh`. It can help when dealing with right-handed meshes (default: false)
  199. * @returns A new CSG from the Mesh
  200. */
  201. static FromMesh(mesh: Mesh, absolute?: boolean): CSG;
  202. /**
  203. * Construct a CSG solid from a list of `CSG.Polygon` instances.
  204. * @param polygons Polygons used to construct a CSG solid
  205. * @returns A new CSG solid
  206. */
  207. private static _FromPolygons;
  208. /**
  209. * Clones, or makes a deep copy, of the CSG
  210. * @returns A new CSG
  211. */
  212. clone(): CSG;
  213. /**
  214. * Unions this CSG with another CSG
  215. * @param csg The CSG to union against this CSG
  216. * @returns The unioned CSG
  217. */
  218. union(csg: CSG): CSG;
  219. /**
  220. * Unions this CSG with another CSG in place
  221. * @param csg The CSG to union against this CSG
  222. */
  223. unionInPlace(csg: CSG): void;
  224. /**
  225. * Subtracts this CSG with another CSG
  226. * @param csg The CSG to subtract against this CSG
  227. * @returns A new CSG
  228. */
  229. subtract(csg: CSG): CSG;
  230. /**
  231. * Subtracts this CSG with another CSG in place
  232. * @param csg The CSG to subtract against this CSG
  233. */
  234. subtractInPlace(csg: CSG): void;
  235. /**
  236. * Intersect this CSG with another CSG
  237. * @param csg The CSG to intersect against this CSG
  238. * @returns A new CSG
  239. */
  240. intersect(csg: CSG): CSG;
  241. /**
  242. * Intersects this CSG with another CSG in place
  243. * @param csg The CSG to intersect against this CSG
  244. */
  245. intersectInPlace(csg: CSG): void;
  246. /**
  247. * Return a new CSG solid with solid and empty space switched. This solid is
  248. * not modified.
  249. * @returns A new CSG solid with solid and empty space switched
  250. */
  251. inverse(): CSG;
  252. /**
  253. * Inverses the CSG in place
  254. */
  255. inverseInPlace(): void;
  256. /**
  257. * This is used to keep meshes transformations so they can be restored
  258. * when we build back a Babylon Mesh
  259. * NB : All CSG operations are performed in world coordinates
  260. * @param csg The CSG to copy the transform attributes from
  261. * @returns This CSG
  262. */
  263. copyTransformAttributes(csg: CSG): CSG;
  264. /**
  265. * Build vertex data from CSG
  266. * Coordinates here are in world space
  267. * @param onBeforePolygonProcessing called before each polygon is being processed
  268. * @param onAfterPolygonProcessing called after each polygon has been processed
  269. * @returns the final vertex data
  270. */
  271. toVertexData(onBeforePolygonProcessing?: Nullable<(polygon: CSGPolygon) => void>, onAfterPolygonProcessing?: Nullable<() => void>): VertexData;
  272. /**
  273. * Build Raw mesh from CSG
  274. * Coordinates here are in world space
  275. * @param name The name of the mesh geometry
  276. * @param scene The Scene
  277. * @param keepSubMeshes Specifies if the submeshes should be kept
  278. * @returns A new Mesh
  279. */
  280. buildMeshGeometry(name: string, scene?: Scene, keepSubMeshes?: boolean): Mesh;
  281. /**
  282. * Build Mesh from CSG taking material and transforms into account
  283. * @param name The name of the Mesh
  284. * @param material The material of the Mesh
  285. * @param scene The Scene
  286. * @param keepSubMeshes Specifies if submeshes should be kept
  287. * @returns The new Mesh
  288. */
  289. toMesh(name: string, material?: Nullable<Material>, scene?: Scene, keepSubMeshes?: boolean): Mesh;
  290. }
  291. export {};