edgesRenderer.d.ts 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. import type { Immutable, Nullable } from "../types";
  2. import { VertexBuffer } from "../Buffers/buffer";
  3. import { AbstractMesh } from "../Meshes/abstractMesh";
  4. import type { Matrix } from "../Maths/math.vector";
  5. import { Vector3 } from "../Maths/math.vector";
  6. import type { IDisposable } from "../scene";
  7. import { ShaderMaterial } from "../Materials/shaderMaterial";
  8. import "../Shaders/line.fragment";
  9. import "../Shaders/line.vertex";
  10. import type { DataBuffer } from "../Buffers/dataBuffer";
  11. import { SmartArray } from "../Misc/smartArray";
  12. import { DrawWrapper } from "../Materials/drawWrapper";
  13. declare module "../scene" {
  14. interface Scene {
  15. /** @internal */
  16. _edgeRenderLineShader: Nullable<ShaderMaterial>;
  17. }
  18. }
  19. declare module "../Meshes/abstractMesh" {
  20. interface AbstractMesh {
  21. /**
  22. * Gets the edgesRenderer associated with the mesh
  23. */
  24. edgesRenderer: Nullable<EdgesRenderer>;
  25. }
  26. }
  27. declare module "../Meshes/linesMesh" {
  28. interface LinesMesh {
  29. /**
  30. * Enables the edge rendering mode on the mesh.
  31. * This mode makes the mesh edges visible
  32. * @param epsilon defines the maximal distance between two angles to detect a face
  33. * @param checkVerticesInsteadOfIndices indicates that we should check vertex list directly instead of faces
  34. * @returns the currentAbstractMesh
  35. * @see https://www.babylonjs-playground.com/#19O9TU#0
  36. */
  37. enableEdgesRendering(epsilon?: number, checkVerticesInsteadOfIndices?: boolean): AbstractMesh;
  38. }
  39. }
  40. declare module "../Meshes/linesMesh" {
  41. interface InstancedLinesMesh {
  42. /**
  43. * Enables the edge rendering mode on the mesh.
  44. * This mode makes the mesh edges visible
  45. * @param epsilon defines the maximal distance between two angles to detect a face
  46. * @param checkVerticesInsteadOfIndices indicates that we should check vertex list directly instead of faces
  47. * @returns the current InstancedLinesMesh
  48. * @see https://www.babylonjs-playground.com/#19O9TU#0
  49. */
  50. enableEdgesRendering(epsilon?: number, checkVerticesInsteadOfIndices?: boolean): InstancedLinesMesh;
  51. }
  52. }
  53. /**
  54. * Defines the minimum contract an Edges renderer should follow.
  55. */
  56. export interface IEdgesRenderer extends IDisposable {
  57. /**
  58. * Gets or sets a boolean indicating if the edgesRenderer is active
  59. */
  60. isEnabled: boolean;
  61. /**
  62. * Renders the edges of the attached mesh,
  63. */
  64. render(): void;
  65. /**
  66. * Checks whether or not the edges renderer is ready to render.
  67. * @returns true if ready, otherwise false.
  68. */
  69. isReady(): boolean;
  70. /**
  71. * List of instances to render in case the source mesh has instances
  72. */
  73. customInstances: SmartArray<Matrix>;
  74. }
  75. /**
  76. * Defines the additional options of the edges renderer
  77. */
  78. export interface IEdgesRendererOptions {
  79. /**
  80. * Gets or sets a boolean indicating that the alternate edge finder algorithm must be used
  81. * If not defined, the default value is true
  82. */
  83. useAlternateEdgeFinder?: boolean;
  84. /**
  85. * Gets or sets a boolean indicating that the vertex merger fast processing must be used.
  86. * If not defined, the default value is true.
  87. * You should normally leave it undefined (or set it to true), except if you see some artifacts in the edges rendering (can happen with complex geometries)
  88. * This option is used only if useAlternateEdgeFinder = true
  89. */
  90. useFastVertexMerger?: boolean;
  91. /**
  92. * During edges processing, the vertices are merged if they are close enough: epsilonVertexMerge is the limit within which vertices are considered to be equal.
  93. * The default value is 1e-6
  94. * This option is used only if useAlternateEdgeFinder = true
  95. */
  96. epsilonVertexMerge?: number;
  97. /**
  98. * Gets or sets a boolean indicating that tessellation should be applied before finding the edges. You may need to activate this option if your geometry is a bit
  99. * unusual, like having a vertex of a triangle in-between two vertices of an edge of another triangle. It happens often when using CSG to construct meshes.
  100. * This option is used only if useAlternateEdgeFinder = true
  101. */
  102. applyTessellation?: boolean;
  103. /**
  104. * The limit under which 3 vertices are considered to be aligned. 3 vertices PQR are considered aligned if distance(PQ) + distance(QR) - distance(PR) < epsilonVertexAligned
  105. * The default value is 1e-6
  106. * This option is used only if useAlternateEdgeFinder = true
  107. */
  108. epsilonVertexAligned?: number;
  109. /**
  110. * Gets or sets a boolean indicating that degenerated triangles should not be processed.
  111. * Degenerated triangles are triangles that have 2 or 3 vertices with the same coordinates
  112. */
  113. removeDegeneratedTriangles?: boolean;
  114. }
  115. /**
  116. * This class is used to generate edges of the mesh that could then easily be rendered in a scene.
  117. */
  118. export declare class EdgesRenderer implements IEdgesRenderer {
  119. /**
  120. * Define the size of the edges with an orthographic camera
  121. */
  122. edgesWidthScalerForOrthographic: number;
  123. /**
  124. * Define the size of the edges with a perspective camera
  125. */
  126. edgesWidthScalerForPerspective: number;
  127. protected _source: AbstractMesh;
  128. protected _linesPositions: number[];
  129. protected _linesNormals: number[];
  130. protected _linesIndices: number[];
  131. protected _epsilon: number;
  132. protected _indicesCount: number;
  133. protected _drawWrapper?: DrawWrapper;
  134. protected _lineShader: ShaderMaterial;
  135. protected _ib: DataBuffer;
  136. protected _buffers: {
  137. [key: string]: Nullable<VertexBuffer>;
  138. };
  139. protected _buffersForInstances: {
  140. [key: string]: Nullable<VertexBuffer>;
  141. };
  142. protected _checkVerticesInsteadOfIndices: boolean;
  143. protected _options: Nullable<IEdgesRendererOptions>;
  144. private _meshRebuildObserver;
  145. private _meshDisposeObserver;
  146. /** Gets or sets a boolean indicating if the edgesRenderer is active */
  147. isEnabled: boolean;
  148. /** Gets the vertices generated by the edge renderer */
  149. get linesPositions(): Immutable<Array<number>>;
  150. /** Gets the normals generated by the edge renderer */
  151. get linesNormals(): Immutable<Array<number>>;
  152. /** Gets the indices generated by the edge renderer */
  153. get linesIndices(): Immutable<Array<number>>;
  154. /**
  155. * Gets or sets the shader used to draw the lines
  156. */
  157. get lineShader(): ShaderMaterial;
  158. set lineShader(shader: ShaderMaterial);
  159. /**
  160. * List of instances to render in case the source mesh has instances
  161. */
  162. customInstances: SmartArray<Matrix>;
  163. private static _GetShader;
  164. /**
  165. * Creates an instance of the EdgesRenderer. It is primarily use to display edges of a mesh.
  166. * Beware when you use this class with complex objects as the adjacencies computation can be really long
  167. * @param source Mesh used to create edges
  168. * @param epsilon sum of angles in adjacency to check for edge
  169. * @param checkVerticesInsteadOfIndices bases the edges detection on vertices vs indices. Note that this parameter is not used if options.useAlternateEdgeFinder = true
  170. * @param generateEdgesLines - should generate Lines or only prepare resources.
  171. * @param options The options to apply when generating the edges
  172. */
  173. constructor(source: AbstractMesh, epsilon?: number, checkVerticesInsteadOfIndices?: boolean, generateEdgesLines?: boolean, options?: IEdgesRendererOptions);
  174. protected _prepareRessources(): void;
  175. /** @internal */
  176. _rebuild(): void;
  177. /**
  178. * Releases the required resources for the edges renderer
  179. */
  180. dispose(): void;
  181. protected _processEdgeForAdjacencies(pa: number, pb: number, p0: number, p1: number, p2: number): number;
  182. protected _processEdgeForAdjacenciesWithVertices(pa: Vector3, pb: Vector3, p0: Vector3, p1: Vector3, p2: Vector3): number;
  183. /**
  184. * Checks if the pair of p0 and p1 is en edge
  185. * @param faceIndex
  186. * @param edge
  187. * @param faceNormals
  188. * @param p0
  189. * @param p1
  190. * @private
  191. */
  192. protected _checkEdge(faceIndex: number, edge: number, faceNormals: Array<Vector3>, p0: Vector3, p1: Vector3): void;
  193. /**
  194. * push line into the position, normal and index buffer
  195. * @param p0
  196. * @param p1
  197. * @param offset
  198. * @protected
  199. */
  200. protected createLine(p0: Vector3, p1: Vector3, offset: number): void;
  201. /**
  202. * See https://playground.babylonjs.com/#R3JR6V#1 for a visual display of the algorithm
  203. * @param edgePoints
  204. * @param indexTriangle
  205. * @param indices
  206. * @param remapVertexIndices
  207. */
  208. private _tessellateTriangle;
  209. private _generateEdgesLinesAlternate;
  210. /**
  211. * Generates lines edges from adjacencjes
  212. * @private
  213. */
  214. _generateEdgesLines(): void;
  215. /**
  216. * Checks whether or not the edges renderer is ready to render.
  217. * @returns true if ready, otherwise false.
  218. */
  219. isReady(): boolean;
  220. /**
  221. * Renders the edges of the attached mesh,
  222. */
  223. render(): void;
  224. }
  225. /**
  226. * LineEdgesRenderer for LineMeshes to remove unnecessary triangulation
  227. */
  228. export declare class LineEdgesRenderer extends EdgesRenderer {
  229. /**
  230. * This constructor turns off auto generating edges line in Edges Renderer to make it here.
  231. * @param source LineMesh used to generate edges
  232. * @param epsilon not important (specified angle for edge detection)
  233. * @param checkVerticesInsteadOfIndices not important for LineMesh
  234. */
  235. constructor(source: AbstractMesh, epsilon?: number, checkVerticesInsteadOfIndices?: boolean);
  236. /**
  237. * Generate edges for each line in LinesMesh. Every Line should be rendered as edge.
  238. */
  239. _generateEdgesLines(): void;
  240. }