Sign in to follow this  
j00hi

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

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
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 [url="http://www.humus.name/index.php?page=3D"]here[/url]. 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
[quote name='j00hi' timestamp='1347550472' post='4979750']
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.
[/quote]
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
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Announcements

  • Forum Statistics

    • Total Topics
      628367
    • Total Posts
      2982284
  • Similar Content

    • By test opty
      Hi all,
       
      I'm starting OpenGL using a tut on the Web. But at this point I would like to know the primitives needed for creating a window using OpenGL. So on Windows and using MS VS 2017, what is the simplest code required to render a window with the title of "First Rectangle", please?
       
       
    • By DejayHextrix
      Hi, New here. 
      I need some help. My fiance and I like to play this mobile game online that goes by real time. Her and I are always working but when we have free time we like to play this game. We don't always got time throughout the day to Queue Buildings, troops, Upgrades....etc.... 
      I was told to look into DLL Injection and OpenGL/DirectX Hooking. Is this true? Is this what I need to learn? 
      How do I read the Android files, or modify the files, or get the in-game tags/variables for the game I want? 
      Any assistance on this would be most appreciated. I been everywhere and seems no one knows or is to lazy to help me out. It would be nice to have assistance for once. I don't know what I need to learn. 
      So links of topics I need to learn within the comment section would be SOOOOO.....Helpful. Anything to just get me started. 
      Thanks, 
      Dejay Hextrix 
    • By mellinoe
      Hi all,
      First time poster here, although I've been reading posts here for quite a while. This place has been invaluable for learning graphics programming -- thanks for a great resource!
      Right now, I'm working on a graphics abstraction layer for .NET which supports D3D11, Vulkan, and OpenGL at the moment. I have implemented most of my planned features already, and things are working well. Some remaining features that I am planning are Compute Shaders, and some flavor of read-write shader resources. At the moment, my shaders can just get simple read-only access to a uniform (or constant) buffer, a texture, or a sampler. Unfortunately, I'm having a tough time grasping the distinctions between all of the different kinds of read-write resources that are available. In D3D alone, there seem to be 5 or 6 different kinds of resources with similar but different characteristics. On top of that, I get the impression that some of them are more or less "obsoleted" by the newer kinds, and don't have much of a place in modern code. There seem to be a few pivots:
      The data source/destination (buffer or texture) Read-write or read-only Structured or unstructured (?) Ordered vs unordered (?) These are just my observations based on a lot of MSDN and OpenGL doc reading. For my library, I'm not interested in exposing every possibility to the user -- just trying to find a good "middle-ground" that can be represented cleanly across API's which is good enough for common scenarios.
      Can anyone give a sort of "overview" of the different options, and perhaps compare/contrast the concepts between Direct3D, OpenGL, and Vulkan? I'd also be very interested in hearing how other folks have abstracted these concepts in their libraries.
    • By aejt
      I recently started getting into graphics programming (2nd try, first try was many years ago) and I'm working on a 3d rendering engine which I hope to be able to make a 3D game with sooner or later. I have plenty of C++ experience, but not a lot when it comes to graphics, and while it's definitely going much better this time, I'm having trouble figuring out how assets are usually handled by engines.
      I'm not having trouble with handling the GPU resources, but more so with how the resources should be defined and used in the system (materials, models, etc).
      This is my plan now, I've implemented most of it except for the XML parts and factories and those are the ones I'm not sure of at all:
      I have these classes:
      For GPU resources:
      Geometry: holds and manages everything needed to render a geometry: VAO, VBO, EBO. Texture: holds and manages a texture which is loaded into the GPU. Shader: holds and manages a shader which is loaded into the GPU. For assets relying on GPU resources:
      Material: holds a shader resource, multiple texture resources, as well as uniform settings. Mesh: holds a geometry and a material. Model: holds multiple meshes, possibly in a tree structure to more easily support skinning later on? For handling GPU resources:
      ResourceCache<T>: T can be any resource loaded into the GPU. It owns these resources and only hands out handles to them on request (currently string identifiers are used when requesting handles, but all resources are stored in a vector and each handle only contains resource's index in that vector) Resource<T>: The handles given out from ResourceCache. The handles are reference counted and to get the underlying resource you simply deference like with pointers (*handle).  
      And my plan is to define everything into these XML documents to abstract away files:
      Resources.xml for ref-counted GPU resources (geometry, shaders, textures) Resources are assigned names/ids and resource files, and possibly some attributes (what vertex attributes does this geometry have? what vertex attributes does this shader expect? what uniforms does this shader use? and so on) Are reference counted using ResourceCache<T> Assets.xml for assets using the GPU resources (materials, meshes, models) Assets are not reference counted, but they hold handles to ref-counted resources. References the resources defined in Resources.xml by names/ids. The XMLs are loaded into some structure in memory which is then used for loading the resources/assets using factory classes:
      Factory classes for resources:
      For example, a texture factory could contain the texture definitions from the XML containing data about textures in the game, as well as a cache containing all loaded textures. This means it has mappings from each name/id to a file and when asked to load a texture with a name/id, it can look up its path and use a "BinaryLoader" to either load the file and create the resource directly, or asynchronously load the file's data into a queue which then can be read from later to create the resources synchronously in the GL context. These factories only return handles.
      Factory classes for assets:
      Much like for resources, these classes contain the definitions for the assets they can load. For example, with the definition the MaterialFactory will know which shader, textures and possibly uniform a certain material has, and with the help of TextureFactory and ShaderFactory, it can retrieve handles to the resources it needs (Shader + Textures), setup itself from XML data (uniform values), and return a created instance of requested material. These factories return actual instances, not handles (but the instances contain handles).
       
       
      Is this a good or commonly used approach? Is this going to bite me in the ass later on? Are there other more preferable approaches? Is this outside of the scope of a 3d renderer and should be on the engine side? I'd love to receive and kind of advice or suggestions!
      Thanks!
    • By nedondev
      I 'm learning how to create game by using opengl with c/c++ coding, so here is my fist game. In video description also have game contain in Dropbox. May be I will make it better in future.
      Thanks.
  • Popular Now