- Home /
Rendering virtual objects within a real scene
I have depth maps and color images (both of them are from a camera), and I create some virtual objects to interact with real objects in the scene. How to render both of them (in Unity)? Maybe a shader will work. But I am relatively new to Cg. In a pixel shader, how to compare depth values of different objects which are projected to one same pixel? Thanks.
So what in my mind is this: A color image with depth data at each pixel (get from a camera), and several virtual objects in Unity. I want to overlay them. I want a pixel shader to check depth buffer for every pixel. If the depth value of the pixel from color image is smaller, rendering that pixel. Or if the virtual object is closer, rendering the virtual object.
Any chance this is $$anonymous$$inect related? Or OpenNI in any case? ZigFu's stuff does what you ask, but only if you're doing $$anonymous$$inect.
I am not using $$anonymous$$inect & OpenNI for now. Thanks.
I am not sure what you are asking, but if you are talking about procedurally generating objects dynamically at runtime then you should take a look at the `$$anonymous$$esh` class.
Answer by spalmer · Jun 30, 2015 at 04:09 PM
I haven't seen any code from you yet, not going to write a shader for you, but your idea seems reasonable. Would be easy as a post-processing effect, just plug in all four images and select based on the depths. Doing this will require Unity Pro. You can use the Depth of Field image effect as an example of how to obtain the camera depth texture from script and access the depth buffer from an image effect shader. A depth texture appears to the shader as a texture of single unorm value in the red channel. Simple comparing the depths to see which is smaller should be simple enough if that's all you need.
Unity 5 has barely any difference in features between Pro and Personal edition... you don't require Pro to do this.
Your answer
![](https://koobas.hobune.stream/wayback/20220613093412im_/https://answers.unity.com/themes/thub/images/avi.jpg)
Follow this Question
Related Questions
How to force the compilation of a shader in Unity? 5 Answers
Cg Language incompatibility Desktop VS GLSL Android 0 Answers
Can anyone help me with reflective shader with fall off property? 0 Answers
Sprite deformation in shader? 0 Answers
Shader: get nearest point light's color, direction and general custom lighting questions 0 Answers