scenekit - 使用 SceneKit 自定义几何体,纹理始终会翻转和反转

标签 scenekit

我已经能够在 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])

最佳答案

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

In Metal, the origin of the pixel coordinate system of a framebuffer attachment is defined at the top left corner. Similarly, the origin of the pixel coordinate system of a framebuffer attachment is the top left corner.

关于scenekit - 使用 SceneKit 自定义几何体,纹理始终会翻转和反转,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40654270/

相关文章:

ios - 展开要打印的 SCNVector3/SCNVector4 值?

ios - 发送双击到 SCNView

xml - 写入 dae 时,Scenekit 会自动在节点名称中的空格处插入下划线

augmented-reality - 我们可以使用 ARKit 和 SceneKit 开发 LiDAR 应用程序吗?

Swift Playgrounds 工作很慢

ios - 使用 UIPanGesture 在 ARScene 中移动节点

ios - SCNPhysicsBody 上的 SceneKit isResting 属性始终返回 false

swift - spritekit如何有选择地缩放节点

c# - SCNText 未显示在 SceneKit 中

scenekit - 如何编辑已包含在 AR 项目中的 USDZ 网格?