babylon.fireMaterial.ts 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  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 UV1 = false;
  11. public NORMAL = false;
  12. public VERTEXCOLOR = false;
  13. public VERTEXALPHA = false;
  14. public BONES = false;
  15. public BONES4 = false;
  16. public BonesPerMesh = 0;
  17. public INSTANCES = false;
  18. constructor() {
  19. super();
  20. this._keys = Object.keys(this);
  21. }
  22. }
  23. export class FireMaterial extends Material {
  24. public diffuseTexture: BaseTexture;
  25. public distortionTexture: BaseTexture;
  26. public opacityTexture: BaseTexture;
  27. public diffuseColor = new Color3(1, 1, 1);
  28. public disableLighting = false;
  29. public speed = 1.0;
  30. private _scaledDiffuse = new Color3();
  31. private _renderId: number;
  32. private _defines = new FireMaterialDefines();
  33. private _cachedDefines = new FireMaterialDefines();
  34. private _lastTime: number = 0;
  35. constructor(name: string, scene: Scene) {
  36. super(name, scene);
  37. this._cachedDefines.BonesPerMesh = -1;
  38. }
  39. public needAlphaBlending(): boolean {
  40. return (this.alpha < 1.0);
  41. }
  42. public needAlphaTesting(): boolean {
  43. return false;
  44. }
  45. public getAlphaTestTexture(): BaseTexture {
  46. return null;
  47. }
  48. // Methods
  49. private _checkCache(scene: Scene, mesh?: AbstractMesh, useInstances?: boolean): boolean {
  50. if (!mesh) {
  51. return true;
  52. }
  53. if (this._defines.INSTANCES !== useInstances) {
  54. return false;
  55. }
  56. if (mesh._materialDefines && mesh._materialDefines.isEqual(this._defines)) {
  57. return true;
  58. }
  59. return false;
  60. }
  61. public isReady(mesh?: AbstractMesh, useInstances?: boolean): boolean {
  62. if (this.checkReadyOnlyOnce) {
  63. if (this._wasPreviouslyReady) {
  64. return true;
  65. }
  66. }
  67. var scene = this.getScene();
  68. if (!this.checkReadyOnEveryCall) {
  69. if (this._renderId === scene.getRenderId()) {
  70. if (this._checkCache(scene, mesh, useInstances)) {
  71. return true;
  72. }
  73. }
  74. }
  75. var engine = scene.getEngine();
  76. var needNormals = false;
  77. var needUVs = false;
  78. this._defines.reset();
  79. // Textures
  80. if (scene.texturesEnabled) {
  81. if (this.diffuseTexture && StandardMaterial.DiffuseTextureEnabled) {
  82. if (!this.diffuseTexture.isReady()) {
  83. return false;
  84. } else {
  85. needUVs = true;
  86. this._defines.DIFFUSE = true;
  87. }
  88. }
  89. }
  90. // Effect
  91. if (scene.clipPlane) {
  92. this._defines.CLIPPLANE = true;
  93. }
  94. this._defines.ALPHATEST = true;
  95. // Point size
  96. if (this.pointsCloud || scene.forcePointsCloud) {
  97. this._defines.POINTSIZE = true;
  98. }
  99. // Fog
  100. if (scene.fogEnabled && mesh && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE && this.fogEnabled) {
  101. this._defines.FOG = true;
  102. }
  103. // Attribs
  104. if (mesh) {
  105. if (needNormals && mesh.isVerticesDataPresent(VertexBuffer.NormalKind)) {
  106. this._defines.NORMAL = true;
  107. }
  108. if (needUVs) {
  109. if (mesh.isVerticesDataPresent(VertexBuffer.UVKind)) {
  110. this._defines.UV1 = true;
  111. }
  112. }
  113. if (mesh.useVertexColors && mesh.isVerticesDataPresent(VertexBuffer.ColorKind)) {
  114. this._defines.VERTEXCOLOR = true;
  115. if (mesh.hasVertexAlpha) {
  116. this._defines.VERTEXALPHA = true;
  117. }
  118. }
  119. if (mesh.useBones && mesh.computeBonesUsingShaders) {
  120. this._defines.BONES = true;
  121. this._defines.BonesPerMesh = (mesh.skeleton.bones.length + 1);
  122. this._defines.BONES4 = true;
  123. }
  124. // Instances
  125. if (useInstances) {
  126. this._defines.INSTANCES = true;
  127. }
  128. }
  129. // Get correct effect
  130. if (!this._defines.isEqual(this._cachedDefines)) {
  131. this._defines.cloneTo(this._cachedDefines);
  132. scene.resetCachedMaterial();
  133. // Fallbacks
  134. var fallbacks = new EffectFallbacks();
  135. if (this._defines.FOG) {
  136. fallbacks.addFallback(1, "FOG");
  137. }
  138. if (this._defines.BONES4) {
  139. fallbacks.addFallback(0, "BONES4");
  140. }
  141. //Attributes
  142. var attribs = [VertexBuffer.PositionKind];
  143. if (this._defines.NORMAL) {
  144. attribs.push(VertexBuffer.NormalKind);
  145. }
  146. if (this._defines.UV1) {
  147. attribs.push(VertexBuffer.UVKind);
  148. }
  149. if (this._defines.VERTEXCOLOR) {
  150. attribs.push(VertexBuffer.ColorKind);
  151. }
  152. if (this._defines.BONES) {
  153. attribs.push(VertexBuffer.MatricesIndicesKind);
  154. attribs.push(VertexBuffer.MatricesWeightsKind);
  155. }
  156. if (this._defines.INSTANCES) {
  157. attribs.push("world0");
  158. attribs.push("world1");
  159. attribs.push("world2");
  160. attribs.push("world3");
  161. }
  162. // Legacy browser patch
  163. var shaderName = "fire";
  164. var join = this._defines.toString();
  165. this._effect = scene.getEngine().createEffect(shaderName,
  166. attribs,
  167. ["world", "view", "viewProjection", "vEyePosition",
  168. "vFogInfos", "vFogColor", "pointSize",
  169. "vDiffuseInfos",
  170. "mBones",
  171. "vClipPlane", "diffuseMatrix",
  172. // Fire
  173. "time", "speed"
  174. ],
  175. ["diffuseSampler",
  176. // Fire
  177. "distortionSampler", "opacitySampler"
  178. ],
  179. join, fallbacks, this.onCompiled, this.onError);
  180. }
  181. if (!this._effect.isReady()) {
  182. return false;
  183. }
  184. this._renderId = scene.getRenderId();
  185. this._wasPreviouslyReady = true;
  186. if (mesh) {
  187. if (!mesh._materialDefines) {
  188. mesh._materialDefines = new FireMaterialDefines();
  189. }
  190. this._defines.cloneTo(mesh._materialDefines);
  191. }
  192. return true;
  193. }
  194. public bindOnlyWorldMatrix(world: Matrix): void {
  195. this._effect.setMatrix("world", world);
  196. }
  197. public bind(world: Matrix, mesh?: Mesh): void {
  198. var scene = this.getScene();
  199. // Matrices
  200. this.bindOnlyWorldMatrix(world);
  201. this._effect.setMatrix("viewProjection", scene.getTransformMatrix());
  202. // Bones
  203. if (mesh && mesh.useBones && mesh.computeBonesUsingShaders) {
  204. this._effect.setMatrices("mBones", mesh.skeleton.getTransformMatrices());
  205. }
  206. if (scene.getCachedMaterial() !== this) {
  207. // Textures
  208. if (this.diffuseTexture && StandardMaterial.DiffuseTextureEnabled) {
  209. this._effect.setTexture("diffuseSampler", this.diffuseTexture);
  210. this._effect.setFloat2("vDiffuseInfos", this.diffuseTexture.coordinatesIndex, this.diffuseTexture.level);
  211. this._effect.setMatrix("diffuseMatrix", this.diffuseTexture.getTextureMatrix());
  212. this._effect.setTexture("distortionSampler", this.distortionTexture);
  213. this._effect.setTexture("opacitySampler", this.opacityTexture);
  214. }
  215. // Clip plane
  216. if (scene.clipPlane) {
  217. var clipPlane = scene.clipPlane;
  218. this._effect.setFloat4("vClipPlane", clipPlane.normal.x, clipPlane.normal.y, clipPlane.normal.z, clipPlane.d);
  219. }
  220. // Point size
  221. if (this.pointsCloud) {
  222. this._effect.setFloat("pointSize", this.pointSize);
  223. }
  224. this._effect.setVector3("vEyePosition", scene._mirroredCameraPosition ? scene._mirroredCameraPosition : scene.activeCamera.position);
  225. }
  226. this._effect.setColor4("vDiffuseColor", this._scaledDiffuse, this.alpha * mesh.visibility);
  227. // View
  228. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE) {
  229. this._effect.setMatrix("view", scene.getViewMatrix());
  230. }
  231. // Fog
  232. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE) {
  233. this._effect.setFloat4("vFogInfos", scene.fogMode, scene.fogStart, scene.fogEnd, scene.fogDensity);
  234. this._effect.setColor3("vFogColor", scene.fogColor);
  235. }
  236. // Time
  237. this._lastTime += scene.getEngine().getDeltaTime();
  238. this._effect.setFloat("time", this._lastTime);
  239. // Speed
  240. this._effect.setFloat("speed", this.speed);
  241. super.bind(world, mesh);
  242. }
  243. public getAnimatables(): IAnimatable[] {
  244. var results = [];
  245. if (this.diffuseTexture && this.diffuseTexture.animations && this.diffuseTexture.animations.length > 0) {
  246. results.push(this.diffuseTexture);
  247. }
  248. if (this.distortionTexture && this.distortionTexture.animations && this.distortionTexture.animations.length > 0) {
  249. results.push(this.distortionTexture);
  250. }
  251. if (this.opacityTexture && this.opacityTexture.animations && this.opacityTexture.animations.length > 0) {
  252. results.push(this.opacityTexture);
  253. }
  254. return results;
  255. }
  256. public dispose(forceDisposeEffect?: boolean): void {
  257. if (this.diffuseTexture) {
  258. this.diffuseTexture.dispose();
  259. }
  260. if (this.distortionTexture) {
  261. this.distortionTexture.dispose();
  262. }
  263. super.dispose(forceDisposeEffect);
  264. }
  265. public clone(name: string): FireMaterial {
  266. var newMaterial = new FireMaterial(name, this.getScene());
  267. // Base material
  268. this.copyTo(newMaterial);
  269. // Fire material
  270. if (this.diffuseTexture && this.diffuseTexture.clone) {
  271. newMaterial.diffuseTexture = this.diffuseTexture.clone();
  272. }
  273. if (this.distortionTexture && this.distortionTexture.clone) {
  274. newMaterial.distortionTexture = this.distortionTexture.clone();
  275. }
  276. if (this.opacityTexture && this.opacityTexture.clone) {
  277. newMaterial.opacityTexture = this.opacityTexture.clone();
  278. }
  279. newMaterial.diffuseColor = this.diffuseColor.clone();
  280. return newMaterial;
  281. }
  282. }
  283. }