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

may*_*c19 22 ios scenekit swift scnnode

我是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
Run Code Online (Sandbox Code Playgroud)

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

Dav*_*ist 22

盒子由六个不同的元素组成(每边一个).您可能还注意到,几何对象具有第一个材质的一个属性,但也有一个材质数组的属性.

具有多个元素和多个材质的对象将为每个元素选取材料(和换行)的增量.

例如4个元素和1个材料

Element   1  2  3  4
Material  1  1  1  1
Run Code Online (Sandbox Code Playgroud)

或4种元素和2种材料

Element   1  2  3  4
Material  1  2  1  2  // note that they are repeating 
Run Code Online (Sandbox Code Playgroud)

例如4个元素和7个材料

Element   1  2  3  4
Material  1  2  3  4  // (5, 6, 7) is unused
Run Code Online (Sandbox Code Playgroud)

对于盒子而言,这意味着您可以使用六种材料的阵列在盒子的每一侧都有独特的材料.我在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];
Run Code Online (Sandbox Code Playgroud)

  • 干得好,先生!值得注意的另一件事是:对于许多几何图形,您可以通过查询其"geometryElementCount"来了解它支持的材料数量.这对于`SCNBox`不起作用,但是......它会根据你分配的材料数量自动重新创建它的几何元素集(如果它没有六种材质,它就不需要六次绘制调用来渲染). (4认同)
  • @RubenMartinezJr.数组顺序定义哪个面是哪个.它应与为[`SCNMaterialProperty.contents`]指定立方体贴图的顺序相同(https://developer.apple.com/documentation/scenekit/scnmaterialproperty/1395372-contents):+ x,-x,+ y, -y,+ z,-z.(相对于默认方向的查看器,即右侧,左侧,顶部,底部,远侧,近侧.) (3认同)
  • 哪一边是哪边(前、右、后、左、上、下)? (2认同)

Dav*_*vid 14

这是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
Run Code Online (Sandbox Code Playgroud)

要更改前面的材料,只需抓取材料并更改其内容即可

let frontMaterial = materials[0]
frontMaterial.diffuse.contents = UIColor.white
Run Code Online (Sandbox Code Playgroud)