float happyNoise(float2 p)
{
	return frac(dot(p, sin(p))) - 0.5;
}

float2x2 rotateAroundZ(float angle)
{
	float c = cos(angle);
	float s = sin(angle);
	return float2x2(c, s, -s, c);
}

float2x2 happyAccidentMatrix(float3 originalPosition, float timeCos)
{
	return float2x2(
		cos(originalPosition.x),
		sin(originalPosition.y),
		-sin(originalPosition.z),
		timeCos);
}

float4 shadeVideo(ShaderContext context)
{
	float2 resolution = max(context.outputResolution, float2(1.0, 1.0));
	float2 fragCoord = context.uv * resolution;
	float2 normalizedCoord = (fragCoord - 0.5 * resolution) / resolution.y / max(scale, 0.001);
	float seed = context.startupRandom;
	float time = context.time * speed + seed * 53.0;
	float timeCos = cos(0.1 * time);
	float3 direction = normalize(float3(normalizedCoord, 1.0));
	float3 origin = float3(seed * 0.4 - 0.2, 0.2 - seed * 0.4, time);

	float edgeAmount = saturate(length(normalizedCoord) * 0.55);
	float z = happyNoise(fragCoord + seed * resolution.yx) * lerp(0.22, 0.08, edgeAmount);
	float distanceToSurface = 0.0;
	float4 accumulated = float4(0.0, 0.0, 0.0, 0.0);
	float clampedSteps = clamp(raySteps, 1.0, 77.0);

	// Ray-march a folded procedural field. distanceToSurface advances the ray,
	// while inverse-distance accumulation creates the glowing filaments.
	for (int i = 0; i < 77; ++i)
	{
		if (float(i) >= clampedSteps)
			break;

		z += 0.6 * distanceToSurface;

		float3 position = origin + z * direction;
		float3 originalPosition = position;

		position.xy = mul(rotateAroundZ(2.0 + originalPosition.z), position.xy);
		position.xy = mul(happyAccidentMatrix(originalPosition, timeCos), position.xy);

		// Color comes from pre-fold space so the palette varies smoothly even as
		// the geometry folds into repeated cells.
		float colorSeed = 0.5 * originalPosition.z + length(position - originalPosition);
		float4 palette = 1.0 + sin(colorSeed + float4(0.0, 4.0, 3.0, 6.0));
		palette /= 0.55 + 1.55 * dot(originalPosition.xy, originalPosition.xy);

		position = abs(frac(position) - 0.5);
		// Distance to a tiny box/cross primitive inside each repeated cell.
		distanceToSurface = abs(min(length(position.xy) - 0.125, min(position.x, position.y) + 0.001)) + 0.001;
		accumulated += palette.w * palette / distanceToSurface;
	}

	float4 color = float4(tanh((accumulated.rgb * intensity) / 20000.0), 1.0);
	return saturate(lerp(color, context.sourceColor, sourceMix));
}
