- Home /
Using compute shader to return whether texture has certain colour
I'm writing a Compute Shader which I need to do a very simple task: check whether any pixel in the image has green == 1 and blue > 0.5. (For clarity - the green channel is a 2d "light" and the blue channel is that lights "trail" - I want to detect whenever the light crosses back over its trail.)
I'm as far as displaying the overlap (shown in white) for debugging purposes, but I have no idea how to do something as simple as return a value indicating whether the texture actually contains an overlap. My confusion stems from how threads work. I have a float buffer with room for a single float - I simply need a 1 or 0.
For clarification the following two images show a "before" and "after" - all I need is a single "true" value telling me that some white exists in the second image.
The compute shader is as follows:
#pragma kernel CSMain
Texture2D<float4> InputTexture;
RWTexture2D<float4> OutputTexture;
RWStructuredBuffer<float> FloatBuffer;
[numthreads(8,8,1)]
void CSMain(uint3 id : SV_DispatchThreadID)
{
// need to detect any region where g == 1 and blue > 0.5
float green = InputTexture[id.xy].g;
float blue = round(InputTexture[id.xy].b);
float overlap = round((green + blue) / 2.0);
OutputTexture[id.xy] = float4(overlap, overlap, overlap, 1);
// answer here?? Note that the output texture is only for debugging purposes
FloatBuffer[0] = ??
}
Is it not possible to write:
if(green > 0.99 && blue > 0.5)
{
FloatBuffer[0] = 1;
}
and after each Dispatch, you set the value of FloatBuffer to zero.
Your answer
![](https://koobas.hobune.stream/wayback/20220612150027im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
Are ComputeShader dispatches sequential? 0 Answers
Swapping Compute Shaders 1 Answer
Setting color for compute shader structure trough c# 0 Answers
Reading and Writing to the same texture 0 Answers
Persistent Data in Compute Shaders. 0 Answers