将3D网格文件放在Camera ARKit(objective-C)前面

时间:2018-03-07 08:30:17

标签: ios objective-c arkit

我正在使用ARKit开发iOS应用。我想在设备前面放置一个3D物体(即ARCamera)。

我目前的代码:

ARFrame* currentFrame = self.sceneView.session.currentFrame;
matrix_float4x4 translation = matrix_identity_float4x4;
translation.columns[3][2] = -1.52; // Translate 5 feet in front of camera
translation.columns[3][1] = -1.22; // Translate 4 feet below the camera

node.simdTransform = matrix_multiply(currentFrame.camera.transform, translation);

node.rotation = SCNVector4Make(0, 0, 0, 0);
node.scale = SCNVector3Make(0.3, 0.3, 0.3);

我有以下问题:

  • 将一个3D物体放在ARCamera前面(相机前面和下面)是正确的代码吗?

我很感激有关此主题的任何建议和想法。谢谢。

1 个答案:

答案 0 :(得分:2)

要将SCNNode放在相机前面(假设您希望它保持锁定),您可以执行此操作:

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

这会将节点设置在距离相机1米以下1米处。

如果您只想在屏幕中心“锁定”某些内容,您还可以使用:

scnView.pointOfView?.addChildNode(node)

如果您想使用SCNNode直接在相机前面放置current.frame,可以使用此功能:

    /// Adds An SCNNode 3m Away From The Current Frame Of The Camera
   func addNodeInFrontOfCamera(){

    guard let currentTransform = augmentedRealitySession.currentFrame?.camera.transform else { return }

    let nodeToAdd = SCNNode()
    let boxGeometry = SCNBox(width: 0.1, height: 0.1, length: 0.1, chamferRadius: 0)
    boxGeometry.firstMaterial?.diffuse.contents = UIColor.red
    nodeToAdd.geometry = boxGeometry

    var translation = matrix_identity_float4x4

     //Change The X Value
     translation.columns.3.x = 0

     //Change The Y Value
     translation.columns.3.y = 0

     //Change The Z Value
     translation.columns.3.z = -3

    nodeToAdd.simdTransform = matrix_multiply(currentTransform, translation)
    augmentedRealityView?.scene.rootNode.addChildNode(nodeToAdd)

}

var augmentedRealityView是ARSCNView

在目标C中更新

-(void)addNodeInFrontOfCamera{

SCNBox * cube = [SCNBox boxWithWidth:0.1 height:0.1 length:0.1 chamferRadius:0.0];
cube.firstMaterial.diffuse.contents = [UIColor redColor];
SCNNode *cubeNode = [SCNNode nodeWithGeometry:cube];
[self.sceneView.scene.rootNode addChildNode:cubeNode];

matrix_float4x4 cameraTransform = self.sceneView.session.currentFrame.camera.transform;
matrix_float4x4 translation = matrix_identity_float4x4;

//Change The X Value
translation.columns[3][0] = 0;

//Change The Y Value
translation.columns[3][1] = 0;

//Change The Z Value
translation.columns[3][2] = -3;

cubeNode.simdTransform = matrix_multiply(cameraTransform, translation);

 }