starfieldProceduralTexture.ts 4.9 KB

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