2
let material = SimpleMaterial.init(color: .red,roughness: 1,isMetallic: false)
let doorBox = MeshResource.generateBox(width: 0.02,height: 1, depth: 0.5)
let doorEntity = ModelEntity(mesh: doorBox, materials: [material])
let anchor = ARAnchorEntity()
anchor.addChild(doorEntity)

In RealityKit, I am having box which is MeshResource, Box looks like a line. I have added this box in ARView, and have set realtime camera position. In one scenario I want to know Box/Line’s starting and ending position.

Lets say box with entity has middle/current position (0.1,0.23,-1.3) then what will be box’s left and right position ? Anchor with box is keep changing it's position with camera movement.

Thanks in advance.

Check explanation with the image

1 Answers1

0

You can use this extension.

extension Entity {
    
    func getDistancedPosition(x: Float, y: Float, z: Float) -> SIMD3<Float> {
        
        let referenceNodeTransform = transform.matrix
        var translationMatrix = matrix_identity_float4x4
        translationMatrix.columns.3.x = x
        translationMatrix.columns.3.y = y
        translationMatrix.columns.3.z = z
        let updatedTransform = matrix_multiply(referenceNodeTransform,
                                               translationMatrix)
        
        return .init(updatedTransform.columns.3.x,
                     updatedTransform.columns.3.y,
                     updatedTransform.columns.3.z)
        
    }
    
}

To get left and right for your box, Use below code:

let side1Position = door.getDistancedPosition(x: 0, y: 0, z: self.viewModel.doorDepth/2)
let side2Position = door.getDistancedPosition(x: 0, y: 0, z: -(self.viewModel.doorDepth/2))

To make the box look like the line you must have used depth. If not then you can change the parameter accordingly. e.g. door.getDistancedPosition(x: -0.1, y: 0, z: 0)

You can also refer to this question and its accepted answer:

Position a SceneKit object in front of SCNCamera's current orientation

indrajit
  • 303
  • 1
  • 14