简体   繁体   中英

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

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?

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.

在此输入图像描述 在此输入图像描述


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. 飞机的材料检查员

light的属性检查员

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)

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 CIColorInvert and 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM