为背景清晰的SCNPlane添加阴影的问题

问题描述 投票:0回答:1

我想在 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 = [] 影子消失了!如何能创建一个透明的材质,只显示影子。

enter image description here

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

ios swift scenekit scnnode
1个回答
0
投票

你可以像下面一样把材料的 "颜色 "设置为.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]

0
投票

我发现了一个技巧 另一个答案.

调整地平面的 blendMode 来改变其像素与底层像素的组合方式。

let clearMaterial = SCNMaterial()
// alter how pixels affect underlying pixels
clearMaterial.blendMode = .multiply
// use the simplest shading that shows a shadow (so not .constant!)
clearMaterial.lightingModel = .lambert
// unobstructed parts are render pure white and thus have no effect
clearMaterial.diffuse.contents = UIColor.white

floorPlane.firstMaterial = clearMaterial

你可以在这张图片中看到效果。主平面是不可见的,但影子是可见的。立方体、它的影子和球网格有相同的XZ位置。请注意 "阴影 "如何影响底层几何体和场景背景。

Sample render of invisible plane with shadow

© www.soinside.com 2019 - 2024. All rights reserved.