【问题标题】:Not able to add texture image to a cube in ARKit无法在 ARKit 中将纹理图像添加到立方体
【发布时间】:2018-07-18 23:10:11
【问题描述】:

我无法使用“材料”对象在 ARKit 中将图像添加到立方体。

代码如下:

import UIKit
import SceneKit
import ARKit

class SimpleBoxViewController: UIViewController, ARSCNViewDelegate {

var sceneView: ARSCNView!

override func viewDidLoad() {
    super.viewDidLoad()

    self.sceneView = ARSCNView(frame: self.view.frame)
    self.view.addSubview(self.sceneView)

    sceneView.delegate = self
    sceneView.showsStatistics = true

    let scene = SCNScene()

    let box = SCNBox(width: 0.2, height: 0.2, length: 0.2, chamferRadius: 0)

    let material = SCNMaterial() 

    //This is not working
    material.diffuse.contents = UIImage(named: "<someImage>.png")

    let node = SCNNode() 
    node.geometry = box
    node.geometry?.materials = [material]
    node.position = SCNVector3(0, -0.1, -0.5)
    scene.rootNode.addChildNode(node)

    sceneView.scene = scene


}

override func viewWillAppear(_ animated: Bool) {
    super.viewWillAppear(animated)

    let configuration = ARWorldTrackingConfiguration()
    sceneView.session.run(configuration)
}

override func viewWillDisappear(_ animated: Bool) {
    super.viewWillDisappear(animated)

    // Pause the view's session
    sceneView.session.pause()
}

我尝试添加各种不同的图像,但没有任何效果。唯一有效的图像是名为“textures.png”的图像,它被预加载到 ARKit 项目中。

加载图像是否有特定要求?

【问题讨论】:

    标签: swift augmented-reality arkit


    【解决方案1】:

    我不能 100% 确定这一点,但问题可能在于包含带有 imageName 的 .png,因为该图像应该在您的 Assets 文件夹中。

    无论如何,这段代码对我有用,并尝试在创建带有图像的立方体方面做同样的事情。

    var box = SCNBox(width: pd.width, height: pd.height, length: 0.01,
    chamferRadius: 0.0)
    var imageMaterial = SCNMaterial()
    var image = UIImage(named: "image")
    imageMaterial.diffuse.contents = image
    box.materials = [imageMaterial, imageMaterial, imageMaterial, imageMaterial, imageMaterial, imageMaterial]
    var cube = SCNNode(geometry: box)
    

    【讨论】:

    • 没错:UIImage(named: "") 不需要扩展名,例如png
    【解决方案2】:

    你必须像这样添加路径:

     material.diffuse.contents = UIImage(named: "art.scnassets/textur")
    

    这对我有用。

    【讨论】:

      【解决方案3】:

      我也面临同样的问题。我已将 png 文件复制到 Assets.xcassets 文件夹中,它对我有用。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-09-15
        • 2021-07-25
        • 2019-06-03
        • 1970-01-01
        • 2023-04-09
        • 2014-05-06
        • 1970-01-01
        • 2013-09-09
        相关资源
        最近更新 更多