SCNBox每个面上都有不同的颜色或纹理 [英] SCNBox different colour or texture on each face
问题描述
我是iOS开发的新手,我自己也很难过。我正在尝试使用每个面具有不同颜色的SceneKit渲染一个立方体。
I'm new to iOS development and I've got myself stumped. I am trying to render a cube using SceneKit that has a different colour for each face.
这是我到目前为止所得到的:
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.
例如4个元素和1个材料
For example 4 elements and 1 material
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个材料
For example 4 elements and 7 materials
Element 1 2 3 4
Material 1 2 3 4 // (5, 6, 7) is unused
对于包装盒,这意味着您可以使用六种材料的阵列在包装盒的每一面都有独特的材料。我在我的Scene Kit书籍的一个章节的示例代码(在Objective-C中):
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):
// 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];
这篇关于SCNBox每个面上都有不同的颜色或纹理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!