babylon.gradientMaterial.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. module BABYLON {
  3. var maxSimultaneousLights = 4;
  4. class GradientMaterialDefines extends MaterialDefines {
  5. public DIFFUSE = false;
  6. public CLIPPLANE = false;
  7. public ALPHATEST = 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 Material {
  57. // The gradient top color, red by default
  58. @serializeAsColor3()
  59. public topColor = new Color3(1, 0, 0);
  60. @serialize()
  61. public topColorAlpha = 1.0;
  62. // The gradient top color, blue by default
  63. @serializeAsColor3()
  64. public bottomColor = new Color3(0, 0, 1);
  65. @serialize()
  66. public bottomColorAlpha = 1.0;
  67. // Gradient offset
  68. @serialize()
  69. public offset = 0;
  70. @serialize()
  71. public smoothness = 1.0;
  72. @serialize()
  73. public disableLighting = false;
  74. private _worldViewProjectionMatrix = Matrix.Zero();
  75. private _scaledDiffuse = new Color3();
  76. private _renderId: number;
  77. private _defines = new GradientMaterialDefines();
  78. private _cachedDefines = new GradientMaterialDefines();
  79. constructor(name: string, scene: Scene) {
  80. super(name, scene);
  81. this._cachedDefines.BonesPerMesh = -1;
  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. private _checkCache(scene: Scene, mesh?: AbstractMesh, useInstances?: boolean): boolean {
  94. if (!mesh) {
  95. return true;
  96. }
  97. if (this._defines.INSTANCES !== useInstances) {
  98. return false;
  99. }
  100. return false;
  101. }
  102. public isReady(mesh?: AbstractMesh, useInstances?: boolean): boolean {
  103. if (this.checkReadyOnlyOnce) {
  104. if (this._wasPreviouslyReady) {
  105. return true;
  106. }
  107. }
  108. var scene = this.getScene();
  109. if (!this.checkReadyOnEveryCall) {
  110. if (this._renderId === scene.getRenderId()) {
  111. if (this._checkCache(scene, mesh, useInstances)) {
  112. return true;
  113. }
  114. }
  115. }
  116. var engine = scene.getEngine();
  117. var needNormals = false;
  118. var needUVs = false;
  119. this._defines.reset();
  120. // No textures
  121. // Effect
  122. if (scene.clipPlane) {
  123. this._defines.CLIPPLANE = true;
  124. }
  125. if (engine.getAlphaTesting()) {
  126. this._defines.ALPHATEST = true;
  127. }
  128. // Point size
  129. if (this.pointsCloud || scene.forcePointsCloud) {
  130. this._defines.POINTSIZE = true;
  131. }
  132. // Fog
  133. if (scene.fogEnabled && mesh && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE && this.fogEnabled) {
  134. this._defines.FOG = true;
  135. }
  136. var lightIndex = 0;
  137. if (scene.lightsEnabled && !this.disableLighting) {
  138. needNormals = MaterialHelper.PrepareDefinesForLights(scene, mesh, this._defines, false);
  139. }
  140. // Attribs
  141. if (mesh) {
  142. if (needNormals && mesh.isVerticesDataPresent(VertexBuffer.NormalKind)) {
  143. this._defines.NORMAL = true;
  144. }
  145. if (needUVs) {
  146. if (mesh.isVerticesDataPresent(VertexBuffer.UVKind)) {
  147. this._defines.UV1 = true;
  148. }
  149. if (mesh.isVerticesDataPresent(VertexBuffer.UV2Kind)) {
  150. this._defines.UV2 = true;
  151. }
  152. }
  153. if (mesh.useVertexColors && mesh.isVerticesDataPresent(VertexBuffer.ColorKind)) {
  154. this._defines.VERTEXCOLOR = true;
  155. if (mesh.hasVertexAlpha) {
  156. this._defines.VERTEXALPHA = true;
  157. }
  158. }
  159. if (mesh.useBones && mesh.computeBonesUsingShaders) {
  160. this._defines.NUM_BONE_INFLUENCERS = mesh.numBoneInfluencers;
  161. this._defines.BonesPerMesh = (mesh.skeleton.bones.length + 1);
  162. }
  163. // Instances
  164. if (useInstances) {
  165. this._defines.INSTANCES = true;
  166. }
  167. }
  168. // Get correct effect
  169. if (!this._defines.isEqual(this._cachedDefines)) {
  170. this._defines.cloneTo(this._cachedDefines);
  171. scene.resetCachedMaterial();
  172. // Fallbacks
  173. var fallbacks = new EffectFallbacks();
  174. if (this._defines.FOG) {
  175. fallbacks.addFallback(1, "FOG");
  176. }
  177. MaterialHelper.HandleFallbacksForShadows(this._defines, fallbacks);
  178. if (this._defines.NUM_BONE_INFLUENCERS > 0) {
  179. fallbacks.addCPUSkinningFallback(0, mesh);
  180. }
  181. //Attributes
  182. var attribs = [VertexBuffer.PositionKind];
  183. if (this._defines.NORMAL) {
  184. attribs.push(VertexBuffer.NormalKind);
  185. }
  186. if (this._defines.UV1) {
  187. attribs.push(VertexBuffer.UVKind);
  188. }
  189. if (this._defines.UV2) {
  190. attribs.push(VertexBuffer.UV2Kind);
  191. }
  192. if (this._defines.VERTEXCOLOR) {
  193. attribs.push(VertexBuffer.ColorKind);
  194. }
  195. MaterialHelper.PrepareAttributesForBones(attribs, mesh, this._defines, fallbacks);
  196. MaterialHelper.PrepareAttributesForInstances(attribs, this._defines);
  197. // Legacy browser patch
  198. var shaderName = "gradient";
  199. var join = this._defines.toString();
  200. this._effect = scene.getEngine().createEffect(shaderName,
  201. attribs,
  202. ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vDiffuseColor",
  203. "vLightData0", "vLightDiffuse0","vLightDirection0", "vLightGround0", "lightMatrix0",
  204. "vLightData1", "vLightDiffuse1", "vLightDirection1", "vLightGround1", "lightMatrix1",
  205. "vLightData2", "vLightDiffuse2", "vLightDirection2", "vLightGround2", "lightMatrix2",
  206. "vLightData3", "vLightDiffuse3", "vLightDirection3", "vLightGround3", "lightMatrix3",
  207. "vFogInfos", "vFogColor", "pointSize",
  208. "vDiffuseInfos",
  209. "mBones",
  210. "vClipPlane", "diffuseMatrix",
  211. "shadowsInfo0", "shadowsInfo1", "shadowsInfo2", "shadowsInfo3", "depthValues", "topColor", "bottomColor", "offset", "smoothness"
  212. ],
  213. ["diffuseSampler",
  214. "shadowSampler0", "shadowSampler1", "shadowSampler2", "shadowSampler3"
  215. ],
  216. join, fallbacks, this.onCompiled, this.onError, { maxSimultaneousLights: 4 });
  217. }
  218. if (!this._effect.isReady()) {
  219. return false;
  220. }
  221. this._renderId = scene.getRenderId();
  222. this._wasPreviouslyReady = true;
  223. return true;
  224. }
  225. public bindOnlyWorldMatrix(world: Matrix): void {
  226. this._effect.setMatrix("world", world);
  227. }
  228. public bind(world: Matrix, mesh?: Mesh): void {
  229. var scene = this.getScene();
  230. // Matrices
  231. this.bindOnlyWorldMatrix(world);
  232. this._effect.setMatrix("viewProjection", scene.getTransformMatrix());
  233. // Bones
  234. MaterialHelper.BindBonesParameters(mesh, this._effect);
  235. if (scene.getCachedMaterial() !== this) {
  236. // Clip plane
  237. MaterialHelper.BindClipPlane(this._effect, scene);
  238. // Point size
  239. if (this.pointsCloud) {
  240. this._effect.setFloat("pointSize", this.pointSize);
  241. }
  242. this._effect.setVector3("vEyePosition", scene._mirroredCameraPosition ? scene._mirroredCameraPosition : scene.activeCamera.position);
  243. }
  244. this._effect.setColor4("vDiffuseColor", this._scaledDiffuse, this.alpha * mesh.visibility);
  245. if (scene.lightsEnabled && !this.disableLighting) {
  246. MaterialHelper.BindLights(scene, mesh, this._effect, this._defines);
  247. }
  248. // View
  249. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE) {
  250. this._effect.setMatrix("view", scene.getViewMatrix());
  251. }
  252. // Fog
  253. MaterialHelper.BindFogParameters(scene, mesh, this._effect);
  254. this._effect.setColor4("topColor", this.topColor, this.topColorAlpha);
  255. this._effect.setColor4("bottomColor", this.bottomColor, this.bottomColorAlpha);
  256. this._effect.setFloat("offset", this.offset);
  257. this._effect.setFloat("smoothness", this.smoothness);
  258. this._afterBind(mesh);
  259. }
  260. public getAnimatables(): IAnimatable[] {
  261. return [];
  262. }
  263. public dispose(forceDisposeEffect?: boolean): void {
  264. super.dispose(forceDisposeEffect);
  265. }
  266. public clone(name: string): GradientMaterial {
  267. return SerializationHelper.Clone(() => new GradientMaterial(name, this.getScene()), this);
  268. }
  269. public serialize(): any {
  270. var serializationObject = SerializationHelper.Serialize(this);
  271. serializationObject.customType = "BABYLON.GradientMaterial";
  272. return serializationObject;
  273. }
  274. // Statics
  275. public static Parse(source: any, scene: Scene, rootUrl: string): GradientMaterial {
  276. return SerializationHelper.Parse(() => new GradientMaterial(source.name, scene), source, scene, rootUrl);
  277. }
  278. }
  279. }