Shadertoy XssGRs to HLSL
https://gyazo.com/297ef7ecaf8787c412bd6ed94660ece8
code:HLSL
Shader "Unlit/XssGRs"
{
Properties
{
_MainTex("Texture", 2D) = "white" {}
}
SubShader
{
Tags { "RenderType" = "Opaque" }
LOD 100
Cull Off
Pass
{
CGPROGRAM
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
};
sampler2D _MainTex;
float4 _MainTex_ST;
v2f vert(appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
}
float maxcomp(float2 v) { return max(v.x, v.y); }
float mod(float x, float y)
{
return x - y * floor(x / y);
}
float2 mod(float2 x, float2 y)
{
return x - y * floor(x / y);
}
float3 mod(float3 x, float3 y)
{
return x - y * floor(x / y);
}
float4 mod(float4 x, float4 y)
{
return x - y * floor(x / y);
}
float2 rot2D(float2 v, float angle) {
float sinA = sin(angle);
float cosA = cos(angle);
return float2(v.x * cosA - v.y * sinA, v.y * cosA + v.x * sinA);
}
float sdCross(float3 p, float w) {
p = abs(p);
float3 d = float3(max(p.x, p.y),
max(p.y, p.z),
max(p.z, p.x));
return min(d.x, min(d.y, d.z)) - w;
}
float sdCrossRep(float3 p, float w) {
float3 q = step(0.7, sin(_Time.y)) * fmod(p + 1.0, 2.0) + step(sin(_Time.y), 0.7) * mod(p + 1.0, 2.0) - 1.0;
return sdCross(q, w);
}
float sdCrossRepScale(float3 p, float s, float w) {
return sdCrossRep(p * s, w) / s;
}
float scene(float3 p, float t) {
int MENGER_ITERATIONS = 1 + 5 * abs(sin(_Time.y));
float scale = 1.0;
float dist = 0.0;
for (int i = 0; i < MENGER_ITERATIONS; i++) {
dist = max(dist, -sdCrossRepScale(p, scale, 1.0 / 3.0));
scale *= 3.0;
}
dist = max(dist, -sdCrossRepScale(p, scale, pow(t, 0.2) / 3.0));
return dist;
}
float3 hsv2rgb(float3 c)
{
float4 K = float4(1.0, 2.0 / 3.0, 1.0 / 3.0, 3.0);
float3 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);
}
float4 colorize(float c)
{
float hue = lerp(0.8, 1.35, min(c * 1.2 + 0.1 * sin(_Time.y) - 0.05, 1.1));
float sat = 1.0 - pow(c, 4.0);
float lum = c;
float3 hsv = float3(hue, sat, lum);
float3 rgb = hsv2rgb(hsv);
return float4(rgb, 1.0);
}
float3 cameraPath(float t) {
t *= UNITY_PI / 2.0;
return 2.0 / 3.0 * float3(0.0, 1.0 - cos(t), sin(t));
}
fixed4 frag(v2f i) : SV_Target
{
static int MAX_RAY_STEPS = 24;
static float RAY_STOP_THRESHOLD = 0.0001;
float2 screenPos = 2 * i.uv - 1;
float s = mod(_Time.y * 0.25, 1.0);
float t = 0.5 * (3.0 * s - s * s);
float3 cameraPos1 = float3(0.0, 0.0, 0.0);
float3 cameraPos2 = float3(0.0, 2.0 / 3.0, 2.0 / 3.0);
float3 cameraPos = cameraPath(t);
float3 cameraDir = float3(0.0, 0.0, 1.0);
float3 cameraPlaneU = float3(1.0, 0.0, 0.0);
float3 cameraPlaneV = float3(0.0, 1.0, 0.0);
float3 rayPos = cameraPos;
float3 rayDir = cameraDir + screenPos.x * cameraPlaneU + screenPos.y * cameraPlaneV;
rayDir.yz = rot2D(rayDir.yz, (-UNITY_PI / 2.0) * s - UNITY_PI / 12.0);
rayDir = normalize(rayDir);
float rayStopThreshold = RAY_STOP_THRESHOLD * pow(3.0, -t);
rayStopThreshold = lerp(RAY_STOP_THRESHOLD, RAY_STOP_THRESHOLD / 3.0, t);
float dist = scene(rayPos, t);
int stepsTaken;
for (int i = 0; i < MAX_RAY_STEPS; i++) {
if (dist < rayStopThreshold) continue;
rayPos += rayDir * dist * 0.9;
dist = scene(rayPos, t);
stepsTaken = i;
}
return colorize(pow(stepsTaken / float(MAX_RAY_STEPS), 0.475 + 0.475 * sin(_Time.y)));
}
ENDCG
}
}
}