SceneKit: Is it possible to cast an shadow on an Transparent Object?

3.2k Views Asked by At

i am trying to cast an shadow on an totally transparent plane in SceneKit on OSX. I am struggling with this problem since several hours and do not come to any solution.

My Purpose is to generate an Screenshot of several objects with an transparent background and just the shadow on an invisible Plane.

Do you have any suggestions for me how i can make this with apples SceneKit?

Do i have to program my own shader, can i make this work with shadermodifiers or can i use built in functionallity?

3

There are 3 best solutions below

0
On BEST ANSWER

I do not have an answer to your question, however I have a workaround:

  1. Render your scene and keep the image in memory
  2. Change all the materials in your object for pure black, no specular
  3. Change the plane and the sky to a fully white material, lights to white
  4. Render the scene to another image
  5. On the second image, apply the CIColorInvertand CIMaskToAlpha Core Image filters
  6. Using Core Image apply the Alpha Mask to the first render.

You'll get an image with a correct Alpha channel, and transparent shadows. You will need to tweak the materials and lights to get the results you want.

The shadow may become lighter on the edges, and the only way around that is rendering it as yet another image, and filling it with black after the Mask to Alpha step.

1
On

UPDATE:

I find an alternative solution for anyone who needs:

  1. create a white plane under 3D model, note that the color of plane must be pure white.
  2. set blend mode of plane's material to SCNBlendModeMultiply.
  3. set light model of plane's material to SCNLightingModelLambert.

This works because any color multiply white color (1, ,1, 1) return itself And lambert light model will not take account of directional light, So the plane will always be background color which look like transparent. Another benefit of this solution is you don't need change light‘s shadow rendering mode.

For people who used to inspector of Xcode.

enter image description here enter image description here


According to SceneKit: What's New.

First, add a plane under you model. Then prevent it from writing to colorBuffer. Second, change your light model's shadow rendering mode to deferred. Notice that you must use light which can cast shadows. plane's material inspector

light's attributes inspector

1
On

Oily Guo, your solution works. Here the solution is in code:

Configuration of the light source:

light.shadowColor = UIColor(red: 0, green: 0, blue: 0, alpha: 0.4)
light.shadowMode = .deferred

And for the floor (ie. SCNFloor underneath your objects):

material.diffuse.contents = UIColor.white
material.colorBufferWriteMask = SCNColorMask(rawValue: 0)