- Home /
How can I render a scene to a surface
Two cameras in different levels. Within camera B's view you can see a plane. On that plane I want to render what camera A can see.
I'm not asking for code specifics, but really just pointers in the right direction/what to look for. If there are particular functions that help with specifically this, mentioning those would be appreciated. Cheers.
Answer by DanJC · Apr 25, 2013 at 07:49 AM
Just going to provide a working non-Unity-Pro solution for anyone else that comes across this.
It's true that you can't use RenderTexture() without Pro, but you don't need it. What you can do instead is use a shader with a ColorMask of 0.
Create 2 cameras, and chuck an object in the view of Camera 2 and another object in the view of Camera 1. On Camera 2's object place a material that has the following shader, and then render first Camera 1, then Camera 2. To do this, simply go to the Inspector and set Camera 1´s Depth to 1, and Camera 2´s Depth to 2.
Camera 2's render will completely cover that of the Camera 1, except for where the object is, creating a functioning hole in the air that you can look through to see what's in the view of Camera 1.
Shader "DepthMask" { SubShader { ColorMask 0 // Masks the object with no rendering to all colour channels Pass {} // Render the object, which in this case means disabling } }
Thanks to Tenebraus for pointing this out.
@jjxtra I recommend switching to unity 5. The free version has rendertextures. If you want to finish your project in 4, then see the posts below for step-by-step instructions (I dont have an example project). Unity 5 is amazing though and if you want real rendertextures without the side-effects of this workaround, upgrade.
Answer by Staakman · Aug 04, 2013 at 09:04 PM
I'm very sorry, but I can't get this to work in Unity 4.0
Can someone please elaborate on this?
I have been having some issues with this too since I created this post. I did neglect to say that I have Camera 1 set to Skybox, and Camera 2 set to Don't Clear. (Depth Only also works on Camera 2.) However, I have definitely used this technique with a skybox on both cameras. It was running perfectly, portals were behaving exactly as expected both ways. Since upgrading to Unity 4.2, something has gone wrong. I don't think it's Unity as the scene I posted above is still working, but rather, that it's just a large coincidence that not one of my backups using 2 skyboxes has this effect working now. I think I've messed up, but haven't worked out what yet. I'm still a beginner with this too.
These are the instructions I think SHOULD work. But right now, for me they don't. I still have a scene with the effect working, but I can't seem to be able to replicate it. $$anonymous$$aybe someone can say what's wrong in these instructions:
Create a scene with 2 cameras.
Call one of them CA$$anonymous$$ERA 1 and set its depth to 1.
Call the other CA$$anonymous$$ERA 2 and set its depth to 2.
Put a cube in a place where CA$$anonymous$$ERA 1 can see it.
Put a sphere in a place where CA$$anonymous$$ERA 2 can see it.
In the project panel, make a new shader. Leave its name as the default "NewShader".
Double-click that shader and once your code editor has opened, replace its contents with this line: Shader "NewShader" { SubShader { Color$$anonymous$$ask 0 Pass {} }}.
Save it.
Back in Unity, in the project panel, make a new material.
Drag the shader onto that material.
Drag that material onto the sphere.
Press Play.
One final note, I did find that when shaders aren't functioning as you expect, sometimes it fixes the problem if you select the "Reimport" option on the material that a shader is applied to. I had to do this just now in fact, for that scene shown above in that older post.
Can you email the scene to staakman@gmail.com ( maybe if it's big through http://www.wetransfer.com )? Would love to check what's going wrong. Thanks in advance.
Checking the scene about to send it to you, I noticed strange behaviour. Simply scaling an object was making the effect switch between functional and non-functional.
So perhaps this is a Unity bug after all, and it might have something to do with this thread:
http://forum.unity3d.com/threads/192467-Unity-4-2-submesh-draw-order
I would also appreciate if you could email me the scene as well. If either you or staakman could send it to gardnedn@miamioh.edu that would be very helpful. Thanks again!
Answer by liszto · Mar 21, 2013 at 12:22 PM
I think this can help you to do what you want :
http://blog.almostlogical.com/2009/12/11/render-to-texture-unity3d/
Thanks. Totally bummed that you have to have Pro to use it. :(