• 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
Question by madpuppet · Jul 08, 2012 at 12:57 PM · shaderviewscene viewprojection

Shader using View Projection matrix in Scene View

I have a shader that needs to know the View Projection matrix (the vertex shader needs to do some work in world space and pass on the results of that to the fragment shader)

I'm currently achieving this by calculating the VP matrix myself and setting it as a shader variable. I do this once using SetGlobalMatrix as there can be 100 or so instances that all need the results. So each Update() I do this:

Matrix4x4 vp = new Matrix4x4(); vp = cam.projectionMatrix * cam.worldToCameraMatrix; Shader.SetGlobalMatrix("_VPTransform",vp);

Problem I have is that 'cam' is the camera for my game view, so it is the wrong VP for the scene view. Is there a better way I should be doing this. I don't want to sacrifice any performance just so it works in Scene View, but would be really nice to have it work in the Scene View.

Comment

People who like this

0 Show 9
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
avatar image Jessy · Jul 08, 2012 at 01:45 PM 0
Share

Do it in the vertex shader (_Object2World * MVP). If your GPU/driver is smart, it will cache the uniform result.

avatar image madpuppet · Jul 08, 2012 at 01:53 PM 0
Share

Not sure I understand. M is the same as _Object2World. Why would I do _Object2World * MVP. That would give me MMVP.

avatar image Jessy · Jul 08, 2012 at 01:57 PM 0
Share

Nobody can understand. I typed the wrong variable name.

avatar image madpuppet · Jul 08, 2012 at 02:00 PM 0
Share

ok, so you mean apply _WorldToObject * MVP to isolate VP. I assumed the drivers would never be able to optimise that out. Is that a bad assumption?

avatar image madpuppet · Jul 08, 2012 at 02:08 PM 0
Share

I should add, if I wasn't doing 60fps high performance shader, that would indeed be my prefered approach since it would just work without any code involvement.

Show more comments

1 Reply

· Add your reply
  • Sort: 
avatar image

Answer by delstrega · Jul 08, 2012 at 01:58 PM

You can get the cameras for all open scene views with:

 var sceneCameras:Camera[] = SceneView.GetAllSceneCameras();
Comment

People who like this

0 Show 5 · 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
avatar image madpuppet · Jul 08, 2012 at 02:06 PM 0
Share

thanks. I didn't know that. Though I still have no idea how I could get a different VP to the shader for each render. Is there some kind of OnRender callback that happens before the mesh is rendered to a view, that I could jam the appropriate shader variables in? In which case, what I really need is the current camera being used.

avatar image delstrega · Jul 08, 2012 at 02:11 PM 0
Share

I don't know if that helps but there's OnPreRender() http://docs.unity3d.com/Documentation/ScriptReference/Camera.OnPreRender.html. This is called in all scripts attached to the camera. So if you were able to add a script to the scene cameras you could update the shader variables accordingly. I don't know if you can add scripts to them like you normally can with AddComponent(). I guess it's worth a try though.

avatar image madpuppet · Jul 08, 2012 at 02:31 PM 0
Share

sounded promising, but OnPreRender seems to only get called for when game view is getting rendered with a game camera, not for the Scene Edit camera :(

avatar image delstrega · Jul 08, 2012 at 02:51 PM 0
Share

Bummer :( It's hard to tell since SceneView is undocumented but maybe SceneView.currentDrawingSceneView.camera or SceneView.lastActiveSceneView.camera are of any help to your situation.

avatar image madpuppet · Jul 11, 2012 at 03:59 PM 0
Share

My big problem is finding a spot to run some code before the sceneView renders. OnPreRender() seems perfect but doesn't run for SceneView.

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.

Welcome to Unity Answers

If you’re new to Unity Answers, please check our User Guide to help you navigate through our website and refer to our FAQ for more information.

Before posting, make sure to check out our Knowledge Base for commonly asked Unity questions.

Check our Moderator Guidelines if you’re a new moderator and want to work together in an effort to improve Unity Answers and support our users.

Follow this Question

Answers Answers and Comments

7 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Previous Model*View*Projection Matrix 0 Answers

Shader: actual view direction/normal? 2 Answers

Project a shape using a mesh 0 Answers

Unable to click on objects in the editor scene view after using a custom shader? 0 Answers

How to force the compilation of a shader in Unity? 5 Answers


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