Hellot there!
I have some problem, i’m developing some study game and in there i have a Particle Effect that works like the game Splatoon , so to simplify, in the Terrain shader, i have One Main Texture, and one Mask Texture that goes on top the Main Texture. So where i Paint is in the Mask. ( The Black Texture)
this part is working Fine.

Now, to Find the Points the player have, i’m using a Compute Shader, that get the Mask Texture, and find for example:
How much blue is in ? 10%
How much pink is in ? 5%
blue win’s, like that
The problem is : It’s not differentiating the colors, and it’s giving me the wrong percentage, I’ll pass down the Compute Shader code and how I consume it
This is the Compute Shader
// Each #kernel tells which function to compile; you can have many kernels
#pragma kernel CSMain
#pragma kernel CSInit
//SamplerState _LinearClamp;
Texture2D<float4> image; // The Mask
float4 reference; // Color reference
RWStructuredBuffer<uint> compute_buffer; //buffer
//RWTexture2D<float4> Result;
[numthreads(64, 1, 1)]
void CSInit(uint3 id : SV_DispatchThreadID)
{
compute_buffer[id.x] = 0.0;
}
[numthreads(8,8,1)]
void CSMain (uint3 id : SV_DispatchThreadID)
{
float fit = dot(image[id.xy].xyz, reference.xyz);
float refMagSq = dot(reference.xyz, reference.xyz);
float threshold = 0.5f;
float sqrt_3 = 1.73205080757f;
if ((fit / refMagSq) / sqrt_3 <= threshold) {
InterlockedAdd(compute_buffer[0], 1);
}
}
This is how i consume , it’s a little extense , the main method is GetPoints
public ComputeShader compute_shader;
[HideInInspector]
public uint[] data;
protected ComputeBuffer compute_buffer;
private RenderTexture render_texture;
int handle_init;
int handle_main;
float percent;
Dictionary<Colors, string> ListPercentual;
void Start()
{
ListPercentual = new Dictionary<Colors, string>();
render_texture = tex.getMask();
handle_init = compute_shader.FindKernel("CSInit");
handle_main = compute_shader.FindKernel("CSMain");
compute_buffer = new ComputeBuffer(1, sizeof(uint));
data = new uint[1];
compute_shader.SetTexture(handle_main, "image", render_texture);
compute_shader.SetBuffer(handle_main, "compute_buffer", compute_buffer);
compute_shader.SetBuffer(handle_init, "compute_buffer", compute_buffer);
}
void Update()
{
var list = GetColorsInserted();
foreach(var item in list)
{
var color = GetValueFromListColors(item);
var value = GetPoints(color);
if (ListPercentual.ContainsKey(item))
ListPercentual[item] = value;
else
ListPercentual.Add(item, value);
}
ShowPoints();
}
private void ShowPoints()
{
foreach( var item in ListPercentual)
{
switch (item.Key)
{
case Colors.Blue: PointBlue.text = item.Value; break;
case Colors.Red: PointRed.text = item.Value; break;
case Colors.Green: PointGreen.text = item.Value; break;
default: break;
}
}
}
private string GetPoints(Color reference)
{
compute_shader.SetVector("reference", reference);
data = new uint[1];
compute_buffer.SetData(data);
compute_shader.Dispatch(handle_init, 64, 1, 1);
compute_shader.Dispatch(handle_main, render_texture.width / 8, render_texture.height / 8, 1);
compute_buffer.GetData(data);
uint result = data[0];
var percent = 100.0f - ((float)result / ((float)render_texture.width * (float)render_texture.height)) * 100.0f;
return percent.ToString() + "%";
}
void OnDestroy()
{
if (compute_buffer != null )
{
compute_buffer.Release();
compute_buffer = null;
}
}
