starfieldProceduralTexture.fragment.fx 1.5 KB

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