fireProceduralTexture.ts 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. import { serialize, serializeAsVector2, SerializationHelper } from "babylonjs/Misc/decorators";
  2. import { Vector2 } from "babylonjs/Maths/math.vector";
  3. import { Color3 } from 'babylonjs/Maths/math.color';
  4. import { Texture } from "babylonjs/Materials/Textures/texture";
  5. import { ProceduralTexture } from "babylonjs/Materials/Textures/Procedurals/proceduralTexture";
  6. import { Scene } from "babylonjs/scene";
  7. import { _TypeStore } from 'babylonjs/Misc/typeStore';
  8. import "./fireProceduralTexture.fragment";
  9. export class FireProceduralTexture extends ProceduralTexture {
  10. private _time: number = 0.0;
  11. private _speed = new Vector2(0.5, 0.3);
  12. private _autoGenerateTime: boolean = true;
  13. private _fireColors: Color3[];
  14. private _alphaThreshold: number = 0.5;
  15. constructor(name: string, size: number, scene: Scene, fallbackTexture?: Texture, generateMipMaps?: boolean) {
  16. super(name, size, "fireProceduralTexture", scene, fallbackTexture, generateMipMaps);
  17. this._fireColors = FireProceduralTexture.RedFireColors;
  18. this.updateShaderUniforms();
  19. }
  20. public updateShaderUniforms() {
  21. this.setFloat("time", this._time);
  22. this.setVector2("speed", this._speed);
  23. this.setColor3("c1", this._fireColors[0]);
  24. this.setColor3("c2", this._fireColors[1]);
  25. this.setColor3("c3", this._fireColors[2]);
  26. this.setColor3("c4", this._fireColors[3]);
  27. this.setColor3("c5", this._fireColors[4]);
  28. this.setColor3("c6", this._fireColors[5]);
  29. this.setFloat("alphaThreshold", this._alphaThreshold);
  30. }
  31. public render(useCameraPostProcess?: boolean) {
  32. let scene = this.getScene();
  33. if (this._autoGenerateTime && scene) {
  34. this._time += scene.getAnimationRatio() * 0.03;
  35. this.updateShaderUniforms();
  36. }
  37. super.render(useCameraPostProcess);
  38. }
  39. public static get PurpleFireColors(): Color3[] {
  40. return [
  41. new Color3(0.5, 0.0, 1.0),
  42. new Color3(0.9, 0.0, 1.0),
  43. new Color3(0.2, 0.0, 1.0),
  44. new Color3(1.0, 0.9, 1.0),
  45. new Color3(0.1, 0.1, 1.0),
  46. new Color3(0.9, 0.9, 1.0)
  47. ];
  48. }
  49. public static get GreenFireColors(): Color3[] {
  50. return [
  51. new Color3(0.5, 1.0, 0.0),
  52. new Color3(0.5, 1.0, 0.0),
  53. new Color3(0.3, 0.4, 0.0),
  54. new Color3(0.5, 1.0, 0.0),
  55. new Color3(0.2, 0.0, 0.0),
  56. new Color3(0.5, 1.0, 0.0)
  57. ];
  58. }
  59. public static get RedFireColors(): Color3[] {
  60. return [
  61. new Color3(0.5, 0.0, 0.1),
  62. new Color3(0.9, 0.0, 0.0),
  63. new Color3(0.2, 0.0, 0.0),
  64. new Color3(1.0, 0.9, 0.0),
  65. new Color3(0.1, 0.1, 0.1),
  66. new Color3(0.9, 0.9, 0.9)
  67. ];
  68. }
  69. public static get BlueFireColors(): Color3[] {
  70. return [
  71. new Color3(0.1, 0.0, 0.5),
  72. new Color3(0.0, 0.0, 0.5),
  73. new Color3(0.1, 0.0, 0.2),
  74. new Color3(0.0, 0.0, 1.0),
  75. new Color3(0.1, 0.2, 0.3),
  76. new Color3(0.0, 0.2, 0.9)
  77. ];
  78. }
  79. @serialize()
  80. public get autoGenerateTime(): boolean {
  81. return this._autoGenerateTime;
  82. }
  83. public set autoGenerateTime(value: boolean) {
  84. this._autoGenerateTime = value;
  85. }
  86. public get fireColors(): Color3[] {
  87. return this._fireColors;
  88. }
  89. public set fireColors(value: Color3[]) {
  90. this._fireColors = value;
  91. this.updateShaderUniforms();
  92. }
  93. @serialize()
  94. public get time(): number {
  95. return this._time;
  96. }
  97. public set time(value: number) {
  98. this._time = value;
  99. this.updateShaderUniforms();
  100. }
  101. @serializeAsVector2()
  102. public get speed(): Vector2 {
  103. return this._speed;
  104. }
  105. public set speed(value: Vector2) {
  106. this._speed = value;
  107. this.updateShaderUniforms();
  108. }
  109. @serialize()
  110. public get alphaThreshold(): number {
  111. return this._alphaThreshold;
  112. }
  113. public set alphaThreshold(value: number) {
  114. this._alphaThreshold = value;
  115. this.updateShaderUniforms();
  116. }
  117. /**
  118. * Serializes this fire procedural texture
  119. * @returns a serialized fire procedural texture object
  120. */
  121. public serialize(): any {
  122. var serializationObject = SerializationHelper.Serialize(this, super.serialize());
  123. serializationObject.customType = "BABYLON.FireProceduralTexture";
  124. serializationObject.fireColors = [];
  125. for (var i = 0; i < this._fireColors.length; i++) {
  126. serializationObject.fireColors.push(this._fireColors[i].asArray());
  127. }
  128. return serializationObject;
  129. }
  130. /**
  131. * Creates a Fire Procedural Texture from parsed fire procedural texture data
  132. * @param parsedTexture defines parsed texture data
  133. * @param scene defines the current scene
  134. * @param rootUrl defines the root URL containing fire procedural texture information
  135. * @returns a parsed Fire Procedural Texture
  136. */
  137. public static Parse(parsedTexture: any, scene: Scene, rootUrl: string): FireProceduralTexture {
  138. var texture = SerializationHelper.Parse(() => new FireProceduralTexture(parsedTexture.name, parsedTexture._size, scene, undefined, parsedTexture._generateMipMaps), parsedTexture, scene, rootUrl);
  139. var colors: Color3[] = [];
  140. for (var i = 0; i < parsedTexture.fireColors.length; i++) {
  141. colors.push(Color3.FromArray(parsedTexture.fireColors[i]));
  142. }
  143. texture.fireColors = colors;
  144. return texture;
  145. }
  146. }
  147. _TypeStore.RegisteredTypes["BABYLON.FireProceduralTexture"] = FireProceduralTexture;