babylon.lavaMaterial.ts 20 KB

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