starfieldProceduralTexture.ts 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. import { Effect, ProceduralTexture, Scene, Texture, serialize, SerializationHelper } from "babylonjs";
  2. Effect.ShadersStore["starfieldProceduralTexturePixelShader"] = require("./starfieldProceduralTexture.fragment.fx");
  3. export class StarfieldProceduralTexture extends ProceduralTexture {
  4. private _time = 1;
  5. private _alpha = 0.5;
  6. private _beta = 0.8;
  7. private _zoom = 0.8;
  8. private _formuparam = 0.53;
  9. private _stepsize = 0.1;
  10. private _tile = 0.850;
  11. private _brightness = 0.0015;
  12. private _darkmatter = 0.400;
  13. private _distfading = 0.730;
  14. private _saturation = 0.850;
  15. constructor(name: string, size: number, scene: Scene, fallbackTexture?: Texture, generateMipMaps?: boolean) {
  16. super(name, size, "starfieldProceduralTexture", scene, fallbackTexture, generateMipMaps);
  17. this.updateShaderUniforms();
  18. }
  19. public updateShaderUniforms() {
  20. this.setFloat("time", this._time);
  21. this.setFloat("alpha", this._alpha);
  22. this.setFloat("beta", this._beta);
  23. this.setFloat("zoom", this._zoom);
  24. this.setFloat("formuparam", this._formuparam);
  25. this.setFloat("stepsize", this._stepsize);
  26. this.setFloat("tile", this._tile);
  27. this.setFloat("brightness", this._brightness);
  28. this.setFloat("darkmatter", this._darkmatter);
  29. this.setFloat("distfading", this._distfading);
  30. this.setFloat("saturation", this._saturation);
  31. }
  32. @serialize()
  33. public get time(): number {
  34. return this._time;
  35. }
  36. public set time(value: number) {
  37. this._time = value;
  38. this.updateShaderUniforms();
  39. }
  40. @serialize()
  41. public get alpha(): number {
  42. return this._alpha;
  43. }
  44. public set alpha(value: number) {
  45. this._alpha = value;
  46. this.updateShaderUniforms();
  47. }
  48. @serialize()
  49. public get beta(): number {
  50. return this._beta;
  51. }
  52. public set beta(value: number) {
  53. this._beta = value;
  54. this.updateShaderUniforms();
  55. }
  56. @serialize()
  57. public get formuparam(): number {
  58. return this._formuparam;
  59. }
  60. public set formuparam(value: number) {
  61. this._formuparam = value;
  62. this.updateShaderUniforms();
  63. }
  64. @serialize()
  65. public get stepsize(): number {
  66. return this._stepsize;
  67. }
  68. public set stepsize(value: number) {
  69. this._stepsize = value;
  70. this.updateShaderUniforms();
  71. }
  72. @serialize()
  73. public get zoom(): number {
  74. return this._zoom;
  75. }
  76. public set zoom(value: number) {
  77. this._zoom = value;
  78. this.updateShaderUniforms();
  79. }
  80. @serialize()
  81. public get tile(): number {
  82. return this._tile;
  83. }
  84. public set tile(value: number) {
  85. this._tile = value;
  86. this.updateShaderUniforms();
  87. }
  88. @serialize()
  89. public get brightness(): number {
  90. return this._brightness;
  91. }
  92. public set brightness(value: number) {
  93. this._brightness = value;
  94. this.updateShaderUniforms();
  95. }
  96. @serialize()
  97. public get darkmatter(): number {
  98. return this._darkmatter;
  99. }
  100. public set darkmatter(value: number) {
  101. this._darkmatter = value;
  102. this.updateShaderUniforms();
  103. }
  104. @serialize()
  105. public get distfading(): number {
  106. return this._distfading;
  107. }
  108. public set distfading(value: number) {
  109. this._distfading = value;
  110. this.updateShaderUniforms();
  111. }
  112. @serialize()
  113. public get saturation(): number {
  114. return this._saturation;
  115. }
  116. public set saturation(value: number) {
  117. this._saturation = value;
  118. this.updateShaderUniforms();
  119. }
  120. /**
  121. * Serializes this starfield procedural texture
  122. * @returns a serialized starfield procedural texture object
  123. */
  124. public serialize(): any {
  125. var serializationObject = SerializationHelper.Serialize(this, super.serialize());
  126. serializationObject.customType = "BABYLON.StarfieldProceduralTexture";
  127. return serializationObject;
  128. }
  129. /**
  130. * Creates a Starfield Procedural Texture from parsed startfield 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 startfield procedural texture information
  134. * @returns a parsed Starfield Procedural Texture
  135. */
  136. public static Parse(parsedTexture: any, scene: Scene, rootUrl: string): StarfieldProceduralTexture {
  137. var texture = SerializationHelper.Parse(() => new StarfieldProceduralTexture(parsedTexture.name, parsedTexture._size, scene, undefined, parsedTexture._generateMipMaps), parsedTexture, scene, rootUrl);
  138. return texture;
  139. }
  140. }