iPad Pro Lidar - Export Geometry & Texture

后端 未结 1 1203
别那么骄傲
别那么骄傲 2020-12-18 09:38

I would like to be able to export a mesh and texture from the iPad Pro Lidar.

There\'s examples here of how to export a mesh, but Id like to be able to export the en

相关标签:
1条回答
  • 2020-12-18 10:16

    You need to compute the texture coordinates for each vertex, apply them to the mesh and supply a texture as a material to the mesh.

    let geom = meshAnchor.geometry
    let vertices = geom.vertices 
    let size = arFrame.camera.imageResolution
    let camera = arFrame.camera
    
    let modelMatrix = meshAnchor.transform
    
    let textureCoordinates = vertices.map { vertex -> vector_float2 in
        let vertex4 = vector_float4(vertex.x, vertex.y, vertex.z, 1)
        let world_vertex4 = simd_mul(modelMatrix!, vertex4)
        let world_vector3 = simd_float3(x: world_vertex4.x, y: world_vertex4.y, z: world_vertex4.z)
        let pt = camera.projectPoint(world_vector3,
            orientation: .portrait,
            viewportSize: CGSize(
                width: CGFloat(size.height),
                height: CGFloat(size.width)))
        let v = 1.0 - Float(pt.x) / Float(size.height)
        let u = Float(pt.y) / Float(size.width)
        return vector_float2(u, v)
    }
    
    // construct your vertices, normals and faces from the source geometry directly and supply the computed texture coords to create new geometry and then apply the texture.
    
    let scnGeometry = SCNGeometry(sources: [verticesSource, textureCoordinates, normalsSource], elements: [facesSource])
    
    let texture = UIImage(pixelBuffer: frame.capturedImage)
    let imageMaterial = SCNMaterial()
    imageMaterial.isDoubleSided = false
    imageMaterial.diffuse.contents = texture
    scnGeometry.materials = [imageMaterial]
    let pcNode = SCNNode(geometry: scnGeometry)
    

    pcNode if added to your scene will contain the mesh with the texture applied.

    Texture coordinates computation from here

    0 讨论(0)
提交回复
热议问题