Files
2022-05-12 17:08:35 +10:00

100 lines
3.0 KiB
Plaintext

Shader "Kaiju/Julia"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_Iterations("Iterations", range(0, 555)) = 255
_Pan("Pan", vector) = (0.5, 0.0, 0.0, 0.0)
_Zoom("Zoom", range(0, 4)) = 3
_Aspect("Aspect", range(0, 4)) = 1
_JuliaSeed("JuliaSeed", vector) = (0.39, -0.2, 0.0, 0.0)
_Colors("Colors", vector) = (3.0, 4.0, 5.0, 0.0)
}
SubShader
{
// No culling or depth
Cull Off ZWrite Off ZTest Always
Pass
{
CGPROGRAM
// #pragma enable_d3d11_debug_symbols
#pragma target 3.0
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float2 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
};
v2f vert (appdata v)
{
v2f o;
o.vertex = UnityObjectToClipPos(v.vertex);
o.uv = v.uv;
return o;
}
float _Iterations;
float2 _Pan;
float _Zoom;
float _Aspect;
float2 _JuliaSeed;
float3 _Colors;
float ComputeValue(float2 v, float2 offset) {
float vxsquare = 0;
float vysquare = 0;
float iteration = 0;
float lastIteration = _Iterations;
do {
vxsquare = v.x * v.x;
vysquare = v.y * v.y;
v = float2(vxsquare - vysquare, v.x * v.y * 2) + offset;
iteration++;
if ((lastIteration == _Iterations) && (vxsquare + vysquare) > 4.0) {
lastIteration = iteration + 1;
}
} while (iteration < lastIteration);
return (float(iteration) - (log(log(sqrt(vxsquare + vysquare))) / log(2.0))) / float(_Iterations);
}
float4 Mandelbrot_PixelShader(float2 texCoord : TEXCOORD0) : COLOR0 {
float2 v = (texCoord - 0.5) * _Zoom * float2(1, _Aspect) - _Pan;
float val = ComputeValue(v, v);
return float4(sin(val * _Colors.x), sin(val * _Colors.y), sin(val * _Colors.z), 1);
}
float4 Julia_PixelShader(float2 texCoord : TEXCOORD0) : COLOR0 {
_JuliaSeed = float2(sin(_Time.y), cos(_Time.y));
float2 v = (texCoord - 0.5) * _Zoom * float2(1, _Aspect) - _Pan;
float val = ComputeValue(v, _JuliaSeed);
return float4(sin(val * _Colors.x), sin(val * _Colors.y), sin(val * _Colors.z), 1);
}
float4 frag (v2f i) : SV_Target {
// return Julia_PixelShader(i.uv);
return Mandelbrot_PixelShader(i.uv);
}
ENDCG
}
}
}