AR与iOS:在场景中点亮使一切变黑?

时间:2017-12-27 04:58:17

标签: swift xcode augmented-reality arkit lighting

好吧,当我在Swift / Xcode中添加到我的ARScene时,我拼命想要在我的物体上实现这种温暖的照明 - 温暖的灯光和周围的小发光灯:

enter image description here

要清楚,我不希望我添加到场景中的对象看起来像是属于周围的房间。我希望它们能够脱颖而出,看起来温暖而且焕发光彩。所有关于ARKit的教程都会教你如何模仿实际房间的灯光。

Xcode有几种照明选项,可以从相机聚集的环境中拉出来,因为:

if let lightEstimate = session.currentFrame?.lightEstimate

我可以打印出温暖,强度等等。而且我目前还设置了这些属性以匹配房间的光线:

sceneView.automaticallyUpdatesLighting = true
extension ARSCNView {

    func setup() { //SCENE SETUP
        antialiasingMode = .multisampling4X
        autoenablesDefaultLighting = true
        preferredFramesPerSecond = 60
        contentScaleFactor = 1.3

        if let camera = pointOfView?.camera {
            camera.wantsHDR = true
            camera.wantsExposureAdaptation = true
            camera.exposureOffset = -1
            camera.minimumExposure = -1
            camera.maximumExposure = 3
        }
    }
}

我已经尝试在物体的纹理和所有物体上增加发射,但没有任何效果。添加灯光只会将对象变为黑色/无颜色。

这里有什么问题?

2 个答案:

答案 0 :(得分:4)

在ARKit中创建这种发光的红色霓虹灯效果。您可以执行以下操作。

enter image description here

您需要创建一个reactor.scnp(scenekit粒子系统文件)并进行以下更改以创建发光的红色光环。这应该放在操场的Resources目录中,并附带文件spark.png

这些是从默认反应堆类型更改的设置。保留所有其他设置。

  1. 将图像动画颜色更改为红色/橙色/红色/黑色

  2. 速度因子= 0.1

  3. 启用照明检查

  4. Emitter Shape = Sphere

  5. 图片尺寸= 0.5

  6. 图像强度= 0.1

  7. 模拟速度因子= 0.1

  8. 注意:下面的代码是我用于测试目的的游乐场应用程序。您只需点按任意位置即可将霓虹灯添加到场景中。您可以随意放置尽可能多的霓虹灯。

    import ARKit
    import SceneKit
    import PlaygroundSupport
    import SceneKit.ModelIO
    
    class ViewController: NSObject {
    
        var sceneView: ARSCNView
        init(sceneView: ARSCNView) {
            self.sceneView = sceneView
    
            super.init()
    
            self.setupWorldTracking()
            self.sceneView.addGestureRecognizer(UITapGestureRecognizer(target: self, action: #selector(ViewController.handleTap(_:))))
        }
    
        private func setupWorldTracking() {
            if ARWorldTrackingConfiguration.isSupported {
                let configuration = ARWorldTrackingConfiguration()
                configuration.planeDetection = .horizontal
                configuration.isLightEstimationEnabled = true
                self.sceneView.session.run(configuration, options: [])
            }
        }
    
        @objc func handleTap(_ gesture: UITapGestureRecognizer) {
            let results = self.sceneView.hitTest(gesture.location(in: gesture.view), types: ARHitTestResult.ResultType.featurePoint)
            guard let result: ARHitTestResult = results.first else {
                return
            }
            let cylinder = SCNCylinder(radius: 0.05, height: 1)
            cylinder.firstMaterial?.emission.contents = UIColor.red
            cylinder.firstMaterial?.emission.intensity = 1
    
    
            let spotLight = SCNNode()
            spotLight.light = SCNLight()
            spotLight.scale = SCNVector3(1,1,1)
            spotLight.light?.intensity = 1000
            spotLight.castsShadow = true
            spotLight.position = SCNVector3Zero
            spotLight.light?.type = SCNLight.LightType.directional
            spotLight.light?.color = UIColor.white
    
            let particleSystem = SCNParticleSystem(named: "reactor", inDirectory: nil)
            let systemNode = SCNNode()
            systemNode.addParticleSystem(particleSystem!)
    
    
            let node = SCNNode(geometry: cylinder)
    
            let position = SCNVector3Make(result.worldTransform.columns.3.x, result.worldTransform.columns.3.y, result.worldTransform.columns.3.z)
    
            systemNode.position = position
            node.position = position
    
            self.sceneView.scene.rootNode.addChildNode(spotLight)
            self.sceneView.scene.rootNode.addChildNode(node)
            self.sceneView.scene.rootNode.addChildNode(systemNode)
        }
    }
    
    let sceneView = ARSCNView()
    
    let viewController = ViewController(sceneView: sceneView)
    sceneView.autoenablesDefaultLighting = false
    PlaygroundPage.current.needsIndefiniteExecution = true
    PlaygroundPage.current.liveView = viewController.sceneView
    

答案 1 :(得分:0)

如果您在场景中寻找霓虹灯/发光效果......以前对类似问题的回答询问发光/霓虹灯照明应该给你一些指导。

正如您将从提供的答案中看到的那样,sceneKit没有内置的体积光照支持,所有这些方法都可以实现与发光效果类似的效果。

iOS SceneKit Neon Glow

添加"红色"对场景的定向光效...这是使用sceneView.autoenablesDefaultLighting = true的替代方法

let myLight = SCNNode()
myLight.light = SCNLight()
myLight.scale = SCNVector3(1,1,1)
myLight.intensity = 1000
myLight.position = SCNVector3Zero
myLight.light?.type = SCNLight.LightType.directional
myLight.light?.color = UIColor.red

// add the light to the scene

sceneView.scene.rootNode.addChildNode(myLight)

注意:此效果会使场景中的所有对象变得偏红。