starfieldProceduralTexture.fragment.fx 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. precision highp float;
  2. //defined as const as fragment shaders does not support uniforms in loops
  3. #define volsteps 20
  4. #define iterations 15
  5. varying vec2 vPosition;
  6. varying vec2 vUV;
  7. uniform float time;
  8. uniform float alpha;
  9. uniform float beta;
  10. uniform float zoom;
  11. uniform float formuparam;
  12. uniform float stepsize;
  13. uniform float tile;
  14. uniform float brightness;
  15. uniform float darkmatter;
  16. uniform float distfading;
  17. uniform float saturation;
  18. void main()
  19. {
  20. vec3 dir = vec3(vUV * zoom, 1.);
  21. float localTime = time * 0.0001;
  22. // Rotation
  23. mat2 rot1 = mat2(cos(alpha), sin(alpha), -sin(alpha), cos(alpha));
  24. mat2 rot2 = mat2(cos(beta), sin(beta), -sin(beta), cos(beta));
  25. dir.xz *= rot1;
  26. dir.xy *= rot2;
  27. vec3 from = vec3(1., .5, 0.5);
  28. from += vec3(-2., localTime*2., localTime);
  29. from.xz *= rot1;
  30. from.xy *= rot2;
  31. //volumetric rendering
  32. float s = 0.1, fade = 1.;
  33. vec3 v = vec3(0.);
  34. for (int r = 0; r < volsteps; r++) {
  35. vec3 p = from + s*dir*.5;
  36. p = abs(vec3(tile) - mod(p, vec3(tile*2.))); // tiling fold
  37. float pa, a = pa = 0.;
  38. for (int i = 0; i < iterations; i++) {
  39. p = abs(p) / dot(p, p) - formuparam; // the magic formula
  40. a += abs(length(p) - pa); // absolute sum of average change
  41. pa = length(p);
  42. }
  43. float dm = max(0., darkmatter - a*a*.001); //dark matter
  44. a *= a*a; // add contrast
  45. if (r > 6) fade *= 1. - dm; // dark matter, don't render near
  46. //v+=vec3(dm,dm*.5,0.);
  47. v += fade;
  48. v += vec3(s, s*s, s*s*s*s)*a*brightness*fade; // coloring based on distance
  49. fade *= distfading; // distance fading
  50. s += stepsize;
  51. }
  52. v = mix(vec3(length(v)), v, saturation); //color adjust
  53. gl_FragColor = vec4(v*.01, 1.);
  54. }