【问题标题】:Metal and Model I/O - Add Texture Coordinates to Mesh金属和模型 I/O - 将纹理坐标添加到网格
【发布时间】:2021-06-10 16:11:53
【问题描述】:

我正在处理一个学生项目,我想为我使用配备新 LiDAR 传感器的 iPad 扫描的网格设置纹理。

但是,要对网格进行纹理化,我需要添加纹理坐标。我目前的计划是将扫描的网格转换为 MDLMesh 并将所有子网格添加到 MDLAsset 容器中。之后,我使用 foreach 循环遍历 MDLMeshes。在每次迭代中,我在当前网格上调用函数“MDLMesh.addUnwrappedTextureCoordinates”。不幸的是,它总是导致崩溃。有时我可以在出现错误之前循环遍历 2 个网格,有时我什至不会将 UV 添加到单个网格。

我不是 swift 或 Model IO 方面的专家,但我觉得很奇怪,这个操作崩溃了,而我可以很好地添加法线。

我得到的错误如下所示:

Can't choose for edge creation 
libc++abi.dylib: terminating with uncaught exception of type std::out_of_range: unordered_map::at: key not found

我使用的代码如下所示:

private func unwrapTextureCoordinates(asset: MDLAsset) -> MDLAsset{
    let objects = asset.childObjects(of: MDLMesh.self)
    
    for object in objects{
        if let mesh = object as? MDLMesh{
            mesh.addNormals(withAttributeNamed: MDLVertexAttributeNormal, creaseThreshold: 0.5)
            mesh.addAttribute(withName: MDLVertexAttributeTextureCoordinate, format: .float2)
            mesh.addUnwrappedTextureCoordinates(forAttributeNamed: MDLVertexAttributeTextureCoordinate)
        }
    }
            
    return asset
}

希望有人能告诉我出了什么问题或指出我正确的方向。

【问题讨论】:

    标签: ios swift graphics arkit metal


    【解决方案1】:

    在我无法弄清楚导致问题的原因后,我求助于 Unity 及其 ARFoundation 包装器,看看我是否能够计算那里的任何 UV。我发现 Unity 相当于 Model I/O 的“addUnwrappedTextureCoordinates”,即Unwrapping.GeneratePerTriangleUV,为每个三角形计算 3 个 UV。

    现在,当我在 Unity 中运行此函数时,我的网格也会出现超出范围的异常,就像在 Swift 中一样。错误描述表明 UV 坐标的数量不能超过网格中的顶点数量 - 这是有道理的,因为我得到的 UV 坐标是网格中顶点数量的三倍。因此,我高度怀疑使用 Model I/O 的 Swift 中的 out-of-range-exception 具有相同的原因。

    当然,有很多解决方法,但我采用了不同的解决方案,因为“Unwrapping”类是“UnityEngine.Editor”命名空间的一部分,因此我无法在完成的构建中使用它(这就是我想要的)。

    相反,我遇到了the function in this thread 来为我的网格计算一组 UV。我使用了它,它完全按照我的意愿工作。代码是用 C# 编写的,因此我决定使用 Unity 引擎继续我的项目。不过,我认为将函数翻译成 Swift 不会很麻烦。

    【讨论】:

      猜你喜欢
      • 2016-01-24
      • 2019-12-29
      • 2018-11-04
      • 2013-05-01
      • 2018-05-13
      • 2014-11-08
      • 1970-01-01
      • 2017-05-28
      • 2017-06-14
      相关资源
      最近更新 更多