babylon.gradientMaterial.ts 11 KB

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