IOS:在场景中放一盏灯会让一切都变黑吗? [英] AR with iOS: putting a light in the scene makes everything black?

查看:24
本文介绍了IOS:在场景中放一盏灯会让一切都变黑吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,当我在Swift/Xcode中添加到我的ARScene中时,我正在拼命尝试在我的对象上实现这种温暖的照明-温暖的照明和周围的小发光灯光:

明确地说,我不希望添加到场景中的对象看起来像是属于周围的房间。我想让他们脱颖而出/看起来温暖而光彩照人。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
        }
    }
}

我已经尝试了增加我的对象的纹理和所有东西的发射,但没有任何东西能达到这个效果。添加灯光只会将对象变为黑色/无颜色。

这里出了什么问题?

推荐答案

创建这种发光的红色霓虹灯会产生ARKIT。您可以执行以下操作。

需要创建一个reactor.scnp(SceneKit粒子系统文件)并进行以下更改以创建发光的红色光晕。该文件应与文件stark.png一起放在游乐场的Resources目录中

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

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

  2. 速度系数=0.1

  3. 选中启用照明

  4. 发射器形状=球形

  5. 图像大小=0.5

  6. 图像强度=0.1

  7. 模拟速度系数=0.1

注意:以下代码是我用于测试的游乐场应用程序。您只需点击任意位置即可将霓虹灯添加到场景中。您可以放置任意数量的霓虹灯。

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

这篇关于IOS:在场景中放一盏灯会让一切都变黑吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆