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.
Solution: Code (CSharp): Shader "CubeSingularity" { SubShader { Pass { CGPROGRAM #pragma vertex VSMain #pragma fragment PSMain #define AA 2 #define HEIGHT 0.25 #define M_PI 3.1415926535897932384626433832795 #define LONGSTEP (M_PI*4.) float gTime; float gABPos; float gDensA; float gDensB; float2 mod(float2 x, float2 y) { return x - y * floor(x/y); } float sdCube( float3 p, float b ) { float3 d = abs(p) - b; return min(max(d.x,max(d.y,d.z)),0.0) + length(max(d,0.0)); } void pR(inout float2 p, float a) { p = cos(a)*p + sin(a)*float2(p.y, -p.x); } float sdSpike2D(float2 p, float h) { float d = p.y - (h*0.1)/(abs(p.x)+0.1); d = min(d, length(p - float2(0, min(h, p.y)))); float d2 = abs(p.x) - ((h*0.1)-0.1*p.y)/p.y; if (p.y<h && d>0.0) d = min(d, d2); return d; } float3 tile(in float4 pin, out float density, out float cubsz) { float r = pin.w; density = lerp(gDensA, gDensB, smoothstep(0., 0.1, r-gABPos)); float3 p = float3(log(r), (pin.y-HEIGHT*0.1/(r+0.1))/r, atan2(pin.z, pin.x)); p *= density; p.x -= gTime*2.0; pR(p.xz, 0.6435); p.xz = mod(p.xz, 2.0) - 1.0; float osc = sin(sqrt(r)-gTime*0.25-1.0); float cubrot = smoothstep(0.5, 0.8, osc); cubsz = sin(p.x*0.1)*0.29 + 0.5; cubsz = lerp(cubsz, 0.96, smoothstep(0.7, 1.0, abs(osc))); pR(p.xy, cubrot); return p; } float sdf(in float3 pin) { float r = length(pin.xz); float cubsz, density; // out float3 tiled = tile(float4(pin, r), density, cubsz); float ret = sdCube(tiled, cubsz); ret *= r/density; float pkofs = r * cubsz / density; float pk = sdSpike2D(float2(r, pin.y), HEIGHT) - pkofs; if (pk < 0.002) pk = ret; ret = min(ret, pk); float shorten = length(pin - float3(0., 0.25, 0.)); shorten = 1. + 1.5*(1.-smoothstep(0., 0.22, shorten)); ret /= shorten; return ret; } float3 colr(in float3 pin) { float a = 0.26; float b = 0.65; float z = 0.19; float cubsz, density; // out float3 p = tile(float4(pin, length(pin.xz)), density, cubsz); if (p.x > abs(p.y) && p.x > abs(p.z)) return float3(z,a,b); if (p.x < -abs(p.y) && p.x < -abs(p.z)) return float3(z,b,a)*0.7; if (p.z > abs(p.x) && p.z > abs(p.y)) return float3(z,a,a); if (p.z < -abs(p.x) && p.z < -abs(p.y)) return float3(b*0.5,z,a); return float3(b,b,a); } float3 calcNormal(in float3 pos) { float2 e = float2(1.0,-1.0)*0.5773; const float eps = 0.0005; return normalize( e.xyy*sdf(pos + e.xyy*eps) + e.yyx*sdf(pos + e.yyx*eps) + e.yxy*sdf(pos + e.yxy*eps) + e.xxx*sdf(pos + e.xxx*eps) ); } float time2density(float x) { float fullMod = frac(x/(LONGSTEP*3.))*3.; if (fullMod > 2.) return 45.; else if (fullMod > 1.) return 25.; else return 15.; } void VSMain (inout float4 vertex:POSITION,inout float2 uv:TEXCOORD0) { vertex = UnityObjectToClipPos(vertex); } void PSMain (float4 vertex:POSITION,float2 uv:TEXCOORD0, out float4 fragColor:SV_TARGET) { float2 iResolution = float2(1024,1024); float2 fragCoord = uv * iResolution; gTime = _Time.g+1.8; float ltime = gTime + M_PI*6.3; gABPos = smoothstep(0.45, 0.6, frac(ltime/LONGSTEP))*2.2-0.2; gDensA = floor(time2density(ltime))/M_PI; gDensB = floor(time2density(ltime-LONGSTEP))/M_PI; float camera_y = pow(sin(gTime*0.2), 3.)*0.2+0.7; float3 ro = float3(0., camera_y, 1.); float3 ta = float3(0.0, 0.0, 0.0); float3 ww = normalize(ta - ro); float3 uu = normalize(cross(ww,float3(0.0,1.0,0.0))); float3 vv = normalize(cross(uu,ww)); float3 tot = 0..xxx; #if AA>1 for(int m=0; m<AA; m++) for(int n=0; n<AA; n++) { float2 o = float2(float(m),float(n)) / float(AA) - 0.5; float2 p = (-iResolution.xy + 2.0*(fragCoord+o))/iResolution.y; #else float2 p = (-iRes.xy + 2.0*fragCoord)/iRes.y; #endif float3 rd = normalize(p.x*uu + p.y*vv + 3.5*ww); // fov const float tmax = 3.0; float t = 0.0; for(int i=0; i<256; i++) { float3 pos = ro + t*rd; float h = sdf(pos); if( h<0.0001 || t>tmax ) break; t += h; } float3 bg = float3(0.1, 0.15, 0.2)*0.3; float3 col = bg; if(t<tmax) { float3 pos = ro + t*rd; float3 nor = calcNormal(pos); float dif = clamp( dot(nor,float3(0.57703, 0.57703, 0.57703)), 0.0, 1.0 ); float amb = 0.5 + 0.5*dot(nor,float3(0.0,1.0,0.0)); col = colr(pos)*amb + colr(pos)*dif; } col = lerp(col, bg, smoothstep(2., 3., t)); col = sqrt(col); tot += col; #if AA>1 } tot /= float(AA*AA); #endif fragColor = float4(tot,1.0); } ENDCG } } }