babylon.fireMaterial.ts 13 KB

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