babylon.gridmaterial.ts 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  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._keys = Object.keys(this);
  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.Material {
  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. private _defines = new GRIDMaterialDefines();
  49. private _cachedDefines = new GRIDMaterialDefines();
  50. /**
  51. * constructor
  52. * @param name The name given to the material in order to identify it afterwards.
  53. * @param scene The scene the material is used in.
  54. */
  55. constructor(name: string, scene: Scene) {
  56. super(name, scene);
  57. }
  58. /**
  59. * Returns wehter or not the grid requires alpha blending.
  60. */
  61. public needAlphaBlending(): boolean {
  62. return this.opacity < 1.0;
  63. }
  64. private _checkCache(scene: Scene, mesh?: AbstractMesh, useInstances?: boolean): boolean {
  65. if (!mesh) {
  66. return true;
  67. }
  68. if (mesh._materialDefines && mesh._materialDefines.isEqual(this._defines)) {
  69. return true;
  70. }
  71. return false;
  72. }
  73. public isReady(mesh?: AbstractMesh, useInstances?: boolean): boolean {
  74. if (this.checkReadyOnlyOnce) {
  75. if (this._wasPreviouslyReady) {
  76. return true;
  77. }
  78. }
  79. var scene = this.getScene();
  80. if (!this.checkReadyOnEveryCall) {
  81. if (this._renderId === scene.getRenderId()) {
  82. if (this._checkCache(scene, mesh, useInstances)) {
  83. return true;
  84. }
  85. }
  86. }
  87. var engine = scene.getEngine();
  88. var needNormals = true;
  89. this._defines.reset();
  90. if (this.opacity < 1.0) {
  91. this._defines.TRANSPARENT = true;
  92. }
  93. // Fog
  94. if (scene.fogEnabled && mesh && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE && this.fogEnabled) {
  95. this._defines.FOG = true;
  96. }
  97. // Get correct effect
  98. if (!this._effect || !this._defines.isEqual(this._cachedDefines)) {
  99. this._defines.cloneTo(this._cachedDefines);
  100. scene.resetCachedMaterial();
  101. // Attributes
  102. var attribs = [VertexBuffer.PositionKind, VertexBuffer.NormalKind];
  103. // Effect
  104. var shaderName = scene.getEngine().getCaps().standardDerivatives ? "grid" : "legacygrid";
  105. // Defines
  106. var join = this._defines.toString();
  107. this._effect = scene.getEngine().createEffect(shaderName,
  108. attribs,
  109. ["worldViewProjection", "mainColor", "lineColor", "gridControl", "vFogInfos", "vFogColor", "world", "view"],
  110. [],
  111. join,
  112. null,
  113. this.onCompiled,
  114. this.onError);
  115. }
  116. if (!this._effect.isReady()) {
  117. return false;
  118. }
  119. this._renderId = scene.getRenderId();
  120. this._wasPreviouslyReady = true;
  121. return true;
  122. }
  123. public bindOnlyWorldMatrix(world: Matrix): void {
  124. var scene = this.getScene();
  125. this._effect.setMatrix("worldViewProjection", world.multiply(scene.getTransformMatrix()));
  126. this._effect.setMatrix("world", world);
  127. this._effect.setMatrix("view", scene.getViewMatrix());
  128. }
  129. public bind(world: Matrix, mesh?: Mesh): void {
  130. var scene = this.getScene();
  131. // Matrices
  132. this.bindOnlyWorldMatrix(world);
  133. // Uniforms
  134. if (scene.getCachedMaterial() !== (<BABYLON.Material>this)) {
  135. this._effect.setColor3("mainColor", this.mainColor);
  136. this._effect.setColor3("lineColor", this.lineColor);
  137. this._gridControl.x = this.gridRatio;
  138. this._gridControl.y = Math.round(this.majorUnitFrequency);
  139. this._gridControl.z = this.minorUnitVisibility;
  140. this._gridControl.w = this.opacity;
  141. this._effect.setVector4("gridControl", this._gridControl);
  142. }
  143. // View
  144. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE) {
  145. this._effect.setMatrix("view", scene.getViewMatrix());
  146. }
  147. // Fog
  148. MaterialHelper.BindFogParameters(scene, mesh, this._effect);
  149. super.bind(world, mesh);
  150. }
  151. public dispose(forceDisposeEffect?: boolean): void {
  152. super.dispose(forceDisposeEffect);
  153. }
  154. public clone(name: string): GridMaterial {
  155. return SerializationHelper.Clone(() => new GridMaterial(name, this.getScene()), this);
  156. }
  157. public serialize(): any {
  158. var serializationObject = SerializationHelper.Serialize(this);
  159. serializationObject.customType = "BABYLON.GridMaterial";
  160. return serializationObject;
  161. }
  162. public static Parse(source: any, scene: Scene, rootUrl: string): GridMaterial {
  163. return SerializationHelper.Parse(() => new GridMaterial(source.name, scene), source, scene, rootUrl);
  164. }
  165. }
  166. }