augmented actuality – Facelandmarks 3D place in IOS Scenekit

0
22
augmented actuality – Facelandmarks 3D place in IOS Scenekit


I am making an attempt to transform facelandmarks to 3d factors in scenekit.Even with incorrect depth worth face tracks accurately however it isn’t helpful as occluder. as a result of worldpoint is improper and face mesh all the time stays entrance of each sceneobject or again of each sceneobject. I’ve tried to make use of faceNode.remodel = matrix (from mediapipe) it really works nice nevertheless it doesn’t monitor landmarks. Proper now I replace geometry vertices through the use of these landmarks and replace geometry uv with uv.json knowledge that mediapipe offered.

How am i able to get appropriate world place of landmarks in scenekit? Thanks.

    if let normalizedLandmarkArray = firstFaceNormalizedLandmarks {
        
        
        let vertexPositionArray = normalizedLandmarkArray.enumerated().map { index, landmark -> simd_float3 in

            let screenX = CGFloat(landmark.x) * imageWidth
            let screenY = CGFloat(landmark.y) * imageHeight
            

            let screenPoint = SCNVector3(screenX, screenY, 1)
            
            let worldPoint = sceneView.unprojectPoint(screenPoint)



            return simd_float3(Float(worldPoint.x), Float(worldPoint.y), Float(worldPoint.z))


            }
    struct MyVertex {
        var x: Float  // Place X
        var y: Float  // Place Y
        var z: Float  // Place Z
        
        var nx: Float // Regular X
        var ny: Float // Regular Y
        var nz: Float // Regular Z
        
        var s: Float  // Texture Coordinate S (U)
        var t: Float  // Texture Coordinate T (V)
    }


    func updateFaceNodeGeometry(landmarks: [simd_float3], geometry: SCNGeometry) -> SCNGeometry? {
        guard !uValues.isEmpty, !vValues.isEmpty else {
            print("❌ UV values aren't loaded.")
            return nil
        }
        
        let limitedLandmarks = landmarks.prefix(468)
        
        var vertices: [MyVertex] = []
        
        for (index, landmark) in limitedLandmarks.enumerated() {
            let vertex = MyVertex(
                x: landmark.x,
                y: landmark.y,
                z: landmark.z,
                nx: 0.0, ny: 0.0, nz: 1.0, // Normaller varsayılan olarak yüzey normaline ayarlandı
                s: uValues[index],
                t: vValues[index]
            )
            vertices.append(vertex)
        }
        
        let knowledge = NSData(bytes: vertices, size: vertices.depend * MemoryLayout.dimension)

        
        let vertexSource = SCNGeometrySource(
            knowledge: knowledge as Knowledge,
            semantic: .vertex,
            vectorCount: vertices.depend,
            usesFloatComponents: true,
            componentsPerVector: 3, // x, y, z
            bytesPerComponent: MemoryLayout.dimension,
            dataOffset: 0,
            dataStride: MemoryLayout.dimension
        )
        
        let normalSource = SCNGeometrySource(
            knowledge: knowledge as Knowledge,
            semantic: .regular,
            vectorCount: vertices.depend,
            usesFloatComponents: true,
            componentsPerVector: 3, // nx, ny, nz
            bytesPerComponent: MemoryLayout.dimension,
            dataOffset: MemoryLayout.offset(of: MyVertex.nx)!,
            dataStride: MemoryLayout.dimension
        )
        
        let texcoordSource = SCNGeometrySource(
            knowledge: knowledge as Knowledge,
            semantic: .texcoord,
            vectorCount: vertices.depend,
            usesFloatComponents: true,
            componentsPerVector: 2, // s, t
            bytesPerComponent: MemoryLayout.dimension,
            dataOffset: MemoryLayout.offset(of: MyVertex.s)!,
            dataStride: MemoryLayout.dimension
        )
        
        let updatedElement = geometry.components.first ?? SCNGeometryElement(
            knowledge: Knowledge(),
            primitiveType: .triangles,
            primitiveCount: limitedLandmarks.depend / 3,
            bytesPerIndex: MemoryLayout.dimension
        )
        
        
        guard let oldVertexSource = geometry.sources(for: .vertex).first else {
            print("❌ Vertex supply not present in geometry!")
            return nil
        }
        // ✅ 6. Yeni Geometry Oluştur
       
        
        let updatedGeometry = SCNGeometry(
            sources: [vertexSource, normalSource, texcoordSource],
            components: [updatedElement]
        )
        
        if let materials = updatedGeometry.firstMaterial {
            materials.isDoubleSided = true
            materials.writesToDepthBuffer = true
            materials.readsFromDepthBuffer = true
            materials.transparency = 1.0
            materials.transparencyMode = .dualLayer
            materials.fillMode = .fill
//            materials.diffuse.contents = UIColor.inexperienced
        }
        
        
        
//        DispatchQueue.fundamental.async {
//            self.faceNode?.geometry = updatedGeometry
//            self.sceneView.scene?.isPaused = true
//            self.sceneView.scene?.isPaused = false
//            self.sceneView.setNeedsDisplay()
//        }
//        
        
//        print("✅ Geometry up to date efficiently with C Struct Type!")
        return updatedGeometry
    }
    

i attempted to transform mediapipe z worth to scenekit z worth however i could not

LEAVE A REPLY

Please enter your comment!
Please enter your name here