- Home /
2d Shader: How to show camera view on a sprite?
I want to write a shader that shows the camera view from the same perspective & ratio (not fit to the shape of the object).
I want to achieve the grander aim of starting with a background (e.g. bricks) and have a shape “punch a see-through hole” that I can move around and place where I want.
Background (Repeated): (series of bricks- I can only upload 2 images so just imagine it!)
Foreground Object:
Hole shape:
I want to write a shader for this hole shape, so that it shows the exact view of a camera parented to the main camera that is set up to see only background layers.
The part I am missing is the actual shader. I have almost no shader experience but am willing to learn. Point me in the right direction?
Well, I was in the same spot you are in now some time ago. I was scared of shader code but I absolutely needed a custom shader which wasn't available so I just dug in and now I can write shaders. It's not as hard as you might think. The Unity website has great tutorials on writing basic and advanced fragment and vertex shaders, so why not start there and try to understand how a shder is built and what each part does. It's actually really simple and it will open all kind of possibilities for your future projects.
I'm not clear what the camera should be showing "around" the holes. If you just want a solid color or static image around the holes, no need for a shader: you can simply child a sprite object to the camera, where the hole is transparent (color.a=0.0 is completely transparent), and the rest is your solid color/static image (color.a=1.0 is opaque). Just make sure it's offset to slightly in front of the camera (probably the same amount as the camera's near plane.)
oops: didn't mean to make this comment a reply.. whatever.
The transparent hole will still show the brick layer behind it. I need to see "through" the brick wall and into the background.
I've looked into masking options for sprites in Unity, but it seems the way to do this is through shaders.
Answer by JonPQ · Sep 20, 2017 at 04:55 PM
make a texture that is of type render texture. create a camera assign it to render to that render texture. make a material.. use the render texture as the material. Now you can put that material on an object or a sprite material.... that gets you half way there. Next on the material, you'll want a shader that can provide a second texture as a mask / alpha to cutout the first one... You can probably find that on Unity Shader wiki website at a guess ;)
Your answer
Follow this Question
Related Questions
Get the screen coordinates of an object altered by Fisheye 0 Answers
How to get the value of a pixel of a noise node if it's greater than some threshold? 0 Answers
How to make Standard Shader _Color property be Per Instance Data? 0 Answers
Shader graph node that return the value of the input node if it's greater than some value? 0 Answers