ios - 自定义 SCNGeometry 不将漫反射内容显示为纹理

标签 ios scenekit scnnode scnmaterial scngeometry

我正在创建自定义 SCNGeometry。首先,我将其开发为平面。它会显示,我可以将颜色应用到它的漫反射内容,但如果我尝试应用任何东西,例如 UIImage 或 CALayer,它会显示为白色。

代码:

let positions = [
    SCNVector3(-1, 0, 0),
    SCNVector3(1, 0, 0),
    SCNVector3(-1, 0, -1),
    SCNVector3(1, 0, -1)
]

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

let vertexSource = SCNGeometrySource(vertices: positions)

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

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

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

geometry.firstMaterial?.diffuse.contents = UIImage(named: "homer")

最佳答案

为了能够对几何体进行纹理处理,您还需要提供 UV 坐标(纹理坐标)源。

let textureCoordinates = [
    CGPoint(x: 0, y: 0),
    CGPoint(x: 1, y: 0),
    CGPoint(x: 0, y: 1),
    CGPoint(x: 1, y: 1)
]

let uvSource = SCNGeometrySource(textureCoordinates: textureCoordinates)

let element = ...

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

关于ios - 自定义 SCNGeometry 不将漫反射内容显示为纹理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48728060/

相关文章:

ios - ARKIT - 它可以跟踪多少个跟踪图像?

ios - SceneKit 加载节点与来自单独的 scn 文件的动画

ios - SCNNode.removeFromParentNode() 和渲染循环

ios - SceneKit - 在任何比例下绘制具有相等线宽的缩放圆

swift - 获取放置的 SCNNode 的世界 x、y 和 z - Swift

ios - 事件应用程序中的应用程序内购买失败

ios - 如何正确配置 UITableView 自定义单元格

objective-c - 具有大量自动释放对象的线程如果是/否,是否必须在这种情况下使用自动释放池,为什么?

ios - 从 Blender 导出为 COLLADA/.dae 格式以在 SceneKit 中使用时包括纹理

ios - iOS:安装Parse 1.7.1错误:找不到'FBSDKCoreKit/FBSDKAccessToken.h'文件