Unity 计算缓冲区可跟踪纹理中的颜色数量
我一直在尝试制作一个计算缓冲区,用于跟踪创建程序岛纹理的着色器中具有某种颜色的像素数量。
这是我的计算着色器的简化版本:
#pragma kernel CSMain
RWTexture2D<float4> Result;
RWStructuredBuffer<int> Biomes; // This buffer
float2 WorldSize;
float IslandSize;
[numthreads(8,8,1)]
void CSMain (uint3 id : SV_DispatchThreadID)
{
float2 position = float2(id.x - WorldSize.x, id.y - WorldSize.y);
float3 color = float3(0, 0, 1);
float2 noise = generateNoise(position / IslandSize * 1000);
float height = noise.x * 100;
float falloff = noise.y;
if (falloff < 1 && height < 5)
{
color = float3(0, 0.1, 1);
InterlockedAdd(Biomes[1], 1); // Data gets added to the buffer
}
Result[id.xy] = float4(color, 1);
}
这是我的简化C#代码(生物群系是纹理中具有特定颜色的像素数量的列表),这是从 OnGUI()
调用的函数的一部分:
// Set shader parameters
TerrainShader.SetVector("WorldSize", new Vector2(xSize, zSize));
TerrainShader.SetFloat("IslandSize", IslandSize * SizeMultiplier);
biomesBuffer = new ComputeBuffer(7, sizeof(int) * 7); // Buffer initialization
TerrainShader.SetBuffer(0, "Biomes", biomesBuffer);
// Dispatch compute shader
TerrainShader.SetTexture(0, "Result", target);
TerrainShader.Dispatch(0, xSize * 2, zSize * 2, 1);
// Get biome data
int[] biomes = new int[7];
biomesBuffer.GetData(biomes); // Retrieving data from buffer
string log = "";
for (int i = 0; i < biomes.Length; i++) {
log += $"Biome {i}: {biomes[i]} ";
}
Debug.Log(log);
biomesBuffer.Dispose();
这就是我的控制台的样子:
Biome 0: 2030086002 Biome 1: 0 Biome 2: 19020880 Biome 3: 52763400 Biome 4: 24948620 Biome 5: 16871260 Biome 6: 15926730
Biome 0: 1462274385 Biome 1: 0 Biome 2: 53590760 Biome 3: 148659300 Biome 4: 70291988 Biome 5: 47534272 Biome 6: 44873085
Biome 0: 843603332 Biome 1: 522 Biome 2: 37425056 Biome 3: 103816080 Biome 4: 49088343 Biome 5: 33195513 Biome 6: 31337076
Biome 0: 2030086002 Biome 1: 0 Biome 2: 19020880 Biome 3: 52763400 Biome 4: 24948620 Biome 5: 16871260 Biome 6: 15926730
Biome 0: 1462274385 Biome 1: 0 Biome 2: 53590760 Biome 3: 148659300 Biome 4: 70291988 Biome 5: 47534272 Biome 6: 44873085
...
我对计算着色器相当陌生,不明白在哪里这些数字来自。它们在控制台的每个新行中都应该相同,因为纹理保持不变。
以下是纹理的外观,供参考: 计算着色器结果
I've been trying to make a compute buffer that keeps track of track of the amount of pixels that have a certain color in my shader that creates procedural island textures.
This is a simplified version of my compute shader:
#pragma kernel CSMain
RWTexture2D<float4> Result;
RWStructuredBuffer<int> Biomes; // This buffer
float2 WorldSize;
float IslandSize;
[numthreads(8,8,1)]
void CSMain (uint3 id : SV_DispatchThreadID)
{
float2 position = float2(id.x - WorldSize.x, id.y - WorldSize.y);
float3 color = float3(0, 0, 1);
float2 noise = generateNoise(position / IslandSize * 1000);
float height = noise.x * 100;
float falloff = noise.y;
if (falloff < 1 && height < 5)
{
color = float3(0, 0.1, 1);
InterlockedAdd(Biomes[1], 1); // Data gets added to the buffer
}
Result[id.xy] = float4(color, 1);
}
And here is my simplified C# code (biomes is a list of numbers of pixels that have a certain color in the texture), which is part of a function that gets called from OnGUI()
:
// Set shader parameters
TerrainShader.SetVector("WorldSize", new Vector2(xSize, zSize));
TerrainShader.SetFloat("IslandSize", IslandSize * SizeMultiplier);
biomesBuffer = new ComputeBuffer(7, sizeof(int) * 7); // Buffer initialization
TerrainShader.SetBuffer(0, "Biomes", biomesBuffer);
// Dispatch compute shader
TerrainShader.SetTexture(0, "Result", target);
TerrainShader.Dispatch(0, xSize * 2, zSize * 2, 1);
// Get biome data
int[] biomes = new int[7];
biomesBuffer.GetData(biomes); // Retrieving data from buffer
string log = "";
for (int i = 0; i < biomes.Length; i++) {
log += quot;Biome {i}: {biomes[i]} ";
}
Debug.Log(log);
biomesBuffer.Dispose();
This is what my console looks like:
Biome 0: 2030086002 Biome 1: 0 Biome 2: 19020880 Biome 3: 52763400 Biome 4: 24948620 Biome 5: 16871260 Biome 6: 15926730
Biome 0: 1462274385 Biome 1: 0 Biome 2: 53590760 Biome 3: 148659300 Biome 4: 70291988 Biome 5: 47534272 Biome 6: 44873085
Biome 0: 843603332 Biome 1: 522 Biome 2: 37425056 Biome 3: 103816080 Biome 4: 49088343 Biome 5: 33195513 Biome 6: 31337076
Biome 0: 2030086002 Biome 1: 0 Biome 2: 19020880 Biome 3: 52763400 Biome 4: 24948620 Biome 5: 16871260 Biome 6: 15926730
Biome 0: 1462274385 Biome 1: 0 Biome 2: 53590760 Biome 3: 148659300 Biome 4: 70291988 Biome 5: 47534272 Biome 6: 44873085
...
I'm fairly new to compute shader and don't understand where these numbers are coming from. They should be the same in every new line of the console because the texture stays the same.
Here's what the texture looks like for reference:
Compute shader result
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我可能是错的,但我相信你的代码应该可以工作,除了你只设置为 Biome[1] 的事实之外 - 你得到的其他数字是内存中随机未设置的位。
I may be wrong, but I believe your code should work apart from the fact that you're only ever setting to Biome[1] - the other numbers you're getting are random unset bits from memory.