gradientMaterial.ts 10.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. import { Nullable } from "babylonjs/types";
  2. import { serialize, expandToProperty, serializeAsColor3, SerializationHelper } from "babylonjs/Misc/decorators";
  3. import { Matrix } from "babylonjs/Maths/math.vector";
  4. import { Color3 } from "babylonjs/Maths/math.color";
  5. import { IAnimatable } from 'babylonjs/Animations/animatable.interface';
  6. import { BaseTexture } from "babylonjs/Materials/Textures/baseTexture";
  7. import { MaterialDefines } from "babylonjs/Materials/materialDefines";
  8. import { MaterialHelper } from "babylonjs/Materials/materialHelper";
  9. import { IEffectCreationOptions } from "babylonjs/Materials/effect";
  10. import { PushMaterial } from "babylonjs/Materials/pushMaterial";
  11. import { VertexBuffer } from "babylonjs/Meshes/buffer";
  12. import { AbstractMesh } from "babylonjs/Meshes/abstractMesh";
  13. import { SubMesh } from "babylonjs/Meshes/subMesh";
  14. import { Mesh } from "babylonjs/Meshes/mesh";
  15. import { Scene } from "babylonjs/scene";
  16. import { _TypeStore } from 'babylonjs/Misc/typeStore';
  17. import "./gradient.fragment";
  18. import "./gradient.vertex";
  19. import { EffectFallbacks } from 'babylonjs/Materials/effectFallbacks';
  20. class GradientMaterialDefines extends MaterialDefines {
  21. public EMISSIVE = false;
  22. public CLIPPLANE = false;
  23. public CLIPPLANE2 = false;
  24. public CLIPPLANE3 = false;
  25. public CLIPPLANE4 = false;
  26. public CLIPPLANE5 = false;
  27. public CLIPPLANE6 = false;
  28. public ALPHATEST = false;
  29. public DEPTHPREPASS = false;
  30. public POINTSIZE = false;
  31. public FOG = false;
  32. public NORMAL = false;
  33. public UV1 = false;
  34. public UV2 = false;
  35. public VERTEXCOLOR = false;
  36. public VERTEXALPHA = false;
  37. public NUM_BONE_INFLUENCERS = 0;
  38. public BonesPerMesh = 0;
  39. public INSTANCES = false;
  40. constructor() {
  41. super();
  42. this.rebuild();
  43. }
  44. }
  45. export class GradientMaterial extends PushMaterial {
  46. @serialize("maxSimultaneousLights")
  47. private _maxSimultaneousLights = 4;
  48. @expandToProperty("_markAllSubMeshesAsLightsDirty")
  49. public maxSimultaneousLights: number;
  50. // The gradient top color, red by default
  51. @serializeAsColor3()
  52. public topColor = new Color3(1, 0, 0);
  53. @serialize()
  54. public topColorAlpha = 1.0;
  55. // The gradient top color, blue by default
  56. @serializeAsColor3()
  57. public bottomColor = new Color3(0, 0, 1);
  58. @serialize()
  59. public bottomColorAlpha = 1.0;
  60. // Gradient offset
  61. @serialize()
  62. public offset = 0;
  63. @serialize()
  64. public scale = 1.0;
  65. @serialize()
  66. public smoothness = 1.0;
  67. @serialize("disableLighting")
  68. private _disableLighting = false;
  69. @expandToProperty("_markAllSubMeshesAsLightsDirty")
  70. public disableLighting: boolean;
  71. constructor(name: string, scene: Scene) {
  72. super(name, scene);
  73. }
  74. public needAlphaBlending(): boolean {
  75. return (this.alpha < 1.0 || this.topColorAlpha < 1.0 || this.bottomColorAlpha < 1.0);
  76. }
  77. public needAlphaTesting(): boolean {
  78. return true;
  79. }
  80. public getAlphaTestTexture(): Nullable<BaseTexture> {
  81. return null;
  82. }
  83. // Methods
  84. public isReadyForSubMesh(mesh: AbstractMesh, subMesh: SubMesh, useInstances?: boolean): boolean {
  85. if (this.isFrozen) {
  86. if (subMesh.effect && subMesh.effect._wasPreviouslyReady) {
  87. return true;
  88. }
  89. }
  90. if (!subMesh._materialDefines) {
  91. subMesh._materialDefines = new GradientMaterialDefines();
  92. }
  93. var defines = <GradientMaterialDefines>subMesh._materialDefines;
  94. var scene = this.getScene();
  95. if (this._isReadyForSubMesh(subMesh)) {
  96. return true;
  97. }
  98. var engine = scene.getEngine();
  99. MaterialHelper.PrepareDefinesForFrameBoundValues(scene, engine, defines, useInstances ? true : false);
  100. MaterialHelper.PrepareDefinesForMisc(mesh, scene, false, this.pointsCloud, this.fogEnabled, this._shouldTurnAlphaTestOn(mesh), defines);
  101. defines._needNormals = MaterialHelper.PrepareDefinesForLights(scene, mesh, defines, false, this._maxSimultaneousLights, this._disableLighting);
  102. defines.EMISSIVE = this._disableLighting;
  103. // Attribs
  104. MaterialHelper.PrepareDefinesForAttributes(mesh, defines, false, true);
  105. // Get correct effect
  106. if (defines.isDirty) {
  107. defines.markAsProcessed();
  108. scene.resetCachedMaterial();
  109. // Fallbacks
  110. var fallbacks = new EffectFallbacks();
  111. if (defines.FOG) {
  112. fallbacks.addFallback(1, "FOG");
  113. }
  114. MaterialHelper.HandleFallbacksForShadows(defines, fallbacks);
  115. if (defines.NUM_BONE_INFLUENCERS > 0) {
  116. fallbacks.addCPUSkinningFallback(0, mesh);
  117. }
  118. //Attributes
  119. var attribs = [VertexBuffer.PositionKind];
  120. if (defines.NORMAL) {
  121. attribs.push(VertexBuffer.NormalKind);
  122. }
  123. if (defines.UV1) {
  124. attribs.push(VertexBuffer.UVKind);
  125. }
  126. if (defines.UV2) {
  127. attribs.push(VertexBuffer.UV2Kind);
  128. }
  129. if (defines.VERTEXCOLOR) {
  130. attribs.push(VertexBuffer.ColorKind);
  131. }
  132. MaterialHelper.PrepareAttributesForBones(attribs, mesh, defines, fallbacks);
  133. MaterialHelper.PrepareAttributesForInstances(attribs, defines);
  134. // Legacy browser patch
  135. var shaderName = "gradient";
  136. var join = defines.toString();
  137. var uniforms = ["world", "view", "viewProjection", "vEyePosition", "vLightsType",
  138. "vFogInfos", "vFogColor", "pointSize",
  139. "mBones",
  140. "vClipPlane", "vClipPlane2", "vClipPlane3", "vClipPlane4", "vClipPlane5", "vClipPlane6",
  141. "topColor", "bottomColor", "offset", "smoothness", "scale"
  142. ];
  143. var samplers: string[] = [];
  144. var uniformBuffers = new Array<string>();
  145. MaterialHelper.PrepareUniformsAndSamplersList(<IEffectCreationOptions>{
  146. uniformsNames: uniforms,
  147. uniformBuffersNames: uniformBuffers,
  148. samplers: samplers,
  149. defines: defines,
  150. maxSimultaneousLights: 4
  151. });
  152. subMesh.setEffect(scene.getEngine().createEffect(shaderName,
  153. <IEffectCreationOptions>{
  154. attributes: attribs,
  155. uniformsNames: uniforms,
  156. uniformBuffersNames: uniformBuffers,
  157. samplers: samplers,
  158. defines: join,
  159. fallbacks: fallbacks,
  160. onCompiled: this.onCompiled,
  161. onError: this.onError,
  162. indexParameters: { maxSimultaneousLights: 4 }
  163. }, engine), defines);
  164. }
  165. if (!subMesh.effect || !subMesh.effect.isReady()) {
  166. return false;
  167. }
  168. defines._renderId = scene.getRenderId();
  169. subMesh.effect._wasPreviouslyReady = true;
  170. return true;
  171. }
  172. public bindForSubMesh(world: Matrix, mesh: Mesh, subMesh: SubMesh): void {
  173. var scene = this.getScene();
  174. var defines = <GradientMaterialDefines>subMesh._materialDefines;
  175. if (!defines) {
  176. return;
  177. }
  178. var effect = subMesh.effect;
  179. if (!effect) {
  180. return;
  181. }
  182. this._activeEffect = effect;
  183. // Matrices
  184. this.bindOnlyWorldMatrix(world);
  185. this._activeEffect.setMatrix("viewProjection", scene.getTransformMatrix());
  186. // Bones
  187. MaterialHelper.BindBonesParameters(mesh, effect);
  188. if (this._mustRebind(scene, effect)) {
  189. // Clip plane
  190. MaterialHelper.BindClipPlane(effect, scene);
  191. // Point size
  192. if (this.pointsCloud) {
  193. this._activeEffect.setFloat("pointSize", this.pointSize);
  194. }
  195. MaterialHelper.BindEyePosition(effect, scene);
  196. }
  197. if (scene.lightsEnabled && !this.disableLighting) {
  198. MaterialHelper.BindLights(scene, mesh, this._activeEffect, defines, this.maxSimultaneousLights);
  199. }
  200. // View
  201. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE) {
  202. this._activeEffect.setMatrix("view", scene.getViewMatrix());
  203. }
  204. // Fog
  205. MaterialHelper.BindFogParameters(scene, mesh, this._activeEffect);
  206. this._activeEffect.setColor4("topColor", this.topColor, this.topColorAlpha);
  207. this._activeEffect.setColor4("bottomColor", this.bottomColor, this.bottomColorAlpha);
  208. this._activeEffect.setFloat("offset", this.offset);
  209. this._activeEffect.setFloat("scale", this.scale);
  210. this._activeEffect.setFloat("smoothness", this.smoothness);
  211. this._afterBind(mesh, this._activeEffect);
  212. }
  213. public getAnimatables(): IAnimatable[] {
  214. return [];
  215. }
  216. public dispose(forceDisposeEffect?: boolean): void {
  217. super.dispose(forceDisposeEffect);
  218. }
  219. public clone(name: string): GradientMaterial {
  220. return SerializationHelper.Clone(() => new GradientMaterial(name, this.getScene()), this);
  221. }
  222. public serialize(): any {
  223. var serializationObject = SerializationHelper.Serialize(this);
  224. serializationObject.customType = "BABYLON.GradientMaterial";
  225. return serializationObject;
  226. }
  227. public getClassName(): string {
  228. return "GradientMaterial";
  229. }
  230. // Statics
  231. public static Parse(source: any, scene: Scene, rootUrl: string): GradientMaterial {
  232. return SerializationHelper.Parse(() => new GradientMaterial(source.name, scene), source, scene, rootUrl);
  233. }
  234. }
  235. _TypeStore.RegisteredTypes["BABYLON.GradientMaterial"] = GradientMaterial;