问题描述
我无法使用材料"对象将图像添加到 ARKit 中的立方体.
I am not able to add an image to the cube in ARKit using the "Material" object.
代码如下:
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 项目中.
I tried to add various different images but nothing works. The only image that works is the image named "textures.png" which is preloaded into an ARKit project.
对要加载的图像有特定要求吗?
Is there a specific requirement for an image to be to loaded?
推荐答案
我对这个不是 100% 确定,但问题可能在于包含 .png 和 imageName,因为此图像应该在您的资产中文件夹.
I'm not 100% sure on this one, but the issue might be with including the .png with the imageName, since this image should be in your Assets folder.
无论如何,这段代码对我有用,并尝试对创建带有图像的立方体做同样的事情.
Anyway, this code is working for me and tries do to the same thing with regards to creating the cube with an image.
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)
这篇关于无法在 ARKit 中向立方体添加纹理图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!