当检测到某个图像时,我使用ARKit图像检测来放置3D对象。除了正在创建的三维模型外,其他一切都很好。它像疯了一样摇晃。我仔细检查了一下,参考图像的尺寸是正确的。
当检测到图像时,我调用addModel()
。以下是我的代码的样子。
查找参考图像:
func renderer(_ renderer: SCNSceneRenderer, nodeFor anchor: ARAnchor) -> SCNNode? {
let node = SCNNode()
if let imageAnchor = anchor as? ARImageAnchor{
let plane = SCNPlane(width: imageAnchor.referenceImage.physicalSize.width, height: imageAnchor.referenceImage.physicalSize.height)
let planeNode = SCNNode(geometry: plane)
addModel(addTo: planeNode)
node.addChildNode(planeNode)
}
return node
}
addModel()
函数如下所示:
func addModel(addTo: SCNNode){
let testScene = SCNScene(named: "art.scnassets/testModel.scn")
let testNode = testScene?.rootNode.childNode(withName: "test", recursively: true)
let testMaterial = SCNMaterial()
testMaterial.diffuse.contents = UIImage(named: "art.scnassets/bricks")
testNode?.geometry?.materials = [testMaterial]
testNode!.position = SCNVector3Zero
testNode!.position.x = -0.3
testNode!.position.z = 0.3
addTo.addChildNode(testNode!)
}
您是否尝试删除:testNode!。位置.x=-0.3testNode!。位置。z=0.3?
因为当您键入testNode!时!。position=SCNVector3据说是零x=0.0,y=0.0,z=0.0,然后键入另一个坐标。