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

余生长醉 提交于 2019-12-08 01:36:43

问题


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?


回答1:


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.




回答2:


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.




回答3:


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)


来源:https://stackoverflow.com/questions/30975695/scenekit-is-it-possible-to-cast-an-shadow-on-an-transparent-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!