babylon.linesMesh.ts 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. module BABYLON {
  2. export class LinesMesh extends Mesh {
  3. public color = new Color3(1, 1, 1);
  4. public alpha = 1;
  5. /**
  6. * The intersection Threshold is the margin applied when intersection a segment of the LinesMesh with a Ray.
  7. * This margin is expressed in world space coordinates, so its value may vary.
  8. * Default value is 0.1
  9. * @returns the intersection Threshold value.
  10. */
  11. public get intersectionThreshold(): number {
  12. return this._intersectionThreshold;
  13. }
  14. /**
  15. * The intersection Threshold is the margin applied when intersection a segment of the LinesMesh with a Ray.
  16. * This margin is expressed in world space coordinates, so its value may vary.
  17. * @param value the new threshold to apply
  18. */
  19. public set intersectionThreshold(value: number) {
  20. if (this._intersectionThreshold === value) {
  21. return;
  22. }
  23. this._intersectionThreshold = value;
  24. if (this.geometry) {
  25. this.geometry.boundingBias = new Vector2(0, value);
  26. }
  27. }
  28. private _intersectionThreshold: number;
  29. private _colorShader: ShaderMaterial;
  30. constructor(name: string, scene: Nullable<Scene> = null, parent: Nullable<Node> = null, source?: LinesMesh, doNotCloneChildren?: boolean, public useVertexColor?: boolean, public useVertexAlpha?: boolean) {
  31. super(name, scene, parent, source, doNotCloneChildren);
  32. if (source) {
  33. this.color = source.color.clone();
  34. this.alpha = source.alpha;
  35. this.useVertexColor = source.useVertexColor;
  36. this.useVertexAlpha = source.useVertexAlpha;
  37. }
  38. this._intersectionThreshold = 0.1;
  39. var defines: String[] = [];
  40. var options = {
  41. attributes: [VertexBuffer.PositionKind],
  42. uniforms: ["world", "viewProjection"],
  43. needAlphaBlending: true,
  44. defines: defines
  45. };
  46. if (useVertexAlpha === false) {
  47. options.needAlphaBlending = false;
  48. }
  49. if (!useVertexColor) {
  50. options.uniforms.push("color");
  51. }
  52. else {
  53. options.defines.push("#define VERTEXCOLOR");
  54. options.attributes.push(VertexBuffer.ColorKind);
  55. }
  56. this._colorShader = new ShaderMaterial("colorShader", this.getScene(), "color", options);
  57. }
  58. /**
  59. * Returns the string "LineMesh"
  60. */
  61. public getClassName(): string {
  62. return "LinesMesh";
  63. }
  64. public get material(): Material {
  65. return this._colorShader;
  66. }
  67. public set material(value: Material) {
  68. // Do nothing
  69. }
  70. public get checkCollisions(): boolean {
  71. return false;
  72. }
  73. public createInstance(name: string): InstancedMesh {
  74. throw new Error("LinesMeshes do not support createInstance.");
  75. }
  76. public _bind(subMesh: SubMesh, effect: Effect, fillMode: number): LinesMesh {
  77. if (!this._geometry) {
  78. return this;
  79. }
  80. // VBOs
  81. this._geometry._bind(this._colorShader.getEffect());
  82. // Color
  83. if (!this.useVertexColor) {
  84. this._colorShader.setColor4("color", this.color.toColor4(this.alpha));
  85. }
  86. return this;
  87. }
  88. public _draw(subMesh: SubMesh, fillMode: number, instancesCount?: number): LinesMesh {
  89. if (!this._geometry || !this._geometry.getVertexBuffers() || !this._geometry.getIndexBuffer()) {
  90. return this;
  91. }
  92. var engine = this.getScene().getEngine();
  93. // Draw order
  94. engine.draw(false, subMesh.indexStart, subMesh.indexCount);
  95. return this;
  96. }
  97. public dispose(doNotRecurse?: boolean): void {
  98. this._colorShader.dispose();
  99. super.dispose(doNotRecurse);
  100. }
  101. /**
  102. * Returns a new LineMesh object cloned from the current one.
  103. */
  104. public clone(name: string, newParent?: Node, doNotCloneChildren?: boolean): LinesMesh {
  105. return new LinesMesh(name, this.getScene(), newParent, this, doNotCloneChildren);
  106. }
  107. }
  108. }