Scenekit shape between 4 points

前端 未结 1 397
一个人的身影
一个人的身影 2021-01-15 03:59

Given that the route I was taking in a previous question:

ARKit - place a SCNPlane between 2 vector points on a plane in Swift 3

...seems to be flawed, I\'m

相关标签:
1条回答
  • 2021-01-15 04:33

    So the answer, at least my solution is to use SCNGeometry triangles. I wanted a square (rather than a cube) to act as a wall in Augmented Reality so I simply built 2 triangles using the 4 nodes that mapped the 4 corners of a wall.

    The class to build the triangle:

    extension SCNGeometry {
    
        class func triangleFrom(vector1: SCNVector3, vector2: SCNVector3, vector3: SCNVector3) -> SCNGeometry {
    
            let indices: [Int32] = [0, 1, 2]
    
            let source = SCNGeometrySource(vertices: [vector1, vector2, vector3])
    
            let element = SCNGeometryElement(indices: indices, primitiveType: .triangles)
    
            return SCNGeometry(sources: [source], elements: [element])
        }
    }
    

    The points for the 2 triangles are [p1, p2, p4] and [p1, p3, p4] called using the following:

    let thirdPoint = firstPoint.clone()
    thirdPoint.position = SCNVector3Make(thirdPoint.position.x, 
    thirdPoint.position.y + Float(1.5), thirdPoint.position.z)
    sceneView.scene.rootNode.addChildNode(thirdPoint)
    
    let fourthPoint = secondPoint.clone()
    fourthPoint.position = SCNVector3Make(fourthPoint.position.x, 
    fourthPoint.position.y + Float(1.5), fourthPoint.position.z)
    sceneView.scene.rootNode.addChildNode(fourthPoint)
    
    let triangle = SCNGeometry.triangleFrom(vector1: firstPoint.position, vector2: secondPoint.position, vector3: fourthPoint.position)
    let triangleNode = SCNNode(geometry: triangle)
    triangleNode.geometry?.firstMaterial?.diffuse.contents = UIColor.blue
    triangleNode.geometry?.firstMaterial?.isDoubleSided = true
    sceneView.scene.rootNode.addChildNode(triangleNode)
    
    let triangle2 = SCNGeometry.triangleFrom(vector1: firstPoint.position, vector2: thirdPoint.position, vector3: fourthPoint.position)
    let triangle2Node = SCNNode(geometry: triangle2)
    triangle2Node.geometry?.firstMaterial?.diffuse.contents = UIColor.blue
    triangle2Node.geometry?.firstMaterial?.isDoubleSided = true
    sceneView.scene.rootNode.addChildNode(triangle2Node)
    

    This is all based on creating 2 initial nodes by selecting the bottom 2 points of a wall in ARKit.

    Hope that makes sense to anybody else searching for a similar answer.

    EDIT: Adding a Material

    Here's a slightly different extension and the code to add a material to it, the end result of a wall remains the same:

    extension SCNGeometry {
    
        class func Quad() -> SCNGeometry {
    
            let verticesPosition = [
                SCNVector3(x: -0.242548823, y: -0.188490361, z: -0.0887458622),
                SCNVector3(x: -0.129298389, y: -0.188490361, z: -0.0820985138),
                SCNVector3(x: -0.129298389, y: 0.2, z: -0.0820985138),
                SCNVector3(x: -0.242548823, y: 0.2, z: -0.0887458622)
            ]
    
            let textureCord = [
                CGPoint(x: 1, y: 1),
                CGPoint(x: 0, y: 1),
                CGPoint(x: 0, y: 0),
                CGPoint(x: 1, y: 0),
            ]
    
            let indices: [CInt] = [
                0, 2, 3,
                0, 1, 2
            ]
    
            let vertexSource = SCNGeometrySource(vertices: verticesPosition)
            let srcTex = SCNGeometrySource(textureCoordinates: textureCord)
            let date = NSData(bytes: indices, length: MemoryLayout<CInt>.size * indices.count)
    
            let scngeometry = SCNGeometryElement(data: date as Data, 
    primitiveType: SCNGeometryPrimitiveType.triangles, primitiveCount: 2, 
    bytesPerIndex: MemoryLayout<CInt>.size)
    
            let geometry = SCNGeometry(sources: [vertexSource,srcTex], 
    elements: [scngeometry])
    
            return geometry
    
    
        }
    
    }
    

    Then simply call it in viewDidLoad() and apply a material

    let scene = SCNScene()
    
    let quad = SCNGeometry.Quad()
    
    let (min, max) = quad.boundingBox
    
    let width = CGFloat(max.x - min.x)
    let height = CGFloat(max.y - min.y)
    
    quad.firstMaterial?.diffuse.contents = UIImage(named: "wallpaper.jpg")
    quad.firstMaterial?.diffuse.contentsTransform = SCNMatrix4MakeScale(Float(width), Float(height), 1)
    quad.firstMaterial?.diffuse.wrapS = SCNWrapMode.repeat
    quad.firstMaterial?.diffuse.wrapT = SCNWrapMode.repeat
    
    let node = SCNNode()
    node.geometry = quad
    scene.rootNode.addChildNode(node)
    
    sceneView.scene = scene
    
    0 讨论(0)
提交回复
热议问题