Created
November 11, 2017 13:41
-
-
Save yumayanagisawa/9c8631e4ae557a7699e0ec53a6fcfa56 to your computer and use it in GitHub Desktop.
Shadertoy to Unity | Julia - Quaternion
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// The code is based on the shader on Shadertoy(https://www.shadertoy.com/view/MsfGRr), and tweaked for use in Unity3D | |
// Original shader is... | |
// Created by inigo quilez - iq/2013 | |
// License Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License. | |
Shader "Custom/Procedural" { | |
Properties | |
{ | |
_MainTex("Texture", 2D) = "white" {} | |
} | |
SubShader | |
{ | |
// No culling or depth | |
//Cull Off ZWrite Off ZTest Always | |
Tags{ "Queue" = "Transparent" "RenderType" = "Transparent" }// "Opaque" } | |
LOD 100 | |
ZWrite Off | |
Blend SrcAlpha OneMinusSrcAlpha | |
Pass | |
{ | |
CGPROGRAM | |
#pragma vertex vert_img | |
#pragma fragment frag | |
#include "UnityCG.cginc" | |
#define AA 1 | |
float map(in float3 p, out float4 oTrap, in float4 c) | |
{ | |
float4 z = float4(p,0.0); | |
float md2 = 1.0; | |
float mz2 = dot(z,z); | |
float4 trap = float4(abs(z.xyz),dot(z,z)); | |
for (int i = 0; i<11; i++) | |
{ | |
// |dz|^2 -> 4*|dz|^2 | |
md2 *= 4.0*mz2; | |
// z -> z2 + c | |
z = float4(z.x*z.x - dot(z.yzw,z.yzw), | |
2.0*z.x*z.yzw) + c; | |
trap = min(trap, float4(abs(z.xyz),dot(z,z))); | |
mz2 = dot(z,z); | |
if (mz2>4.0) break; | |
} | |
oTrap = trap; | |
return 0.25*sqrt(mz2 / md2)*log(mz2); | |
} | |
// analytic normal for quadratic formula | |
fixed3 calcNormal(in fixed3 p, in fixed4 c) | |
{ | |
fixed4 z = fixed4(p,0.0); | |
fixed4 dz0 = fixed4(1.0,0.0,0.0,0.0); | |
fixed4 dz1 = fixed4(0.0,1.0,0.0,0.0); | |
fixed4 dz2 = fixed4(0.0,0.0,1.0,0.0); | |
fixed4 dz3 = fixed4(0.0,0.0,0.0,1.0); | |
for (int i = 0; i<11; i++) | |
{ | |
fixed4 mz = fixed4(z.x,-z.y,-z.z,-z.w); | |
// derivative | |
dz0 = fixed4(dot(mz,dz0),z.x*dz0.yzw + dz0.x*z.yzw); | |
dz1 = fixed4(dot(mz,dz1),z.x*dz1.yzw + dz1.x*z.yzw); | |
dz2 = fixed4(dot(mz,dz2),z.x*dz2.yzw + dz2.x*z.yzw); | |
dz3 = fixed4(dot(mz,dz3),z.x*dz3.yzw + dz3.x*z.yzw); | |
z = fixed4(dot(z, mz), 2.0*z.x*z.yzw) + c; | |
if (dot(z,z)>4.0) break; | |
} | |
return normalize(fixed3(dot(z,dz0), | |
dot(z,dz1), | |
dot(z,dz2))); | |
} | |
float intersect(in fixed3 ro, in fixed3 rd, out fixed4 res, in fixed4 c) | |
{ | |
fixed4 tmp; | |
float resT = -1.0; | |
float maxd = 10.0; | |
float h = 1.0; | |
float t = 0.0; | |
for (int i = 0; i<150; i++) | |
{ | |
if (h<0.002 || t>maxd) break; | |
h = map(ro + rd*t, tmp, c); | |
t += h; | |
} | |
if (t<maxd) { resT = t; res = tmp; } | |
return resT; | |
} | |
float softshadow(in fixed3 ro, in fixed3 rd, float mint, float k, in fixed4 c) | |
{ | |
float res = 1.0; | |
float t = mint; | |
for (int i = 0; i<64; i++) | |
{ | |
fixed4 kk; | |
float h = map(ro + rd*t, kk, c); | |
res = min(res, k*h / t); | |
if (res<0.001) break; | |
t += clamp(h, 0.01, 0.5); | |
} | |
return clamp(res,0.0,1.0); | |
} | |
fixed3 render(in fixed3 ro, in fixed3 rd, in fixed4 c) | |
{ | |
fixed3 light1 = fixed3(0.577, 0.577, 0.577); | |
fixed3 light2 = fixed3(-0.707, 0.000, -0.707); | |
fixed4 tra; | |
fixed3 col; | |
float t = intersect(ro, rd, tra, c); | |
if (t < 0.0) | |
{ | |
col = fixed3(0.8,0.9,1.0)*(0.7 + 0.3*rd.y); | |
col += fixed3(0.8,0.7,0.5)*pow(clamp(dot(rd,light1),0.0,1.0), 48.0); | |
} | |
else | |
{ | |
col = fixed3(1.0,0.8,0.7)*0.3; | |
fixed3 pos = ro + t*rd; | |
fixed3 nor = calcNormal(pos, c); | |
fixed3 ref = reflect(rd, nor); | |
float dif1 = clamp(dot(light1, nor), 0.0, 1.0); | |
float dif2 = clamp(0.5 + 0.5*dot(light2, nor), 0.0, 1.0); | |
float occ = clamp(2.5*tra.w - 0.15,0.0,1.0); | |
float sha = softshadow(pos, light1, 0.001, 64.0, c); | |
float fre = pow(clamp(1. + dot(rd,nor), 0.0, 1.0), 2.0); | |
fixed3 lin = 1.5*fixed3(0.15,0.20,0.25)*(0.6 + 0.4*nor.y)*(0.1 + 0.9*occ); | |
lin += 3.5*fixed3(1.00,0.90,0.70)*dif1*sha; | |
lin += 1.5*fixed3(0.14,0.14,0.14)*dif2*occ; | |
lin += 0.3*fixed3(1.00,0.80,0.60)*fre*(0.5 + 0.5*occ); | |
col *= lin; | |
col += pow(clamp(dot(ref, light1), 0.0, 1.0), 32.0)*dif1*sha; | |
col += 0.1*fixed3(0.8,0.9,1.0)*smoothstep(0.0, 0.1, ref.y)*occ*(0.5 + 0.5*nor.y); | |
} | |
return pow(col, fixed3(0.4545, 0.4545, 0.4545)); | |
} | |
fixed4 frag(v2f_img i) : SV_Target | |
{ | |
fixed2 uv = (i.uv*_ScreenParams.xy) / _ScreenParams.xy; | |
// anim | |
float time = _Time.y*.15; | |
fixed4 c = 0.4*cos(fixed4(0.5,3.9,1.4,1.1) + time*fixed4(1.2,1.7,1.3,2.5)) - fixed4(0.3,0.0,0.0,0.0); | |
// camera | |
float r = 1.4 + 0.15*cos(0.0 + 0.29*time); | |
fixed3 ro = fixed3(r*cos(0.3 + 0.37*time), 0.3 + 0.8*r*cos(1.0 + 0.33*time), r*cos(2.2 + 0.31*time)); | |
//fixed3 ro = fixed3(1.0, 1.0, 1.0); | |
fixed3 ta = fixed3(0.0,0.0,0.0); | |
float cr = 0.1*cos(0.1*time); | |
// render | |
fixed3 col = fixed3(0.0, 0.0, 0.0); | |
for(int j = 0; j<AA; j++) | |
for(int i = 0; i<AA; i++) | |
{ | |
fixed2 p = (-_ScreenParams.xy + 2.0*(uv * _ScreenParams.xy + fixed2(1.0, 1.0) / 1.0)) / _ScreenParams.y; | |
fixed3 cw = normalize(ta - ro); | |
fixed3 cp = fixed3(sin(cr), cos(cr),0.0); | |
fixed3 cu = normalize(cross(cw,cp)); | |
fixed3 cv = normalize(cross(cu,cw)); | |
fixed3 rd = normalize(p.x*cu + p.y*cv + 2.0*cw); | |
col += render(ro, rd, c); | |
} | |
col /= float(AA*AA); | |
col *= 0.7 + 0.3*pow(16.0*uv.x*uv.y*(1.0 - uv.x)*(1.0 - uv.y),0.25); | |
return fixed4(col, 1); | |
} | |
ENDCG | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment