首页 文章

如何添加阴影平面和.scn文件之间的差异apple提供和我们创建

提问于
浏览
0

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

1.The image Apple provides
1.The image Apple provides,

2.The file I created (source: turbosquid.com)

2.The file I created (source: turbosquid.com)
谢谢

1 回答

  • 10

    投射阴影有两种方法:

    • 首先在模型(或检测到的平面)下放置一个平面,将其colorBufferWriteMask设置为SCNColorMask(0) . 使用spotlight并将其castsShadow设置为true,将shadowMode设置为延迟 .
    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)
        }
    }
    
    • 在苹果项目中使用具有阴影纹理的平面 . 在scn文件中有一个用于板的阴影平面和用于勺子和杯子的盘子 . 如果你玩它们,你可以看到它 .

    enter image description here

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

相关问题