【问题标题】:Textures always flipped and inverted with SceneKit Custom Geometry使用 SceneKit 自定义几何图形始终翻转和反转纹理
【发布时间】:2016-11-17 20:05:10
【问题描述】:

我已经能够在 SceneKit 中创建自定义几何体,但我总是得到翻转/倒置的纹理。

我已经尝试了很多其他人的自定义几何示例代码,但最终还是遇到了翻转/反转纹理问题。任何帮助将不胜感激!

flipped/inverted texture screenshot

  // v1 +----+ v0
  //    |    |
  // v2 +----+ v3

let positions: [Float] = [
     5.0,   5.0,  0.0,
    -5.0,   5.0,  0.0,
    -5.0,  -5.0,  0.0,
     5.0,  -5.0,  0.0
]

let normals: [Float] = [
    0.0,  0.0,  1.0,
    0.0,  0.0,  1.0,
    0.0,  0.0,  1.0,
    0.0,  0.0,  1.0
]

let tcoords: [Float] = [
    1.0,  1.0,
    0.0,  1.0,
    0.0,  0.0,
    1.0,  0.0
]

let positionsData = Data(bytes: positions, count: positions.count * 4)

let positionSource = SCNGeometrySource(data: positionsData,
                                       semantic: SCNGeometrySource.Semantic.vertex,
                                       vectorCount: positions.count / 3,
                                       usesFloatComponents: true,
                                       componentsPerVector: 3,
                                       bytesPerComponent: 4,
                                       dataOffset: 0,
                                       dataStride: 12)

let normalsData = Data(bytes: normals, count: normals.count * 4)

let normalSource = SCNGeometrySource(data: normalsData,
                                       semantic: SCNGeometrySource.Semantic.normal,
                                       vectorCount: normals.count / 3,
                                       usesFloatComponents: true,
                                       componentsPerVector: 3,
                                       bytesPerComponent: 4,
                                       dataOffset: 0,
                                       dataStride: 12)

let tcoordsData = Data(bytes: tcoords, count: tcoords.count * 4)

let tcoordSource = SCNGeometrySource(data: tcoordsData,
                                     semantic: SCNGeometrySource.Semantic.texcoord,
                                     vectorCount: tcoords.count / 2,
                                     usesFloatComponents: true,
                                     componentsPerVector: 2,
                                     bytesPerComponent: 4,
                                     dataOffset: 0,
                                     dataStride: 8)

let index: [UInt8] = [0,1,2,0,2,3]

let indexData = Data(bytes: index, count: index.count)

let indexElement = SCNGeometryElement(data: indexData,
                                      primitiveType: SCNGeometryPrimitiveType.triangles,
                                      primitiveCount: index.count / 3,
                                      bytesPerIndex: 1)

let geometry = SCNGeometry(sources: [positionSource, normalSource, tcoordSource],
                           elements: [indexElement])

【问题讨论】:

    标签: scenekit


    【解决方案1】:

    您需要翻转tcoords 中的纹理坐标。 SceneKit 遵循convention 纹理坐标的原点在纹理的左上角。

    在 Metal 中,帧缓冲区附件的像素坐标系的原点定义在左上角。同样,帧缓冲附件的像素坐标系的原点是左上角。

    【讨论】:

    • 我的天啊,一个多星期以来,我一直在谷歌上搜索并试图自己解决这个问题。我一直是个潜伏者,我通常不发布问题。您的解决方案完美运行!你是救命稻草,我本来打算停止开发我的 FBX 到 SceneKit 导入器工具,但现在我可以继续了!
    猜你喜欢
    • 2021-08-01
    • 2018-03-05
    • 2023-04-10
    • 2016-04-26
    • 2016-04-01
    • 2018-08-06
    • 2017-03-28
    • 1970-01-01
    • 2019-08-23
    相关资源
    最近更新 更多