简体   繁体   English

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

[英]SCNBox different colour or texture on each face

I'm new to iOS development and I've got myself stumped. 我是iOS开发的新手,我自己也很难过。 I am trying to render a cube using SceneKit that has a different colour for each face. 我正在尝试使用每个面具有不同颜色的SceneKit渲染立方体。

This is what I've got so far: 这是我到目前为止所得到的:

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

But I'd like each face to have a different colour. 但我希望每张脸都有不同的颜色。 How do I do that? 我怎么做?

The box is composed out of six different elements (one for each side). 盒子由六个不同的元素组成(每边一个)。 You may also have noticed that a geometry object has one property for the first material but also a property for an array of materials. 您可能还注意到,几何对象具有第一个材质的一个属性,但也有一个材质数组的属性。

An object with multiple elements and multiple materials will pick the increment the material (and wrap) for each element. 具有多个元素和多个材质的对象将为每个元素选取材料(和换行)的增量。

For example 4 elements and 1 material 例如4个元素和1个材料

Element   1  2  3  4
Material  1  1  1  1

or 4 elements and 2 materials 或4种元素和2种材料

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

For example 4 elements and 7 materials 例如4个元素和7个材料

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

In the case of the box this means that you can use an array of six materials to have a unique material on each side of the box. 对于盒子而言,这意味着您可以使用六种材料的阵列在盒子的每一侧都有独特的材料。 I have an example of this in the sample code for one of the chapters for my Scene Kit book (in Objective-C): 我在Scene Kit的一个章节的示例代码中有一个例子(在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];

Here's a Swift 4 answer. 这是Swift 4的答案。

    let colors = [UIColor.green, // front
                  UIColor.red, // right
                  UIColor.blue, // back
                  UIColor.yellow, // left
                  UIColor.purple, // top
                  UIColor.gray] // bottom

    let sideMaterials = colors.map { color -> SCNMaterial in
        let material = SCNMaterial()
        material.diffuse.contents = color
        material.locksAmbientWithDiffuse = true
        return material
    }

    materials = sideMaterials

To change the front material just grab the material and change it's content 要更改前面的材料,只需抓取材料并更改其内容即可

let frontMaterial = materials[0]
frontMaterial.diffuse.contents = UIColor.white

thai you for the fast help. 泰国你的快速帮助。 i used the code you posed but was unable to use NSColor so i tried uicolor but all i was getting was a black cube so i tried this and i got it working 我使用你提出的代码,但无法使用NSColor所以我尝试了uicolor但我得到的只是一个黑色的立方体,所以我尝试了这个,我让它工作

    let BoxGeometry = SCNBox(width: 0.95, height: 0.95, length: 0.95, chamferRadius: 0.0)

    let greenMaterial = SCNMaterial()
    greenMaterial.diffuse.contents = UIImage(named: "g")
    greenMaterial.locksAmbientWithDiffuse = true;

    let redMaterial = SCNMaterial()
    redMaterial.diffuse.contents = UIImage(named: "r")
    redMaterial.locksAmbientWithDiffuse = true;


    let blueMaterial  = SCNMaterial()
    blueMaterial.diffuse.contents = UIImage(named: "b")
    blueMaterial.locksAmbientWithDiffuse = true;


    let yellowMaterial = SCNMaterial()
    yellowMaterial.diffuse.contents = UIImage(named: "y")
    yellowMaterial.locksAmbientWithDiffuse = true;


    let purpleMaterial = SCNMaterial()
    purpleMaterial.diffuse.contents = UIImage(named: "p")
    purpleMaterial.locksAmbientWithDiffuse = true;


    let WhiteMaterial = SCNMaterial()
    WhiteMaterial.diffuse.contents = UIImage(named: "w")
    WhiteMaterial.locksAmbientWithDiffuse   = true;


    BoxGeometry.materials =  [greenMaterial,  redMaterial,    blueMaterial,
    yellowMaterial, purpleMaterial, WhiteMaterial];

g is a jpeg of a green and so on and that has got it working now. g是一个绿色的jpeg等等,现在它已经运行了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM