一尘不染

SCNBox每个面上的颜色或纹理不同

swift

我是iOS开发的新手,但我陷入了困境。我正在尝试使用每个面都有不同颜色的SceneKit渲染立方体。

到目前为止,这是我得到的:

func sceneSetup() {
    // 1
    let scene = SCNScene()

    // 2
    let BoxGeometry = SCNBox(width: 0.9, height: 0.9, length: 0.9, chamferRadius: 0.0)

    BoxGeometry.firstMaterial?.diffuse.contents = UIColor.redColor()
    let cube = SCNNode(geometry: BoxGeometry)
    cube.position = SCNVector3(x: 0, y: 0, z: -1)
    scene.rootNode.addChildNode(cube)

    // 3
    sceneView.scene = scene
    sceneView.autoenablesDefaultLighting = true
    sceneView.allowsCameraControl = true

但是我希望每张脸都有不同的颜色。我怎么做?


阅读 160

收藏
2020-07-07

共1个答案

一尘不染

该盒子由六个不同的元素组成(每侧一个)。您可能还已经注意到,几何对象具有 第一种 材料的一个属性,但也具有一系列材料的属性。

具有多个元素和多种材质的对象将选择每个元素的材质增量(并环绕)。

例如4种元素和1种材料

Element   1  2  3  4
Material  1  1  1  1

或4种元素和2种材料

Element   1  2  3  4
Material  1  2  1  2  // note that they are repeating

例如4种元素和7种材料

Element   1  2  3  4
Material  1  2  3  4  // (5, 6, 7) is unused

对于盒子来说,这意味着您可以使用六种材料组成的阵列在盒子的每一侧上具有唯一的材料。我的场景工具包(在Objective-
C中)的其中一章的示例代码中有一个这样的示例

// Each side of the box has its own color
// --------------------------------------
// All have the same diffuse and ambient colors to show the
// effect of the ambient light, even with these materials.

SCNMaterial *greenMaterial              = [SCNMaterial material];
greenMaterial.diffuse.contents          = [NSColor greenColor];
greenMaterial.locksAmbientWithDiffuse   = YES;

SCNMaterial *redMaterial                = [SCNMaterial material];
redMaterial.diffuse.contents            = [NSColor redColor];
redMaterial.locksAmbientWithDiffuse     = YES;

SCNMaterial *blueMaterial               = [SCNMaterial material];
blueMaterial.diffuse.contents           = [NSColor blueColor];
blueMaterial.locksAmbientWithDiffuse    = YES;

SCNMaterial *yellowMaterial             = [SCNMaterial material];
yellowMaterial.diffuse.contents         = [NSColor yellowColor];
yellowMaterial.locksAmbientWithDiffuse  = YES;

SCNMaterial *purpleMaterial             = [SCNMaterial material];
purpleMaterial.diffuse.contents         = [NSColor purpleColor];
purpleMaterial.locksAmbientWithDiffuse  = YES;

SCNMaterial *magentaMaterial            = [SCNMaterial material];
magentaMaterial.diffuse.contents        = [NSColor magentaColor];
magentaMaterial.locksAmbientWithDiffuse = YES;


box.materials =  @[greenMaterial,  redMaterial,    blueMaterial,
                   yellowMaterial, purpleMaterial, magentaMaterial];
2020-07-07