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

前端 未结 3 1472
梦如初夏
梦如初夏 2021-01-15 05:50

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.

相关标签:
3条回答
  • 2021-01-15 06:14

    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.

    0 讨论(0)
  • 2021-01-15 06:19

    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.

    0 讨论(0)
  • 2021-01-15 06:27

    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)
    
    0 讨论(0)
提交回复
热议问题