【问题标题】:how to apply normal map texture to custom geometry in scenekit, ios?如何将法线贴图纹理应用于scenekit,ios中的自定义几何体?
【发布时间】:2017-10-10 06:04:01
【问题描述】:

我使用以下代码创建了一个自定义 SCNGeometry:

let data = NSData(bytes: position, length: MemoryLayout<float3>.size * self.objectMesh.pointCount)
                                let vertexSource = SCNGeometrySource(data: data as Data,
                                                                     semantic: .vertex,
                                                                     vectorCount: self.objectMesh.pointCount,
                                                                     usesFloatComponents: true,
                                                                     componentsPerVector: 3,
                                                                     bytesPerComponent: MemoryLayout<Float32>.size,
                                                                     dataOffset: 0,
                                                                     dataStride: MemoryLayout<float3>.size)
                                let data2 = NSData(bytes: uvs, length: MemoryLayout<float2>.size * self.objectMesh.pointCount)
                                let tSource = SCNGeometrySource(data: data2 as Data,
                                                                semantic: .texcoord,
                                                                vectorCount: self.objectMesh.pointCount,
                                                                usesFloatComponents: true,
                                                                componentsPerVector: 2,
                                                                bytesPerComponent: MemoryLayout<Float32>.size,
                                                                dataOffset: 0,
                                                                dataStride: MemoryLayout<float2>.stride)

                                let indexData = NSData(bytes: faces, length: MemoryLayout<Int32>.size * self.objectMesh.faceCount)
                                let element = SCNGeometryElement(
                                    data: indexData as Data,
                                    primitiveType: .triangles,
                                    primitiveCount: self.objectMesh.faceCount/3,
                                    bytesPerIndex: MemoryLayout<Int32>.size)
                                let Geometry = SCNGeometry(sources: [vertexSource, tSource], elements: [element])
                                Geometry.firstMaterial?.diffuse.contents = self.MainTexture
                                Geometry.firstMaterial?.normal.contents = self.NormalTexture

现在的问题是:纹理显示了,但法线纹理贴图没有应用到几何体。我不会使用每个顶点法线。此纹理和法线纹理适用于例如 SCNPlane 几何体。但我现在不知道如何在我的自定义几何体中使用法线贴图纹理。

【问题讨论】:

    标签: scenekit


    【解决方案1】:

    SCNMaterial 上的法线属性需要一个切线空间中的法线贴图,然后有效法线基于法线贴图和自定义几何体的顶点法线的组合。

    模型 i/o 包括 MDLMesh,它提供了一个 addNormals 方法,可用于以编程方式创建法线(基于 SCNGeometry 创建一个 MDLMesh,使用 addNormals,并基于 MDLMesh 创建一个 SCNGeometry 并使用新的 SCNGeometry反而)。但理想情况下,您可以在自定义几何体中包含法线。

    【讨论】:

    • 我知道这是一篇旧帖子。但是您是否碰巧知道为什么 addNormals 似乎会在自定义几何图形上创建倒置法线?它在错误的方向上做得很好。我可以在 SCNMaterial 参数中弥补它。但我杀了一天破解它。我没有尝试顺时针排列数组,看看是否会改变任何东西,但这似乎也是一个黑客解决方案。
    猜你喜欢
    • 2018-08-06
    • 2023-04-10
    • 2016-04-28
    • 2018-03-05
    • 2021-08-01
    • 2016-04-01
    • 2016-11-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多