【问题标题】:Add a UIImage as Material to a SCNGeometry in ARKit with Swift将UIImage添加为具有Swift的Arkit中的ScnGeometry
【发布时间】:2018-07-03 16:50:25
【问题描述】:

将图像作为材质添加到预定义的 3d 对象(如 Swift 中的 Cube)对我来说效果很好。现在我正在尝试将图像作为材质添加到在运行时动态创建的多边形中。

我希望这样的事情会奏效:

let source = SCNGeometrySource(vertices: vertices)

var data = [Int32]()
for (index, value) in vertices.enumerated() {
    data.append(Int32(index))
}
data.insert(Int32(vertices.count), at: 0)

let indexData = Data(bytes: data, count: data.count * MemoryLayout<Int32>.size)
let element = SCNGeometryElement(data: indexData, primitiveType: .polygon, primitiveCount: 1, bytesPerIndex: MemoryLayout<Int32>.size)
geometry = SCNGeometry(sources: [source], elements: [element])
if let material = geometry?.firstMaterial {
    material.diffuse.contents = UIImage( named: "Diffuse" )
    material.isDoubleSided = true
}

当我将 UIImage 更改为 UIColor 时,它工作正常。 我建议,我需要纹理的 UV 贴图。是否可以即时生成某些东西?

【问题讨论】:

    标签: ios swift scenekit arkit


    【解决方案1】:

    这就是我将图像添加为 SCNMaterials 的方式

    var imageMaterial = SCNMaterial()
    imageMaterial.isDoubleSided = false
    imageMaterial.diffuse.contents = UIImage(named: "myImage")
    var cube: SCNGeometry? = SCNBox(width: 1.0, height: 1.0, length: 1, chamferRadius: 0)
    var node = SCNNode(geometry: cube)
    node.geometry?.materials = [imageMaterial]
    

    【讨论】:

      【解决方案2】:

      对于基于公式或简单的多边形或多边形集合(即网格)来说是可能且可行的。例如,对于三角形或正方形或圆形等,或立方体或球体等的单个多边形,可以手动输入或计算 UV 值(每个顶点的值介于 0.0 和 1.0 之间)。对于任意多边形和模型,获得跨多个多边形的无缝且均匀分布的 UV 贴图变得更加复杂。

      所以最难的部分是确定 UV 坐标,它们不能自动生成。但是,一旦您确定甚至计算出 UV 坐标,您就可以通过添加第二个 SCNGeometrySource 来添加它们,使用 texcoord 语义:https://developer.apple.com/documentation/scenekit/scngeometrysource.semantic/1523762-texcoord

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2019-08-07
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-06-02
        • 2018-01-01
        • 2018-07-19
        相关资源
        最近更新 更多