babylon.gradientMaterial.ts 11 KB

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