如何在ARkIt2中的3d对象上包裹图像?

时间:2018-12-19 20:04:26

标签: ios iphone scenekit augmented-reality arkit

我需要用图像包裹3d对象(scene)。像这样:

enter image description here

我正在使用以下代码:

let imageMaterial = SCNMaterial()
let image = UIImage(named: "temp_flow_image")
imageMaterial.diffuse.contents = image

let objectScene = SCNScene(named: "art.scnassets/frame.scn")
    let objectNode: SCNNode = objectScene!.rootNode.childNode(withName: "frame", recursively: true)! 
objectNode.geometry?.firstMaterial?.diffuse.contents = imageMaterial

objectNode.position = SCNVector3(0,0,-4)

let scene = SCNScene() // Main scene of the app
self.sceneView.scene = objectScene!

整个过程是这样的:

3d对象加载为SCNNode,并以编程方式将图像包装在其周围。我可以看到3d对象,但看不到它上面的图像。我在这里做错了什么?我们不能实用地编辑scene的几何部分吗?

我可以将图像包裹在SCNBoxarkit中的任何其他预定义形状周围,但不能与外部3d对象一起包裹

1 个答案:

答案 0 :(得分:1)

以下代码有效:

self.sceneView.scene.rootNode.enumerateChildNodes { (node, _) in
if(node.name == "box") {
   parentNode = node
   let geometry = parentNode.geometry
   geometry?.firstMaterial?.diffuse.contents = imageToDisplay
   parentNode.position = SCNVector3Make(0, 0, -2.9)
   let boxShape:SCNPhysicsShape = SCNPhysicsShape(geometry: geometry!, options: nil)
            parentNode.physicsBody = SCNPhysicsBody(type: .static, shape: boxShape)
            parentNode.physicsBody?.restitution = 1
        }
    }

 sceneView.scene.rootNode.addChildNode(parentNode)

我在Xcode中创建了scene box