smkplus / ShaderMan

Convert ShaderToy to Unity HLSL/CG
https://smkplus.github.io/ShaderMan.io
MIT License
1.42k stars 197 forks source link

Always an error no matter what (Token and TOK and more #39

Open VirtualyMichael opened 3 years ago

VirtualyMichael commented 3 years ago

This is what it made you should be able to find the original link in it:

Shader "ShaderMan/CloudyCrystal" {

Properties{
//Properties
}

SubShader
{
Tags { "RenderType" = "Transparent" "Queue" = "Transparent" }

Pass
{
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha

CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"

struct VertexInput {
fixed4 vertex : POSITION;
fixed2 uv:TEXCOORD0;
fixed4 tangent : TANGENT;
fixed3 normal : NORMAL;
//VertexInput
};

struct VertexOutput {
fixed4 pos : SV_POSITION;
fixed2 uv:TEXCOORD0;
//VertexOutput
};

//Variables

// License CC0: Cloudy crystal

// Late to the party I discovered: https://www.shadertoy.com/view/MtX3Ws // Played around with it for a bit and thought it looked quite nice so I shared

define TIME _Time.y

define RESOLUTION 1

define ROT(a) fixed2x2(cos(a), sin(a), -sin(a), cos(a))

define PI 3.141592654

define TAU (2.0*PI)

define L2(x) dot(x, x)

define RAYSHAPE(ro, rd) raySphere4(ro, rd, 0.5)

define IRAYSHAPE(ro, rd) iraySphere4(ro, rd, 0.5)

const fixed miss = 1E4; const fixed refrIndex = 0.85; const fixed3 lightPos = 2.0fixed3(1.5, 2.0, 1.0); const fixed3 skyCol1 = pow(fixed3(0.2, 0.4, 0.6), fixed3(0.25,0.25,0.25))1.0; const fixed3 skyCol2 = pow(fixed3(0.4, 0.7, 1.0), fixed3(2.0,2.0,2.0))*1.0; const fixed3 sunCol = fixed3(8.0,7.0,6.0)/8.0;

fixed tanh_approx(fixed x) { // return tanh(x); fixed x2 = xx; return clamp(x(27.0 + x2)/(27.0+9.0*x2), -1.0, 1.0); }

// https://stackoverflow.com/questions/15095909/from-rgb-to-hsv-in-opengl-glsl fixed3 hsv2rgb(fixed3 c) { const fixed4 K = fixed4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0); fixed3 p = abs(frac(c.xxx + K.xyz) 6.0 - K.www); return c.z lerp(K.xxx, clamp(p - K.xxx, 0.0, 1.0), c.y); }

// Various ray object intersection from IQ: // https://www.iquilezles.org/www/articles/intersectors/intersectors.htm fixed raySphere4(fixed3 ro, fixed3 rd, fixed ra) { fixed r2 = rara; fixed3 d2 = rdrd; fixed3 d3 = d2rd; fixed3 o2 = roro; fixed3 o3 = o2ro; fixed ka = 1.0/dot(d2,d2); fixed k3 = ka dot(ro,d3); fixed k2 = ka dot(o2,d2); fixed k1 = ka dot(o3,rd); fixed k0 = ka(dot(o2,o2) - r2r2); fixed c2 = k2 - k3k3; fixed c1 = k1 + 2.0k3k3k3 - 3.0k3k2; fixed c0 = k0 - 3.0k3k3k3k3 + 6.0k3k3k2 - 4.0k3k1; fixed p = c2c2 + c0/3.0; fixed q = c2c2c2 - c2c0 + c1c1; fixed h = qq - ppp; if (h<0.0) return miss; //no intersection fixed sh = sqrt(h); fixed s = sign(q+sh)pow(abs(q+sh),1.0/3.0); // cuberoot fixed t = sign(q-sh)pow(abs(q-sh),1.0/3.0); // cuberoot fixed2 w = fixed2( s+t,s-t ); fixed2 v = fixed2( w.x+c24.0, w.ysqrt(3.0) )0.5; fixed r = length(v); return -abs(v.y)/sqrt(r+v.x) - c1/r - k3; }

fixed3 sphere4Normal(fixed3 pos) { return normalize( pospospos ); }

fixed iraySphere4(fixed3 ro, fixed3 rd, fixed ra) { // Computes inner intersection by intersecting a reverse outer intersection fixed3 rro = ro + rdra4.0; fixed3 rrd = -rd; fixed rt = raySphere4(rro, rrd, ra);

if (rt == miss) return miss;

fixed3 rpos = rro + rrd*rt; return length(rpos - ro); }

fixed rayPlane(fixed3 ro, fixed3 rd, fixed4 p ) { return -(dot(ro,p.xyz)+p.w)/dot(rd,p.xyz); }

fixed3 skyColor(fixed3 ro, fixed3 rd) { const fixed3 sunDir = normalize(lightPos); fixed sunDot = max(dot(rd, sunDir), 0.0);
fixed3 final = fixed3(0.,0.,0.);

final += lerp(skyCol1, skyCol2, rd.y); final += 0.5sunColpow(sunDot, 20.0); final += 4.0sunColpow(sunDot, 400.0);

fixed tp = rayPlane(ro, rd, fixed4(fixed3(0.0, 1.0, 0.0), 0.505)); if (tp > 0.0) { fixed3 pos = ro + tprd; fixed3 ld = normalize(lightPos - pos); fixed ts4 = RAYSHAPE(pos, ld); fixed3 spos = pos + ldts4; fixed its4= IRAYSHAPE(spos, ld); // Extremely fake soft shadows fixed sha = ts4 == miss ? 1.0 : (1.0-1.0tanh_approx(its41.5/(0.5+.5ts4))); fixed3 nor = fixed3(0.0, 1.0, 0.0); fixed3 icol = 1.5skyCol1 + 4.0sunColshadot(-rd, nor); fixed2 ppos = pos.xz0.75; ppos = frac(ppos+0.5)-0.5; fixed pd = min(abs(ppos.x), abs(ppos.y)); fixed3 pcol= lerp(fixed3(0.4,0.4,0.4), fixed3(0.3,0.3,0.3), exp(-60.0*pd));

fixed3 col  = icol*pcol;
col = clamp(col, 0.0, 1.25);
fixed f   = exp(-10.0*(max(tp-10.0, 0.0) / 100.0));
return lerp(final, col , f);

} else{ return final; } }

// Marble fracal from https://www.shadertoy.com/view/MtX3Ws fixed2 cmul(fixed2 a, fixed2 b) { return fixed2(a.xb.x - a.yb.y, a.xb.y + a.yb.x); }

fixed2 csqr(fixed2 a) { return fixed2(a.xa.x - a.ya.y, 2.a.xa.y); }

fixed marble_df(fixed3 p) {
fixed res = 0.;

fixed3 c = p; fixed scale = 0.72; const int max_iter = 10; for (int i = 0; i < max_iter; ++i) { p = scaleabs(p)/dot(p,p) - scale; p.yz = csqr(p.yz); p = p.zxy; res += exp(-19. abs(dot(p,c))); } return res; }

fixed3 marble_march(fixed3 ro, fixed3 rd, fixed2 tminmax) { fixed t = tminmax.x; fixed dt = 0.02; fixed3 col = fixed3(0.0,0.0,0.0); fixed c = 0.; const int max_iter = 64; [unroll(100)] for(int i = 0; i < max_iter; ++i) { t += dtexp(-2.0c); if(t>tminmax.y) { break; } fixed3 pos = ro+t*rd;

  c = marble_df(ro+t*rd); 
  c *= 0.5;

  fixed dist = (abs(pos.x + pos.y-0.15))*10.0;
  fixed3 dcol = fixed3(c*c*c-c*dist, c*c-c, c);
  col = col + dcol;

}
const fixed scale = 0.005; fixed td = (t - tminmax.x)/(tminmax.y - tminmax.x); col = exp(-10.0td); col *= scale; return col; }

fixed3 render1(fixed3 ro, fixed3 rd) { fixed3 ipos = ro; fixed3 ird = rd;

fixed its4 = IRAYSHAPE(ipos, ird); return marble_march(ipos, ird, fixed2(0.0, its4)); }

fixed3 render(fixed3 ro, fixed3 rd) { fixed3 skyCol = skyColor(ro, rd); fixed3 col = fixed3(0.0,0.0,0.0);

fixed t = 1E6; fixed ts4 = RAYSHAPE(ro, rd); if (ts4 < miss) { t = ts4; fixed3 pos = ro + ts4*rd; fixed3 nor = sphere4Normal(pos); fixed3 refr = refract(rd, nor, refrIndex); fixed3 refl = reflect(rd, nor); fixed3 rcol = skyColor(pos, refl); fixed fre = lerp(0.0, 1.0, pow(1.0-dot(-rd, nor), 4.0));

fixed3 lv   = lightPos - pos;
fixed ll2 = L2(lv);
fixed ll  = sqrt(ll2);
fixed3 ld   = lv / ll;

fixed dm  = min(1.0, 40.0/ll2);
fixed dif = pow(max(dot(nor,ld),0.0), 8.0)*dm;
fixed spe = pow(max(dot(reflect(-ld, nor), -rd), 0.), 100.);
fixed l   = dif;

fixed lin = lerp(0.0, 1.0, l);
const fixed3 lcol = 2.0*sqrt(sunCol);
col = render1(pos, refr);
fixed3 diff = hsv2rgb(fixed3(0.7, fre, 0.075*lin))*lcol;
col += fre*rcol+diff+spe*lcol;
if (refr == fixed3(0.0,0.0,0.0),0.0),0.0)) {
  // Not expected to happen as the refraction index < 1.0
  col = fixed3(1.0, 0.0, 0.0);
}

} else { // Ray intersected sky return skyCol; }

return col; }

fixed3 effect(fixed2 p, fixed2 q) { fixed3 ro = 0.6fixed3(2.0, 0, 0.2)+fixed3(0.0, 0.75, 0.0); ro.xz = ROT(PI/2.0+sin(TIME0.05)); ro.yz = ROT(0.5+0.25sin(TIME0.05sqrt(0.5))0.5);

fixed3 ww = normalize(fixed3(0.0, 0.0, 0.0) - ro); fixed3 uu = normalize(cross( fixed3(0.0,1.0,0.0), ww)); fixed3 vv = normalize(cross(ww,uu)); fixed rdd = 2.0; fixed3 rd = normalize( p.xuu + p.yvv + rdd*ww);

fixed3 col = render(ro, rd); return col; }

fixed3 postProcess(fixed3 col, fixed2 q) { col = clamp(col, 0.0, 1.0); col = pow(col, fixed3(1.0/2.2,1.0/2.2,1.0/2.2)); col = col0.6+0.4colcol(3.0-2.0col); col = lerp(col, fixed3(dot(col, fixed3(0.33,0.33,0.33))), -0.4); col =0.5+0.5pow(19.0q.xq.y(1.0-q.x)*(1.0-q.y),0.7); return col; }

VertexOutput vert (VertexInput v)
{
VertexOutput o;
o.pos = UnityObjectToClipPos (v.vertex);
o.uv = v.uv;
//VertexFactory
return o;
}
fixed4 frag(VertexOutput i) : SV_Target
{

fixed2 q = i.uv/RESOLUTION.xy; fixed2 p = -1. + 2. q; p.x = RESOLUTION.x/RESOLUTION.y;

fixed3 col = effect(p, q); col = postProcess(col, q);

return fixed4(col, 1.0);

}
ENDCG
}

} }