Game Development Asked by Glen Pierce on November 2, 2021
I am trying to follow this excellent Coding Adventure: https://www.youtube.com/watch?v=lctXaT9pxA0 in order to get a very simple mesh deformation on a mesh using a compute shader.
I’m getting an error that reads:
Compute shader (NewComputeShader): Property (vertices) at kernel index (0) is not set
UnityEngine.ComputeShader:Dispatch(Int32, Int32, Int32, Int32)
MoonShader:runShader() (at Assets/MoonShader.cs:27)
MoonShader:Update() (at Assets/MoonShader.cs:15)
I’m calling runShader() in Update (maybe that’s a terrible idea, but I don’t think it’s contributing to this specific problem yet).
Do I need to somehow pass my mesh’s vertices into StructuredBuffer<float3> vertices;
? If so, how am I supposed to do that?
This is the call to the compute shader in my .cs file.
void runShader() {
int kernelHandle = computeShader.FindKernel("CSMain");
computeShader.Dispatch(kernelHandle, 512, 1, 1); //this is the line it appears to be complaining about
}
This is my .compute file.
#pragma kernel CSMain
StructuredBuffer<float3> vertices;
RWStructuredBuffer<float> heights;
uint numVertices;
float testValue = 20;
[numthreads(512,1,1)]
void CSMain (uint id : SV_DispatchThreadID) {
if(id >= numVertices) {
} else {
float3 vertexPosition = vertices[id];
heights[id] = 1 + sin(vertexPosition.y * testValue) * 0.05;
}
}
Link to repo if it’s helpful: https://github.com/glenpierce/proceduralPlanets
You need "link" your StructuredBuffer to a ComputeBuffer that you initialize in the CS script. In your code, you've never specified a compute shader to write to, so the compiler is basically saying, "yo, there's nowhere to write this data to!"
In your .cs file, you should have:
void runShader() {
int kernelHandle = computeShader.FindKernel("CSMain");
//New Things here
ComputeBuffer vertexBuffer = new ComputeBuffer(mesh.vertices.Length, sizeof(float) * 3); //make new compute buffer with specified size, and specified "stride"
//stride is like the size of each element, in your case it would be 3 floats, since Vector3 is 3 floats.
vertexBuffer.setData(mesh.vertices); //set data inside buffer
computeShader.setBuffer(kernelHandle, "vertices", vertexBuffer); //Linking the compute shader and cs shader buffers
computeShader.Dispatch(kernelHandle, 512, 1, 1);
}
I hope that helps!
I'd also recommend checking out these two ComputeShader guides, the OP walks you through his own personal project and explains things pretty well.
(Easy) https://www.reddit.com/r/Unity3D/comments/7pa6bq/drawing_mandelbrot_fractal_using_gpu_compute/
(Hard) https://www.reddit.com/r/Unity3D/comments/7ppldz/physics_simulation_on_gpu_with_compute_shader_in/
Answered by TakeMeHomeCountryRoads on November 2, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP