2017-09-03 61 views
0

我無法使用「材質」對象將圖像添加到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」的圖像。

是否有加載圖像的特定要求?

回答

2

我不是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) 
+1

那是正確的:UIImage(named:「」)不需要擴展名,例如PNG –

0

必須添加喜歡的路徑:

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

這對我的作品。

相關問題