babylon.gradientMaterial.ts 11 KB

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