Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
0
Question by RSpicer · Oct 07, 2014 at 07:38 PM · shaderscamerasdx11

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?

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

1 Reply

· Add your reply
  • Sort: 
avatar image
0

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

2 People are following this question.

avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges