Shader "CrispyPin/Lenia"
{
	Properties
	{
		_LastFrame ("Texture", 2D) = "white" {}
		_GrowtCenter ("Growth fn center (mu)", Range(0, 1)) = 0.2
		_GrowthWidth ("Growth fn width (sigma / std deviation)", Range(0, 1)) = 0.07
		_Speed ("Speed factor", Range(0.01, 0.5)) = 0.05
	}
	SubShader
	{
		Tags { "RenderType"="Opaque" }
		LOD 100

		Pass
		{
			CGPROGRAM
			#pragma vertex vert
			#pragma fragment frag

			#include "UnityCG.cginc"

			#define WIDTH 512

			struct appdata {
				float4 vertex : POSITION;
				float2 uv : TEXCOORD0;
			};

			struct v2f {
				float2 uv : TEXCOORD0;
				float4 vertex : SV_POSITION;
			};

			texture2D _LastFrame;
			float _GrowtCenter;
			float _GrowthWidth;
			float _Speed;

			v2f vert (appdata v) {
				v2f o;
				o.vertex = UnityObjectToClipPos(v.vertex);
				o.uv = v.uv;
				return o;
			}

			float kernel(float p) {
				float r = p / 8.0;
				float k_sharpness = 28;
				float k_offset = 0.435;
				return exp(-((r - k_offset) * (r - k_offset)) * k_sharpness);
				// float r = p / _Radius;
				// // -- normal
				// return exp(-((r - _KOffset)*(r - _KOffset)) * _KSharpness);

				// -- example from the lenia paper
				// if (r >= 1) return 0;
				// const float alpha = 4;
				// return exp(alpha - alpha/(4.0 * r * (1.0 - r)));
			}

			float activation(float neighbors) {
				// return old_state * (neighbors > 2 && neighbors < 5) +
				// ((1 - old_state) * neighbors == 3);

				// const float sharpness = 1000.0;
				// float x = neighbors - _GrowtCenter;
				// return exp(-(x*x) * _GrowthWidth) * 2.0 - 1.0;
				const float mu = _GrowtCenter;
				const float sigma = _GrowthWidth;
				const float u = neighbors;
				return exp(-((u-mu) * (u-mu)) / (2 * sigma * sigma)) * 2.0 - 1.0;
			}

			float value(uint2 p, int dx, int dy) {
				const uint x = (p.x + dx) % WIDTH;
				const uint y = (p.y + dy) % WIDTH;
				return _LastFrame[uint2(x, y)].r;
			}

			float4 frag (v2f i) : SV_Target {
				if(_ProjectionParams.z > 1) discard;

				// Defines RADIUS Kernel total_max
				#include "lenia_generated_kernel.cginc"
				const uint2 p = i.uv * WIDTH;

				float total = 0.0;
				[unroll(RADIUS)]
				for (int y = 0; y < RADIUS; y++) {
					[unroll(RADIUS)]
					for (int x = 1; x <= RADIUS; x++) {
						total += value(p, x, y) * Kernel[y][x - 1];
						total += value(p, -y, x) * Kernel[y][x - 1];
						total += value(p, -x, -y) * Kernel[y][x - 1];
						total += value(p, y, -x) * Kernel[y][x - 1];
					}
				}
				float old_state = value(p, 0, 0) ;
				float count = total / total_max;

				const float step = _Speed * unity_DeltaTime.x * 60;
				float state = activation(count) * step + old_state;
				state = clamp(state, 0, 1);

				// kernel visualization: lookup table (VERY SLOW)
				// float k = 0;
				// {
				// 	float2 p = (i.uv - 0.5 ) * WIDTH;
				// 	p = floor(p);
				// 	if (p.x > 0 && p.y >= 0) {
				// 		k = Kernel[p.y][p.x-1];
				// 	} else if (p.x <= 0 && p.y > 0) {
				// 		k = Kernel[-p.x][p.y-1];
				// 	} else if (p.x < 0 && p.y <= 0) {
				// 		k = Kernel[-p.y][-p.x-1];
				// 	} else if (p.x >= 0 && p.y < 0) {
				// 		k = Kernel[p.x][-p.y-1];
				// 	}
				// }

				// kernel visualisation: real size
				// float2 p = (i.uv - 0.5) * WIDTH;
				// float k = kernel(length(p)) * (max(abs(p.x), abs(p.y)) <= RADIUS);

				// kernel visualisation: fill square
				// float k = kernel(length(i.uv - 0.5) * RADIUS * 2);

				// float a = activation(i.uv.x);
				// float4 col = float4(state, k, a, 1);

				float4 col = float4(state, i.uv.x * state, i.uv.y * state, 1);
				return col;
			}
			ENDCG
		}
	}
}
