如何添加阴影平面以及苹果提供的和我们创建的.scn文件的区别

How to add a shadow plane and the difference between the .scn file apple provide and we create

在我使用Xcode为我的项目创建的.scn文件中,该文件在透视图中看起来是这样的(导弹文件包含在附件中)。但在 apple 在 ARKit 示例项目中提供的 .scn 模型中,平面和文件看起来差别很大。它有一个阴影平面,而且视图看起来也完全不同。所以我的问题是, 1. 如何在我的文件中添加阴影平面,它有什么用。 2.这两个文件有什么区别。一个苹果给了我,一个是我创造的。 3.创建AR应用的最佳实践。

1.The 图片苹果提供

2.The 我创建的文件(来源:turbosquid.com)

谢谢

投射阴影有两种方式:

  1. 首先在模型下放置一个平面(或检测到的平面),将其colorBufferWriteMask设置为SCNColorMask(0)。使用 spotlight 并将其 castsShadow 设置为 true 并将 shadowMode 设置为 deferred。

    override func viewDidLoad() {
    
        // setup ARScene
    
        // Disable automatic lighting
        sceneView.autoenablesDefaultLighting = false
        sceneView.automaticallyUpdatesLighting = false
    
        // create secondary light
        lightNode.light = SCNLight()
        lightNode.light!.type = .omni
        lightNode.position = SCNVector3(x: 0, y: 1, z: 1)
        scene.rootNode.addChildNode(lightNode)
    
        // create main light that cast shadow
        lightNode2.light = SCNLight()
        lightNode2.light!.type = .spot
        lightNode2.position = SCNVector3(x: -1, y: 10, z: 1)
        lightNode2.eulerAngles = SCNVector3(-Float.pi/2, 0, 0)
        lightNode2.light?.castsShadow = true // to cast shadow 
        lightNode2.light?.shadowMode = .deferred // to render shadow in transparent plane
        lightNode2.light?.shadowSampleCount = 64 //remove flickering of shadow and soften shadow
        lightNode2.light?.shadowMapSize = CGSize(width: 2048, height: 2048) //sharpen or detail shadow
        scene.rootNode.addChildNode(lightNode2)
    
        // create ambient light
        ambientLightNode.light = SCNLight()
        ambientLightNode.light!.type = .ambient
        ambientLightNode.light!.color = UIColor.darkGray
        scene.rootNode.addChildNode(ambientLightNode)
    }
    
    // match the lighting with real world
    func renderer(_ renderer: SCNSceneRenderer, updateAtTime time: TimeInterval) {
        if let estimate = sceneView.session.currentFrame?.lightEstimate{
            lightNode2.light?.intensity = estimate.ambientIntensity
            lightNode2.light?.temperature = estimate.ambientColorTemperature
            lightNode.light?.intensity = estimate.ambientIntensity/3
            lightNode.light?.temperature = estimate.ambientColorTemperature
        }
    }
    
    // place transparent plane to capture shadow
    func renderer(_ renderer: SCNSceneRenderer, didAdd node: SCNNode, for anchor: ARAnchor) {
        if anchor is ARPlaneAnchor{ 
    
            // place object on the detected plane anchor
    
            // place shadow plane under your object
            let shadowPlane = SCNPlane(width: 2, height: 2)
            shadowPlane.materials.first?.colorBufferWriteMask = SCNColorMask(rawValue:0)
            let shadowPlaneNode = SCNNode(geometry: shadowPlane)
            planeNode.transform = SCNMatrix4MakeRotation(-Float.pi/2, 1, 0, 0) // because plane is created vertical
            node.addChildNode(shadowPlaneNode)
        }
    }
    
  2. 在苹果项目中使用带有阴影纹理的平面。盘子的scn文件和勺子和杯子的盘子里都有一个阴影平面。和他们一起玩就知道了

唯一不同的是苹果知道如何巧妙地让他们的模型看起来自然。使用阴影平面,PBR。
对于最佳实践,这取决于您要实现的目标。