babylon.lavaMaterial.ts 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. module BABYLON {
  3. var maxSimultaneousLights = 4;
  4. class LavaMaterialDefines 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 SHADOWVSM0 = false;
  36. public SHADOWVSM1 = false;
  37. public SHADOWVSM2 = false;
  38. public SHADOWVSM3 = 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 LavaMaterial extends Material {
  57. @serializeAsTexture()
  58. public diffuseTexture: BaseTexture;
  59. @serializeAsTexture()
  60. public noiseTexture: BaseTexture;
  61. @serializeAsColor3()
  62. public fogColor: Color3;
  63. @serialize()
  64. public speed : number = 1;
  65. @serialize()
  66. public movingSpeed : number = 1;
  67. @serialize()
  68. public lowFrequencySpeed : number = 1;
  69. @serialize()
  70. public fogDensity : number = 0.15;
  71. private _lastTime : number = 0;
  72. @serializeAsColor3()
  73. public diffuseColor = new Color3(1, 1, 1);
  74. @serialize()
  75. public disableLighting = false;
  76. private _worldViewProjectionMatrix = Matrix.Zero();
  77. private _scaledDiffuse = new Color3();
  78. private _renderId: number;
  79. private _defines = new LavaMaterialDefines();
  80. private _cachedDefines = new LavaMaterialDefines();
  81. constructor(name: string, scene: Scene) {
  82. super(name, scene);
  83. this._cachedDefines.BonesPerMesh = -1;
  84. }
  85. public needAlphaBlending(): boolean {
  86. return (this.alpha < 1.0);
  87. }
  88. public needAlphaTesting(): boolean {
  89. return false;
  90. }
  91. public getAlphaTestTexture(): BaseTexture {
  92. return null;
  93. }
  94. // Methods
  95. private _checkCache(scene: Scene, mesh?: AbstractMesh, useInstances?: boolean): boolean {
  96. if (!mesh) {
  97. return true;
  98. }
  99. if (this._defines.INSTANCES !== useInstances) {
  100. return false;
  101. }
  102. if (mesh._materialDefines && mesh._materialDefines.isEqual(this._defines)) {
  103. return true;
  104. }
  105. return false;
  106. }
  107. public isReady(mesh?: AbstractMesh, useInstances?: boolean): boolean {
  108. if (this.checkReadyOnlyOnce) {
  109. if (this._wasPreviouslyReady) {
  110. return true;
  111. }
  112. }
  113. var scene = this.getScene();
  114. if (!this.checkReadyOnEveryCall) {
  115. if (this._renderId === scene.getRenderId()) {
  116. if (this._checkCache(scene, mesh, useInstances)) {
  117. return true;
  118. }
  119. }
  120. }
  121. var engine = scene.getEngine();
  122. var needUVs = false;
  123. this._defines.reset();
  124. // Textures
  125. if (scene.texturesEnabled) {
  126. if (this.diffuseTexture && StandardMaterial.DiffuseTextureEnabled) {
  127. if (!this.diffuseTexture.isReady()) {
  128. return false;
  129. } else {
  130. needUVs = true;
  131. this._defines.DIFFUSE = true;
  132. }
  133. }
  134. }
  135. // Effect
  136. if (scene.clipPlane) {
  137. this._defines.CLIPPLANE = true;
  138. }
  139. if (engine.getAlphaTesting()) {
  140. this._defines.ALPHATEST = true;
  141. }
  142. // Point size
  143. if (this.pointsCloud || scene.forcePointsCloud) {
  144. this._defines.POINTSIZE = true;
  145. }
  146. // Fog
  147. if (scene.fogEnabled && mesh && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE && this.fogEnabled) {
  148. this._defines.FOG = true;
  149. }
  150. var lightIndex = 0;
  151. if (scene.lightsEnabled && !this.disableLighting) {
  152. MaterialHelper.PrepareDefinesForLights(scene, mesh, this._defines);
  153. }
  154. // Attribs
  155. if (mesh) {
  156. if (mesh.isVerticesDataPresent(VertexBuffer.NormalKind)) {
  157. this._defines.NORMAL = true;
  158. }
  159. if (needUVs) {
  160. if (mesh.isVerticesDataPresent(VertexBuffer.UVKind)) {
  161. this._defines.UV1 = true;
  162. }
  163. if (mesh.isVerticesDataPresent(VertexBuffer.UV2Kind)) {
  164. this._defines.UV2 = true;
  165. }
  166. }
  167. if (mesh.useVertexColors && mesh.isVerticesDataPresent(VertexBuffer.ColorKind)) {
  168. this._defines.VERTEXCOLOR = true;
  169. if (mesh.hasVertexAlpha) {
  170. this._defines.VERTEXALPHA = true;
  171. }
  172. }
  173. if (mesh.useBones && mesh.computeBonesUsingShaders) {
  174. this._defines.NUM_BONE_INFLUENCERS = mesh.numBoneInfluencers;
  175. this._defines.BonesPerMesh = (mesh.skeleton.bones.length + 1);
  176. }
  177. // Instances
  178. if (useInstances) {
  179. this._defines.INSTANCES = true;
  180. }
  181. }
  182. // Get correct effect
  183. if (!this._defines.isEqual(this._cachedDefines)) {
  184. this._defines.cloneTo(this._cachedDefines);
  185. scene.resetCachedMaterial();
  186. // Fallbacks
  187. var fallbacks = new EffectFallbacks();
  188. if (this._defines.FOG) {
  189. fallbacks.addFallback(1, "FOG");
  190. }
  191. MaterialHelper.HandleFallbacksForShadows(this._defines, fallbacks);
  192. if (this._defines.NUM_BONE_INFLUENCERS > 0) {
  193. fallbacks.addCPUSkinningFallback(0, mesh);
  194. }
  195. //Attributes
  196. var attribs = [VertexBuffer.PositionKind];
  197. if (this._defines.NORMAL) {
  198. attribs.push(VertexBuffer.NormalKind);
  199. }
  200. if (this._defines.UV1) {
  201. attribs.push(VertexBuffer.UVKind);
  202. }
  203. if (this._defines.UV2) {
  204. attribs.push(VertexBuffer.UV2Kind);
  205. }
  206. if (this._defines.VERTEXCOLOR) {
  207. attribs.push(VertexBuffer.ColorKind);
  208. }
  209. MaterialHelper.PrepareAttributesForBones(attribs, mesh, this._defines, fallbacks);
  210. MaterialHelper.PrepareAttributesForInstances(attribs, this._defines);
  211. // Legacy browser patch
  212. var shaderName = "lava";
  213. var join = this._defines.toString();
  214. this._effect = scene.getEngine().createEffect(shaderName,
  215. attribs,
  216. ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vDiffuseColor",
  217. "vLightData0", "vLightDiffuse0", "vLightSpecular0", "vLightDirection0", "vLightGround0", "lightMatrix0",
  218. "vLightData1", "vLightDiffuse1", "vLightSpecular1", "vLightDirection1", "vLightGround1", "lightMatrix1",
  219. "vLightData2", "vLightDiffuse2", "vLightSpecular2", "vLightDirection2", "vLightGround2", "lightMatrix2",
  220. "vLightData3", "vLightDiffuse3", "vLightSpecular3", "vLightDirection3", "vLightGround3", "lightMatrix3",
  221. "vFogInfos", "vFogColor", "pointSize",
  222. "vDiffuseInfos",
  223. "mBones",
  224. "vClipPlane", "diffuseMatrix",
  225. "shadowsInfo0", "shadowsInfo1", "shadowsInfo2", "shadowsInfo3", "depthValues",
  226. "time", "speed","movingSpeed",
  227. "fogColor","fogDensity", "lowFrequencySpeed"
  228. ],
  229. ["diffuseSampler",
  230. "shadowSampler0", "shadowSampler1", "shadowSampler2", "shadowSampler3", "noiseTexture"
  231. ],
  232. join, fallbacks, this.onCompiled, this.onError);
  233. }
  234. if (!this._effect.isReady()) {
  235. return false;
  236. }
  237. this._renderId = scene.getRenderId();
  238. this._wasPreviouslyReady = true;
  239. if (mesh) {
  240. if (!mesh._materialDefines) {
  241. mesh._materialDefines = new LavaMaterialDefines();
  242. }
  243. this._defines.cloneTo(mesh._materialDefines);
  244. }
  245. return true;
  246. }
  247. public bindOnlyWorldMatrix(world: Matrix): void {
  248. this._effect.setMatrix("world", world);
  249. }
  250. public bind(world: Matrix, mesh?: Mesh): void {
  251. var scene = this.getScene();
  252. // Matrices
  253. this.bindOnlyWorldMatrix(world);
  254. this._effect.setMatrix("viewProjection", scene.getTransformMatrix());
  255. // Bones
  256. MaterialHelper.BindBonesParameters(mesh, this._effect);
  257. if (scene.getCachedMaterial() !== this) {
  258. // Textures
  259. if (this.diffuseTexture && StandardMaterial.DiffuseTextureEnabled) {
  260. this._effect.setTexture("diffuseSampler", this.diffuseTexture);
  261. this._effect.setFloat2("vDiffuseInfos", this.diffuseTexture.coordinatesIndex, this.diffuseTexture.level);
  262. this._effect.setMatrix("diffuseMatrix", this.diffuseTexture.getTextureMatrix());
  263. }
  264. if (this.noiseTexture) {
  265. this._effect.setTexture("noiseTexture", this.noiseTexture);
  266. }
  267. // Clip plane
  268. MaterialHelper.BindClipPlane(this._effect, scene);
  269. // Point size
  270. if (this.pointsCloud) {
  271. this._effect.setFloat("pointSize", this.pointSize);
  272. }
  273. this._effect.setVector3("vEyePosition", scene._mirroredCameraPosition ? scene._mirroredCameraPosition : scene.activeCamera.position);
  274. }
  275. this._effect.setColor4("vDiffuseColor", this._scaledDiffuse, this.alpha * mesh.visibility);
  276. if (scene.lightsEnabled && !this.disableLighting) {
  277. MaterialHelper.BindLights(scene, mesh, this._effect, this._defines);
  278. }
  279. // View
  280. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE) {
  281. this._effect.setMatrix("view", scene.getViewMatrix());
  282. }
  283. // Fog
  284. MaterialHelper.BindFogParameters(scene, mesh, this._effect);
  285. this._lastTime += scene.getEngine().getDeltaTime();
  286. this._effect.setFloat("time", this._lastTime * this.speed / 1000);
  287. if (! this.fogColor) {
  288. this.fogColor = Color3.Black();
  289. }
  290. this._effect.setColor3("fogColor", this.fogColor);
  291. this._effect.setFloat("fogDensity", this.fogDensity);
  292. this._effect.setFloat("lowFrequencySpeed", this.lowFrequencySpeed);
  293. this._effect.setFloat("movingSpeed", this.movingSpeed);
  294. super.bind(world, mesh);
  295. }
  296. public getAnimatables(): IAnimatable[] {
  297. var results = [];
  298. if (this.diffuseTexture && this.diffuseTexture.animations && this.diffuseTexture.animations.length > 0) {
  299. results.push(this.diffuseTexture);
  300. }
  301. if (this.noiseTexture && this.noiseTexture.animations && this.noiseTexture.animations.length > 0) {
  302. results.push(this.noiseTexture);
  303. }
  304. return results;
  305. }
  306. public dispose(forceDisposeEffect?: boolean): void {
  307. if (this.diffuseTexture) {
  308. this.diffuseTexture.dispose();
  309. }
  310. if (this.noiseTexture) {
  311. this.noiseTexture.dispose();
  312. }
  313. super.dispose(forceDisposeEffect);
  314. }
  315. public clone(name: string): LavaMaterial {
  316. return SerializationHelper.Clone(() => new LavaMaterial(name, this.getScene()), this);
  317. }
  318. public serialize(): any {
  319. var serializationObject = SerializationHelper.Serialize(this);
  320. serializationObject.customType = "BABYLON.LavaMaterial";
  321. return serializationObject;
  322. }
  323. // Statics
  324. public static Parse(source: any, scene: Scene, rootUrl: string): LavaMaterial {
  325. return SerializationHelper.Parse(() => new LavaMaterial(source.name, scene), source, scene, rootUrl);
  326. }
  327. }
  328. }