【问题标题】:SceneKit - Adding an image as Normal MapSceneKit - 将图像添加为法线贴图
【发布时间】:2019-03-15 06:05:42
【问题描述】:

到目前为止,我能够下载 Collada 文件 (dae) 并将其显示在屏幕上。 现在我正在尝试以编程方式将图像放置在 Normal 属性中,因此正面纹理将获得不同的效果。

根据文件检查器,Normal 选项位于 材料 -> 正常。

我尝试通过材质更改“法线贴图”的内容,但没有成功。 这是我的代码不起作用。

/// Creating the Node
    let node = SCNNode()
    /// The dae file
    let scene = SCNScene(named: "bluebag.dae")
    let arryNode = scene?.rootNode.childNodes
    /// Override the assets
    for childNode in arryNode! {
        if let geo = childNode.geometry {
            for geoItem in geo.materials {
                geoItem.normal.contents = UIImage(named:"fabric.jpg") /// This is not working
            }
        }
        node.addChildNode(childNode)
    }

我也尝试在其内部创建一个包含材质和法线贴图图像的新节点并将其添加到前面的纹理中,但我还是没有成功。 谁能指点我如何将法线贴图添加到纹理?我错过了什么吗?

【问题讨论】:

    标签: swift scenekit collada


    【解决方案1】:

    查看您的代码:

    /// Creating the Node
    let node = SCNNode()
    /// The dae file
    let scene = SCNScene(named: "bluebag.dae")
    let arryNode = scene?.rootNode.childNodes                         // See Note 1
    /// Override the assets
    for childNode in arryNode! {
        if let geo = childNode.geometry {
            for geoItem in geo.materials {
                geoItem.normal.contents = UIImage(named:"fabric.jpg") // See Note 2
            }
        }
        node.addChildNode(childNode)
    }
    

    注意1:你确定你所有的子节点都在rootNode的第一层吗?

    注意 2:如果 Assets.xcassets 中有 fabric.jpg,则使用 UIImage(named:"fabric")

    试试这个遍历节点层次结构:

    scene.rootNode.enumerateChildNodes { (node, stop) in
        if let geo = node.geometry {
            for material in geo.materials {
                material.normal.contents = UIImage(named: "fabric")
            }
        }
        scnView.scene?.rootNode.addChildNode(node) // or something like this
    }
    

    【讨论】:

      猜你喜欢
      • 2020-04-15
      • 2015-09-24
      • 2018-05-26
      • 2019-04-07
      • 2016-07-04
      • 2019-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多