babylon.gradientMaterial.ts 11 KB

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