简体   繁体   English

SceneKit:是否可以在透明对象上投射阴影?

[英]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. 我想在OSX上的SceneKit中在一个完全透明的平面上投下阴影。 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? 你对我如何用苹果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. 在3D模型下创建一个白色平面,注意平面的颜色必须是纯白色。
  2. set blend mode of plane's material to SCNBlendModeMultiply . 将平面材质的混合模式设置为SCNBlendModeMultiply
  3. set light model of plane's material to SCNLightingModelLambert . 将平面材质的光模型设置为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. 这是有效的,因为任何颜色乘以白色(1,1,1)都会返回自身而朗伯光模型不会考虑定向光,因此平面将始终是看起来像透明的背景颜色。 Another benefit of this solution is you don't need change light's shadow rendering mode. 此解决方案的另一个好处是您不需要更换灯光的阴影渲染模式。

For people who used to inspector of Xcode. 对于习惯Xcode检查员的人。

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


According to SceneKit: What's New . 根据SceneKit:什么是新的

First, add a plane under you model. 首先,在模型下添加一个平面。 Then prevent it from writing to colorBuffer. 然后阻止它写入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): 对于地板(即对象下面的SCNFloor):

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 在第二个图像上,应用CIColorInvertCIMaskToAlpha核心图像过滤器
  6. Using Core Image apply the Alpha Mask to the first render. 使用Core Image将Alpha Mask应用于第一个渲染。

You'll get an image with a correct Alpha channel, and transparent shadows. 您将获得具有正确Alpha通道和透明阴影的图像。 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. 阴影可能会变得更亮,并且唯一的方法是将其渲染为另一个图像,并在Mask to Alpha步骤之后用黑色填充它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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