Skip to content

Instantly share code, notes, and snippets.

@michael-sacco
Last active October 12, 2022 23:57
Show Gist options
  • Select an option

  • Save michael-sacco/db75b9e908ba109c574cafa2d76eaf9f to your computer and use it in GitHub Desktop.

Select an option

Save michael-sacco/db75b9e908ba109c574cafa2d76eaf9f to your computer and use it in GitHub Desktop.
A copy of the Subpixel AA method from the Visual Effect Graph shader.
// This method is called from the Vertex stage of Unity's Visual Effect Graph shader.
// This function scales the size of the VFX Particle such that it occupies at least one pixel on screen.
// It also modifies the alpha such that the alpha is inversely faded according to the change in size.
void SubpixelAA(float3 position, inout float alpha, float size, inout float scaleX, inout float scaleY)
{
float2 localSize = size * float2(scaleX, scaleY);
float clipPosW = TransformPositionVFXToClip(position).w;
float minSize = clipPosW / (0.5f * min(abs(UNITY_MATRIX_P[0][0]) * _ScreenParams.x, abs(UNITY_MATRIX_P[1][1]) * _ScreenParams.y)); // max size in one pixel
float2 clampedSize = max(localSize,minSize);
float fade = (localSize.x * localSize.y) / (clampedSize.x * clampedSize.y);
alpha *= fade;
localSize = clampedSize;
scaleX = localSize.x / size;
scaleY = localSize.y / size;
}
float3x3 GetScaleMatrix(float3 scale)
{
return float3x3(scale.x, 0, 0,
0, scale.y, 0,
0, 0, scale.z);
}
float4x4 GetElementToVFXMatrix(float3 axisX, float3 axisY, float3 axisZ, float3x3 rot, float3 pivot, float3 size, float3 pos)
{
float3x3 rotAndScale = GetScaleMatrix(size);
rotAndScale = mul(rot, rotAndScale);
rotAndScale = mul(transpose(float3x3(axisX, axisY, axisZ)), rotAndScale);
pos -= mul(rotAndScale, pivot);
return float4x4(
float4(rotAndScale[0], pos.x),
float4(rotAndScale[1], pos.y),
float4(rotAndScale[2], pos.z),
float4(0, 0, 0, 1));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment