babylon.fireMaterial.ts 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375
  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: BaseTexture;
  24. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  25. public diffuseTexture: BaseTexture;
  26. @serializeAsTexture("distortionTexture")
  27. private _distortionTexture: BaseTexture;
  28. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  29. public distortionTexture: BaseTexture;
  30. @serializeAsTexture("opacityTexture")
  31. private _opacityTexture: BaseTexture;
  32. @expandToProperty("_markAllSubMeshesAsTexturesDirty")
  33. public opacityTexture: BaseTexture;
  34. @serialize("diffuseColor")
  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(): 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);
  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. <EffectCreationOptions>{
  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. }, engine), defines);
  138. }
  139. if (!subMesh.effect.isReady()) {
  140. return false;
  141. }
  142. this._renderId = scene.getRenderId();
  143. this._wasPreviouslyReady = true;
  144. return true;
  145. }
  146. public bindForSubMesh(world: Matrix, mesh: Mesh, subMesh: SubMesh): void {
  147. var scene = this.getScene();
  148. var defines = <FireMaterialDefines>subMesh._materialDefines;
  149. if (!defines) {
  150. return;
  151. }
  152. var effect = subMesh.effect;
  153. this._activeEffect = effect;
  154. // Matrices
  155. this.bindOnlyWorldMatrix(world);
  156. this._activeEffect.setMatrix("viewProjection", scene.getTransformMatrix());
  157. // Bones
  158. MaterialHelper.BindBonesParameters(mesh, this._activeEffect);
  159. if (this._mustRebind(scene, effect)) {
  160. // Textures
  161. if (this._diffuseTexture && StandardMaterial.DiffuseTextureEnabled) {
  162. this._activeEffect.setTexture("diffuseSampler", this._diffuseTexture);
  163. this._activeEffect.setFloat2("vDiffuseInfos", this._diffuseTexture.coordinatesIndex, this._diffuseTexture.level);
  164. this._activeEffect.setMatrix("diffuseMatrix", this._diffuseTexture.getTextureMatrix());
  165. this._activeEffect.setTexture("distortionSampler", this._distortionTexture);
  166. this._activeEffect.setTexture("opacitySampler", this._opacityTexture);
  167. }
  168. // Clip plane
  169. if (scene.clipPlane) {
  170. var clipPlane = scene.clipPlane;
  171. this._activeEffect.setFloat4("vClipPlane", clipPlane.normal.x, clipPlane.normal.y, clipPlane.normal.z, clipPlane.d);
  172. }
  173. // Point size
  174. if (this.pointsCloud) {
  175. this._activeEffect.setFloat("pointSize", this.pointSize);
  176. }
  177. this._activeEffect.setVector3("vEyePosition", scene._mirroredCameraPosition ? scene._mirroredCameraPosition : scene.activeCamera.position);
  178. }
  179. this._activeEffect.setColor4("vDiffuseColor", this._scaledDiffuse, this.alpha * mesh.visibility);
  180. // View
  181. if (scene.fogEnabled && mesh.applyFog && scene.fogMode !== Scene.FOGMODE_NONE) {
  182. this._activeEffect.setMatrix("view", scene.getViewMatrix());
  183. }
  184. // Fog
  185. MaterialHelper.BindFogParameters(scene, mesh, this._activeEffect);
  186. // Time
  187. this._lastTime += scene.getEngine().getDeltaTime();
  188. this._activeEffect.setFloat("time", this._lastTime);
  189. // Speed
  190. this._activeEffect.setFloat("speed", this.speed);
  191. this._afterBind(mesh, this._activeEffect);
  192. }
  193. public getAnimatables(): IAnimatable[] {
  194. var results = [];
  195. if (this._diffuseTexture && this._diffuseTexture.animations && this._diffuseTexture.animations.length > 0) {
  196. results.push(this._diffuseTexture);
  197. }
  198. if (this._distortionTexture && this._distortionTexture.animations && this._distortionTexture.animations.length > 0) {
  199. results.push(this._distortionTexture);
  200. }
  201. if (this._opacityTexture && this._opacityTexture.animations && this._opacityTexture.animations.length > 0) {
  202. results.push(this._opacityTexture);
  203. }
  204. return results;
  205. }
  206. public getActiveTextures(): BaseTexture[] {
  207. var activeTextures = super.getActiveTextures();
  208. if (this._diffuseTexture) {
  209. activeTextures.push(this._diffuseTexture);
  210. }
  211. if (this._distortionTexture) {
  212. activeTextures.push(this._distortionTexture);
  213. }
  214. if (this._opacityTexture) {
  215. activeTextures.push(this._opacityTexture);
  216. }
  217. return activeTextures;
  218. }
  219. public hasTexture(texture: BaseTexture): boolean {
  220. if (super.hasTexture(texture)) {
  221. return true;
  222. }
  223. if (this._diffuseTexture === texture) {
  224. return true;
  225. }
  226. if (this._distortionTexture === texture) {
  227. return true;
  228. }
  229. if (this._opacityTexture === texture) {
  230. return true;
  231. }
  232. return false;
  233. }
  234. public getClassName(): string {
  235. return "FireMaterial";
  236. }
  237. public dispose(forceDisposeEffect?: boolean): void {
  238. if (this._diffuseTexture) {
  239. this._diffuseTexture.dispose();
  240. }
  241. if (this._distortionTexture) {
  242. this._distortionTexture.dispose();
  243. }
  244. super.dispose(forceDisposeEffect);
  245. }
  246. public clone(name: string): FireMaterial {
  247. return SerializationHelper.Clone<FireMaterial>(() => new FireMaterial(name, this.getScene()), this);
  248. }
  249. public serialize(): any {
  250. var serializationObject = super.serialize();
  251. serializationObject.customType = "BABYLON.FireMaterial";
  252. serializationObject.diffuseColor = this.diffuseColor.asArray();
  253. serializationObject.speed = this.speed;
  254. if (this._diffuseTexture) {
  255. serializationObject._diffuseTexture = this._diffuseTexture.serialize();
  256. }
  257. if (this._distortionTexture) {
  258. serializationObject._distortionTexture = this._distortionTexture.serialize();
  259. }
  260. if (this._opacityTexture) {
  261. serializationObject._opacityTexture = this._opacityTexture.serialize();
  262. }
  263. return serializationObject;
  264. }
  265. public static Parse(source: any, scene: Scene, rootUrl: string): FireMaterial {
  266. var material = new FireMaterial(source.name, scene);
  267. material.diffuseColor = Color3.FromArray(source.diffuseColor);
  268. material.speed = source.speed;
  269. material.alpha = source.alpha;
  270. material.id = source.id;
  271. Tags.AddTagsTo(material, source.tags);
  272. material.backFaceCulling = source.backFaceCulling;
  273. material.wireframe = source.wireframe;
  274. if (source._diffuseTexture) {
  275. material._diffuseTexture = Texture.Parse(source._diffuseTexture, scene, rootUrl);
  276. }
  277. if (source._distortionTexture) {
  278. material._distortionTexture = Texture.Parse(source._distortionTexture, scene, rootUrl);
  279. }
  280. if (source._opacityTexture) {
  281. material._opacityTexture = Texture.Parse(source._opacityTexture, scene, rootUrl);
  282. }
  283. if (source.checkReadyOnlyOnce) {
  284. material.checkReadyOnlyOnce = source.checkReadyOnlyOnce;
  285. }
  286. return material;
  287. }
  288. }
  289. }