1
$\begingroup$

There are some 3D applications which can cast shadow or silhouette below 3D models. They render pretty fast and smooth. I wonder what kind of technology is the standard procedure to get 3D model shadow/silhouette.

For example is there any C++ library like libigl or CGAL to get shadow/silhouette pretty fast? Or maybe GLSL shading is used? Any hint would be appreciated on the standard technology stack.

Screenshot

$\endgroup$
1
  • 1
    $\begingroup$ This looks like an implementation of "planar shadows" but the "standard" implementation of shadows for rasterized graphics is "shadow mapping". You can google either of these and get a resplendent amount of info on how to implement and use them. $\endgroup$ – pmw1234 Dec 15 '20 at 0:31
3
$\begingroup$

There is a trick to shadows like this, but only this exact usecase. While you can certainly draw a true shadow using many techniques. The trick is you can just scale the object flat onto the floor plane.

So all you need to do is draw the object 2 times one time with a matrix that is scaled to zero on the floor axis. You can also use this technique to draw shadows that arent entirely top down by adding skew to counter the now rotated shadow.

This is super easy to implement. No extra cameras. But works only on one plane. This is a special case of shadow volumes without the need to draw the volumes.

$\endgroup$
1
$\begingroup$

I can't tell you how exactly each 3d modeling software calculates shadows since I don't know their implementations. However, I guess the usual candidates for real-time shadow calculations are used. These are:

To my knowledge, the second one isn't that popular since it is more complicated and usually computationally more expensive. If you just want to cast a silhouette as a shadow onto a flat surface without caring about the correct lighting of other objects that lie in the shadows you can use a simple approach like this:

Select a (single-channel) texture as your render target and clear its content. Now draw all your objects from the top without perspective projection. The fragment shader returns always the same color that must be different from the clear color. In the end, you have a texture that contains all the silhouettes from your objects. Now you only need to render a quad that uses this texture at the correct position into your scene slightly above the flat surface. Set the output color of the fragment shader to black. The alpha channel is either set to 0 if the texture color at the current fragment is equal to the clear color or to a value of your liking if it is not. (This requires blending to be enabled)

This is basically a very specialized and simplified shadow mapping special case that uses a texture instead of a z-buffer.

Note:

I originally suggested using the stencil buffer for this, but it would add an unnecessary indirection, and getting it working as intended can be quite a pain (at least in OpenGL).

$\endgroup$
5
  • $\begingroup$ "Draw all your objects "looking from the top" and populate your *stencil buffer"* Did you mean "Z buffer"? $\endgroup$ – Simon F Dec 3 '20 at 14:57
  • 1
    $\begingroup$ @SimonF I actually meant the stencil buffer. After rendering all the objects, the stencil buffer should contain basically a true or false value for each pixel. However, you still need to transform the data to the camera perspective which most likely requires creating a texture from it or populating the z-buffer. In the latter case, it is probably much easier to use the standard shadow mapping technique. In fact, I think shadow mapping is generally the best approach for this purpose. It's just that the word silhouette always makes me automatically think about stencil testing. $\endgroup$ – wychmaster Dec 3 '20 at 16:31
  • $\begingroup$ After some time of thinking, I think this whole stencil buffer thing is unnecessary and makes everything overly complicated. I'll update my answer as soon as I find the time. $\endgroup$ – wychmaster Dec 3 '20 at 17:35
  • $\begingroup$ I'm reasonably familiar with shadow volume approaches (from development of PowerVR series 1 and 2 (Dreamcast) which had optimised support for volumes) but what you were describing sounded more like a shadow buffer approach, ie. rendering from the light's view. $\endgroup$ – Simon F Dec 4 '20 at 8:41
  • 1
    $\begingroup$ @SimonF I already updated my answer and you are right, what I described was a specialized shadow mapping approach and I never meant it to be something else. However, in my original approach, I unnecessarily wanted to use the stencil buffer to cut out the silhouettes. But it has nothing to do with the stencil buffer based shadow volumes technique. I think this is where the confusion came from. $\endgroup$ – wychmaster Dec 4 '20 at 8:53
1
$\begingroup$

One approach for stuff like this that I just heard is to do raymarching on simple analytical shapes that approximate the mesh, so you basically create an abstract coarse SDF representation of the mesh in question and raymarch some shadows. I have heard Iñigo Quilez mention it and talking on how he gave a conference about it once but I haven't found it, I think it was either Gamescon or SIGGRAPH.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.