ios - 为每个 SCNNode 人脸设置不同的图像

标签 ios swift scenekit

我想为SCNNode的每张脸设置不同的图像,我该怎么做? 我试过了 this但它没有用,第一种 Material 出现在所有的脸上。我正在使用 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]

最佳答案

我的解决方案: 因为我用了一个.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)

谢谢大家:)

关于ios - 为每个 SCNNode 人脸设置不同的图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51949792/

相关文章:

ios - 在 SceneKit 中,快速尝试通过触摸来移动角色

ios - 添加更多项目时滑动手势不起作用

ios - Xamarin 中缺少 ios 选项

iOS 应用使用 ShareKit 发布到 Facebook,为什么需要隐私政策?

swift - 控制子类是否符合基类中的协议(protocol)是否合法、安全或有目的

ios - 在 SceneKit 中更改 "field of view"

ios - 如何使用具有 affineTransform 的 TouchMoved 移动 UIView

ios - GameScene.sks 和 GameScene.swift

json - 快速将简单字符串转换为 JSON 字符串

ios - 我如何知道几何体 (SCNNodes) 何时位于 View 的边缘?