Jump to content
  • Advertisement
Sign in to follow this  
j00hi

OpenGL How to implement Texture Projectors (projective texture mapping) like in Unity

This topic is 2219 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm asking myself how to implement something like the projectors in Unity using OpenGL or DirectX.

I know how to do projective texture mapping, but in the implementations I've seen, the projected texture is passed as an additional texture to a shader - so it's basically multitexturing. If I would render a scene like in the attached screenshot, I would pass the grass texture and one "blob"-texture to my shader and render that.

Unity, however, supports an arbitrary number of projectors. The scene on the attached screenshot shows a terrain with 16 projectors. But Unty supports way more than that - I could easily use 100 projectors, and all would be rendered, correctly perspectively projected onto the terrain, including correct occlusions if a "blob" is projected, for example, over a mountain ridge. It's perfectly projected onto the underlying geometry always.

So, Unity obviously uses a different approach than the one I know, which is passing all the projected textures (and their texture-matrices, etc.) to the shader.
Which one?
How could I implement the same functionality using OpenGL or DirectX?

Share this post


Link to post
Share on other sites
Advertisement
It depends on the rendering technique.

When using a deferred render you can project a texture using the g-buffer by simple rendering a cube and using a little projection math in your shader . Humus has an article about volume decals over here. I use this technique too, rendering blood decals with it. You can use the stencil buffer to restrict them to certain surfaces (only terrain, no characters etc.). Edited by Ashaman73

Share this post


Link to post
Share on other sites
Thanks for the link!

I don't think that Unity is using a deferred renderer, because those projectors work on mobile platforms (iOS, Android) as well. According to the license comparison (http://unity3d.com/unity/licenses) deferred rendering is not supported on mobile at all.

Maybe there is another approach which isn't based upon a deferred renderer?

Share this post


Link to post
Share on other sites
Hmm, I have also thought about those two possibilities, but I think, there are problems with both of them:

1) tri-overlay and use alpha blending:
That wouldn't represent underlying geometry exactly, would it?
(The Unity projectors are exactly on the geometry, every pixel)

2) Precalculate the texture (on-the-fly):
Would this be even possible in realtime? If there were lots of objects and lots of projectors, I think that would have a huge impact on performance.
Furthermore, I think that wouldn't work for every texture-combination. The terrain could have a repeating grass texture, and the projector could be a non-repeating texture => how would you precalculate those? I doubt that would be possible in a fast/easy way.

Share this post


Link to post
Share on other sites
For 1) you can clip the affected geometry against the projector frustum, which should produce a near-exact result. For float inaccuracies you will likely need a small depth bias. However, if you have separate LODs of for example terrain, you would need to prepare the projector geometry for each LOD separately so that the geometries will also match when LOD changes.

For 2) you can use a separate UV coordinate channel for the projectors, and a shader which will blend the original texture, and the generated texture which combines all the projectors. The generation of the combined texture could happen once on level load time, assuming it was only used for non-moving objects.

Share this post


Link to post
Share on other sites

2) Precalculate the texture (on-the-fly):
Would this be even possible in realtime? If there were lots of objects and lots of projectors, I think that would have a huge impact on performance.
Furthermore, I think that wouldn't work for every texture-combination. The terrain could have a repeating grass texture, and the projector could be a non-repeating texture => how would you precalculate those? I doubt that would be possible in a fast/easy way.

I do it in my engine. My level terrain texture has the size of 16k x 16x texels, which would be roughly 1 gb (3 channels, no compression, mipmapping ). But I've subdivided my terrain in patches and calculate it on-the-fly (even on the CPU not GPU which would be much faster), you can download the pre-alpha version from my website to see it in action.

But the real question is, what are your requirements ? There're several solutions to a single problem and trying to copy a technique is not always the best solution. Do you want to write an engine for mobiles ?

Share this post


Link to post
Share on other sites
Oh nice! Then I guess I should take a look into that technique.

Yes, I'm mainly developing for mobiles. And in my Unity projects, I'm using projectors a lot.
I use them for:
-) grenade craters on the terrain, after a grenade exploded
-) blob shadows for enemies
-) hexagonal grid projection onto the terrain (using a repeating texture and an orthogonal projector)

Basically I could use an orthogonal projector for all those, but I want to have the possibility of perspective projection, too.

Share this post


Link to post
Share on other sites
Was there any answer to this? I'm very curious on how this is implemented too.

I noticed someone said:
2) Precalculate the texture (on-the-fly):

Can you go into detail on how that's done?

Thanks
Jeff

Share this post


Link to post
Share on other sites
I've asked in the Unity forum, how they have implemented projectors.

Here's the question:
http://answers.unity3d.com/questions/319622/what-is-the-algorithm-used-behind-projectors.html

There are some very interesting comments and results.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!