- Home /
DrawProcedural execution order with multiple cameras
I'm writing a system that uses procedurally-generated geometry to produce complex objects in the Unity scene. This works awesome with a single camera.
My scene uses multiple cameras. With two or more cameras in the scene, it seems like the result of DrawProcedural() is being executed on the GPU in the context of the NEXT camera to be drawn! This makes a bit of sense, if the DrawProcedural() call is added to the list of GPU instructions after whatever instructions end the current frame and start the next frame. I'm looking for a way to resolve this, short of hacking around to find Camera.current's index in Camera.allCameras and then feed my shader the next camera's MVP matrix, which works but introduces a one-frame delay and feels like a hack that's liable to introduce more problems later.
I call DrawProcedural like so, from a MonoBehaviour on a GameObject that has a 100% transparent object applied to a cube mesh (hacky, but makes sure OnRenderObject is called when a camera is looking at that volume of space):
void OnRenderObject()
{
Matrix4x4 m = transform.localToWorldMatrix;
Matrix4x4 v = Camera.current.worldToCameraMatrix;
Matrix4x4 p = Camera.current.projectionMatrix;
Matrix4x4 MVP = p * v * m;
Graphics.ClearRandomWriteTargets();
proceduralMaterial.SetPass(0);
proceduralMaterial.SetMatrix("ModelViewProjection", MVP);
proceduralMaterial.SetMatrix("model", m);
Graphics.DrawProcedural(MeshTopology.Points, 640 * 480);
Graphics.ClearRandomWriteTargets();
}
and my geometry shader works like so:
// Geometry Shader -----------------------------------------------------
[maxvertexcount(4)]
void GS_Main(point GS_INPUT p[1], uint primID : SV_PrimitiveID, inout TriangleStream<FS_INPUT> triStream)
{
// set up modelviewprojection matrix.
float4x4 xf = ModelViewProjection;
//snip.. v[0] is a float4 vertex.. do this 4 times to make up a quad of two tris.
pIn.pos = mul(xf, v[0]);
triStream.Append(pIn);
}
All is well with a single camera, but things start to break badly as soon as two cameras are both looking at the object in question. Thoughts?
Answer by RSpicer · Nov 11, 2014 at 05:44 PM
After working on this intermittently and collaborating with some colleagues, I've got an answer for this. Posting in case anyone else encounters this:
My approach to calculating the MVP matrix was not working. I'm really not sure why, but the solution I found was to call the code per-camera from OnPostRender() rather than OnRenderObject(). Instead of calculating an MVP matrix by hand, I used UNITY_MATRIX_VP and set only the placeholder object's transform.localToWorld matrix as a shader uniform. Bug fixed, everything working as expected.
Your answer
Follow this Question
Related Questions
Chain multiple custom camera image effects? 1 Answer
Rendering Depth Correctly With Multiple Cameras 0 Answers
Where can I find some examples of using DrawProcedural and DrawProceduralIndirect in unity? 1 Answer
OnPreRender updates shader parameters for each eye in 5.3 but not in 5.4? 0 Answers
Difference between OnWillRenderObject and OnRenderObject 1 Answer