ARKit - 在平面上绘制3D对象的阴影

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

这是我用来在平面上显示对象的函数。

private func loadScene(path: String) -> SCNNode {

    let spotLight = SCNLight()
    spotLight.type = SCNLight.LightType.probe

    spotLight.spotInnerAngle = 30.0
    spotLight.spotOuterAngle = 80.0
    spotLight.castsShadow = true

    let result = SCNNode()
    result.light = spotLight
    result.position = SCNVector3(-10.0, 20.0, 10.5)
    result.addChildNode(result)

    let scene = SCNScene(named: path)!
    for node in scene.rootNode.childNodes {
        result.addChildNode(node)
    }       
    return result
}

我想在平面上显示阴影,就像这张图片一样。

当我设置如下的聚光灯类型

spotLight.type = SCNLight.LightType.directional

它显示物体本身具有亮/暗阴影,并且不会在表面上留下阴影。

有人可以指导我如何实现输出,如图所示?

swift xcode scenekit augmented-reality arkit
1个回答
4
投票

//在3D模型上添加阴影只需复制粘贴此代码,它将显示为3D模型在地面上的阴影

let flourPlane = SCNFloor()
let groundPlane = SCNNode()
let groundMaterial = SCNMaterial()
groundMaterial.lightingModel = .constant
groundMaterial.writesToDepthBuffer = true
groundMaterial.colorBufferWriteMask = []
groundMaterial.isDoubleSided = true
flourPlane.materials = [groundMaterial]
groundPlane.geometry = flourPlane
//
mainNode.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 = SCNLight.LightType.directional
myNode.light?.color = UIColor.white
myNode.light?.castsShadow = true
myNode.light?.automaticallyAdjustsShadowProjection = true
myNode.light?.shadowSampleCount = 64
myNode.light?.shadowRadius = 16
myNode.light?.shadowMode = .deferred
myNode.light?.shadowMapSize = CGSize(width: 2048, height: 2048)
myNode.light?.shadowColor = UIColor.black.withAlphaComponent(0.75)
myNode.position = SCNVector3(x: 0,y: 5,z: 0)
myNode.eulerAngles = SCNVector3(-Float.pi / 2, 0, 0)
// Add the lights to the container
mainNode.addChildNode(ambientLight)
mainNode.addChildNode(myNode)
// End
© www.soinside.com 2019 - 2024. All rights reserved.