Advertisement
Guest User

GLSL CG Shader

a guest
Aug 1st, 2014
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. Shader "Custom/Stars" {
  2. Properties {
  3. iterations("_iterations", float) = 17
  4. formuparam("formuparam", float) = 0.53
  5.  
  6. volsteps("volsteps",float) = 20
  7. stepsize("stepsize", float) = 0.1
  8.  
  9. zoom("zoom", float) = 0.800
  10. tile("tile", float) = 0.850
  11. speed("speed", float) = 0.010
  12.  
  13. brightness("brightness", float) = 0.00015
  14. darkmatter("darkmatter", float) = 0.30
  15. distfading("distfading", float) = 0.70
  16. saturation("saturation", float) = 0.80
  17. mouse("mouse",Vector) = (0., 0., 0., 0.)
  18. resolution("resolution",Vector) = (1280, 720, 0., 0.)
  19. }
  20. SubShader {
  21. Pass {
  22. CGPROGRAM
  23.  
  24. #pragma vertex vert
  25. #pragma fragment frag
  26.  
  27. #include "UnityCG.cginc"
  28.  
  29.  
  30. // uniforms corresponding to properties
  31. uniform float iterations;
  32. uniform float formuparam;
  33.  
  34. uniform float volsteps;
  35. uniform float stepsize;
  36.  
  37. uniform float zoom;
  38. uniform float tile;
  39. uniform float speed;
  40.  
  41. uniform float brightness;
  42. uniform float darkmatter;
  43. uniform float distfading;
  44. uniform float saturation;
  45. uniform float2 mouse;
  46. uniform float2 resolution;
  47.  
  48. float4 vert(appdata_base v) : POSITION {
  49. return mul (UNITY_MATRIX_MVP, v.vertex);
  50. }
  51.  
  52.  
  53. float4 frag(float4 sp:WPOS) : COLOR {
  54. //get coords and direction
  55. float2 uv = sp.xy/resolution.xy-.5;
  56. uv.y*=resolution.y/resolution.x;
  57. float3 dir = float3(uv*zoom,1.);
  58. float time=_Time*speed+.25;
  59.  
  60. //mouse rotation
  61. float a1=.5+mouse.x/resolution.x*2.;
  62. float a2=.8+mouse.y/resolution.y*2.;
  63. float2x2 rot1 = float2x2(cos(a1),sin(a1),-sin(a1),cos(a1));
  64. float2x2 rot2 = float2x2(cos(a2),sin(a2),-sin(a2),cos(a2));
  65. dir.xz=mul(rot1, dir.xz);
  66. dir.xy=mul(rot2, dir.xy);
  67. float3 from=float3(1.,.5,0.5);
  68. from+=float3(time*2.,time,-2.);
  69. from.xz=mul(rot1,from.xz);
  70. from.xy=mul(rot2,from.xy);
  71.  
  72. //volumetric rendering
  73. float s=0.1,fade=1.;
  74. float3 v=float3(0,0,0);
  75. for (int r=0; r<volsteps; r++) {
  76. float3 p=from+s*dir*.5;
  77. p = abs(float3(tile,0, 0)-fmod(p,float3(tile*2,0,0))); // tiling fold
  78. float pa,a=pa=0.;
  79. for (int i=0; i<iterations; i++) {
  80. p=abs(p)/dot(p,p)-formuparam; // the magic formula
  81. a+=abs(length(p)-pa); // absolute sum of average change
  82. pa=length(p);
  83. }
  84. float dm=max(0.,darkmatter-a*a*.001); //dark matter
  85. a*=a*a; // add contrast
  86. if (r>6) fade*=1.-dm; // dark matter, don't render near
  87. //v+=vec3(dm,dm*.5,0.);
  88. v+=fade;
  89. v+=float3(s,s*s,s*s*s*s)*a*brightness*fade; // coloring based on distance
  90. fade*=distfading; // distance fading
  91. s+=stepsize;
  92. }
  93. v= v * saturation + length(v) * (1.0 - saturation);//mix(float3(length(v)),v,saturation); //color adjust
  94. return float4(v*.01,1.);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement