Skip to content

Instantly share code, notes, and snippets.

@cs-altshift
Created September 24, 2021 11:45
Show Gist options
  • Save cs-altshift/d88f52d983f0b9493e0f03893e36486d to your computer and use it in GitHub Desktop.
Save cs-altshift/d88f52d983f0b9493e0f03893e36486d to your computer and use it in GitHub Desktop.
Crying Suns' Rim Light Effect Normal Map Generator
#pragma kernel CSMain
RWTexture2D<float4> Result;
Texture2D<float4> InputTexture;
uint textureWidth;
uint textureHeight;
uint maxIteration;
[numthreads(8,8,1)]
void CSMain (uint3 id : SV_DispatchThreadID)
{
float4 px = InputTexture[id.xy];
float hWeight = 0.5;
float vWeight = 0.5;
float intensity = 1;
float intensityIncrement = 1.0 / maxIteration;
bool trace = false;
if (px.a > 0) {
for (uint i = 1; i <= maxIteration && !trace; i++) {
uint limitCheck = i - 1;
hWeight = 0.5;
vWeight = 0.5;
if (id.x > limitCheck && InputTexture[uint2(id.x - i, id.y)].a < 0.5) {
trace = true;
hWeight -= 0.5;
}
if (id.x < textureWidth - limitCheck - 1 && InputTexture[uint2(id.x + i, id.y)].a < 0.5) {
trace = true;
hWeight += 0.5;
}
if (id.y > limitCheck && InputTexture[uint2(id.x, id.y - i)].a < 0.5) {
trace = true;
vWeight -= 0.5;
}
if (id.y < textureHeight - limitCheck - 1 && InputTexture[uint2(id.x, id.y + i)].a < 0.5) {
trace = true;
vWeight += 0.5;
}
if (trace) {
intensity = intensityIncrement * (maxIteration - limitCheck);
}
}
}
Result[id.xy] = float4(
hWeight,
trace ? intensity : 0,
vWeight,
1
);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment