gradientMaterial.ts 10 KB

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