ios – SCNBox在每张脸上都有不同的颜色或纹理

前端之家收集整理的这篇文章主要介绍了ios – SCNBox在每张脸上都有不同的颜色或纹理前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我是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

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

解决方法

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

具有多个元素和多个材质的对象将选择每个元素的材质(和包裹)的增量.

例如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

在盒子的情况下,这意味着您可以使用六种材料的阵列在盒子的每一侧上具有独特的材料.我在the sample code for one of the chapters为我的场景套件书(在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];
原文链接:https://www.f2er.com/iOS/328927.html

猜你在找的iOS相关文章