gridmaterial.ts 6.6 KB

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