Search Unity

Help with ShaderToy conversion

Discussion in 'Shaders' started by mite51, Mar 15, 2019.

  1. mite51

    mite51

    Joined:
    Jun 25, 2015
    Posts:
    25
    I've converted some simpler shaders from shader toys, but I've having a bit of an issue with this one.
    https://www.shadertoy.com/view/wdsSWs
    It renders something, but it doesn't quite look like the output on shader toys. I've looked it over and over, twiddled with it but I'm having a hard time figuring out what the problem might be. Does anyone have any ideas? I appreciate any help.
     

    Attached Files:

  2. Przemyslaw_Zaworski

    Przemyslaw_Zaworski

    Joined:
    Jun 9, 2017
    Posts:
    328
    Solution:

    Code (CSharp):
    1. Shader "CubeSingularity"
    2. {
    3.     SubShader
    4.     {
    5.         Pass
    6.         {
    7.             CGPROGRAM
    8.             #pragma vertex VSMain
    9.             #pragma fragment PSMain
    10.             #define AA 2
    11.             #define HEIGHT 0.25
    12.             #define M_PI 3.1415926535897932384626433832795
    13.             #define LONGSTEP (M_PI*4.)
    14.  
    15.             float gTime;
    16.             float gABPos;
    17.             float gDensA;
    18.             float gDensB;
    19.  
    20.             float2 mod(float2 x, float2 y)
    21.             {
    22.                 return x - y * floor(x/y);
    23.             }
    24.  
    25.             float sdCube( float3 p, float b )
    26.             {
    27.                 float3 d = abs(p) - b;
    28.                 return min(max(d.x,max(d.y,d.z)),0.0) + length(max(d,0.0));
    29.             }
    30.  
    31.             void pR(inout float2 p, float a)
    32.             {
    33.                 p = cos(a)*p + sin(a)*float2(p.y, -p.x);
    34.             }
    35.  
    36.             float sdSpike2D(float2 p, float h)
    37.             {
    38.                 float d = p.y - (h*0.1)/(abs(p.x)+0.1);
    39.                 d = min(d, length(p - float2(0, min(h, p.y))));
    40.                 float d2 = abs(p.x) - ((h*0.1)-0.1*p.y)/p.y;
    41.                 if (p.y<h && d>0.0)
    42.                     d = min(d, d2);
    43.                 return d;
    44.             }
    45.  
    46.             float3 tile(in float4 pin, out float density, out float cubsz)
    47.             {
    48.                 float r = pin.w;
    49.                 density = lerp(gDensA, gDensB, smoothstep(0., 0.1, r-gABPos));
    50.                 float3 p = float3(log(r), (pin.y-HEIGHT*0.1/(r+0.1))/r, atan2(pin.z, pin.x));
    51.                 p *= density;
    52.                 p.x -= gTime*2.0;
    53.                 pR(p.xz, 0.6435);
    54.                 p.xz = mod(p.xz, 2.0) - 1.0;
    55.                 float osc = sin(sqrt(r)-gTime*0.25-1.0);
    56.                 float cubrot = smoothstep(0.5, 0.8, osc);
    57.                 cubsz = sin(p.x*0.1)*0.29 + 0.5;
    58.                 cubsz = lerp(cubsz, 0.96, smoothstep(0.7, 1.0, abs(osc)));
    59.                 pR(p.xy, cubrot);
    60.                 return p;
    61.             }
    62.  
    63.             float sdf(in float3 pin)
    64.             {
    65.                 float r = length(pin.xz);
    66.                 float cubsz, density; // out
    67.                 float3 tiled = tile(float4(pin, r), density, cubsz);
    68.                 float ret = sdCube(tiled, cubsz);
    69.                 ret *= r/density;
    70.                 float pkofs = r * cubsz / density;
    71.                 float pk = sdSpike2D(float2(r, pin.y), HEIGHT) - pkofs;
    72.                 if (pk < 0.002) pk = ret;
    73.                 ret = min(ret, pk);
    74.                 float shorten = length(pin - float3(0., 0.25, 0.));
    75.                 shorten = 1. + 1.5*(1.-smoothstep(0., 0.22, shorten));
    76.                 ret /= shorten;
    77.                 return ret;
    78.             }
    79.  
    80.             float3 colr(in float3 pin)
    81.             {
    82.                 float a = 0.26;
    83.                 float b = 0.65;
    84.                 float z = 0.19;
    85.                 float cubsz, density; // out
    86.                 float3 p = tile(float4(pin, length(pin.xz)), density, cubsz);
    87.                 if (p.x > abs(p.y) && p.x > abs(p.z)) return float3(z,a,b);
    88.                 if (p.x < -abs(p.y) && p.x < -abs(p.z)) return float3(z,b,a)*0.7;
    89.                 if (p.z > abs(p.x) && p.z > abs(p.y)) return float3(z,a,a);
    90.                 if (p.z < -abs(p.x) && p.z < -abs(p.y)) return float3(b*0.5,z,a);
    91.                 return float3(b,b,a);
    92.             }
    93.  
    94.             float3 calcNormal(in float3 pos)
    95.             {
    96.                 float2 e = float2(1.0,-1.0)*0.5773;
    97.                 const float eps = 0.0005;
    98.                 return normalize(
    99.                     e.xyy*sdf(pos + e.xyy*eps) +
    100.                     e.yyx*sdf(pos + e.yyx*eps) +
    101.                     e.yxy*sdf(pos + e.yxy*eps) +
    102.                     e.xxx*sdf(pos + e.xxx*eps)
    103.                 );
    104.             }
    105.  
    106.             float time2density(float x)
    107.             {
    108.                 float fullMod = frac(x/(LONGSTEP*3.))*3.;
    109.                 if (fullMod > 2.) return 45.;
    110.                 else if (fullMod > 1.) return 25.;
    111.                 else return 15.;
    112.             }
    113.        
    114.             void VSMain (inout float4 vertex:POSITION,inout float2 uv:TEXCOORD0)
    115.             {
    116.                 vertex = UnityObjectToClipPos(vertex);
    117.             }
    118.        
    119.             void PSMain (float4 vertex:POSITION,float2 uv:TEXCOORD0, out float4 fragColor:SV_TARGET)
    120.             {
    121.                 float2 iResolution = float2(1024,1024);
    122.                 float2 fragCoord = uv * iResolution;
    123.                 gTime = _Time.g+1.8;
    124.                 float ltime = gTime + M_PI*6.3;
    125.                 gABPos = smoothstep(0.45, 0.6, frac(ltime/LONGSTEP))*2.2-0.2;
    126.                 gDensA = floor(time2density(ltime))/M_PI;
    127.                 gDensB = floor(time2density(ltime-LONGSTEP))/M_PI;
    128.                 float camera_y = pow(sin(gTime*0.2), 3.)*0.2+0.7;
    129.                 float3 ro = float3(0., camera_y, 1.);
    130.                 float3 ta = float3(0.0, 0.0, 0.0);
    131.                 float3 ww = normalize(ta - ro);
    132.                 float3 uu = normalize(cross(ww,float3(0.0,1.0,0.0)));
    133.                 float3 vv = normalize(cross(uu,ww));
    134.                 float3 tot = 0..xxx;  
    135.                 #if AA>1
    136.                 for(int m=0; m<AA; m++)
    137.                 for(int n=0; n<AA; n++)
    138.                 {
    139.                     float2 o = float2(float(m),float(n)) / float(AA) - 0.5;
    140.                     float2 p = (-iResolution.xy + 2.0*(fragCoord+o))/iResolution.y;
    141.                     #else  
    142.                     float2 p = (-iRes.xy + 2.0*fragCoord)/iRes.y;
    143.                     #endif
    144.                     float3 rd = normalize(p.x*uu + p.y*vv + 3.5*ww); // fov
    145.                     const float tmax = 3.0;
    146.                     float t = 0.0;
    147.                     for(int i=0; i<256; i++)
    148.                     {
    149.                         float3 pos = ro + t*rd;
    150.                         float h = sdf(pos);
    151.                         if( h<0.0001 || t>tmax ) break;
    152.                         t += h;
    153.                     }  
    154.                     float3 bg = float3(0.1, 0.15, 0.2)*0.3;
    155.                     float3 col = bg;
    156.                     if(t<tmax)
    157.                     {
    158.                         float3 pos = ro + t*rd;
    159.                         float3 nor = calcNormal(pos);
    160.                         float dif = clamp( dot(nor,float3(0.57703, 0.57703, 0.57703)), 0.0, 1.0 );
    161.                         float amb = 0.5 + 0.5*dot(nor,float3(0.0,1.0,0.0));
    162.                         col = colr(pos)*amb + colr(pos)*dif;
    163.                     }
    164.                     col = lerp(col, bg, smoothstep(2., 3., t));      
    165.                     col = sqrt(col);
    166.                     tot += col;
    167.                 #if AA>1
    168.                 }
    169.                 tot /= float(AA*AA);
    170.                 #endif
    171.                 fragColor = float4(tot,1.0);
    172.             }
    173.             ENDCG
    174.         }
    175.     }
    176. }

    upload_2019-3-15_12-6-43.png
     
    konsic likes this.
  3. konsic

    konsic

    Joined:
    Oct 19, 2015
    Posts:
    995
    How to convert this in SRP shader HLSL ?
     
  4. mite51

    mite51

    Joined:
    Jun 25, 2015
    Posts:
    25
    Awesome Przemuslaw! Thank you very much... Now I can reverse engineer what I did wrong ;)