Hey I am new to compute shaders and have been playing around with a pixelation and palletization shader, but after a bit of poking around It started erroring on the line "color = float4(_Colors[pos]);". I cannot figure out why, I tried setting the value to a blank white, and that errored too so i don't think the _Colors[pos] is anything it shouldn't be. I have also tried removing the line, at which point it runs, but doesn't apply the color. I have also set the value of color elsewhere on the script and it hasn't errored there. I am probably missing something obvious here, but I'd appreciate any help.
Error:
Compute shader (Pixelate): Property (_Colors) at kernel index (0): Attempting to bind Texture ID 1761 as UAV but the texture wasn't created with the UAV usage flag set!
UnityEngine.StackTraceUtility:ExtractStackTrace ()
PixelRunner:OnRenderImage (UnityEngine.RenderTexture,UnityEngine.RenderTexture) (at Assets/PixelRunner.cs:48)
UnityEngine.GUIUtility:ProcessEvent (int,intptr,bool&)
Code:
#pragma kernel Pixelate
RWTexture2D _Result;
int _BlockSize;
int _ResultWidth;
int _ResultHeight;
RWTexture2D _Colors;
int _numColors;
[numthreads(8,8,1)]
void Pixelate (uint3 id : SV_DispatchThreadID)
{
const float2 startPos = id.xy * _BlockSize;
if (startPos.x >= _ResultWidth || startPos.y >= _ResultHeight)
return;
const int blockWidth = min(_BlockSize, _ResultWidth - startPos.x);
const int blockHeight = min(_BlockSize, _ResultHeight - startPos.y);
const int numPixels = blockHeight * blockWidth;
float4 color = float4(0, 0, 0, 0);
for (int i = 0; i < blockWidth; ++i)
{
for (int j = 0; j < blockHeight; ++j)
{
const uint2 pixelPos = uint2(startPos.x + i, startPos.y + j);
color += _Result[pixelPos];
}
}
color /= numPixels;
float1 diffCurr = float1(3);
float1 diffMin = float1(3);
float4 UseCOl;
for (int c = 0; c < _numColors; ++c){
uint2 pos = uint2(c,1);
diffCurr = abs(color.r - _Colors[pos].r)+abs(color.g - _Colors[pos].g)+abs(color.b - _Colors[pos].b);
if (diffCurr < diffMin){
diffMin = diffCurr;
color = float4(_Colors[pos]);
}
}
for (int l = 0; l < blockWidth; ++l)
{
for (int j = 0; j < blockHeight; ++j)
{
const uint2 pixelPos = uint2(startPos.x + l, startPos.y + j);
_Result[pixelPos] = color;
}
}
}
↧