gridMaterial.ts 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. import { serializeAsTexture, serialize, expandToProperty, serializeAsColor3, SerializationHelper } from "babylonjs/Misc/decorators";
  2. import { Matrix, Vector4, Vector3 } from "babylonjs/Maths/math.vector";
  3. import { Color3 } from "babylonjs/Maths/math.color";
  4. import { BaseTexture } from "babylonjs/Materials/Textures/baseTexture";
  5. import { MaterialDefines } from "babylonjs/Materials/materialDefines";
  6. import { MaterialHelper } from "babylonjs/Materials/materialHelper";
  7. import { PushMaterial } from "babylonjs/Materials/pushMaterial";
  8. import { MaterialFlags } from "babylonjs/Materials/materialFlags";
  9. import { VertexBuffer } from "babylonjs/Meshes/buffer";
  10. import { AbstractMesh } from "babylonjs/Meshes/abstractMesh";
  11. import { SubMesh } from "babylonjs/Meshes/subMesh";
  12. import { Mesh } from "babylonjs/Meshes/mesh";
  13. import { Scene } from "babylonjs/scene";
  14. import { _TypeStore } from 'babylonjs/Misc/typeStore';
  15. import "./grid.fragment";
  16. import "./grid.vertex";
  17. class GridMaterialDefines extends MaterialDefines {
  18. public OPACITY = false;
  19. public TRANSPARENT = false;
  20. public FOG = false;
  21. public HIGH_DEFINITION_PIPELINE = false;
  22. public SCENE_MRT_COUNT = 0;
  23. public PREMULTIPLYALPHA = false;
  24. public UV1 = false;
  25. public UV2 = false;
  26. public INSTANCES = false;
  27. constructor() {
  28. super();
  29. this.rebuild();
  30. }
  31. }
  32. /**
  33. * The grid materials allows you to wrap any shape with a grid.
  34. * Colors are customizable.
  35. */
  36. export class GridMaterial extends PushMaterial {
  37. /**
  38. * Main color of the grid (e.g. between lines)
  39. */
  40. @serializeAsColor3()
  41. public mainColor = Color3.Black();
  42. /**
  43. * Color of the grid lines.
  44. */
  45. @serializeAsColor3()
  46. public lineColor = Color3.Teal();
  47. /**
  48. * The scale of the grid compared to unit.
  49. */
  50. @serialize()
  51. public gridRatio = 1.0;
  52. /**
  53. * Allows setting an offset for the grid lines.
  54. */
  55. @serializeAsColor3()
  56. public gridOffset = Vector3.Zero();
  57. /**
  58. * The frequency of thicker lines.
  59. */
  60. @serialize()
  61. public majorUnitFrequency = 10;
  62. /**
  63. * The visibility of minor units in the grid.
  64. */
  65. @serialize()
  66. public minorUnitVisibility = 0.33;
  67. /**
  68. * The grid opacity outside of the lines.
  69. */
  70. @serialize()
  71. public opacity = 1.0;
  72. /**
  73. * Determine RBG output is premultiplied by alpha value.
  74. */
  75. @serialize()
  76. public preMultiplyAlpha = false;
  77. @serializeAsTexture("opacityTexture")
  78. private _opacityTexture: BaseTexture;
  79. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  80. public opacityTexture: BaseTexture;
  81. private _gridControl: Vector4 = new Vector4(this.gridRatio, this.majorUnitFrequency, this.minorUnitVisibility, this.opacity);
  82. /**
  83. * constructor
  84. * @param name The name given to the material in order to identify it afterwards.
  85. * @param scene The scene the material is used in.
  86. */
  87. constructor(name: string, scene: Scene) {
  88. super(name, scene);
  89. }
  90. /**
  91. * Returns wehter or not the grid requires alpha blending.
  92. */
  93. public needAlphaBlending(): boolean {
  94. return this.opacity < 1.0 || this._opacityTexture && this._opacityTexture.isReady();
  95. }
  96. public needAlphaBlendingForMesh(mesh: AbstractMesh): boolean {
  97. return this.needAlphaBlending();
  98. }
  99. public isReadyForSubMesh(mesh: AbstractMesh, subMesh: SubMesh, useInstances?: boolean): boolean {
  100. if (this.isFrozen) {
  101. if (subMesh.effect && subMesh.effect._wasPreviouslyReady) {
  102. return true;
  103. }
  104. }
  105. if (!subMesh._materialDefines) {
  106. subMesh._materialDefines = new GridMaterialDefines();
  107. }
  108. var defines = <GridMaterialDefines>subMesh._materialDefines;
  109. var scene = this.getScene();
  110. if (this._isReadyForSubMesh(subMesh)) {
  111. return true;
  112. }
  113. if (defines.TRANSPARENT !== (this.opacity < 1.0)) {
  114. defines.TRANSPARENT = !defines.TRANSPARENT;
  115. defines.markAsUnprocessed();
  116. }
  117. if (defines.PREMULTIPLYALPHA != this.preMultiplyAlpha) {
  118. defines.PREMULTIPLYALPHA = !defines.PREMULTIPLYALPHA;
  119. defines.markAsUnprocessed();
  120. }
  121. // Textures
  122. if (defines._areTexturesDirty) {
  123. defines._needUVs = false;
  124. if (scene.texturesEnabled) {
  125. if (this._opacityTexture && MaterialFlags.OpacityTextureEnabled) {
  126. if (!this._opacityTexture.isReady()) {
  127. return false;
  128. } else {
  129. defines._needUVs = true;
  130. defines.OPACITY = true;
  131. }
  132. }
  133. }
  134. }
  135. MaterialHelper.PrepareDefinesForMisc(mesh, scene, false, false, this.fogEnabled, false, defines);
  136. // Deferred
  137. MaterialHelper.PrepareDefinesForDeferred(scene, defines);
  138. // Values that need to be evaluated on every frame
  139. MaterialHelper.PrepareDefinesForFrameBoundValues(scene, scene.getEngine(), defines, !!useInstances);
  140. // Get correct effect
  141. if (defines.isDirty) {
  142. defines.markAsProcessed();
  143. scene.resetCachedMaterial();
  144. // Attributes
  145. MaterialHelper.PrepareDefinesForAttributes(mesh, defines, false, false);
  146. var attribs = [VertexBuffer.PositionKind, VertexBuffer.NormalKind];
  147. if (defines.UV1) {
  148. attribs.push(VertexBuffer.UVKind);
  149. }
  150. if (defines.UV2) {
  151. attribs.push(VertexBuffer.UV2Kind);
  152. }
  153. MaterialHelper.PrepareAttributesForInstances(attribs, defines);
  154. // Defines
  155. var join = defines.toString();
  156. subMesh.setEffect(scene.getEngine().createEffect("grid",
  157. attribs,
  158. ["projection", "mainColor", "lineColor", "gridControl", "gridOffset", "vFogInfos", "vFogColor", "world", "view",
  159. "opacityMatrix", "vOpacityInfos"],
  160. ["opacitySampler"],
  161. join,
  162. undefined,
  163. this.onCompiled,
  164. this.onError), defines);
  165. }
  166. if (!subMesh.effect || !subMesh.effect.isReady()) {
  167. return false;
  168. }
  169. defines._renderId = scene.getRenderId();
  170. subMesh.effect._wasPreviouslyReady = true;
  171. return true;
  172. }
  173. public bindForSubMesh(world: Matrix, mesh: Mesh, subMesh: SubMesh): void {
  174. var scene = this.getScene();
  175. var defines = <GridMaterialDefines>subMesh._materialDefines;
  176. if (!defines) {
  177. return;
  178. }
  179. var effect = subMesh.effect;
  180. if (!effect) {
  181. return;
  182. }
  183. this._activeEffect = effect;
  184. // Matrices
  185. if (!defines.INSTANCES) {
  186. this.bindOnlyWorldMatrix(world);
  187. }
  188. this._activeEffect.setMatrix("view", scene.getViewMatrix());
  189. this._activeEffect.setMatrix("projection", scene.getProjectionMatrix());
  190. // Uniforms
  191. if (this._mustRebind(scene, effect)) {
  192. this._activeEffect.setColor3("mainColor", this.mainColor);
  193. this._activeEffect.setColor3("lineColor", this.lineColor);
  194. this._activeEffect.setVector3("gridOffset", this.gridOffset);
  195. this._gridControl.x = this.gridRatio;
  196. this._gridControl.y = Math.round(this.majorUnitFrequency);
  197. this._gridControl.z = this.minorUnitVisibility;
  198. this._gridControl.w = this.opacity;
  199. this._activeEffect.setVector4("gridControl", this._gridControl);
  200. if (this._opacityTexture && MaterialFlags.OpacityTextureEnabled) {
  201. this._activeEffect.setTexture("opacitySampler", this._opacityTexture);
  202. this._activeEffect.setFloat2("vOpacityInfos", this._opacityTexture.coordinatesIndex, this._opacityTexture.level);
  203. this._activeEffect.setMatrix("opacityMatrix", this._opacityTexture.getTextureMatrix());
  204. }
  205. }
  206. // Fog
  207. MaterialHelper.BindFogParameters(scene, mesh, this._activeEffect);
  208. this._afterBind(mesh, this._activeEffect);
  209. }
  210. /**
  211. * Dispose the material and its associated resources.
  212. * @param forceDisposeEffect will also dispose the used effect when true
  213. */
  214. public dispose(forceDisposeEffect?: boolean): void {
  215. super.dispose(forceDisposeEffect);
  216. }
  217. public clone(name: string): GridMaterial {
  218. return SerializationHelper.Clone(() => new GridMaterial(name, this.getScene()), this);
  219. }
  220. public serialize(): any {
  221. var serializationObject = SerializationHelper.Serialize(this);
  222. serializationObject.customType = "BABYLON.GridMaterial";
  223. return serializationObject;
  224. }
  225. public getClassName(): string {
  226. return "GridMaterial";
  227. }
  228. public static Parse(source: any, scene: Scene, rootUrl: string): GridMaterial {
  229. return SerializationHelper.Parse(() => new GridMaterial(source.name, scene), source, scene, rootUrl);
  230. }
  231. }
  232. _TypeStore.RegisteredTypes["BABYLON.GridMaterial"] = GridMaterial;