babylon.linesMesh.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. module BABYLON {
  2. /**
  3. * Line mesh
  4. * @see https://doc.babylonjs.com/babylon101/parametric_shapes
  5. */
  6. export class LinesMesh extends Mesh {
  7. /**
  8. * Color of the line (Default: White)
  9. */
  10. public color = new Color3(1, 1, 1);
  11. /**
  12. * Alpha of the line (Default: 1)
  13. */
  14. public alpha = 1;
  15. /**
  16. * The intersection Threshold is the margin applied when intersection a segment of the LinesMesh with a Ray.
  17. * This margin is expressed in world space coordinates, so its value may vary.
  18. * Default value is 0.1
  19. * @returns the intersection Threshold value.
  20. */
  21. public get intersectionThreshold(): number {
  22. return this._intersectionThreshold;
  23. }
  24. /**
  25. * The intersection Threshold is the margin applied when intersection a segment of the LinesMesh with a Ray.
  26. * This margin is expressed in world space coordinates, so its value may vary.
  27. */
  28. public set intersectionThreshold(value: number) {
  29. if (this._intersectionThreshold === value) {
  30. return;
  31. }
  32. this._intersectionThreshold = value;
  33. }
  34. private _intersectionThreshold: number;
  35. private _colorShader: ShaderMaterial;
  36. /**
  37. * Creates a new LinesMesh
  38. * @param name defines the name
  39. * @param scene defines the hosting scene
  40. * @param parent defines the parent mesh if any
  41. * @param source defines the optional source LinesMesh used to clone data from
  42. * @param doNotCloneChildren When cloning, skip cloning child meshes of source, default False.
  43. * When false, achieved by calling a clone(), also passing False.
  44. * This will make creation of children, recursive.
  45. * @param useVertexColor defines if this LinesMesh supports vertex color
  46. * @param useVertexAlpha defines if this LinesMesh supports vertex alpha
  47. */
  48. constructor(
  49. name: string,
  50. scene: Nullable<Scene> = null,
  51. parent: Nullable<Node> = null,
  52. source?: LinesMesh,
  53. doNotCloneChildren?: boolean,
  54. /**
  55. * If vertex color should be applied to the mesh
  56. */
  57. public useVertexColor?: boolean,
  58. /**
  59. * If vertex alpha should be applied to the mesh
  60. */
  61. public useVertexAlpha?: boolean
  62. ) {
  63. super(name, scene, parent, source, doNotCloneChildren);
  64. if (source) {
  65. this.color = source.color.clone();
  66. this.alpha = source.alpha;
  67. this.useVertexColor = source.useVertexColor;
  68. this.useVertexAlpha = source.useVertexAlpha;
  69. }
  70. this._intersectionThreshold = 0.1;
  71. var defines: string[] = [];
  72. var options = {
  73. attributes: [VertexBuffer.PositionKind, "world0", "world1", "world2", "world3"],
  74. uniforms: ["world", "viewProjection"],
  75. needAlphaBlending: true,
  76. defines: defines
  77. };
  78. if (useVertexAlpha === false) {
  79. options.needAlphaBlending = false;
  80. }
  81. if (!useVertexColor) {
  82. options.uniforms.push("color");
  83. }
  84. else {
  85. options.defines.push("#define VERTEXCOLOR");
  86. options.attributes.push(VertexBuffer.ColorKind);
  87. }
  88. this._colorShader = new ShaderMaterial("colorShader", this.getScene(), "color", options);
  89. }
  90. /**
  91. * Returns the string "LineMesh"
  92. */
  93. public getClassName(): string {
  94. return "LinesMesh";
  95. }
  96. /**
  97. * @hidden
  98. */
  99. public get material(): Material {
  100. return this._colorShader;
  101. }
  102. /**
  103. * @hidden
  104. */
  105. public set material(value: Material) {
  106. // Do nothing
  107. }
  108. /**
  109. * @hidden
  110. */
  111. public get checkCollisions(): boolean {
  112. return false;
  113. }
  114. /** @hidden */
  115. public _bind(subMesh: SubMesh, effect: Effect, fillMode: number): LinesMesh {
  116. if (!this._geometry) {
  117. return this;
  118. }
  119. // VBOs
  120. this._geometry._bind(this._colorShader.getEffect());
  121. // Color
  122. if (!this.useVertexColor) {
  123. this._colorShader.setColor4("color", this.color.toColor4(this.alpha));
  124. }
  125. return this;
  126. }
  127. /** @hidden */
  128. public _draw(subMesh: SubMesh, fillMode: number, instancesCount?: number): LinesMesh {
  129. if (!this._geometry || !this._geometry.getVertexBuffers() || (!this._unIndexed && !this._geometry.getIndexBuffer())) {
  130. return this;
  131. }
  132. var engine = this.getScene().getEngine();
  133. // Draw order
  134. engine.drawElementsType(Material.LineListDrawMode, subMesh.indexStart, subMesh.indexCount, instancesCount);
  135. return this;
  136. }
  137. /**
  138. * Disposes of the line mesh
  139. * @param doNotRecurse If children should be disposed
  140. */
  141. public dispose(doNotRecurse?: boolean): void {
  142. this._colorShader.dispose(false, false, true);
  143. super.dispose(doNotRecurse);
  144. }
  145. /**
  146. * Returns a new LineMesh object cloned from the current one.
  147. */
  148. public clone(name: string, newParent?: Node, doNotCloneChildren?: boolean): LinesMesh {
  149. return new LinesMesh(name, this.getScene(), newParent, this, doNotCloneChildren);
  150. }
  151. }
  152. }