【问题标题】:Set a different image for each SCNNode face为每个 SCNNode 人脸设置不同的图像
【发布时间】:2019-01-27 16:59:07
【问题描述】:

我想为SCNNode的每张脸设置不同的图像,我该怎么做? 我试过this,但没有用,第一种材料出现在所有的脸上。我正在使用 Swift 4.2。

我试过了:

self.addChildNode(playerNode!)

let head = playerNode?.childNode(withName: "head", 
                                 recursively: true)!

let greenMaterial = SCNMaterial()
greenMaterial.diffuse.contents = UIColor.green
greenMaterial.locksAmbientWithDiffuse = true;

let redMaterial = SCNMaterial()
redMaterial.diffuse.contents = UIColor.red
redMaterial.locksAmbientWithDiffuse = true;

let blueMaterial  = SCNMaterial()
blueMaterial.diffuse.contents = UIColor.blue
blueMaterial.locksAmbientWithDiffuse = true;

let yellowMaterial = SCNMaterial()
yellowMaterial.diffuse.contents = UIColor.yellow
yellowMaterial.locksAmbientWithDiffuse = true;

let purpleMaterial = SCNMaterial()
purpleMaterial.diffuse.contents = UIColor.purple
purpleMaterial.locksAmbientWithDiffuse = true

let WhiteMaterial = SCNMaterial()
WhiteMaterial.diffuse.contents = UIColor.white
WhiteMaterial.locksAmbientWithDiffuse = true

head?.geometry?.materials = [redMaterial, 
                             greenMaterial, 
                             blueMaterial, 
                             WhiteMaterial, 
                             yellowMaterial, 
                             purpleMaterial]

【问题讨论】:

  • “头”节点是什么形状的?
  • @JoshRobbins 盒子
  • 如果你print(head.geometry) 显示SCNBoxSCNGeometry?如果它是一个盒子,那么你的代码应该可以工作。
  • @JamesP 它打印“可选()”
  • 我猜你没有像你链接的问题那样使用SCNBox。如果您使用另一个应用程序创建了您的盒子,您必须修改源文件以具有 6 种材质,每个面一个,并带有适当的 UV 贴图。

标签: ios swift scenekit


【解决方案1】:

我的解决方案: 因为我是用.scn文件设计播放器的,所以我做的没有效果,所以我用了另一个和原头相同属性的盒子,添加到播放器中(去掉原头后) .为了创建新的头部,我这样做了:

    let head = playerNode?.childNode(withName: "head", recursively: true)!

    let head2 = SCNNode(geometry: SCNBox(width: 0.3, height: 0.3, length: 0.3, chamferRadius: 0))

    let greenMaterial = SCNMaterial()
    greenMaterial.diffuse.contents = UIColor.green
    greenMaterial.locksAmbientWithDiffuse = true;

    let redMaterial = SCNMaterial()
    redMaterial.diffuse.contents = UIColor.red
    redMaterial.locksAmbientWithDiffuse = true;


    let blueMaterial  = SCNMaterial()
    blueMaterial.diffuse.contents = UIColor.blue
    blueMaterial.locksAmbientWithDiffuse = true;


    let yellowMaterial = SCNMaterial()
    yellowMaterial.diffuse.contents = UIColor.yellow
    yellowMaterial.locksAmbientWithDiffuse = true;


    let purpleMaterial = SCNMaterial()
    purpleMaterial.diffuse.contents = UIColor.purple
    purpleMaterial.locksAmbientWithDiffuse = true


    let WhiteMaterial = SCNMaterial()
    WhiteMaterial.diffuse.contents = UIColor.white
    WhiteMaterial.locksAmbientWithDiffuse = true

    head2.geometry?.materials = [redMaterial, greenMaterial, blueMaterial, WhiteMaterial, yellowMaterial, purpleMaterial]

    head2.position = head?.position ?? SCNVector3(0, 0.95, 0)
    head2.scale = head?.scale ?? SCNVector3(1, 1, 1)

    head?.removeFromParentNode()

    playerHead = head2

    playerNode?.addChildNode(head2)

Tnx 大家:)

【讨论】:

    猜你喜欢
    • 2013-08-04
    • 2017-12-10
    • 2020-03-27
    • 2020-09-21
    • 2017-04-13
    • 1970-01-01
    • 1970-01-01
    • 2019-02-26
    • 2019-10-11
    相关资源
    最近更新 更多