- Home /
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.
Do it in the vertex shader (_Object2World * $$anonymous$$VP). If your GPU/driver is smart, it will cache the uniform result.
Not sure I understand. $$anonymous$$ is the same as _Object2World. Why would I do _Object2World * $$anonymous$$VP. That would give me $$anonymous$$$$anonymous$$VP.
ok, so you mean apply _WorldToObject * $$anonymous$$VP to isolate VP. I assumed the drivers would never be able to optimise that out. Is that a bad assumption?
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.
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();
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.
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.
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 :(
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.
$$anonymous$$y 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
Follow this Question
Related Questions
Previous Model*View*Projection Matrix 0 Answers
Shader: actual view direction/normal? 2 Answers
how to change progection matrix for some objects 0 Answers
SurfaceOutput.viewDir not the same in deferred 1 Answer
Drawing outlines on terrain; how? 1 Answer