【问题标题】:RealityKit only displays dark imagesRealityKit 仅显示深色图像
【发布时间】:2019-11-21 00:13:11
【问题描述】:

我正在使用 ARKit 3 和 RealityKit 开发 iOS 应用程序。我想在包含图像的房间内创建虚拟 2d 平面。所以我用了

Adding a material to a ModelEntity programmatically

但不接近白色的图像会显示得很暗,接近黑色。我也试过UnlitMaterial。它确实创造了更好的结果,但图像仍然很暗。此问题在 JPEG 和 PNG 文件中仍然存在。

在我使用 SceneKit 的早期版本中,我通过以下方式解决了类似的问题

sceneView.automaticallyUpdatesLighting = true
sceneView.autoenablesDefaultLighting = true

我猜图像确实通过这段代码照亮了自己。

如何解决 RealityKit 中的问题?

可能相关:https://forums.developer.apple.com/thread/119265

【问题讨论】:

    标签: ios swift augmented-reality arkit realitykit


    【解决方案1】:

    您可以使用 Reality Composer 中每个 .rcproject 包含的 ARView 自动照明功能。试试这个简单的 iOS 代码,看看它是如何工作的:

    import ARKit
    import RealityKit
    
    class GameViewController: UIViewController {
    
        @IBOutlet var arView: ARView!
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            arView.cameraMode = ARView.CameraMode.ar
    
            arView.renderOptions = [.disableMotionBlur,
                                    .disableDepthOfField]
    
            arView.automaticallyConfigureSession = true
    
            var material = SimpleMaterial()
            material.tintColor = UIColor.yellow
            material.baseColor = try! MaterialColorParameter.texture(
                                              TextureResource.load(named: "yourImg.jpg"))
            material.roughness = MaterialScalarParameter(floatLiteral: 0.1)
            material.metallic = MaterialScalarParameter(floatLiteral: 1.0)
    
            let sceneElement = try! Experience.loadBox()
            sceneElement.scale = SIMD3<Float>(x: 0.5, y: 0.5, z: 0.5)
            sceneElement.position = SIMD3<Float>(x: 1, y: 0.5, z: 0)
    
            let mesh: MeshResource = .generateSphere(radius: 1.0)
            let component = ModelComponent(mesh: mesh,
                                     materials: [material])
    
            sceneElement.components.set(component)
            arView.scene.anchors.append(sceneElement)
        }
    }
    

    或尝试类似的 macOS 代码:

    import AppKit
    import RealityKit
    
    class GameViewController: NSViewController {
    
        @IBOutlet var arView: ARView!
    
        override func awakeFromNib() {
    
            arView.debugOptions = [.showStatistics,
                                   .showPhysics]
    
            arView.environment.background = .color(ARView.Environment.Color.black)
    
            var material = SimpleMaterial()    
            material.tintColor = NSColor.yellow
            material.baseColor = try! MaterialColorParameter.texture(
                                              TextureResource.load(named: "yourImg.jpg"))
            material.roughness = MaterialScalarParameter(floatLiteral: 0.1)
            material.metallic = MaterialScalarParameter(floatLiteral: 1.0)           
    
            let sceneElement = try! TwoScenes.loadSecondScene()
            sceneElement.scale = SIMD3<Float>(x: 0.5, y: 0.5, z: 0.5)
            sceneElement.position = SIMD3<Float>(x: 1, y: 0.5, z: 0)
    
            let mesh: MeshResource = .generateSphere(radius: 1.0)
            let component = ModelComponent(mesh: mesh,
                                      materials: [material])
    
            sceneElement.components.set(component)
            arView.scene.anchors.append(sceneElement)
        }
    }
    

    【讨论】:

      【解决方案2】:
       let img = UIImage(named: ouputSrc, in: Bundle(path: resourcePath), compatibleWith: nil)
                      
        var myMaterial = UnlitMaterial()
        myMaterial.baseColor = try! .texture(.load(named: ouputSrc, in: Bundle(path: resourcePath)))
        myMaterial.tintColor = UIColor.white.withAlphaComponent(1)
      

      【讨论】:

      • 这对我来说非常有效 - 谢谢。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-05-11
      • 2012-01-26
      • 2015-08-13
      • 2016-09-15
      • 1970-01-01
      • 2014-06-13
      相关资源
      最近更新 更多