babylon.gridmaterial.ts 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. * Allows setting an offset for the grid lines.
  34. */
  35. @serializeAsColor3()
  36. public gridOffset = Vector3.Zero();
  37. /**
  38. * The frequency of thicker lines.
  39. */
  40. @serialize()
  41. public majorUnitFrequency = 10;
  42. /**
  43. * The visibility of minor units in the grid.
  44. */
  45. @serialize()
  46. public minorUnitVisibility = 0.33;
  47. /**
  48. * The grid opacity outside of the lines.
  49. */
  50. @serialize()
  51. public opacity = 1.0;
  52. /**
  53. * Determine RBG output is premultiplied by alpha value.
  54. */
  55. @serialize()
  56. public preMultiplyAlpha = false;
  57. private _gridControl: Vector4 = new Vector4(this.gridRatio, this.majorUnitFrequency, this.minorUnitVisibility, this.opacity);
  58. private _renderId: number;
  59. /**
  60. * constructor
  61. * @param name The name given to the material in order to identify it afterwards.
  62. * @param scene The scene the material is used in.
  63. */
  64. constructor(name: string, scene: Scene) {
  65. super(name, scene);
  66. }
  67. /**
  68. * Returns wehter or not the grid requires alpha blending.
  69. */
  70. public needAlphaBlending(): boolean {
  71. return this.opacity < 1.0;
  72. }
  73. public needAlphaBlendingForMesh(mesh: AbstractMesh): boolean {
  74. return this.needAlphaBlending();
  75. }
  76. public isReadyForSubMesh(mesh: AbstractMesh, subMesh: SubMesh, useInstances?: boolean): boolean {
  77. if (this.isFrozen) {
  78. if (this._wasPreviouslyReady && subMesh.effect) {
  79. return true;
  80. }
  81. }
  82. if (!subMesh._materialDefines) {
  83. subMesh._materialDefines = new GridMaterialDefines();
  84. }
  85. var defines = <GridMaterialDefines>subMesh._materialDefines;
  86. var scene = this.getScene();
  87. if (!this.checkReadyOnEveryCall && subMesh.effect) {
  88. if (this._renderId === scene.getRenderId()) {
  89. return true;
  90. }
  91. }
  92. if (defines.TRANSPARENT !== (this.opacity < 1.0)) {
  93. defines.TRANSPARENT = !defines.TRANSPARENT;
  94. defines.markAsUnprocessed();
  95. }
  96. if (defines.PREMULTIPLYALPHA != this.preMultiplyAlpha) {
  97. defines.PREMULTIPLYALPHA = !defines.PREMULTIPLYALPHA;
  98. defines.markAsUnprocessed();
  99. }
  100. MaterialHelper.PrepareDefinesForMisc(mesh, scene, false, false, this.fogEnabled, defines);
  101. // Get correct effect
  102. if (defines.isDirty) {
  103. defines.markAsProcessed();
  104. scene.resetCachedMaterial();
  105. // Attributes
  106. var attribs = [VertexBuffer.PositionKind, VertexBuffer.NormalKind];
  107. // Defines
  108. var join = defines.toString();
  109. subMesh.setEffect(scene.getEngine().createEffect("grid",
  110. attribs,
  111. ["projection", "worldView", "mainColor", "lineColor", "gridControl", "gridOffset", "vFogInfos", "vFogColor", "world", "view"],
  112. [],
  113. join,
  114. undefined,
  115. this.onCompiled,
  116. this.onError), defines);
  117. }
  118. if (!subMesh.effect || !subMesh.effect.isReady()) {
  119. return false;
  120. }
  121. this._renderId = scene.getRenderId();
  122. this._wasPreviouslyReady = true;
  123. return true;
  124. }
  125. public bindForSubMesh(world: Matrix, mesh: Mesh, subMesh: SubMesh): void {
  126. var scene = this.getScene();
  127. var defines = <GridMaterialDefines>subMesh._materialDefines;
  128. if (!defines) {
  129. return;
  130. }
  131. var effect = subMesh.effect;
  132. if (!effect) {
  133. return;
  134. }
  135. this._activeEffect = effect;
  136. // Matrices
  137. this.bindOnlyWorldMatrix(world);
  138. this._activeEffect.setMatrix("worldView", world.multiply(scene.getViewMatrix()));
  139. this._activeEffect.setMatrix("view", scene.getViewMatrix());
  140. this._activeEffect.setMatrix("projection", scene.getProjectionMatrix());
  141. // Uniforms
  142. if (this._mustRebind(scene, effect)) {
  143. this._activeEffect.setColor3("mainColor", this.mainColor);
  144. this._activeEffect.setColor3("lineColor", this.lineColor);
  145. this._activeEffect.setVector3("gridOffset", this.gridOffset);
  146. this._gridControl.x = this.gridRatio;
  147. this._gridControl.y = Math.round(this.majorUnitFrequency);
  148. this._gridControl.z = this.minorUnitVisibility;
  149. this._gridControl.w = this.opacity;
  150. this._activeEffect.setVector4("gridControl", this._gridControl);
  151. }
  152. // Fog
  153. MaterialHelper.BindFogParameters(scene, mesh, this._activeEffect);
  154. this._afterBind(mesh, this._activeEffect);
  155. }
  156. public dispose(forceDisposeEffect?: boolean): void {
  157. super.dispose(forceDisposeEffect);
  158. }
  159. public clone(name: string): GridMaterial {
  160. return SerializationHelper.Clone(() => new GridMaterial(name, this.getScene()), this);
  161. }
  162. public serialize(): any {
  163. var serializationObject = SerializationHelper.Serialize(this);
  164. serializationObject.customType = "BABYLON.GridMaterial";
  165. return serializationObject;
  166. }
  167. public getClassName(): string {
  168. return "GridMaterial";
  169. }
  170. public static Parse(source: any, scene: Scene, rootUrl: string): GridMaterial {
  171. return SerializationHelper.Parse(() => new GridMaterial(source.name, scene), source, scene, rootUrl);
  172. }
  173. }
  174. }