How to implement Texture Projectors (projective texture mapping) like in Unity
#1 Members - Reputation: 106
Posted 13 September 2012 - 03:51 AM
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?
#2 Members - Reputation: 4604
Posted 13 September 2012 - 05:03 AM
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 Members - Reputation: 106
Posted 13 September 2012 - 05:56 AM
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 Members - Reputation: 4604
Posted 13 September 2012 - 06:45 AM
My game: Gnoblins
Developer journal about Gnoblins
Small goodies: Simple alpha transparency in deferred shader
#5 Members - Reputation: 106
Posted 13 September 2012 - 09:34 AM
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 Members - Reputation: 675
Posted 14 September 2012 - 01:05 AM
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.
#7 Members - Reputation: 4604
Posted 14 September 2012 - 01:31 AM
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.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.
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 ?
My game: Gnoblins
Developer journal about Gnoblins
Small goodies: Simple alpha transparency in deferred shader
#8 Members - Reputation: 106
Posted 14 September 2012 - 02:53 AM
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.
#10 Members - Reputation: 106
Posted 21 September 2012 - 07:56 AM
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.






