【问题标题】:SceneKit: how to create chessboard pattern for SCNFloor from image?SceneKit:如何从图像中为 SCNFloor 创建棋盘图案?
【发布时间】:2017-02-04 00:47:41
【问题描述】:

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

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

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

底层图像是否不正确,或者我们需要为 SCNFloor 更改什么设置?

重复图片:

结果:

【问题讨论】:

  • 我在 SCNFloor 上尝试了您的纹理,正方形看起来很规则,与您的屏幕截图不同。您是否更改了任何其他设置?
  • @JamesP 不。所以你是说你的 SCNFloor 看起来像一个普通的棋盘?你只是使用默认设置吗?
  • 是的,一切都是默认的,比例为 1。看起来就像您所期望的那样。你在角落里的小预览球看起来也错了,纹理是偏移的,在我的它是居中的。
  • @JamesP 好的,非常感谢。将尝试一个新的项目。顺便说一句,您对此 q 有什么建议:stackoverflow.com/questions/42019638/…

标签: scenekit skscene sceneview


【解决方案1】:

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

【讨论】:

  • 我会在这里第二次混淆并说你不需要图像来做到这一点。你所需要的只是有一个由许多飞机组成的地板。每个平面都将具有白色材料或黑色材料。您可以共享所有白色节点和所有黑色节点的几何体和材质。所有节点都将附加到一个父节点。
  • @KarlSigiscar 谢谢,但有点困惑。如何使用多个平面来模拟地板?你如何使白色平面与黑色平面重叠(或任何颜色/图像,如果不是黑色/白色)以产生棋盘图案?
【解决方案2】:
#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

【讨论】:

  • 谢谢!但这会创建一个像 SCNFloor 这样的无限板?如果这是无限的,不清楚chessBoardWidthchessBoardDepth 是干什么用的?例如,固定这些值是否意味着如果相机经过chessBoardDepth,棋盘就会消失?
  • 没错,但是,您可以增加这些值或图块的大小,并确保它始终覆盖相机的视锥体。它不必是无限的。
  • 谢谢卡尔。您是否尝试过这种方法并且可以保证它的性能?如果没有,我可以测试一些,但我担心这种方法不如 SCNFloor 性能好。
猜你喜欢
  • 2021-07-07
  • 2016-03-06
  • 2021-06-10
  • 1970-01-01
  • 1970-01-01
  • 2017-10-04
  • 2022-11-04
  • 1970-01-01
  • 2016-05-23
相关资源
最近更新 更多