babylon.lavaMaterial.ts 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622
  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. public diffuseTexture: BaseTexture;
  58. public noiseTexture: BaseTexture;
  59. public fogColor: Color3;
  60. public speed : number = 1;
  61. public movingSpeed : number = 1;
  62. public lowFrequencySpeed : number = 1;
  63. public fogDensity : number = 0.15;
  64. private _lastTime : number = 0;
  65. public diffuseColor = new Color3(1, 1, 1);
  66. public disableLighting = false;
  67. private _worldViewProjectionMatrix = Matrix.Zero();
  68. private _scaledDiffuse = new Color3();
  69. private _renderId: number;
  70. private _defines = new LavaMaterialDefines();
  71. private _cachedDefines = new LavaMaterialDefines();
  72. constructor(name: string, scene: Scene) {
  73. super(name, scene);
  74. this._cachedDefines.BonesPerMesh = -1;
  75. }
  76. public needAlphaBlending(): boolean {
  77. return (this.alpha < 1.0);
  78. }
  79. public needAlphaTesting(): boolean {
  80. return false;
  81. }
  82. public getAlphaTestTexture(): BaseTexture {
  83. return null;
  84. }
  85. // Methods
  86. private _checkCache(scene: Scene, mesh?: AbstractMesh, useInstances?: boolean): boolean {
  87. if (!mesh) {
  88. return true;
  89. }
  90. if (this._defines.INSTANCES !== useInstances) {
  91. return false;
  92. }
  93. if (mesh._materialDefines && mesh._materialDefines.isEqual(this._defines)) {
  94. return true;
  95. }
  96. return false;
  97. }
  98. public isReady(mesh?: AbstractMesh, useInstances?: boolean): boolean {
  99. if (this.checkReadyOnlyOnce) {
  100. if (this._wasPreviouslyReady) {
  101. return true;
  102. }
  103. }
  104. var scene = this.getScene();
  105. if (!this.checkReadyOnEveryCall) {
  106. if (this._renderId === scene.getRenderId()) {
  107. if (this._checkCache(scene, mesh, useInstances)) {
  108. return true;
  109. }
  110. }
  111. }
  112. var engine = scene.getEngine();
  113. var needNormals = false;
  114. var needUVs = false;
  115. this._defines.reset();
  116. // Textures
  117. if (scene.texturesEnabled) {
  118. if (this.diffuseTexture && StandardMaterial.DiffuseTextureEnabled) {
  119. if (!this.diffuseTexture.isReady()) {
  120. return false;
  121. } else {
  122. needUVs = true;
  123. this._defines.DIFFUSE = true;
  124. }
  125. }
  126. }
  127. // Effect
  128. if (scene.clipPlane) {
  129. this._defines.CLIPPLANE = true;
  130. }
  131. if (engine.getAlphaTesting()) {
  132. this._defines.ALPHATEST = true;
  133. }
  134. // Point size
  135. if (this.pointsCloud || scene.forcePointsCloud) {
  136. this._defines.POINTSIZE = true;
  137. }
  138. // Fog
  139. if (scene.fogEnabled && mesh && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE && this.fogEnabled) {
  140. this._defines.FOG = true;
  141. }
  142. var lightIndex = 0;
  143. if (scene.lightsEnabled && !this.disableLighting) {
  144. for (var index = 0; index < scene.lights.length; index++) {
  145. var light = scene.lights[index];
  146. if (!light.isEnabled()) {
  147. continue;
  148. }
  149. // Excluded check
  150. if (light._excludedMeshesIds.length > 0) {
  151. for (var excludedIndex = 0; excludedIndex < light._excludedMeshesIds.length; excludedIndex++) {
  152. var excludedMesh = scene.getMeshByID(light._excludedMeshesIds[excludedIndex]);
  153. if (excludedMesh) {
  154. light.excludedMeshes.push(excludedMesh);
  155. }
  156. }
  157. light._excludedMeshesIds = [];
  158. }
  159. // Included check
  160. if (light._includedOnlyMeshesIds.length > 0) {
  161. for (var includedOnlyIndex = 0; includedOnlyIndex < light._includedOnlyMeshesIds.length; includedOnlyIndex++) {
  162. var includedOnlyMesh = scene.getMeshByID(light._includedOnlyMeshesIds[includedOnlyIndex]);
  163. if (includedOnlyMesh) {
  164. light.includedOnlyMeshes.push(includedOnlyMesh);
  165. }
  166. }
  167. light._includedOnlyMeshesIds = [];
  168. }
  169. if (!light.canAffectMesh(mesh)) {
  170. continue;
  171. }
  172. needNormals = true;
  173. this._defines["LIGHT" + lightIndex] = true;
  174. var type;
  175. if (light instanceof SpotLight) {
  176. type = "SPOTLIGHT" + lightIndex;
  177. } else if (light instanceof HemisphericLight) {
  178. type = "HEMILIGHT" + lightIndex;
  179. } else if (light instanceof PointLight) {
  180. type = "POINTLIGHT" + lightIndex;
  181. } else {
  182. type = "DIRLIGHT" + lightIndex;
  183. }
  184. this._defines[type] = true;
  185. // Shadows
  186. if (scene.shadowsEnabled) {
  187. var shadowGenerator = light.getShadowGenerator();
  188. if (mesh && mesh.receiveShadows && shadowGenerator) {
  189. this._defines["SHADOW" + lightIndex] = true;
  190. this._defines.SHADOWS = true;
  191. if (shadowGenerator.useVarianceShadowMap || shadowGenerator.useBlurVarianceShadowMap) {
  192. this._defines["SHADOWVSM" + lightIndex] = true;
  193. }
  194. if (shadowGenerator.usePoissonSampling) {
  195. this._defines["SHADOWPCF" + lightIndex] = true;
  196. }
  197. }
  198. }
  199. lightIndex++;
  200. if (lightIndex === maxSimultaneousLights)
  201. break;
  202. }
  203. }
  204. // Attribs
  205. if (mesh) {
  206. if (needNormals && mesh.isVerticesDataPresent(VertexBuffer.NormalKind)) {
  207. this._defines.NORMAL = true;
  208. }
  209. if (needUVs) {
  210. if (mesh.isVerticesDataPresent(VertexBuffer.UVKind)) {
  211. this._defines.UV1 = true;
  212. }
  213. if (mesh.isVerticesDataPresent(VertexBuffer.UV2Kind)) {
  214. this._defines.UV2 = true;
  215. }
  216. }
  217. if (mesh.useVertexColors && mesh.isVerticesDataPresent(VertexBuffer.ColorKind)) {
  218. this._defines.VERTEXCOLOR = true;
  219. if (mesh.hasVertexAlpha) {
  220. this._defines.VERTEXALPHA = true;
  221. }
  222. }
  223. if (mesh.useBones && mesh.computeBonesUsingShaders) {
  224. this._defines.NUM_BONE_INFLUENCERS = mesh.numBoneInfluencers;
  225. this._defines.BonesPerMesh = (mesh.skeleton.bones.length + 1);
  226. }
  227. // Instances
  228. if (useInstances) {
  229. this._defines.INSTANCES = true;
  230. }
  231. }
  232. // Get correct effect
  233. if (!this._defines.isEqual(this._cachedDefines)) {
  234. this._defines.cloneTo(this._cachedDefines);
  235. scene.resetCachedMaterial();
  236. // Fallbacks
  237. var fallbacks = new EffectFallbacks();
  238. if (this._defines.FOG) {
  239. fallbacks.addFallback(1, "FOG");
  240. }
  241. for (lightIndex = 0; lightIndex < maxSimultaneousLights; lightIndex++) {
  242. if (!this._defines["LIGHT" + lightIndex]) {
  243. continue;
  244. }
  245. if (lightIndex > 0) {
  246. fallbacks.addFallback(lightIndex, "LIGHT" + lightIndex);
  247. }
  248. if (this._defines["SHADOW" + lightIndex]) {
  249. fallbacks.addFallback(0, "SHADOW" + lightIndex);
  250. }
  251. if (this._defines["SHADOWPCF" + lightIndex]) {
  252. fallbacks.addFallback(0, "SHADOWPCF" + lightIndex);
  253. }
  254. if (this._defines["SHADOWVSM" + lightIndex]) {
  255. fallbacks.addFallback(0, "SHADOWVSM" + lightIndex);
  256. }
  257. }
  258. if (this._defines.NUM_BONE_INFLUENCERS > 0){
  259. fallbacks.addCPUSkinningFallback(0, mesh);
  260. }
  261. //Attributes
  262. var attribs = [VertexBuffer.PositionKind];
  263. if (this._defines.NORMAL) {
  264. attribs.push(VertexBuffer.NormalKind);
  265. }
  266. if (this._defines.UV1) {
  267. attribs.push(VertexBuffer.UVKind);
  268. }
  269. if (this._defines.UV2) {
  270. attribs.push(VertexBuffer.UV2Kind);
  271. }
  272. if (this._defines.VERTEXCOLOR) {
  273. attribs.push(VertexBuffer.ColorKind);
  274. }
  275. if (this._defines.NUM_BONE_INFLUENCERS > 0) {
  276. attribs.push(VertexBuffer.MatricesIndicesKind);
  277. attribs.push(VertexBuffer.MatricesWeightsKind);
  278. if (this._defines.NUM_BONE_INFLUENCERS > 4) {
  279. attribs.push(VertexBuffer.MatricesIndicesExtraKind);
  280. attribs.push(VertexBuffer.MatricesWeightsExtraKind);
  281. }
  282. }
  283. if (this._defines.INSTANCES) {
  284. attribs.push("world0");
  285. attribs.push("world1");
  286. attribs.push("world2");
  287. attribs.push("world3");
  288. }
  289. // Legacy browser patch
  290. var shaderName = "lava";
  291. var join = this._defines.toString();
  292. this._effect = scene.getEngine().createEffect(shaderName,
  293. attribs,
  294. ["world", "view", "viewProjection", "vEyePosition", "vLightsType", "vDiffuseColor",
  295. "vLightData0", "vLightDiffuse0", "vLightSpecular0", "vLightDirection0", "vLightGround0", "lightMatrix0",
  296. "vLightData1", "vLightDiffuse1", "vLightSpecular1", "vLightDirection1", "vLightGround1", "lightMatrix1",
  297. "vLightData2", "vLightDiffuse2", "vLightSpecular2", "vLightDirection2", "vLightGround2", "lightMatrix2",
  298. "vLightData3", "vLightDiffuse3", "vLightSpecular3", "vLightDirection3", "vLightGround3", "lightMatrix3",
  299. "vFogInfos", "vFogColor", "pointSize",
  300. "vDiffuseInfos",
  301. "mBones",
  302. "vClipPlane", "diffuseMatrix",
  303. "shadowsInfo0", "shadowsInfo1", "shadowsInfo2", "shadowsInfo3", "depthValues",
  304. "time", "speed","movingSpeed",
  305. "fogColor","fogDensity", "lowFrequencySpeed"
  306. ],
  307. ["diffuseSampler",
  308. "shadowSampler0", "shadowSampler1", "shadowSampler2", "shadowSampler3", "noiseTexture"
  309. ],
  310. join, fallbacks, this.onCompiled, this.onError);
  311. }
  312. if (!this._effect.isReady()) {
  313. return false;
  314. }
  315. this._renderId = scene.getRenderId();
  316. this._wasPreviouslyReady = true;
  317. if (mesh) {
  318. if (!mesh._materialDefines) {
  319. mesh._materialDefines = new LavaMaterialDefines();
  320. }
  321. this._defines.cloneTo(mesh._materialDefines);
  322. }
  323. return true;
  324. }
  325. public bindOnlyWorldMatrix(world: Matrix): void {
  326. this._effect.setMatrix("world", world);
  327. }
  328. public bind(world: Matrix, mesh?: Mesh): void {
  329. var scene = this.getScene();
  330. // Matrices
  331. this.bindOnlyWorldMatrix(world);
  332. this._effect.setMatrix("viewProjection", scene.getTransformMatrix());
  333. // Bones
  334. if (mesh && mesh.useBones && mesh.computeBonesUsingShaders) {
  335. this._effect.setMatrices("mBones", mesh.skeleton.getTransformMatrices(mesh));
  336. }
  337. if (scene.getCachedMaterial() !== this) {
  338. // Textures
  339. if (this.diffuseTexture && StandardMaterial.DiffuseTextureEnabled) {
  340. this._effect.setTexture("diffuseSampler", this.diffuseTexture);
  341. this._effect.setFloat2("vDiffuseInfos", this.diffuseTexture.coordinatesIndex, this.diffuseTexture.level);
  342. this._effect.setMatrix("diffuseMatrix", this.diffuseTexture.getTextureMatrix());
  343. }
  344. if (this.noiseTexture) {
  345. this._effect.setTexture("noiseTexture", this.noiseTexture);
  346. }
  347. // Clip plane
  348. if (scene.clipPlane) {
  349. var clipPlane = scene.clipPlane;
  350. this._effect.setFloat4("vClipPlane", clipPlane.normal.x, clipPlane.normal.y, clipPlane.normal.z, clipPlane.d);
  351. }
  352. // Point size
  353. if (this.pointsCloud) {
  354. this._effect.setFloat("pointSize", this.pointSize);
  355. }
  356. this._effect.setVector3("vEyePosition", scene._mirroredCameraPosition ? scene._mirroredCameraPosition : scene.activeCamera.position);
  357. }
  358. this._effect.setColor4("vDiffuseColor", this._scaledDiffuse, this.alpha * mesh.visibility);
  359. if (scene.lightsEnabled && !this.disableLighting) {
  360. var lightIndex = 0;
  361. var depthValuesAlreadySet = false;
  362. for (var index = 0; index < scene.lights.length; index++) {
  363. var light = scene.lights[index];
  364. if (!light.isEnabled()) {
  365. continue;
  366. }
  367. if (!light.canAffectMesh(mesh)) {
  368. continue;
  369. }
  370. if (light instanceof PointLight) {
  371. // Point Light
  372. light.transferToEffect(this._effect, "vLightData" + lightIndex);
  373. } else if (light instanceof DirectionalLight) {
  374. // Directional Light
  375. light.transferToEffect(this._effect, "vLightData" + lightIndex);
  376. } else if (light instanceof SpotLight) {
  377. // Spot Light
  378. light.transferToEffect(this._effect, "vLightData" + lightIndex, "vLightDirection" + lightIndex);
  379. } else if (light instanceof HemisphericLight) {
  380. // Hemispheric Light
  381. light.transferToEffect(this._effect, "vLightData" + lightIndex, "vLightGround" + lightIndex);
  382. }
  383. light.diffuse.scaleToRef(light.intensity, this._scaledDiffuse);
  384. this._effect.setColor4("vLightDiffuse" + lightIndex, this._scaledDiffuse, light.range);
  385. // Shadows
  386. if (scene.shadowsEnabled) {
  387. var shadowGenerator = light.getShadowGenerator();
  388. if (mesh.receiveShadows && shadowGenerator) {
  389. if (!(<any>light).needCube()) {
  390. this._effect.setMatrix("lightMatrix" + lightIndex, shadowGenerator.getTransformMatrix());
  391. } else {
  392. if (!depthValuesAlreadySet) {
  393. depthValuesAlreadySet = true;
  394. this._effect.setFloat2("depthValues", scene.activeCamera.minZ, scene.activeCamera.maxZ);
  395. }
  396. }
  397. this._effect.setTexture("shadowSampler" + lightIndex, shadowGenerator.getShadowMapForRendering());
  398. this._effect.setFloat3("shadowsInfo" + lightIndex, shadowGenerator.getDarkness(), shadowGenerator.getShadowMap().getSize().width, shadowGenerator.bias);
  399. }
  400. }
  401. lightIndex++;
  402. if (lightIndex === maxSimultaneousLights)
  403. break;
  404. }
  405. }
  406. // View
  407. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE) {
  408. this._effect.setMatrix("view", scene.getViewMatrix());
  409. }
  410. // Fog
  411. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE) {
  412. this._effect.setFloat4("vFogInfos", scene.fogMode, scene.fogStart, scene.fogEnd, scene.fogDensity);
  413. this._effect.setColor3("vFogColor", scene.fogColor);
  414. }
  415. this._lastTime += scene.getEngine().getDeltaTime();
  416. this._effect.setFloat("time", this._lastTime * this.speed / 1000);
  417. if (! this.fogColor) {
  418. this.fogColor = Color3.Black();
  419. }
  420. this._effect.setColor3("fogColor", this.fogColor);
  421. this._effect.setFloat("fogDensity", this.fogDensity);
  422. this._effect.setFloat("lowFrequencySpeed", this.lowFrequencySpeed);
  423. this._effect.setFloat("movingSpeed", this.movingSpeed);
  424. super.bind(world, mesh);
  425. }
  426. public getAnimatables(): IAnimatable[] {
  427. var results = [];
  428. if (this.diffuseTexture && this.diffuseTexture.animations && this.diffuseTexture.animations.length > 0) {
  429. results.push(this.diffuseTexture);
  430. }
  431. if (this.noiseTexture && this.noiseTexture.animations && this.noiseTexture.animations.length > 0) {
  432. results.push(this.noiseTexture);
  433. }
  434. return results;
  435. }
  436. public dispose(forceDisposeEffect?: boolean): void {
  437. if (this.diffuseTexture) {
  438. this.diffuseTexture.dispose();
  439. }
  440. if (this.noiseTexture) {
  441. this.noiseTexture.dispose();
  442. }
  443. super.dispose(forceDisposeEffect);
  444. }
  445. public clone(name: string): LavaMaterial {
  446. var newMaterial = new LavaMaterial(name, this.getScene());
  447. // Base material
  448. this.copyTo(newMaterial);
  449. // Lava material
  450. if (this.diffuseTexture && this.diffuseTexture.clone) {
  451. newMaterial.diffuseTexture = this.diffuseTexture.clone();
  452. }
  453. if (this.noiseTexture && this.noiseTexture.clone) {
  454. newMaterial.noiseTexture = this.noiseTexture.clone();
  455. }
  456. if (this.fogColor && this.fogColor.clone) {
  457. newMaterial.fogColor = this.fogColor.clone();
  458. }
  459. return newMaterial;
  460. }
  461. public serialize(): any {
  462. var serializationObject = super.serialize();
  463. serializationObject.customType = "BABYLON.LavaMaterial";
  464. serializationObject.diffuseColor = this.diffuseColor.asArray();
  465. serializationObject.fogColor = this.fogColor.asArray();
  466. serializationObject.speed = this.speed;
  467. serializationObject.movingSpeed = this.movingSpeed;
  468. serializationObject.lowFrequencySpeed = this.lowFrequencySpeed;
  469. serializationObject.fogDensity = this.fogDensity;
  470. serializationObject.checkReadyOnlyOnce = this.checkReadyOnlyOnce;
  471. if (this.diffuseTexture) {
  472. serializationObject.diffuseTexture = this.diffuseTexture.serialize();
  473. }
  474. if (this.noiseTexture) {
  475. serializationObject.noiseTexture = this.noiseTexture.serialize();
  476. }
  477. return serializationObject;
  478. }
  479. public static Parse(source: any, scene: Scene, rootUrl: string): LavaMaterial {
  480. var material = new LavaMaterial(source.name, scene);
  481. material.diffuseColor = Color3.FromArray(source.diffuseColor);
  482. material.speed = source.speed;
  483. material.fogColor = Color3.FromArray(source.fogColor);
  484. material.movingSpeed = source.movingSpeed;
  485. material.lowFrequencySpeed = source.lowFrequencySpeed;
  486. material.fogDensity = source.lowFrequencySpeed;
  487. material.alpha = source.alpha;
  488. material.id = source.id;
  489. Tags.AddTagsTo(material, source.tags);
  490. material.backFaceCulling = source.backFaceCulling;
  491. material.wireframe = source.wireframe;
  492. if (source.diffuseTexture) {
  493. material.diffuseTexture = Texture.Parse(source.diffuseTexture, scene, rootUrl);
  494. }
  495. if (source.noiseTexture) {
  496. material.noiseTexture = Texture.Parse(source.noiseTexture, scene, rootUrl);
  497. }
  498. if (source.checkReadyOnlyOnce) {
  499. material.checkReadyOnlyOnce = source.checkReadyOnlyOnce;
  500. }
  501. return material;
  502. }
  503. }
  504. }