【问题标题】:How to add back side image as texture with SceneKit iOS如何使用 SceneKit iOS 将背面图像添加为纹理
【发布时间】:2019-12-24 10:58:10
【问题描述】:

我是 SceneKit 的新手并创建了一个对象我将图像作为纹理添加到对象但它显示在正面和背面。我想要在它后面显示另一个图像。是否可以添加两个纹理对于一个对象?

    let scene = SCNScene()
    var planet : SCNGeometry
    let tempScene = SCNScene(named: "FrameOBJ.obj")
    planet = tempScene!.rootNode.childNodes[0].geometry!
    let material = SCNMaterial()

    let url = URL(string: "www.imageasf.com/123.png")
    let data = try? Data(contentsOf: url!) //make sure your image in this url does exist, otherwise unwrap in a if let check / try-catch
    let image = UIImage(data: data!)

    material.diffuse.contents = image
    material.diffuse.wrapT = SCNWrapMode.mirror
    material.diffuse.wrapS = SCNWrapMode.mirror
    material.isDoubleSided = true
    planet.materials = [material]
    let planetNode = SCNNode(geometry: planet)
    scene.rootNode.addChildNode(planetNode)
    sceneView.cameraControlConfiguration.allowsTranslation = true
    sceneView.allowsCameraControl = true
    sceneView.scene = scene

Front View Back View

【问题讨论】:

  • Materials 是一个材质数组 [material, material1, etc.],所以是的,你可以有多种材质 - 比如一个立方体 [front, right, back, left, top, bottom]。既然您称它为行星,假设您正在尝试对球体进行纹理贴图?搜索 scenekit 纹理贴图星球 - 有一些关于如何做到这一点的视频。

标签: ios 3d textures scenekit scene


【解决方案1】:

我已经通过添加材料数组来做到这一点。

    let tempScene = SCNScene(named: "untitled.obj")
    planet = tempScene!.rootNode.childNodes[0].geometry!

    var image = UIImage()
    let url = URL(string: "https://www.images123.jpg")!
    //make sure your image in this url does exist, otherwise unwrap in a if let check / try-catch
    var node = SCNNode()

    do {
        let data = try Data(contentsOf: url)
        image = UIImage(data: data)!
        node = SCNNode(geometry: SCNBox(width: 0.05, height: 1, length:1, chamferRadius: 0.5))
        node.geometry?.firstMaterial?.diffuse.contents = image
        // do something with data
        // if the call fails, the catch block is executed
    } catch {
        print(error.localizedDescription)
    }

    let  allMaterial = SCNMaterial()
    allMaterial.diffuse.contents = UIImage(named: "woods.png")

    let smallMaterial1 = SCNMaterial()
    smallMaterial1.diffuse.contents = image
    node.scale = SCNVector3(0.5,0.5,0.5)
    node.geometry?.materials = [smallMaterial1,allMaterial,smallMaterial1,smallMaterial1,smallMaterial1,smallMaterial1];

    scene.rootNode.addChildNode(node)
    print(node.scale)
    print(scene.rootNode.scale)
    sceneView.cameraControlConfiguration.allowsTranslation = true
    sceneView.allowsCameraControl = true
    sceneView.scene = scene

【讨论】:

    猜你喜欢
    • 2016-07-04
    • 2018-07-31
    • 2016-02-20
    • 1970-01-01
    • 2018-05-13
    • 1970-01-01
    • 2019-03-15
    • 2018-02-15
    • 1970-01-01
    相关资源
    最近更新 更多