babylon.gridmaterial.ts 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. module BABYLON {
  3. class GridMaterialDefines extends MaterialDefines {
  4. public TRANSPARENT = false;
  5. public FOG = false;
  6. constructor() {
  7. super();
  8. this.rebuild();
  9. }
  10. }
  11. /**
  12. * The grid materials allows you to wrap any shape with a grid.
  13. * Colors are customizable.
  14. */
  15. export class GridMaterial extends BABYLON.PushMaterial {
  16. /**
  17. * Main color of the grid (e.g. between lines)
  18. */
  19. @serializeAsColor3()
  20. public mainColor = Color3.White();
  21. /**
  22. * Color of the grid lines.
  23. */
  24. @serializeAsColor3()
  25. public lineColor = Color3.Black();
  26. /**
  27. * The scale of the grid compared to unit.
  28. */
  29. @serialize()
  30. public gridRatio = 1.0;
  31. /**
  32. * The frequency of thicker lines.
  33. */
  34. @serialize()
  35. public majorUnitFrequency = 10;
  36. /**
  37. * The visibility of minor units in the grid.
  38. */
  39. @serialize()
  40. public minorUnitVisibility = 0.33;
  41. /**
  42. * The grid opacity outside of the lines.
  43. */
  44. @serialize()
  45. public opacity = 1.0;
  46. private _gridControl: Vector4 = new Vector4(this.gridRatio, this.majorUnitFrequency, this.minorUnitVisibility, this.opacity);
  47. private _renderId: number;
  48. /**
  49. * constructor
  50. * @param name The name given to the material in order to identify it afterwards.
  51. * @param scene The scene the material is used in.
  52. */
  53. constructor(name: string, scene: Scene) {
  54. super(name, scene);
  55. }
  56. /**
  57. * Returns wehter or not the grid requires alpha blending.
  58. */
  59. public needAlphaBlending(): boolean {
  60. return this.opacity < 1.0;
  61. }
  62. public isReadyForSubMesh(mesh: AbstractMesh, subMesh: SubMesh, useInstances?: boolean): boolean {
  63. if (this.isFrozen) {
  64. if (this._wasPreviouslyReady && subMesh.effect) {
  65. return true;
  66. }
  67. }
  68. if (!subMesh._materialDefines) {
  69. subMesh._materialDefines = new GridMaterialDefines();
  70. }
  71. var defines = <GridMaterialDefines>subMesh._materialDefines;
  72. var scene = this.getScene();
  73. if (!this.checkReadyOnEveryCall) {
  74. if (this._renderId === scene.getRenderId()) {
  75. return true;
  76. }
  77. }
  78. var engine = scene.getEngine();
  79. if (this.opacity < 1.0 && !defines.TRANSPARENT) {
  80. defines.TRANSPARENT = true;
  81. defines.markAsUnprocessed();
  82. }
  83. MaterialHelper.PrepareDefinesForMisc(mesh, scene, false, false, this.fogEnabled, defines);
  84. // Get correct effect
  85. if (defines.isDirty) {
  86. defines.markAsProcessed();
  87. scene.resetCachedMaterial();
  88. // Attributes
  89. var attribs = [VertexBuffer.PositionKind, VertexBuffer.NormalKind];
  90. // Effect
  91. var shaderName = scene.getEngine().getCaps().standardDerivatives ? "grid" : "legacygrid";
  92. // Defines
  93. var join = defines.toString();
  94. subMesh.setEffect(scene.getEngine().createEffect(shaderName,
  95. attribs,
  96. ["worldViewProjection", "mainColor", "lineColor", "gridControl", "vFogInfos", "vFogColor", "world", "view"],
  97. [],
  98. join,
  99. null,
  100. this.onCompiled,
  101. this.onError), defines);
  102. }
  103. if (!subMesh.effect.isReady()) {
  104. return false;
  105. }
  106. this._renderId = scene.getRenderId();
  107. this._wasPreviouslyReady = true;
  108. return true;
  109. }
  110. public bindForSubMesh(world: Matrix, mesh: Mesh, subMesh: SubMesh): void {
  111. var scene = this.getScene();
  112. var defines = <GridMaterialDefines>subMesh._materialDefines;
  113. if (!defines) {
  114. return;
  115. }
  116. var effect = subMesh.effect;
  117. this._activeEffect = effect;
  118. // Matrices
  119. this.bindOnlyWorldMatrix(world);
  120. this._activeEffect.setMatrix("worldViewProjection", world.multiply(scene.getTransformMatrix()));
  121. this._activeEffect.setMatrix("view", scene.getViewMatrix());
  122. // Uniforms
  123. if (this._mustRebind(scene, effect)) {
  124. this._activeEffect.setColor3("mainColor", this.mainColor);
  125. this._activeEffect.setColor3("lineColor", this.lineColor);
  126. this._gridControl.x = this.gridRatio;
  127. this._gridControl.y = Math.round(this.majorUnitFrequency);
  128. this._gridControl.z = this.minorUnitVisibility;
  129. this._gridControl.w = this.opacity;
  130. this._activeEffect.setVector4("gridControl", this._gridControl);
  131. }
  132. // Fog
  133. MaterialHelper.BindFogParameters(scene, mesh, this._activeEffect);
  134. this._afterBind(mesh, this._activeEffect);
  135. }
  136. public dispose(forceDisposeEffect?: boolean): void {
  137. super.dispose(forceDisposeEffect);
  138. }
  139. public clone(name: string): GridMaterial {
  140. return SerializationHelper.Clone(() => new GridMaterial(name, this.getScene()), this);
  141. }
  142. public serialize(): any {
  143. var serializationObject = SerializationHelper.Serialize(this);
  144. serializationObject.customType = "BABYLON.GridMaterial";
  145. return serializationObject;
  146. }
  147. public static Parse(source: any, scene: Scene, rootUrl: string): GridMaterial {
  148. return SerializationHelper.Parse(() => new GridMaterial(source.name, scene), source, scene, rootUrl);
  149. }
  150. }
  151. }