Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby-on-rails-4/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 使用SceneKit和ARKit创建长方体_Swift_Scenekit_Arkit - Fatal编程技术网

Swift 使用SceneKit和ARKit创建长方体

Swift 使用SceneKit和ARKit创建长方体,swift,scenekit,arkit,Swift,Scenekit,Arkit,我试图用SceneKit和ARKit创建一个基本体。不管出于什么原因,它都不起作用 let box = SCNBox(width: 0.1, height: 0.1, length: 0.1, chamferRadius: 0) let node = SCNNode(geometry: box) node.position = SCNVector3(0,0,0) sceneView.scene.rootNode.addChildNode(node) 我也需要拍摄摄

我试图用SceneKit和ARKit创建一个基本体。不管出于什么原因,它都不起作用

let box = SCNBox(width: 0.1, height: 0.1, length: 0.1, chamferRadius: 0)

    let node = SCNNode(geometry: box)

    node.position = SCNVector3(0,0,0)

    sceneView.scene.rootNode.addChildNode(node)

我也需要拍摄摄像机坐标吗

您应该点击位置并使用世界坐标正确放置立方体。我不确定(0,0,0)是否是ARKit的正常位置。您可以尝试以下方法:
将其放入您的viewDidLoad中:

let tapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(handleTapFrom))
tapGestureRecognizer.numberOfTapsRequired = 1
self.sceneView.addGestureRecognizer(tapGestureRecognizer)
然后添加此方法:

@objc func handleTapFrom(recognizer: UITapGestureRecognizer) {
    let tapPoint = recognizer.location(in: self.sceneView)
    let result = self.sceneView.hitTest(tapPoint, types: ARHitTestResult.ResultType.existingPlaneUsingExtent)

    if result.count == 0 {
        return
    }

    let hitResult = result.first

    let box = SCNBox(width: 0.1, height: 0.1, length: 0.1, chamferRadius: 0)

    let node = SCNNode(geometry: box)
    node.physicsBody = SCNPhysicsBody(type: SCNPhysicsBodyType.static, shape: nil)
    node.position = SCNVector3Make(hitResult.worldTransform.columns.3.x, hitResult.worldTransform.columns.3.y, hitResult.worldTransform.columns.3.z)

    sceneView.scene.rootNode.addChildNode(node)
}

然后,当您点击检测到的平面时,它会在您点击的平面上添加一个框。

您的代码看起来不错,应该可以工作。我尝试了以下代码:在使用ARKit模板创建了一个新应用程序后,我替换了函数viewDidLoad

override func viewDidLoad() {
    super.viewDidLoad()

    // Set the view's delegate
    sceneView.delegate = self

    let box = SCNBox(width: 0.1, height: 0.1, length: 0.1, chamferRadius: 0)
    let node = SCNNode(geometry: box)
    node.position = SCNVector3(0,0,0)
    sceneView.scene.rootNode.addChildNode(node)
}
它在原点(0,0,0)处创建一个长方体。不幸的是,你的设备在盒子里,因此你不能直接看到盒子。要查看该框,请将设备稍微移远一点

移动设备后,附加的图像将显示在方框中:

如果您想立即看到它,请将长方体向前移动一点,添加颜色,并使第一种材质为双面(以便在内侧或外侧都能看到):


我得到了一个构建错误并更改了这个部分。它不起作用,所以我把它改成了这个node.position=SCInvertor3Make((hitResult?.worldTransform.columns.3.x)!,(hitResult?.worldTransform.columns.3.y)!,(hitResult?.worldTransform.columns.3.z)!)它没有添加多维数据集。我尝试了一些简单的方法,只是在AR中向场景添加一个立方体。我不知道为什么它不起作用。0,0,0在ARKit中是一个完全有效的位置。世界坐标系上的原点是(意外发送按钮太早)…是启动ARSession时设备所在的位置。如果从那以后你再也没有移动过,你的设备仍然在一个10厘米的立方体里,你看不到这个立方体,因为里面的面没有被渲染。如果你往后拉一点,你会看到立方体漂浮在那里。不过,世界起源地可能不是放置东西的好地方。这个答案中显示的基于点击测试的布局只是(许多可能的)在有用位置获取内容的一种方法。
    let box = SCNBox(width: 0.1, height: 0.1, length: 0.1, chamferRadius: 0)
    box.firstMaterial?.diffuse.contents = UIColor.red
    box.firstMaterial?.isDoubleSided = true
    let boxNode = SCNNode(geometry: box)
    boxNode.position = SCNVector3(0, 0, -1)
    sceneView.scene.rootNode.addChildNode(boxNode)