babylon.fireMaterial.ts 12 KB

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