SceneKit使用Vuforia AR库自定义模型

时间:2017-07-07 10:35:23

标签: ios swift swift3 scenekit vuforia

我是SceneKit的新用户,与swift 3和最新Vuforia图书馆6-2-9合作。

我在自定义3D模型上应用textures时遇到问题。

fileprivate func createDefaultMarkerScene(with view: VuforiaEAGLView) -> SCNScene {
    let scene = SCNScene()

    let lightNode = SCNNode()
    lightNode.light = SCNLight()
    lightNode.light?.type = .omni
    lightNode.light?.color = UIColor.lightGray
    lightNode.position = SCNVector3(x: 0, y: 0, z: 1000)
    scene.rootNode.addChildNode(lightNode)

    let ambientLightNode = SCNNode()
    ambientLightNode.light = SCNLight()
    ambientLightNode.light?.type = .ambient
    ambientLightNode.light?.color = UIColor.darkGray
    scene.rootNode.addChildNode(ambientLightNode)

    let geoNode = SCNNode()
    geoNode.name = "model_model"

    let normalsSources = SCNGeometrySource(normals: normalsSCNVector3)
    let verticesSources = SCNGeometrySource(vertices: verticesSCNVector3)
    let texCoordsSources = SCNGeometrySource(textureCoordinates: texCoordCGPoint)
    let indicesElements = SCNGeometryElement(indices: indices, primitiveType: SCNGeometryPrimitiveType.triangles)

    geoNode.geometry = SCNGeometry(sources: [verticesSources, normalsSources, texCoordsSources], elements: [indicesElements])
    geoNode.position = SCNVector3(x: 0, y: 0, z: 0)
    geoNode.scale = SCNVector3(x: 50, y: 50, z: 50)

    let material = SCNMaterial()

    material.diffuse.contents = UIImage(named: "grad_ao.png")

    geoNode.geometry?.firstMaterial = material

    scene.rootNode.addChildNode(geoNode)

    return scene
}

模型已正确渲染,但应用的纹理完全搞砸了。我试过旋转的图像,也uv坐标都好[0..1]。

有什么想法吗?谢谢

Textures are messed up

1 个答案:

答案 0 :(得分:0)

我设法fix出了问题。

关键是" texture.png"文件。

我只需要将图像翻转180度:

material.diffuse.contentsTransform = SCNMatrix4Translate(SCNMatrix4MakeScale(1, -1, 1), 0, 1, 0)