无法将纹理图像添加到ARKit中的多维数据集中
问题描述:
我无法使用“材质”对象将图像添加到ARKit中的多维数据集。无法将纹理图像添加到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()
}
我尝试添加各种不同的图像,但没有任何工程。唯一可用的图像是预先加载到ARKit项目中的名为“textures.png”的图像。
是否有加载图像的特定要求?
答
我不是100%肯定在这一个,但问题可能与包括.png与imageName,因为这个图像应该在你的资产文件夹。
无论如何,这段代码正在为我工作,并尝试做同样的事情来创建与图像的多维数据集。
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)
答
必须添加喜欢的路径:
material.diffuse.contents = UIImage(named: "art.scnassets/textur")
这对我的作品。
那是正确的:UIImage(named:“”)不需要扩展名,例如PNG –