linesMesh.js 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. import { Color3, Color4 } from "../Maths/math.color.js";
  2. import { VertexBuffer } from "../Buffers/buffer.js";
  3. import { Mesh } from "../Meshes/mesh.js";
  4. import { InstancedMesh } from "../Meshes/instancedMesh.js";
  5. import { Material } from "../Materials/material.js";
  6. import { ShaderMaterial } from "../Materials/shaderMaterial.js";
  7. import "../Shaders/color.fragment.js";
  8. import "../Shaders/color.vertex.js";
  9. Mesh._LinesMeshParser = (parsedMesh, scene) => {
  10. return LinesMesh.Parse(parsedMesh, scene);
  11. };
  12. /**
  13. * Line mesh
  14. * @see https://doc.babylonjs.com/features/featuresDeepDive/mesh/creation/param
  15. */
  16. export class LinesMesh extends Mesh {
  17. _isShaderMaterial(shader) {
  18. return shader.getClassName() === "ShaderMaterial";
  19. }
  20. /**
  21. * Creates a new LinesMesh
  22. * @param name defines the name
  23. * @param scene defines the hosting scene
  24. * @param parent defines the parent mesh if any
  25. * @param source defines the optional source LinesMesh used to clone data from
  26. * @param doNotCloneChildren When cloning, skip cloning child meshes of source, default False.
  27. * When false, achieved by calling a clone(), also passing False.
  28. * This will make creation of children, recursive.
  29. * @param useVertexColor defines if this LinesMesh supports vertex color
  30. * @param useVertexAlpha defines if this LinesMesh supports vertex alpha
  31. * @param material material to use to draw the line. If not provided, will create a new one
  32. */
  33. constructor(name, scene = null, parent = null, source = null, doNotCloneChildren,
  34. /**
  35. * If vertex color should be applied to the mesh
  36. */
  37. useVertexColor,
  38. /**
  39. * If vertex alpha should be applied to the mesh
  40. */
  41. useVertexAlpha, material) {
  42. super(name, scene, parent, source, doNotCloneChildren);
  43. this.useVertexColor = useVertexColor;
  44. this.useVertexAlpha = useVertexAlpha;
  45. /**
  46. * Color of the line (Default: White)
  47. */
  48. this.color = new Color3(1, 1, 1);
  49. /**
  50. * Alpha of the line (Default: 1)
  51. */
  52. this.alpha = 1;
  53. if (source) {
  54. this.color = source.color.clone();
  55. this.alpha = source.alpha;
  56. this.useVertexColor = source.useVertexColor;
  57. this.useVertexAlpha = source.useVertexAlpha;
  58. }
  59. this.intersectionThreshold = 0.1;
  60. const defines = [];
  61. const options = {
  62. attributes: [VertexBuffer.PositionKind],
  63. uniforms: ["world", "viewProjection"],
  64. needAlphaBlending: true,
  65. defines: defines,
  66. useClipPlane: null,
  67. };
  68. if (useVertexAlpha === false) {
  69. options.needAlphaBlending = false;
  70. }
  71. else {
  72. options.defines.push("#define VERTEXALPHA");
  73. }
  74. if (!useVertexColor) {
  75. options.uniforms.push("color");
  76. this._color4 = new Color4();
  77. }
  78. else {
  79. options.defines.push("#define VERTEXCOLOR");
  80. options.attributes.push(VertexBuffer.ColorKind);
  81. }
  82. if (material) {
  83. this.material = material;
  84. }
  85. else {
  86. this.material = new ShaderMaterial("colorShader", this.getScene(), "color", options, false);
  87. this.material.doNotSerialize = true;
  88. }
  89. }
  90. isReady() {
  91. if (!this._lineMaterial.isReady(this, !!this._userInstancedBuffersStorage || this.hasThinInstances)) {
  92. return false;
  93. }
  94. return super.isReady();
  95. }
  96. /**
  97. * @returns the string "LineMesh"
  98. */
  99. getClassName() {
  100. return "LinesMesh";
  101. }
  102. /**
  103. * @internal
  104. */
  105. get material() {
  106. return this._lineMaterial;
  107. }
  108. /**
  109. * @internal
  110. */
  111. set material(value) {
  112. this._lineMaterial = value;
  113. this._lineMaterial.fillMode = Material.LineListDrawMode;
  114. }
  115. /**
  116. * @internal
  117. */
  118. get checkCollisions() {
  119. return false;
  120. }
  121. set checkCollisions(value) {
  122. // Just ignore it
  123. }
  124. /**
  125. * @internal
  126. */
  127. _bind(_subMesh, colorEffect) {
  128. if (!this._geometry) {
  129. return this;
  130. }
  131. // VBOs
  132. const indexToBind = this.isUnIndexed ? null : this._geometry.getIndexBuffer();
  133. if (!this._userInstancedBuffersStorage || this.hasThinInstances) {
  134. this._geometry._bind(colorEffect, indexToBind);
  135. }
  136. else {
  137. this._geometry._bind(colorEffect, indexToBind, this._userInstancedBuffersStorage.vertexBuffers, this._userInstancedBuffersStorage.vertexArrayObjects);
  138. }
  139. // Color
  140. if (!this.useVertexColor && this._isShaderMaterial(this._lineMaterial)) {
  141. const { r, g, b } = this.color;
  142. this._color4.set(r, g, b, this.alpha);
  143. this._lineMaterial.setColor4("color", this._color4);
  144. }
  145. return this;
  146. }
  147. /**
  148. * @internal
  149. */
  150. _draw(subMesh, fillMode, instancesCount) {
  151. if (!this._geometry || !this._geometry.getVertexBuffers() || (!this._unIndexed && !this._geometry.getIndexBuffer())) {
  152. return this;
  153. }
  154. const engine = this.getScene().getEngine();
  155. // Draw order
  156. if (this._unIndexed) {
  157. engine.drawArraysType(Material.LineListDrawMode, subMesh.verticesStart, subMesh.verticesCount, instancesCount);
  158. }
  159. else {
  160. engine.drawElementsType(Material.LineListDrawMode, subMesh.indexStart, subMesh.indexCount, instancesCount);
  161. }
  162. return this;
  163. }
  164. /**
  165. * Disposes of the line mesh
  166. * @param doNotRecurse If children should be disposed
  167. * @param disposeMaterialAndTextures This parameter is not used by the LineMesh class
  168. * @param doNotDisposeMaterial If the material should not be disposed (default: false, meaning the material is disposed)
  169. */
  170. // eslint-disable-next-line @typescript-eslint/no-unused-vars
  171. dispose(doNotRecurse, disposeMaterialAndTextures = false, doNotDisposeMaterial) {
  172. if (!doNotDisposeMaterial) {
  173. this._lineMaterial.dispose(false, false, true);
  174. }
  175. super.dispose(doNotRecurse);
  176. }
  177. /**
  178. * Returns a new LineMesh object cloned from the current one.
  179. * @param name defines the cloned mesh name
  180. * @param newParent defines the new mesh parent
  181. * @param doNotCloneChildren if set to true, none of the mesh children are cloned (false by default)
  182. * @returns the new mesh
  183. */
  184. clone(name, newParent = null, doNotCloneChildren) {
  185. return new LinesMesh(name, this.getScene(), newParent, this, doNotCloneChildren);
  186. }
  187. /**
  188. * Creates a new InstancedLinesMesh object from the mesh model.
  189. * @see https://doc.babylonjs.com/features/featuresDeepDive/mesh/copies/instances
  190. * @param name defines the name of the new instance
  191. * @returns a new InstancedLinesMesh
  192. */
  193. createInstance(name) {
  194. const instance = new InstancedLinesMesh(name, this);
  195. if (this.instancedBuffers) {
  196. instance.instancedBuffers = {};
  197. for (const key in this.instancedBuffers) {
  198. instance.instancedBuffers[key] = this.instancedBuffers[key];
  199. }
  200. }
  201. return instance;
  202. }
  203. /**
  204. * Serializes this ground mesh
  205. * @param serializationObject object to write serialization to
  206. */
  207. serialize(serializationObject) {
  208. super.serialize(serializationObject);
  209. serializationObject.color = this.color.asArray();
  210. serializationObject.alpha = this.alpha;
  211. }
  212. /**
  213. * Parses a serialized ground mesh
  214. * @param parsedMesh the serialized mesh
  215. * @param scene the scene to create the ground mesh in
  216. * @returns the created ground mesh
  217. */
  218. static Parse(parsedMesh, scene) {
  219. const result = new LinesMesh(parsedMesh.name, scene);
  220. result.color = Color3.FromArray(parsedMesh.color);
  221. result.alpha = parsedMesh.alpha;
  222. return result;
  223. }
  224. }
  225. /**
  226. * Creates an instance based on a source LinesMesh
  227. */
  228. export class InstancedLinesMesh extends InstancedMesh {
  229. constructor(name, source) {
  230. super(name, source);
  231. this.intersectionThreshold = source.intersectionThreshold;
  232. }
  233. /**
  234. * @returns the string "InstancedLinesMesh".
  235. */
  236. getClassName() {
  237. return "InstancedLinesMesh";
  238. }
  239. }
  240. //# sourceMappingURL=linesMesh.js.map