babylon.fireMaterial.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. module BABYLON {
  3. class FireMaterialDefines extends MaterialDefines {
  4. public DIFFUSE = false;
  5. public CLIPPLANE = false;
  6. public ALPHATEST = false;
  7. public POINTSIZE = false;
  8. public FOG = false;
  9. public UV1 = false;
  10. public VERTEXCOLOR = false;
  11. public VERTEXALPHA = false;
  12. public BonesPerMesh = 0;
  13. public NUM_BONE_INFLUENCERS = 0;
  14. public INSTANCES = false;
  15. constructor() {
  16. super();
  17. this.rebuild();
  18. }
  19. }
  20. export class FireMaterial extends PushMaterial {
  21. @serializeAsTexture("diffuseTexture")
  22. private _diffuseTexture: BaseTexture;
  23. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  24. public diffuseTexture: BaseTexture;
  25. @serializeAsTexture("distortionTexture")
  26. private _distortionTexture: BaseTexture;
  27. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  28. public distortionTexture: BaseTexture;
  29. @serializeAsTexture("opacityTexture")
  30. private _opacityTexture: BaseTexture;
  31. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  32. public opacityTexture: BaseTexture;
  33. @serialize("diffuseColor")
  34. public diffuseColor = new Color3(1, 1, 1);
  35. @serialize()
  36. public speed = 1.0;
  37. private _scaledDiffuse = new Color3();
  38. private _renderId: number;
  39. private _lastTime: number = 0;
  40. constructor(name: string, scene: Scene) {
  41. super(name, scene);
  42. }
  43. public needAlphaBlending(): boolean {
  44. return false;
  45. }
  46. public needAlphaTesting(): boolean {
  47. return true;
  48. }
  49. public getAlphaTestTexture(): BaseTexture {
  50. return null;
  51. }
  52. // Methods
  53. public isReadyForSubMesh(mesh: AbstractMesh, subMesh: SubMesh, useInstances?: boolean): boolean {
  54. if (this.isFrozen) {
  55. if (this._wasPreviouslyReady && subMesh.effect) {
  56. return true;
  57. }
  58. }
  59. if (!subMesh._materialDefines) {
  60. subMesh._materialDefines = new FireMaterialDefines();
  61. }
  62. var defines = <FireMaterialDefines>subMesh._materialDefines;
  63. var scene = this.getScene();
  64. if (!this.checkReadyOnEveryCall && subMesh.effect) {
  65. if (this._renderId === scene.getRenderId()) {
  66. return true;
  67. }
  68. }
  69. var engine = scene.getEngine();
  70. // Textures
  71. if (defines._areTexturesDirty) {
  72. defines._needUVs = false;
  73. if (this._diffuseTexture && StandardMaterial.DiffuseTextureEnabled) {
  74. if (!this._diffuseTexture.isReady()) {
  75. return false;
  76. } else {
  77. defines._needUVs = true;
  78. defines.DIFFUSE = true;
  79. }
  80. }
  81. }
  82. // Misc.
  83. if (defines._areMiscDirty) {
  84. defines.POINTSIZE = (this.pointsCloud || scene.forcePointsCloud);
  85. defines.FOG = (scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE && this.fogEnabled);
  86. }
  87. // Values that need to be evaluated on every frame
  88. MaterialHelper.PrepareDefinesForFrameBoundValues(scene, engine, defines, useInstances);
  89. // Attribs
  90. MaterialHelper.PrepareDefinesForAttributes(mesh, defines, false, true);
  91. // Get correct effect
  92. if (defines.isDirty) {
  93. defines.markAsProcessed();
  94. scene.resetCachedMaterial();
  95. // Fallbacks
  96. var fallbacks = new EffectFallbacks();
  97. if (defines.FOG) {
  98. fallbacks.addFallback(1, "FOG");
  99. }
  100. if (defines.NUM_BONE_INFLUENCERS > 0) {
  101. fallbacks.addCPUSkinningFallback(0, mesh);
  102. }
  103. //Attributes
  104. var attribs = [VertexBuffer.PositionKind];
  105. if (defines.UV1) {
  106. attribs.push(VertexBuffer.UVKind);
  107. }
  108. if (defines.VERTEXCOLOR) {
  109. attribs.push(VertexBuffer.ColorKind);
  110. }
  111. MaterialHelper.PrepareAttributesForBones(attribs, mesh, defines, fallbacks);
  112. MaterialHelper.PrepareAttributesForInstances(attribs, defines);
  113. // Legacy browser patch
  114. var shaderName = "fire";
  115. var join = defines.toString();
  116. subMesh.setEffect(scene.getEngine().createEffect(shaderName,
  117. <EffectCreationOptions>{
  118. attributes: attribs,
  119. uniformsNames: ["world", "view", "viewProjection", "vEyePosition",
  120. "vFogInfos", "vFogColor", "pointSize",
  121. "vDiffuseInfos",
  122. "mBones",
  123. "vClipPlane", "diffuseMatrix",
  124. // Fire
  125. "time", "speed"
  126. ],
  127. uniformBuffersNames: [],
  128. samplers: ["diffuseSampler",
  129. // Fire
  130. "distortionSampler", "opacitySampler"
  131. ],
  132. defines: join,
  133. fallbacks: fallbacks,
  134. onCompiled: this.onCompiled,
  135. onError: this.onError
  136. }, engine), defines);
  137. }
  138. if (!subMesh.effect.isReady()) {
  139. return false;
  140. }
  141. this._renderId = scene.getRenderId();
  142. this._wasPreviouslyReady = true;
  143. return true;
  144. }
  145. public bindForSubMesh(world: Matrix, mesh: Mesh, subMesh: SubMesh): void {
  146. var scene = this.getScene();
  147. var defines = <FireMaterialDefines>subMesh._materialDefines;
  148. if (!defines) {
  149. return;
  150. }
  151. var effect = subMesh.effect;
  152. this._activeEffect = effect;
  153. // Matrices
  154. this.bindOnlyWorldMatrix(world);
  155. this._activeEffect.setMatrix("viewProjection", scene.getTransformMatrix());
  156. // Bones
  157. MaterialHelper.BindBonesParameters(mesh, this._activeEffect);
  158. if (this._mustRebind(scene, effect)) {
  159. // Textures
  160. if (this._diffuseTexture && StandardMaterial.DiffuseTextureEnabled) {
  161. this._activeEffect.setTexture("diffuseSampler", this._diffuseTexture);
  162. this._activeEffect.setFloat2("vDiffuseInfos", this._diffuseTexture.coordinatesIndex, this._diffuseTexture.level);
  163. this._activeEffect.setMatrix("diffuseMatrix", this._diffuseTexture.getTextureMatrix());
  164. this._activeEffect.setTexture("distortionSampler", this._distortionTexture);
  165. this._activeEffect.setTexture("opacitySampler", this._opacityTexture);
  166. }
  167. // Clip plane
  168. if (scene.clipPlane) {
  169. var clipPlane = scene.clipPlane;
  170. this._activeEffect.setFloat4("vClipPlane", clipPlane.normal.x, clipPlane.normal.y, clipPlane.normal.z, clipPlane.d);
  171. }
  172. // Point size
  173. if (this.pointsCloud) {
  174. this._activeEffect.setFloat("pointSize", this.pointSize);
  175. }
  176. this._activeEffect.setVector3("vEyePosition", scene._mirroredCameraPosition ? scene._mirroredCameraPosition : scene.activeCamera.position);
  177. }
  178. this._activeEffect.setColor4("vDiffuseColor", this._scaledDiffuse, this.alpha * mesh.visibility);
  179. // View
  180. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE) {
  181. this._activeEffect.setMatrix("view", scene.getViewMatrix());
  182. }
  183. // Fog
  184. MaterialHelper.BindFogParameters(scene, mesh, this._activeEffect);
  185. // Time
  186. this._lastTime += scene.getEngine().getDeltaTime();
  187. this._activeEffect.setFloat("time", this._lastTime);
  188. // Speed
  189. this._activeEffect.setFloat("speed", this.speed);
  190. this._afterBind(mesh, this._activeEffect);
  191. }
  192. public getAnimatables(): IAnimatable[] {
  193. var results = [];
  194. if (this._diffuseTexture && this._diffuseTexture.animations && this._diffuseTexture.animations.length > 0) {
  195. results.push(this._diffuseTexture);
  196. }
  197. if (this._distortionTexture && this._distortionTexture.animations && this._distortionTexture.animations.length > 0) {
  198. results.push(this._distortionTexture);
  199. }
  200. if (this._opacityTexture && this._opacityTexture.animations && this._opacityTexture.animations.length > 0) {
  201. results.push(this._opacityTexture);
  202. }
  203. return results;
  204. }
  205. public getActiveTextures(): BaseTexture[] {
  206. var activeTextures = super.getActiveTextures();
  207. if (this._diffuseTexture) {
  208. activeTextures.push(this._diffuseTexture);
  209. }
  210. if (this._distortionTexture) {
  211. activeTextures.push(this._distortionTexture);
  212. }
  213. if (this._opacityTexture) {
  214. activeTextures.push(this._opacityTexture);
  215. }
  216. return activeTextures;
  217. }
  218. public hasTexture(texture: BaseTexture): boolean {
  219. if (super.hasTexture(texture)) {
  220. return true;
  221. }
  222. if (this._diffuseTexture === texture) {
  223. return true;
  224. }
  225. if (this._distortionTexture === texture) {
  226. return true;
  227. }
  228. if (this._opacityTexture === texture) {
  229. return true;
  230. }
  231. return false;
  232. }
  233. public dispose(forceDisposeEffect?: boolean): void {
  234. if (this._diffuseTexture) {
  235. this._diffuseTexture.dispose();
  236. }
  237. if (this._distortionTexture) {
  238. this._distortionTexture.dispose();
  239. }
  240. super.dispose(forceDisposeEffect);
  241. }
  242. public clone(name: string): FireMaterial {
  243. return SerializationHelper.Clone<FireMaterial>(() => new FireMaterial(name, this.getScene()), this);
  244. }
  245. public serialize(): any {
  246. var serializationObject = super.serialize();
  247. serializationObject.customType = "BABYLON.FireMaterial";
  248. serializationObject.diffuseColor = this.diffuseColor.asArray();
  249. serializationObject.speed = this.speed;
  250. if (this._diffuseTexture) {
  251. serializationObject._diffuseTexture = this._diffuseTexture.serialize();
  252. }
  253. if (this._distortionTexture) {
  254. serializationObject._distortionTexture = this._distortionTexture.serialize();
  255. }
  256. if (this._opacityTexture) {
  257. serializationObject._opacityTexture = this._opacityTexture.serialize();
  258. }
  259. return serializationObject;
  260. }
  261. public static Parse(source: any, scene: Scene, rootUrl: string): FireMaterial {
  262. var material = new FireMaterial(source.name, scene);
  263. material.diffuseColor = Color3.FromArray(source.diffuseColor);
  264. material.speed = source.speed;
  265. material.alpha = source.alpha;
  266. material.id = source.id;
  267. Tags.AddTagsTo(material, source.tags);
  268. material.backFaceCulling = source.backFaceCulling;
  269. material.wireframe = source.wireframe;
  270. if (source._diffuseTexture) {
  271. material._diffuseTexture = Texture.Parse(source._diffuseTexture, scene, rootUrl);
  272. }
  273. if (source._distortionTexture) {
  274. material._distortionTexture = Texture.Parse(source._distortionTexture, scene, rootUrl);
  275. }
  276. if (source._opacityTexture) {
  277. material._opacityTexture = Texture.Parse(source._opacityTexture, scene, rootUrl);
  278. }
  279. if (source.checkReadyOnlyOnce) {
  280. material.checkReadyOnlyOnce = source.checkReadyOnlyOnce;
  281. }
  282. return material;
  283. }
  284. }
  285. }