SCNBox different colour or texture on each face

前端 未结 3 1021
天涯浪人
天涯浪人 2020-12-08 01:10

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 g

3条回答
  •  南笙
    南笙 (楼主)
    2020-12-08 01:36

    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

    Element   1  2  3  4
    Material  1  1  1  1
    

    or 4 elements and 2 materials

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

    For example 4 elements and 7 materials

    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):

    // 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];
    

提交回复
热议问题