babylon.starfieldProceduralTexture.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /// <reference path="../../../dist/preview release/babylon.d.ts"/>
  2. var BABYLON;
  3. (function (BABYLON) {
  4. var StarfieldProceduralTexture = (function (_super) {
  5. __extends(StarfieldProceduralTexture, _super);
  6. function StarfieldProceduralTexture(name, size, scene, fallbackTexture, generateMipMaps) {
  7. _super.call(this, name, size, "starfieldProceduralTexture", scene, fallbackTexture, generateMipMaps);
  8. this._time = 1;
  9. this._alpha = 0.5;
  10. this._beta = 0.8;
  11. this._zoom = 0.8;
  12. this._formuparam = 0.53;
  13. this._stepsize = 0.1;
  14. this._tile = 0.850;
  15. this._brightness = 0.0015;
  16. this._darkmatter = 0.400;
  17. this._distfading = 0.730;
  18. this._saturation = 0.850;
  19. this.updateShaderUniforms();
  20. }
  21. StarfieldProceduralTexture.prototype.updateShaderUniforms = function () {
  22. this.setFloat("time", this._time);
  23. this.setFloat("alpha", this._alpha);
  24. this.setFloat("beta", this._beta);
  25. this.setFloat("zoom", this._zoom);
  26. this.setFloat("formuparam", this._formuparam);
  27. this.setFloat("stepsize", this._stepsize);
  28. this.setFloat("tile", this._tile);
  29. this.setFloat("brightness", this._brightness);
  30. this.setFloat("darkmatter", this._darkmatter);
  31. this.setFloat("distfading", this._distfading);
  32. this.setFloat("saturation", this._saturation);
  33. };
  34. Object.defineProperty(StarfieldProceduralTexture.prototype, "time", {
  35. get: function () {
  36. return this._time;
  37. },
  38. set: function (value) {
  39. this._time = value;
  40. this.updateShaderUniforms();
  41. },
  42. enumerable: true,
  43. configurable: true
  44. });
  45. Object.defineProperty(StarfieldProceduralTexture.prototype, "alpha", {
  46. get: function () {
  47. return this._alpha;
  48. },
  49. set: function (value) {
  50. this._alpha = value;
  51. this.updateShaderUniforms();
  52. },
  53. enumerable: true,
  54. configurable: true
  55. });
  56. Object.defineProperty(StarfieldProceduralTexture.prototype, "beta", {
  57. get: function () {
  58. return this._beta;
  59. },
  60. set: function (value) {
  61. this._beta = value;
  62. this.updateShaderUniforms();
  63. },
  64. enumerable: true,
  65. configurable: true
  66. });
  67. Object.defineProperty(StarfieldProceduralTexture.prototype, "formuparam", {
  68. get: function () {
  69. return this._formuparam;
  70. },
  71. set: function (value) {
  72. this._formuparam = value;
  73. this.updateShaderUniforms();
  74. },
  75. enumerable: true,
  76. configurable: true
  77. });
  78. Object.defineProperty(StarfieldProceduralTexture.prototype, "stepsize", {
  79. get: function () {
  80. return this._stepsize;
  81. },
  82. set: function (value) {
  83. this._stepsize = value;
  84. this.updateShaderUniforms();
  85. },
  86. enumerable: true,
  87. configurable: true
  88. });
  89. Object.defineProperty(StarfieldProceduralTexture.prototype, "zoom", {
  90. get: function () {
  91. return this._zoom;
  92. },
  93. set: function (value) {
  94. this._zoom = value;
  95. this.updateShaderUniforms();
  96. },
  97. enumerable: true,
  98. configurable: true
  99. });
  100. Object.defineProperty(StarfieldProceduralTexture.prototype, "tile", {
  101. get: function () {
  102. return this._tile;
  103. },
  104. set: function (value) {
  105. this._tile = value;
  106. this.updateShaderUniforms();
  107. },
  108. enumerable: true,
  109. configurable: true
  110. });
  111. Object.defineProperty(StarfieldProceduralTexture.prototype, "brightness", {
  112. get: function () {
  113. return this._brightness;
  114. },
  115. set: function (value) {
  116. this._brightness = value;
  117. this.updateShaderUniforms();
  118. },
  119. enumerable: true,
  120. configurable: true
  121. });
  122. Object.defineProperty(StarfieldProceduralTexture.prototype, "darkmatter", {
  123. get: function () {
  124. return this._darkmatter;
  125. },
  126. set: function (value) {
  127. this._darkmatter = value;
  128. this.updateShaderUniforms();
  129. },
  130. enumerable: true,
  131. configurable: true
  132. });
  133. Object.defineProperty(StarfieldProceduralTexture.prototype, "distfading", {
  134. get: function () {
  135. return this._distfading;
  136. },
  137. set: function (value) {
  138. this._distfading = value;
  139. this.updateShaderUniforms();
  140. },
  141. enumerable: true,
  142. configurable: true
  143. });
  144. Object.defineProperty(StarfieldProceduralTexture.prototype, "saturation", {
  145. get: function () {
  146. return this._saturation;
  147. },
  148. set: function (value) {
  149. this._saturation = value;
  150. this.updateShaderUniforms();
  151. },
  152. enumerable: true,
  153. configurable: true
  154. });
  155. return StarfieldProceduralTexture;
  156. })(BABYLON.ProceduralTexture);
  157. BABYLON.StarfieldProceduralTexture = StarfieldProceduralTexture;
  158. })(BABYLON || (BABYLON = {}));
  159. BABYLON.Effect.ShadersStore['starfieldProceduralTexturePixelShader'] = "precision highp float;\n\n//defined as const as fragment shaders does not support uniforms in loops\n#define volsteps 20\n#define iterations 15\n\nvarying vec2 vPosition;\nvarying vec2 vUV;\n\nuniform float time;\nuniform float alpha;\nuniform float beta;\nuniform float zoom;\nuniform float formuparam;\nuniform float stepsize;\nuniform float tile;\nuniform float brightness;\nuniform float darkmatter;\nuniform float distfading;\nuniform float saturation;\n\nvoid main()\n{\n\tvec3 dir = vec3(vUV * zoom, 1.);\n\n\tfloat localTime = time * 0.0001;\n\n\t// Rotation\n\tmat2 rot1 = mat2(cos(alpha), sin(alpha), -sin(alpha), cos(alpha));\n\tmat2 rot2 = mat2(cos(beta), sin(beta), -sin(beta), cos(beta));\n\tdir.xz *= rot1;\n\tdir.xy *= rot2;\n\tvec3 from = vec3(1., .5, 0.5);\n\tfrom += vec3(-2., localTime*2., localTime);\n\tfrom.xz *= rot1;\n\tfrom.xy *= rot2;\n\n\t//volumetric rendering\n\tfloat s = 0.1, fade = 1.;\n\tvec3 v = vec3(0.);\n\tfor (int r = 0; r < volsteps; r++) {\n\t\tvec3 p = from + s*dir*.5;\n\t\tp = abs(vec3(tile) - mod(p, vec3(tile*2.))); // tiling fold\n\t\tfloat pa, a = pa = 0.;\n\t\tfor (int i = 0; i < iterations; i++) {\n\t\t\tp = abs(p) / dot(p, p) - formuparam; // the magic formula\n\t\t\ta += abs(length(p) - pa); // absolute sum of average change\n\t\t\tpa = length(p);\n\t\t}\n\t\tfloat dm = max(0., darkmatter - a*a*.001); //dark matter\n\t\ta *= a*a; // add contrast\n\t\tif (r > 6) fade *= 1. - dm; // dark matter, don't render near\n\t\t\t\t\t\t\t\t //v+=vec3(dm,dm*.5,0.);\n\t\tv += fade;\n\t\tv += vec3(s, s*s, s*s*s*s)*a*brightness*fade; // coloring based on distance\n\t\tfade *= distfading; // distance fading\n\t\ts += stepsize;\n\t}\n\tv = mix(vec3(length(v)), v, saturation); //color adjust\n\tgl_FragColor = vec4(v*.01, 1.);\n}";