【问题标题】:Creating a custom geometry with polygon triangulation and applying texture coordinates in SceneKit使用多边形三角剖分创建自定义几何体并在 SceneKit 中应用纹理坐标
【发布时间】:2019-09-24 07:53:23
【问题描述】:

我正在尝试在 SceneKit 中创建一个自定义几何对象,它应该是一个具有任意形状的平面。我正在提供形状的轮廓顶点,并想填充它的内部。

到目前为止我一直在使用这个代码:

extension SCNGeometry {

    static func polygonPlane(vertices: [SCNVector3]) -> SCNGeometry {
        var indices: [Int32] = [Int32(vertices.count)]

        var index: Int32 = 0
        for _ in vertices {
            indices.append(index)
            index += 1
        }

        let vertexSource = SCNGeometrySource(vertices: vertices)

        let textureCoords : [CGPoint] = [] // Fix to map textures to the polygon plane...
        let textureCoordsSource = SCNGeometrySource(textureCoordinates: textureCoords)

        let indexData = Data(bytes: indices, count: indices.count * MemoryLayout<Int32>.size)

        let element = SCNGeometryElement(data: indexData, primitiveType: .polygon, primitiveCount: 1, bytesPerIndex: MemoryLayout<Int32>.size)

        let geometry = SCNGeometry(sources: [vertexSource, textureCoordsSource], elements: [element])

        let imageMaterial = SCNMaterial()
        imageMaterial.diffuse.contents = UIImage(named: "texture.jpg")

        let scaleX = (Float(1)).rounded()
        let scaleY = (Float(1)).rounded()
        imageMaterial.diffuse.contentsTransform = SCNMatrix4MakeScale(scaleX, scaleY, 0)

        imageMaterial.isDoubleSided = true
        geometry.firstMaterial = imageMaterial

        return geometry
    }
}

这在制作更简单的多边形形状时效果相当好,但当形状在不同的地方变得更复杂和狭窄时,它就不能按预期工作了。我也不知道有什么方法可以创建纹理坐标以便使用这种方法应用自定义纹理。

我认为我需要使用某种polygon triangulation 算法来将形状分解为三角形,然后使用正确的SCNGeometryPrimitiveType,例如.triangles 或.triangleStrip。这可能还可以让我对纹理坐标进行 UV 映射,但我现在不确定它是如何工作的。

多边形三角剖分算法需要能够处理 3D 坐标,因为创建的 2D 几何体应该存在于 3D 世界中(您应该能够创建倾斜的多边形平面等)。我还没有找到任何已经在 Swift 中实现的 3D 多边形三角剖分算法。

要清楚纹理坐标;将使用的纹理是重复的纹理,例如:

【问题讨论】:

    标签: swift 3d textures scenekit triangulation


    【解决方案1】:

    对于复杂的情况,SCNShape 更适合,因为它使用了更精细的三角测量 (Delaunay)。 SCNGeometryPrimitiveTypePolygon 类型的简单 SCNGeometryElement 将生成一个三角形扇形。

    【讨论】:

    • 您好,感谢您的建议。您能否详细说明在这种特殊情况下这将如何帮助我?我对 SceneKit 和 3D 图形都很陌生。
    猜你喜欢
    • 2023-04-10
    • 1970-01-01
    • 2011-07-15
    • 2018-08-06
    • 2012-09-28
    • 2015-03-19
    • 2016-09-28
    • 2018-09-10
    • 2011-01-14
    相关资源
    最近更新 更多