使用 Swift 在 ARKit 中显示白色图像 material

Display white image material in ARKit with Swift

我已经使用 SCNGeometryElement 创建了节点并应用图像内容 material 以显示白色并应用颜色内容工作正常。我附上了代码。

func getsquareDrawnLineFrom(pos1: SCNVector3,
                            pos2: SCNVector3,
                            pos3: SCNVector3) -> SCNNode {

    let square = SquareplanlineFrom(vector1: pos1, vector2: pos2, vector3: pos3)
    let material = SCNMaterial()
    // material.diffuse.contents = UIColor.red
    material.diffuse.contents = UIImage(named: "grid")
    square.materials = [material]
    let square1 = SCNNode(geometry: square)
    square1.name = "tringle"
    return square1
}

// get line geometry between three vectors
func SquareplanlineFrom(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])
}

要在您的 自定义几何体 三角形上放置纹理,您需要实现两个更重要的类型属性:

SCNGeometrySource.Semantic.texcoord
SCNGeometrySource.Semantic.normal

让一个贴图知道什么是UV-coordinates和Poly的法线方向,所以贴图可以在一个poly表面上占据一定的space。

查看 this post and this post GitHub 以了解如何实施它

虽然,当您使用常规 SceneKit 原语(例如 SCNSphere、SCNBox、SCNPlane 等)或导入的 3D 模型(DAE、OBJ 或 USDZ 文件格式)时,您不会考虑这些类型属性的实现,因为上述几何图形已经包含它们。