gradientMaterial.ts 9.7 KB

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