babylon.gradientMaterial.ts 13 KB

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