babylon.starfieldProceduralTexture.js 4.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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.updateShaderUniforms();
  13. }
  14. StarfieldProceduralTexture.prototype.updateShaderUniforms = function () {
  15. this.setFloat("time", this._time);
  16. this.setFloat("alpha", this._alpha);
  17. this.setFloat("beta", this._beta);
  18. this.setFloat("zoom", this._zoom);
  19. };
  20. Object.defineProperty(StarfieldProceduralTexture.prototype, "time", {
  21. get: function () {
  22. return this._time;
  23. },
  24. set: function (value) {
  25. this._time = value;
  26. this.updateShaderUniforms();
  27. },
  28. enumerable: true,
  29. configurable: true
  30. });
  31. Object.defineProperty(StarfieldProceduralTexture.prototype, "alpha", {
  32. get: function () {
  33. return this._alpha;
  34. },
  35. set: function (value) {
  36. this._alpha = value;
  37. this.updateShaderUniforms();
  38. },
  39. enumerable: true,
  40. configurable: true
  41. });
  42. Object.defineProperty(StarfieldProceduralTexture.prototype, "beta", {
  43. get: function () {
  44. return this._beta;
  45. },
  46. set: function (value) {
  47. this._beta = value;
  48. this.updateShaderUniforms();
  49. },
  50. enumerable: true,
  51. configurable: true
  52. });
  53. Object.defineProperty(StarfieldProceduralTexture.prototype, "zoom", {
  54. get: function () {
  55. return this._zoom;
  56. },
  57. set: function (value) {
  58. this._zoom = value;
  59. this.updateShaderUniforms();
  60. },
  61. enumerable: true,
  62. configurable: true
  63. });
  64. return StarfieldProceduralTexture;
  65. })(BABYLON.ProceduralTexture);
  66. BABYLON.StarfieldProceduralTexture = StarfieldProceduralTexture;
  67. })(BABYLON || (BABYLON = {}));
  68. BABYLON.Effect.ShadersStore['starfieldProceduralTexturePixelShader'] = "precision highp float;\r\n\r\n#define iterations 15\r\n#define formuparam 0.53\r\n\r\n#define volsteps 20\r\n#define stepsize 0.1\r\n\r\n#define tile 0.850\r\n\r\n#define brightness 0.0015\r\n#define darkmatter 0.400\r\n#define distfading 0.730\r\n#define saturation 0.850\r\n\r\nvarying vec2 vPosition;\r\nvarying vec2 vUV;\r\n\r\nuniform float time;\r\nuniform float alpha;\r\nuniform float beta;\r\nuniform float zoom;\r\n\r\nvoid main()\r\n{\r\n\tvec3 dir = vec3(vUV * zoom, 1.);\r\n\r\n\tfloat localTime = time * 0.0001;\r\n\r\n\t// Rotation\r\n\tmat2 rot1 = mat2(cos(alpha), sin(alpha), -sin(alpha), cos(alpha));\r\n\tmat2 rot2 = mat2(cos(beta), sin(beta), -sin(beta), cos(beta));\r\n\tdir.xz *= rot1;\r\n\tdir.xy *= rot2;\r\n\tvec3 from = vec3(1., .5, 0.5);\r\n\tfrom += vec3(localTime*2., localTime, -2.);\r\n\tfrom.xz *= rot1;\r\n\tfrom.xy *= rot2;\r\n\r\n\t//volumetric rendering\r\n\tfloat s = 0.1, fade = 1.;\r\n\tvec3 v = vec3(0.);\r\n\tfor (int r = 0; r < volsteps; r++) {\r\n\t\tvec3 p = from + s*dir*.5;\r\n\t\tp = abs(vec3(tile) - mod(p, vec3(tile*2.))); // tiling fold\r\n\t\tfloat pa, a = pa = 0.;\r\n\t\tfor (int i = 0; i < iterations; i++) {\r\n\t\t\tp = abs(p) / dot(p, p) - formuparam; // the magic formula\r\n\t\t\ta += abs(length(p) - pa); // absolute sum of average change\r\n\t\t\tpa = length(p);\r\n\t\t}\r\n\t\tfloat dm = max(0., darkmatter - a*a*.001); //dark matter\r\n\t\ta *= a*a; // add contrast\r\n\t\tif (r > 6) fade *= 1. - dm; // dark matter, don't render near\r\n\t\t\t\t\t\t\t\t //v+=vec3(dm,dm*.5,0.);\r\n\t\tv += fade;\r\n\t\tv += vec3(s, s*s, s*s*s*s)*a*brightness*fade; // coloring based on distance\r\n\t\tfade *= distfading; // distance fading\r\n\t\ts += stepsize;\r\n\t}\r\n\tv = mix(vec3(length(v)), v, saturation); //color adjust\r\n\tgl_FragColor = vec4(v*.01, 1.);\r\n}";