babylon.gridmaterial.ts 6.6 KB

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