简体   繁体   English

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

[英]Display white image material in ARKit with Swift

I have created node with SCNGeometryElement and apply image content material to display white and apply color content is working fine.我已经用 SCNGeometryElement 创建了节点并应用图像内容材料来显示白色和应用颜色内容工作正常。 I have attached code.我附上了代码。

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])
}

For placing a texture on your custom geometry triangle you need implement two more important type properties:要在自定义几何三角形上放置纹理,您需要实现两个更重要的类型属性:

SCNGeometrySource.Semantic.texcoord
SCNGeometrySource.Semantic.normal

which let a texture find out what are UV-coordinates and Poly normal direction, so texture can occupy a definite space on a poly surface.它让纹理找出什么是 UV 坐标和多边形法线方向,因此纹理可以在多边形表面上占据一定的空间。

Look at this post and this post at a GitHub to get an idea how to implement it .查看 GitHub 上的这篇文章和这篇文章,了解如何实现它

Although, when you use regular SceneKit primitives (such as SCNSphere, SCNBox, SCNPlane, etc) or imported 3D models (in DAE, OBJ or USDZ file formats) you don't think about implementation of these type properties because the aforementioned geometry already contain them.虽然,当您使用常规 SceneKit 原语(例如 SCNSphere、SCNBox、SCNPlane 等)或导入的 3D 模型(DAE、OBJ 或 USDZ 文件格式)时,您不会考虑这些类型属性的实现,因为上述几何体已经包含他们。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM