babylon.gradientMaterial.ts 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  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 POINTSIZE = false;
  8. public FOG = false;
  9. public LIGHT0 = false;
  10. public LIGHT1 = false;
  11. public LIGHT2 = false;
  12. public LIGHT3 = false;
  13. public SPOTLIGHT0 = false;
  14. public SPOTLIGHT1 = false;
  15. public SPOTLIGHT2 = false;
  16. public SPOTLIGHT3 = false;
  17. public HEMILIGHT0 = false;
  18. public HEMILIGHT1 = false;
  19. public HEMILIGHT2 = false;
  20. public HEMILIGHT3 = false;
  21. public DIRLIGHT0 = false;
  22. public DIRLIGHT1 = false;
  23. public DIRLIGHT2 = false;
  24. public DIRLIGHT3 = false;
  25. public POINTLIGHT0 = false;
  26. public POINTLIGHT1 = false;
  27. public POINTLIGHT2 = false;
  28. public POINTLIGHT3 = false;
  29. public SHADOW0 = false;
  30. public SHADOW1 = false;
  31. public SHADOW2 = false;
  32. public SHADOW3 = false;
  33. public SHADOWS = false;
  34. public SHADOWESM0 = false;
  35. public SHADOWESM1 = false;
  36. public SHADOWESM2 = false;
  37. public SHADOWESM3 = false;
  38. public SHADOWPCF0 = false;
  39. public SHADOWPCF1 = false;
  40. public SHADOWPCF2 = false;
  41. public SHADOWPCF3 = false;
  42. public NORMAL = false;
  43. public UV1 = false;
  44. public UV2 = false;
  45. public VERTEXCOLOR = false;
  46. public VERTEXALPHA = false;
  47. public NUM_BONE_INFLUENCERS = 0;
  48. public BonesPerMesh = 0;
  49. public INSTANCES = false;
  50. constructor() {
  51. super();
  52. this.rebuild();
  53. }
  54. }
  55. export class GradientMaterial extends PushMaterial {
  56. @serialize("maxSimultaneousLights")
  57. private _maxSimultaneousLights = 4;
  58. @expandToProperty("_markAllSubMeshesAsLightsDirty")
  59. public maxSimultaneousLights: number;
  60. // The gradient top color, red by default
  61. @serializeAsColor3()
  62. public topColor = new Color3(1, 0, 0);
  63. @serialize()
  64. public topColorAlpha = 1.0;
  65. // The gradient top color, blue by default
  66. @serializeAsColor3()
  67. public bottomColor = new Color3(0, 0, 1);
  68. @serialize()
  69. public bottomColorAlpha = 1.0;
  70. // Gradient offset
  71. @serialize()
  72. public offset = 0;
  73. @serialize()
  74. public smoothness = 1.0;
  75. @serialize()
  76. public disableLighting = false;
  77. private _worldViewProjectionMatrix = Matrix.Zero();
  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(): 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);
  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. subMesh.setEffect(scene.getEngine().createEffect(shaderName,
  148. attribs,
  149. ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vDiffuseColor",
  150. "vLightData0", "vLightDiffuse0","vLightDirection0", "vLightGround0", "lightMatrix0",
  151. "vLightData1", "vLightDiffuse1", "vLightDirection1", "vLightGround1", "lightMatrix1",
  152. "vLightData2", "vLightDiffuse2", "vLightDirection2", "vLightGround2", "lightMatrix2",
  153. "vLightData3", "vLightDiffuse3", "vLightDirection3", "vLightGround3", "lightMatrix3",
  154. "vFogInfos", "vFogColor", "pointSize",
  155. "vDiffuseInfos",
  156. "mBones",
  157. "vClipPlane", "diffuseMatrix",
  158. "shadowsInfo0", "shadowsInfo1", "shadowsInfo2", "shadowsInfo3", "depthValues", "topColor", "bottomColor", "offset", "smoothness"
  159. ],
  160. ["diffuseSampler",
  161. "shadowSampler0", "shadowSampler1", "shadowSampler2", "shadowSampler3"
  162. ],
  163. join, fallbacks, this.onCompiled, this.onError, { maxSimultaneousLights: 4 }), defines);
  164. }
  165. if (!subMesh.effect.isReady()) {
  166. return false;
  167. }
  168. this._renderId = scene.getRenderId();
  169. this._wasPreviouslyReady = true;
  170. return true;
  171. }
  172. public bindForSubMesh(world: Matrix, mesh: Mesh, subMesh: SubMesh): void {
  173. var scene = this.getScene();
  174. var defines = <GradientMaterialDefines>subMesh._materialDefines;
  175. if (!defines) {
  176. return;
  177. }
  178. var effect = subMesh.effect;
  179. this._activeEffect = effect;
  180. // Matrices
  181. this.bindOnlyWorldMatrix(world);
  182. this._activeEffect.setMatrix("viewProjection", scene.getTransformMatrix());
  183. // Bones
  184. MaterialHelper.BindBonesParameters(mesh, this._effect);
  185. if (this._mustRebind(scene, effect)) {
  186. // Clip plane
  187. MaterialHelper.BindClipPlane(this._effect, scene);
  188. // Point size
  189. if (this.pointsCloud) {
  190. this._activeEffect.setFloat("pointSize", this.pointSize);
  191. }
  192. this._activeEffect.setVector3("vEyePosition", scene._mirroredCameraPosition ? scene._mirroredCameraPosition : scene.activeCamera.position);
  193. }
  194. this._activeEffect.setColor4("vDiffuseColor", this._scaledDiffuse, this.alpha * mesh.visibility);
  195. if (scene.lightsEnabled && !this.disableLighting) {
  196. MaterialHelper.BindLights(scene, mesh, this._activeEffect, defines);
  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("smoothness", this.smoothness);
  208. this._afterBind(mesh, this._activeEffect);
  209. }
  210. public getAnimatables(): IAnimatable[] {
  211. return [];
  212. }
  213. public dispose(forceDisposeEffect?: boolean): void {
  214. super.dispose(forceDisposeEffect);
  215. }
  216. public clone(name: string): GradientMaterial {
  217. return SerializationHelper.Clone(() => new GradientMaterial(name, this.getScene()), this);
  218. }
  219. public serialize(): any {
  220. var serializationObject = SerializationHelper.Serialize(this);
  221. serializationObject.customType = "BABYLON.GradientMaterial";
  222. return serializationObject;
  223. }
  224. // Statics
  225. public static Parse(source: any, scene: Scene, rootUrl: string): GradientMaterial {
  226. return SerializationHelper.Parse(() => new GradientMaterial(source.name, scene), source, scene, rootUrl);
  227. }
  228. }
  229. }