Jump to content

  • Log In with Google      Sign In   
  • Create Account

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


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 j00hi   Members   -  Reputation: 106

Like
0Likes
Like

Posted 13 September 2012 - 03:51 AM

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?

Attached Thumbnails

  • Unity3D_projectors.PNG


Sponsor:

#2 Ashaman73   Crossbones+   -  Reputation: 7500

Like
0Likes
Like

Posted 13 September 2012 - 05:03 AM

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, 13 September 2012 - 05:05 AM.


#3 j00hi   Members   -  Reputation: 106

Like
0Likes
Like

Posted 13 September 2012 - 05:56 AM

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?

#4 Ashaman73   Crossbones+   -  Reputation: 7500

Like
0Likes
Like

Posted 13 September 2012 - 06:45 AM

An other way is to create a tri-overlay and use alpha blending or to precalculate the texture (on-the-fly).

#5 j00hi   Members   -  Reputation: 106

Like
0Likes
Like

Posted 13 September 2012 - 09:34 AM

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.

#6 AgentC   Members   -  Reputation: 1341

Like
0Likes
Like

Posted 14 September 2012 - 01:05 AM

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.

Every time you add a boolean member variable, God kills a kitten. Every time you create a Manager class, God kills a kitten. Every time you create a Singleton...

Urho3D (engine)  Hessian (C64 game project)


#7 Ashaman73   Crossbones+   -  Reputation: 7500

Like
0Likes
Like

Posted 14 September 2012 - 01:31 AM

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 ?

#8 j00hi   Members   -  Reputation: 106

Like
0Likes
Like

Posted 14 September 2012 - 02:53 AM

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.

#9 jeffkingdev   Members   -  Reputation: 771

Like
0Likes
Like

Posted 15 September 2012 - 12:05 PM

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

#10 j00hi   Members   -  Reputation: 106

Like
0Likes
Like

Posted 21 September 2012 - 07:56 AM

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.




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS