ios - 向具有清晰背景的 SCNPlane 添加阴影的问题

标签 ios swift scenekit scnnode

我正在尝试在 SCNPlane 上添加阴影,一切正常,但我无法使 SCNPlane 透明以仅显示阴影而不显示白色背景。这是代码:

        let flourPlane = SCNPlane()
        let groundPlane = SCNNode()


        let clearMaterial = SCNMaterial()
        clearMaterial.lightingModel = .constant
        //clearMaterial.colorBufferWriteMask = []
        clearMaterial.writesToDepthBuffer = true
        clearMaterial.transparencyMode = .default

        flourPlane.materials = [clearMaterial]

        groundPlane.scale = SCNVector3(200, 200, 200)
        groundPlane.geometry = flourPlane
        groundPlane.castsShadow = false
        groundPlane.eulerAngles = SCNVector3Make(-Float.pi/2, 0, 0)
        groundPlane.position = SCNVector3(x: 0.0, y: shadowY, z: 0.0)
        node.addChildNode(groundPlane)

        // Create a ambient light
        let ambientLight = SCNNode()
        ambientLight.light = SCNLight()
        ambientLight.light?.shadowMode = .deferred
        ambientLight.light?.color = UIColor.white
        ambientLight.light?.type = SCNLight.LightType.ambient
        ambientLight.position = SCNVector3(x: 0,y: 5,z: 0)

        // Create a directional light node with shadow
         let myNode = SCNNode()
        myNode.light = SCNLight()
        myNode.light?.type = .directional
        myNode.light?.castsShadow = true
        myNode.light?.automaticallyAdjustsShadowProjection = true
        myNode.light?.shadowSampleCount = 80
        myNode.light?.shadowBias = 1
        myNode.light?.orthographicScale = 1
        myNode.light?.shadowMode = .deferred
        myNode.light?.shadowMapSize = CGSize(width: 2048, height: 2048)
        myNode.light?.shadowColor = UIColor.black.withAlphaComponent(0.5)
        myNode.light?.shadowRadius = 10.0
        myNode.eulerAngles = SCNVector3Make(-Float.pi/2, 0, 0)
        node.addChildNode(ambientLight)
        node.addChildNode(myNode)

当我添加 clearMaterial.colorBufferWriteMask = []影子消失!如何创建透明 Material 以仅显示阴影。

enter image description here

白色区域是 SCNPlane,红色是背景。

最佳答案

您可以将 Material “颜色”设置为 .clear,如下所示:

extension SCNMaterial {
convenience init(color: UIColor) {
    self.init()
    diffuse.contents = color
}
convenience init(image: UIImage) {
    self.init()
    diffuse.contents = image
}
}

let clearColor = SCNMaterial(color: .clear)
flourPlane.materials = [clearColor]

关于ios - 向具有清晰背景的 SCNPlane 添加阴影的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61676888/

相关文章:

ios - 如何在 swift 中设置 AppDelegate 进行推送通知

ios - Swift iOS SceneKit 如何使用 NSSecureCoding 对 SCNMatrix4 进行安全编码?

swift - spritekit如何有选择地缩放节点

php - 使用 REST API 快速上传图片

ios - 如何从 collectionView 中选择项目并将其添加到 SCNScene 中?

android - 如何在移动应用程序中实现推荐 friend ?

ios - 如何使用其IP地址检查本地WiFi网络中的设备是否已连接?

ios - 场景套件 : Apply transformation on vertices

ios - 返回主视图时,快速模糊效果不会消失

ios - 快速更改 Storyboard