Skip to content

Instantly share code, notes, and snippets.

@kamyker
Created April 18, 2020 10:25
Show Gist options
  • Save kamyker/fc0ac80a2aea421550559fd5f2518e8e to your computer and use it in GitHub Desktop.
Save kamyker/fc0ac80a2aea421550559fd5f2518e8e to your computer and use it in GitHub Desktop.
Apollonian by iq/2013 modified by kamyker for https://twitter.com/kamyker/status/1250900430495899648
Shader "Unlit/ShaderToyVR"
{
Properties
{
_MainTex("Texture", 2D) = "white" {}
_test("Float", Range(0.45, 0.55)) = 0.5
_test2("Float", Range(-2, 3)) = -1
_test3("Float", Range(-2, 30)) = 8
_Brightness("_Brightness", Range(0, 1)) = 1
_BrightnessColor("_BrightnessColor", Range(0, 1)) = 1
}
SubShader
{
Tags { "RenderType" = "Background" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fog
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
};
struct v2f
{
UNITY_FOG_COORDS(1)
float4 vertex : SV_POSITION;
float3 Point : TEXCOORD1;
};
sampler2D _MainTex;
float4 _MainTex_ST;
float _test;
float _test2;
float _test3;
float _Brightness;
float _BrightnessColor;
v2f vert(appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.Point = mul(unity_ObjectToWorld, v.vertex);
UNITY_TRANSFER_FOG(o,o.vertex);
return o;
}
// Created by inigo quilez - iq/2013 modified by kamyker 2020
// License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.
// Antialiasing level
#if HW_PERFORMANCE==0
#define AA 1
#else
#define AA 2 // Make it 3 if you have a fast machine
#endif
float4 orb;
float map(float3 p, float s)
{
float scale = 1.0;
orb = 1000.0;
for (int i = 0; i < 8 ; i++)
{
p = -1.0 + 2.0 * frac(0.5 * p + 0.5 );
float r2 = dot(p, p);
orb = min(orb, float4(abs(p), r2));
float k = s / r2;
p *= k;
scale *= k;
}
return 0.25 * abs(p.y) / scale;
}
float trace(in float3 ro, in float3 rd, float s)
{
float maxd = 30.0 ;/*cutout world ege*/
float t = 0.01 ;
for (int i = 0; i < 200 ; i++)
{
float precis = 0.001 * t;
float h = map(ro + rd * t, s);
if (h<precis || t>maxd) break;
t += h;
}
if (t > maxd) t = -1.0;
return t;
}
float3 calcNormal(in float3 pos, in float t, in float s)
{
float precis = 0.001 * t;
float2 e = float2(1.0, -1.0) * precis;
return normalize(e.xyy * map(pos + e.xyy, s * (1 + _SinTime.y)) +
e.yyx * map(pos + e.yyx, s * (1 + _CosTime.y)) +
e.yxy * map(pos + e.yxy, s * (1 + _SinTime.y)) +
e.xxx * map(pos + e.xxx, s ));
}
float3 render(in float3 ro, in float3 rd, in float anim)
{
// trace
float3 col = 0.0;
float t = trace(ro, rd, anim);
if (t > 0.0)
{
float4 tra = orb;
float3 pos = ro + t * rd;
float3 nor = calcNormal(pos, t, anim);
// lighting
float3 light1 = float3(0.577, 0.577, -0.577);
float3 light2 = float3(-0.707, 0.000, 0.707);
float key = clamp(dot(light1, nor) , 0.0, 1.0);
float bac = clamp(0.2 + 0.8 * dot(light2, nor), 0.0, 1.0);
float amb = (0.7 + 0.3 * nor.y);
float ao = pow(clamp(tra.w * 2.0, 0.0, 1.0), 1.2);
float3 brdf = 1.0 * float3(0.40, 0.40, 0.40) * amb * ao;
brdf += 1.0 * float3(1.00, 1.00, 1.00) * key * ao;
brdf += 1.0 * float3(0.40, 0.40, 0.40) * bac * ao;
// material
float3 rgb = 0.0001;
rgb = lerp(rgb, float3(0.5, 0.3, 0.5), clamp(6.0 * tra.y, 0.0, 1.0));
rgb = lerp(rgb, float3(0.1, 20 * _BrightnessColor, 0.0), pow(clamp(1.0 - 2.0 * tra.z, 0.0, 1.0), 8.0));
// color
col = rgb * brdf * exp(-0.2 * t) * _Brightness;
}
return sqrt(col);
}
fixed4 frag(v2f i) : SV_Target
{
float time = _Time.y * 0.25 + 0.01 * _Time.y;
float anim = 1.1 + 0.5 * smoothstep(-0.3, 0.3, cos(0.1 * _Time.y));
float3 cam = _WorldSpaceCameraPos.xyz * 0.1 + float3(1, 0, 0);
float3 fragRayOri = cam;
float3 fragRayDir = normalize(i.Point - cam);
//float3 col = render(fragRayOri + float3(1, 0, _Time.y * 0.2), fragRayDir, anim);
float3 col = render(fragRayOri, fragRayDir , anim);
return float4(col, 1.0);
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment