babylon.fireMaterial.ts 20 KB

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