3
$\begingroup$

I have a model of an object and a ground

Object with the ground

and I need to separate the two into two different render images. First image -- object with a transparent shadow, second image -- ground without an object. Then I need to place the resulting images in Unity on top of each other so that they would look the same as the render of an object unseparated from the ground.

I managed to make a render of the object with a shadow using render layers and a "shadow catcher" feature. But when I try to combine it in Unity or Photoshop with a render of a ground, the shadow looks too dark (much darker than in a combied render). Surprisingly, when I mix an object with a ground in a Blender compositor, the shadow looks exactly right:

Alpha over in Blender works different from Photoshop and Unity

So here are the questions:

  1. Is it possible to make a shadow lighter using shadow catcher? I tried to make a shadow catcher material semi-transparent, but the shadow stayed the same. I also tried to fix the issue with adding a third render -- a render of object without a shadow: I placed it on top, the layer of an object with a shadow in the middle (with the opacity of 50% for a lighter shadow) and the render of a ground in the bottom. Unfortunately it renders some artifacts on the fringes of an object. I also tried to make shadow dimmer in Blender compositor using object ID mask, but again it renders some artifacts on the fringe of an object. The same goes with the method of dividing image with a shadow on the image without a shadow and then placing object on top.

  2. Why does blender overlaying method differ from method in Photoshop or Unity? (I tried to convert renders from standard to premultiplied and from premultiplied to standard, but it did not change much).

  3. Is it possible at all to simulate Photoshop blending algorithm in Blender compositor, so that I will have an idea of how the blending would look in Photoshop or unity without using them?

Would be grateful for any advice.

$\endgroup$
  • $\begingroup$ What file format was used for exporting images into Photoshop/Unity? What options were used for the export? $\endgroup$ – Mr Zak Jul 9 '17 at 19:02
  • 2
    $\begingroup$ If you can do it in blender, why do you need photoshop? The main difference from blender and photoshop is that blender does the compositing using linear values before the color management transform. Photoshop is using display referred values that presume some form of transform (or gamma curve) $\endgroup$ – cegaton Jul 9 '17 at 19:38
  • 2
    $\begingroup$ @cegaton , I can merge two layers in Blender compositor, but there is no point because I need those images to be separate layers in Unity. And I only mentioned Photoshop because it has the same overlaying algorithm as Unity. $\endgroup$ – Ivan Jul 9 '17 at 20:17
  • 1
    $\begingroup$ @ Mr Zak , I kind of solved the issue thanks to your and @cegaton advises. I just found a way to make Unity blend images in linear space, the same way Blender apparently does it. Not shure yet if it will backfire somehow, but at least I know what direction to look further. $\endgroup$ – Ivan Jul 9 '17 at 21:34
  • 1
    $\begingroup$ @Ivan please write an answer, others users that run into this issue will surely appreciate learning form you. $\endgroup$ – cegaton Jul 9 '17 at 23:29
6
$\begingroup$

1) It is possible with the compositor. You need to pull a pass with the shadow catcher object only, modify its alpha and composite it. However, compositing back the foreground object with the shadow catcher pass might be tricky, specially in the edges where the solid object meets the shadow. A useful tip in this case is to work with both alphas (solid foreground's and catcher's) and make sure they are combined properly rather than working with RGBA. Check whether those alphas are correlated or uncorrelated, depending on your setup.

2) As cegaton pointed out and you realized already, the reason for the difference is blending images in a linear space (blender) vs. blending images in a non-linear space (photoshop and others). Blender works with linear images all the way. What you get from the compositor is linear, even though the display transform makes the final composite non-linear and also saving as byte-images (like PNG, JPG, etc. does ,as 8-bit images need to be gamma-corrected to avoid artifacts. So, even when images are converted to non-linear in the end, all the blending operations inside the compositor are performed on linear data with associated alpha (also called pre-multiplied). Photoshop does it completely different, for a number of reasons, being legacy the main one: Back in the day when it was only possible to edit 8-bit images, it made sense to work with gamma-corrected sources with unassociated alpha (like PNGs). Things have changed since, but Photoshop kept the non-linear editing and a lot of graphics applications followed its lead doing the same. It's good to keep in mind that linear blending works closer to reality than non-linear blending, so take Blender's result as the right one (which means that you should try making Photoshop look like Blender, not the other way around).

3) Yes it is! You need to turn your linear associated images to non-linear unassociated ones. With the alpha convert node, you have to make the buffers unassociated (premul to straight), then with a gamma correct node "bend" the linear image to 0.4545 (1/2.2). The result will be merged with an alpha-over node (make sure you mark the convert premul checkbox, as the input is unassociated), and right after that node a gamma node set to 2.2 will take the result back to linear (which is necessary as Blender colour management expects linear to apply the display transform and the output gamma-correction.

Check the following image, it comes with a bonus: Did you see the ugly dark fringe around the foreground? That's one of the nasty effects of alpha blending non-linear images ;-) simulating non-linear alpha blending in Blender's compositor

$\endgroup$
  • 2
    $\begingroup$ Pretty much nails it all. One caveat: Remember you can't encode a shadow into an alpha channel. ;) $\endgroup$ – troy_s Jul 12 '17 at 3:38
  • $\begingroup$ I tend to agree, but the shadow catcher object is rendered to RGBA when you render a beauty pass, and the OP asked about how to composite that over a background. $\endgroup$ – Gez Jul 12 '17 at 12:14
  • 1
    $\begingroup$ It's important to keep in mind that the shadow catcher is a hack to simplify the process of producing a shadow pass that can be used with the beauty pass. A proper shadow can't be composited with alpha blending and as @troy_s pointed out, it can't even be properly encoded into an alpha channel. $\endgroup$ – Gez Jul 12 '17 at 14:00
  • $\begingroup$ @Gez This alpha thingy keeps backfiring :) $\endgroup$ – aliasguru Jul 12 '17 at 17:32
  • 3
    $\begingroup$ @aliasguru Yes, but the alpha-team never sleeps. :-D $\endgroup$ – Gez Jul 12 '17 at 17:53
1
$\begingroup$

Short answer is that one should change Unity render mode to "Linear" (Edit -> Project Settings -> Player -> Other Settings -> Linear rendering), then all the shadows appear as light as in Blender (no similar option in Photoshop though).

Long answer: as far as I could understand, the problem is that all the images stored on any computer undergo a certain transformation called "gamma transformation" before being shown on a screen. Blender mixes images before applying gamma transform (it applies gamma transformation to the result of the mix), and Photoshop / Unity (in default mode) mix images only after each image is being transformed.

Blender way is physically accurate, but Photoshop way is widely used as well.

Here is a good article abount linear vs gamma space:http://filmicworlds.com/blog/linear-space-lighting-i-e-gamma/ Here is an article abount linear space rendering in Unity: https://docs.unity3d.com/530/Documentation/Manual/LinearLighting.html

Thanks for @cegaton and @ Mr Zak for pointing me to linear space problem.

Although I am now able to recreate accurate shadows in unity, I still dont know how to control the intencity of shadows in blender shadow catcher, should I need to do so in a future (according to my research there is not such a way).

Also there appears to be no way to overlay images in Blender using "gamma space". I think Blender is lacking in this regard -- "linear blending" is physically accurate, but gamma blending is used in many programms, so it would be useful to have one in Blender.

$\endgroup$
  • 1
    $\begingroup$ This is utterly false and confuses the issue. The issue is the encoding of alpha when used as a shadow; you cannot encode a shadow via the alpha channel hack in any nonlinear manipulation reference. It is impossible. $\endgroup$ – troy_s Aug 6 '17 at 2:20

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.