fireProceduralTexture.ts 5.7 KB

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