Created
April 19, 2021 08:10
-
-
Save unitycoder/0393f3bcfe2bda8d757bf512d7e513ea to your computer and use it in GitHub Desktop.
compute shader, grayscale and gradient op
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // My first pass is a grayscale filter | |
| // and my second "pass" is a gradient operation that requires to look to adjacent pixels. | |
| // https://forum.unity.com/threads/it-is-possible-to-have-a-multipass-compute-shader.1094386/#post-7053916 | |
| #pragma kernel CSMain | |
| // ( CPU -> GPU ) | |
| Texture2D inputTexture; | |
| // ( GPU -> CPU ) | |
| RWTexture2D <float4> outputTexture; | |
| int textWidth; | |
| int textHeight; | |
| [numthreads(8, 8, 1)] | |
| void CSMain(uint3 id : SV_DispatchThreadID) | |
| { | |
| float R = inputTexture[id.xy].r; | |
| float G = inputTexture[id.xy].g; | |
| float B = inputTexture[id.xy].b; | |
| //Grayscale | |
| float l = R * 0.299 + G * 0.587 + B * 0.114; | |
| float3 outputColor = float3(l, l, l); | |
| // | |
| //Gradient | |
| if (id.x == 0 || id.y == 0 || id.x == textWidth - 1 || id.y == textHeight - 1) | |
| outputColor = float3(0, 0, 0); | |
| else | |
| { | |
| float3 c = outputTexture[float2(id.x - 1, id.y - 1)]; | |
| float3 cSampleNegXNegY = float3(c.r, c.g, c.g); | |
| c = outputTexture[float2(id.x, id.y - 1)]; | |
| float3 cSampleZerXNegY = float3(c.r, c.g, c.g); | |
| c = outputTexture[float2(id.x + 1, id.y - 1)]; | |
| float3 cSamplePosXNegY = float3(c.r, c.g, c.g); | |
| c = outputTexture[float2(id.x - 1, id.y)]; | |
| float3 cSampleNegXZerY = float3(c.r, c.g, c.g); | |
| c = outputTexture[float2(id.x + 1, id.y)]; | |
| float3 cSamplePosXZerY = float3(c.r, c.g, c.g); | |
| c = outputTexture[float2(id.x - 1, id.y + 1)]; | |
| float3 cSampleNegXPosY = float3(c.r, c.g, c.g); | |
| c = outputTexture[float2(id.x, id.y + 1)]; | |
| float3 cSampleZerXPosY = float3(c.r, c.g, c.g); | |
| c = outputTexture[float2(id.x + 1, id.y + 1)]; | |
| float3 cSamplePosXPosY = float3(c.r, c.g, c.g); | |
| float fSampleNegXNegY = cSampleNegXNegY.x; | |
| float fSampleZerXNegY = cSampleZerXNegY.x; | |
| float fSamplePosXNegY = cSamplePosXNegY.x; | |
| float fSampleNegXZerY = cSampleNegXZerY.x; | |
| float fSamplePosXZerY = cSamplePosXZerY.x; | |
| float fSampleNegXPosY = cSampleNegXPosY.x; | |
| float fSampleZerXPosY = cSampleZerXPosY.x; | |
| float fSamplePosXPosY = cSamplePosXPosY.x; | |
| float edgeX = (fSampleNegXNegY - fSamplePosXNegY) * 0.25 + (fSampleNegXZerY - fSamplePosXZerY) * 0.5 + (fSampleNegXPosY - fSamplePosXPosY) * 0.25; | |
| float edgeY = (fSampleNegXNegY - fSampleNegXPosY) * 0.25 + (fSampleZerXNegY - fSampleZerXPosY) * 0.5 + (fSamplePosXNegY - fSamplePosXPosY) * 0.25; | |
| float fValue = (edgeX + edgeY) / 2.0f; | |
| fValue = 1 - fValue / gradientStrenght; | |
| outputColor = new Color(fValue, fValue, fValue, tc.a); | |
| } | |
| // | |
| outputTexture[id.xy] = outputColor; | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment