fluentMaterial.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. import { Nullable } from "babylonjs/types";
  2. import { serializeAsColor4, serializeAsVector3, serializeAsTexture, serialize, expandToProperty, serializeAsColor3, SerializationHelper } from "babylonjs/Misc/decorators";
  3. import { Color3, Vector3, Color4, Matrix, TmpVectors } from "babylonjs/Maths/math";
  4. import { BaseTexture } from "babylonjs/Materials/Textures/baseTexture";
  5. import { MaterialDefines } from "babylonjs/Materials/materialDefines";
  6. import { IEffectCreationOptions } from "babylonjs/Materials/effect";
  7. import { MaterialHelper } from "babylonjs/Materials/materialHelper";
  8. import { PushMaterial } from "babylonjs/Materials/pushMaterial";
  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 "./shaders/fluent.vertex";
  16. import "./shaders/fluent.fragment";
  17. /** @hidden */
  18. export class FluentMaterialDefines extends MaterialDefines {
  19. public INNERGLOW = false;
  20. public BORDER = false;
  21. public HOVERLIGHT = false;
  22. public TEXTURE = false;
  23. constructor() {
  24. super();
  25. this.rebuild();
  26. }
  27. }
  28. /**
  29. * Class used to render controls with fluent desgin
  30. */
  31. export class FluentMaterial extends PushMaterial {
  32. /**
  33. * Gets or sets inner glow intensity. A value of 0 means no glow (default is 0.5)
  34. */
  35. @serialize()
  36. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  37. public innerGlowColorIntensity = 0.5;
  38. /**
  39. * Gets or sets the inner glow color (white by default)
  40. */
  41. @serializeAsColor3()
  42. public innerGlowColor = new Color3(1.0, 1.0, 1.0);
  43. /**
  44. * Gets or sets alpha value (default is 1.0)
  45. */
  46. @serialize()
  47. public alpha = 1.0;
  48. /**
  49. * Gets or sets the albedo color (Default is Color3(0.3, 0.35, 0.4))
  50. */
  51. @serializeAsColor3()
  52. public albedoColor = new Color3(0.3, 0.35, 0.4);
  53. /**
  54. * Gets or sets a boolean indicating if borders must be rendered (default is false)
  55. */
  56. @serialize()
  57. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  58. public renderBorders = false;
  59. /**
  60. * Gets or sets border width (default is 0.5)
  61. */
  62. @serialize()
  63. public borderWidth = 0.5;
  64. /**
  65. * Gets or sets a value indicating the smoothing value applied to border edges (0.02 by default)
  66. */
  67. @serialize()
  68. public edgeSmoothingValue = 0.02;
  69. /**
  70. * Gets or sets the minimum value that can be applied to border width (default is 0.1)
  71. */
  72. @serialize()
  73. public borderMinValue = 0.1;
  74. /**
  75. * Gets or sets a boolean indicating if hover light must be rendered (default is false)
  76. */
  77. @serialize()
  78. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  79. public renderHoverLight = false;
  80. /**
  81. * Gets or sets the radius used to render the hover light (default is 1.0)
  82. */
  83. @serialize()
  84. public hoverRadius = 1.0;
  85. /**
  86. * Gets or sets the color used to render the hover light (default is Color4(0.3, 0.3, 0.3, 1.0))
  87. */
  88. @serializeAsColor4()
  89. public hoverColor = new Color4(0.3, 0.3, 0.3, 1.0);
  90. /**
  91. * Gets or sets the hover light position in world space (default is Vector3.Zero())
  92. */
  93. @serializeAsVector3()
  94. public hoverPosition = Vector3.Zero();
  95. @serializeAsTexture("albedoTexture")
  96. private _albedoTexture: Nullable<BaseTexture>;
  97. /** Gets or sets the texture to use for albedo color */
  98. @expandToProperty("_markAllSubMeshesAsTexturesAndMiscDirty")
  99. public albedoTexture: Nullable<BaseTexture>;
  100. /**
  101. * Creates a new Fluent material
  102. * @param name defines the name of the material
  103. * @param scene defines the hosting scene
  104. */
  105. constructor(name: string, scene: Scene) {
  106. super(name, scene);
  107. }
  108. public needAlphaBlending(): boolean {
  109. return this.alpha !== 1.0;
  110. }
  111. public needAlphaTesting(): boolean {
  112. return false;
  113. }
  114. public getAlphaTestTexture(): Nullable<BaseTexture> {
  115. return null;
  116. }
  117. public isReadyForSubMesh(mesh: AbstractMesh, subMesh: SubMesh, useInstances?: boolean): boolean {
  118. if (this.isFrozen) {
  119. if (subMesh.effect && subMesh.effect._wasPreviouslyReady) {
  120. return true;
  121. }
  122. }
  123. if (!subMesh._materialDefines) {
  124. subMesh._materialDefines = new FluentMaterialDefines();
  125. }
  126. var scene = this.getScene();
  127. var defines = <FluentMaterialDefines>subMesh._materialDefines;
  128. if (!this.checkReadyOnEveryCall && subMesh.effect) {
  129. if (defines._renderId === scene.getRenderId()) {
  130. return true;
  131. }
  132. }
  133. if (defines._areTexturesDirty) {
  134. defines.INNERGLOW = this.innerGlowColorIntensity > 0;
  135. defines.BORDER = this.renderBorders;
  136. defines.HOVERLIGHT = this.renderHoverLight;
  137. if (this._albedoTexture) {
  138. if (!this._albedoTexture.isReadyOrNotBlocking()) {
  139. return false;
  140. } else {
  141. defines.TEXTURE = true;
  142. }
  143. } else {
  144. defines.TEXTURE = false;
  145. }
  146. }
  147. var engine = scene.getEngine();
  148. // Get correct effect
  149. if (defines.isDirty) {
  150. defines.markAsProcessed();
  151. scene.resetCachedMaterial();
  152. //Attributes
  153. var attribs = [VertexBuffer.PositionKind];
  154. attribs.push(VertexBuffer.NormalKind);
  155. attribs.push(VertexBuffer.UVKind);
  156. var shaderName = "fluent";
  157. var uniforms = ["world", "viewProjection", "innerGlowColor", "albedoColor", "borderWidth", "edgeSmoothingValue", "scaleFactor", "borderMinValue",
  158. "hoverColor", "hoverPosition", "hoverRadius"
  159. ];
  160. var samplers = ["albedoSampler"];
  161. var uniformBuffers = new Array<string>();
  162. MaterialHelper.PrepareUniformsAndSamplersList(<IEffectCreationOptions>{
  163. uniformsNames: uniforms,
  164. uniformBuffersNames: uniformBuffers,
  165. samplers: samplers,
  166. defines: defines,
  167. maxSimultaneousLights: 4
  168. });
  169. var join = defines.toString();
  170. subMesh.setEffect(scene.getEngine().createEffect(shaderName,
  171. <IEffectCreationOptions>{
  172. attributes: attribs,
  173. uniformsNames: uniforms,
  174. uniformBuffersNames: uniformBuffers,
  175. samplers: samplers,
  176. defines: join,
  177. fallbacks: null,
  178. onCompiled: this.onCompiled,
  179. onError: this.onError,
  180. indexParameters: { maxSimultaneousLights: 4 }
  181. }, engine));
  182. }
  183. if (!subMesh.effect || !subMesh.effect.isReady()) {
  184. return false;
  185. }
  186. defines._renderId = scene.getRenderId();
  187. subMesh.effect._wasPreviouslyReady = true;
  188. return true;
  189. }
  190. public bindForSubMesh(world: Matrix, mesh: Mesh, subMesh: SubMesh): void {
  191. var scene = this.getScene();
  192. var defines = <FluentMaterialDefines>subMesh._materialDefines;
  193. if (!defines) {
  194. return;
  195. }
  196. var effect = subMesh.effect;
  197. if (!effect) {
  198. return;
  199. }
  200. this._activeEffect = effect;
  201. // Matrices
  202. this.bindOnlyWorldMatrix(world);
  203. this._activeEffect.setMatrix("viewProjection", scene.getTransformMatrix());
  204. if (this._mustRebind(scene, effect)) {
  205. this._activeEffect.setColor4("albedoColor", this.albedoColor, this.alpha);
  206. if (defines.INNERGLOW) {
  207. this._activeEffect.setColor4("innerGlowColor", this.innerGlowColor, this.innerGlowColorIntensity);
  208. }
  209. if (defines.BORDER) {
  210. this._activeEffect.setFloat("borderWidth", this.borderWidth);
  211. this._activeEffect.setFloat("edgeSmoothingValue", this.edgeSmoothingValue);
  212. this._activeEffect.setFloat("borderMinValue", this.borderMinValue);
  213. mesh.getBoundingInfo().boundingBox.extendSize.multiplyToRef(mesh.scaling, TmpVectors.Vector3[0]);
  214. this._activeEffect.setVector3("scaleFactor", TmpVectors.Vector3[0]);
  215. }
  216. if (defines.HOVERLIGHT) {
  217. this._activeEffect.setDirectColor4("hoverColor", this.hoverColor);
  218. this._activeEffect.setFloat("hoverRadius", this.hoverRadius);
  219. this._activeEffect.setVector3("hoverPosition", this.hoverPosition);
  220. }
  221. if (defines.TEXTURE) {
  222. this._activeEffect.setTexture("albedoSampler", this._albedoTexture);
  223. }
  224. }
  225. this._afterBind(mesh, this._activeEffect);
  226. }
  227. public getActiveTextures(): BaseTexture[] {
  228. var activeTextures = super.getActiveTextures();
  229. return activeTextures;
  230. }
  231. public hasTexture(texture: BaseTexture): boolean {
  232. if (super.hasTexture(texture)) {
  233. return true;
  234. }
  235. return false;
  236. }
  237. public dispose(forceDisposeEffect?: boolean): void {
  238. super.dispose(forceDisposeEffect);
  239. }
  240. public clone(name: string): FluentMaterial {
  241. return SerializationHelper.Clone(() => new FluentMaterial(name, this.getScene()), this);
  242. }
  243. public serialize(): any {
  244. var serializationObject = SerializationHelper.Serialize(this);
  245. serializationObject.customType = "BABYLON.GUI.FluentMaterial";
  246. return serializationObject;
  247. }
  248. public getClassName(): string {
  249. return "FluentMaterial";
  250. }
  251. // Statics
  252. public static Parse(source: any, scene: Scene, rootUrl: string): FluentMaterial {
  253. return SerializationHelper.Parse(() => new FluentMaterial(source.name, scene), source, scene, rootUrl);
  254. }
  255. }
  256. _TypeStore.RegisteredTypes["BABYLON.GUI.FluentMaterial"] = FluentMaterial;