- Home /
Geometry shader output to mesh in Unity 4
I've written an implementation of the marching cubes algorithm in a geometry shader in Unity 4. It works great, but it has no lighting, because of course writing a shader program with vertex and geometry shaders requires overriding Unity's surface shaders, and thus you lose the built-in lighting engine.
When working in OpenGL or Direct3D directly, you can make a shader that outputs the results of the geometry shader stage directly to a VBO, instead of rendering it to the screen. In this way, you can write a shader that modifies a mesh, and then a separate shader that renders it. With this feature, assuming I could get a Mesh object associated with the output VBO, I could then render that using Unity's standard shaders (or my own surface shader), both in order to cache the output of marching cubes (so I wouldn't have to re-run it each frame) and to use Unity's existing lighting model.
In Unity 4 you can do something like this with compute shaders, but unfortunately I can't treat the ComputeBuffer as a Mesh, so I can't render it with a surface shader, and thus still can't use Unity's lighting model on it.
So firstly, is there a way in Unity 4 to output the geometry shader output to a VBO associated with a Mesh object?
And if not, is there a way to access Unity 4's lighting model from a custom fragment shader?