SceneKit:如何从图像中为SCNFloor创建棋盘图案?

时间:2017-02-03 01:54:03

标签: scenekit skscene sceneview

目标是创造一个无限的棋盘图案。

使用SCNFloor和附加图像,我们生成了一些接近但不太像棋盘的东西。一些黑色方块合并在一起,但不应该合并。

我们为ScaleWrapSWrapTMin filterMap filterMip filter尝试了不同的值。屏幕截图显示了当前值。

底层图片是否不正确,或者我们需要为SCNFloor更改哪些设置?

重复图片:

enter image description here

结果:

enter image description here

2 个答案:

答案 0 :(得分:0)

看起来SCNFloor有接缝,使其具有无限的能力,并且可能允许系统进行一些LOD优化。您可能需要创建自己的楼层。

答案 1 :(得分:0)

#import "GameViewController.h"

@interface GameViewController ()

@property (nonatomic) CGFloat chessBoardWidth;
@property (nonatomic) CGFloat chessBoardDepth;
@property (nonatomic) CGFloat tileWidth;
@property (nonatomic) CGFloat tileDepth;
@property (nonatomic, getter=isOdd) BOOL odd;

@end

@implementation GameViewController

-(instancetype)init {
    self = [super init];

    if(self) {
        self.chessBoardWidth = 10.0f;
        self.chessBoardDepth = 10.0f;
        self.tileWidth = 1.0f;
        self.tileDepth = 1.0f;
    }

    return self;
}

-(void)awakeFromNib
{
    [super awakeFromNib];

    // create a new scene
    SCNScene *scene = [SCNScene sceneNamed:@"art.scnassets/chessboard.scn"];

    // create and add a camera to the scene
    SCNNode *cameraNode = [SCNNode node];
    cameraNode.camera = [SCNCamera camera];
    [scene.rootNode addChildNode:cameraNode];

    // place the camera
    cameraNode.position = SCNVector3Make(0, 0, 150);

    // create and add a light to the scene
    SCNNode *lightNode = [SCNNode node];
    lightNode.light = [SCNLight light];
    lightNode.light.type = SCNLightTypeOmni;
    lightNode.position = SCNVector3Make(0, 10, 10);
    [scene.rootNode addChildNode:lightNode];

    // create and add an ambient light to the scene
    SCNNode *ambientLightNode = [SCNNode node];
    ambientLightNode.light = [SCNLight light];
    ambientLightNode.light.type = SCNLightTypeAmbient;
    ambientLightNode.light.color = [NSColor darkGrayColor];
    [scene.rootNode addChildNode:ambientLightNode];

    // Material

    SCNMaterial *blackMaterial = [SCNMaterial material];
    blackMaterial.diffuse.contents = [NSColor blackColor];

    SCNMaterial *whiteMaterial = [SCNMaterial material];
    whiteMaterial.diffuse.contents = [NSColor whiteColor];

    // Geometry

    SCNPlane *blackTile = [[SCNPlane alloc] init];
    blackTile.firstMaterial = blackMaterial;

    SCNPlane *whiteTile = [[SCNPlane alloc] init];
    whiteTile.firstMaterial = whiteMaterial;

    // Parent node

    SCNNode *parentNode = [[SCNNode alloc] init];
    [scene.rootNode addChildNode:parentNode];

    self.odd = YES;

    for (uint x=0; x < self.chessBoardWidth; x++) {
        for (uint z=0; z < self.chessBoardDepth; z++) {

            // Add tile

            SCNNode *tileNode = [[SCNNode alloc] init];

            if(self.isOdd) {
                tileNode.geometry = blackTile;
            } else {
                tileNode.geometry = whiteTile;
            }

            [parentNode addChildNode:tileNode];

            // Position tile
            tileNode.position = SCNVector3Make(self.tileWidth * x, 0, self.tileDepth * z);

            // Alternate

            if(self.isOdd) {
                self.odd = NO;
            } else {
                self.odd = YES;
            }
        }
    }

    // set the scene to the view
    self.gameView.scene = scene;

    // allows the user to manipulate the camera
    self.gameView.allowsCameraControl = YES;

    // show statistics such as fps and timing information
    self.gameView.showsStatistics = YES;

    // configure the view
    self.gameView.backgroundColor = [NSColor grayColor];
}

@end