【问题标题】:Change color of SCNNode/Geometry/Material改变 SCNNode/Geometry/Material 的颜色
【发布时间】:2016-02-23 01:15:25
【问题描述】:

我对 SceneKit 非常陌生,并且正在开始一个测试应用程序。我有一个可以四处移动的小立方体。我想添加轴(X、Y、Z)。我是这样创建它们的。

    SCNVector3 xPositions[] = {SCNVector3Make(-50, 0, 0), SCNVector3Make(50, 0, 0)};

    int indicies[] = {0, 1};
    NSData *indexData = [NSData dataWithBytes:indicies length:sizeof(indicies)];

// X axis
    SCNGeometrySource *xSource = [SCNGeometrySource geometrySourceWithVertices:xPositions count:2];
    SCNGeometryElement *xElement = [SCNGeometryElement geometryElementWithData:indexData primitiveType:SCNGeometryPrimitiveTypeLine primitiveCount:2 bytesPerIndex:sizeof(int)];
    SCNGeometry *xAxis = [SCNGeometry geometryWithSources:@[xSource] elements:@[xElement]];
    SCNNode *xNode = [SCNNode nodeWithGeometry:xAxis];
    [self.gameView.scene.rootNode addChildNode:xNode];

    // Y axis
    SCNVector3 yPositions[] = {SCNVector3Make(0, -50, 0), SCNVector3Make(0, 50, 0)};
    SCNGeometrySource *ySource = [SCNGeometrySource geometrySourceWithVertices:yPositions count:2];
    SCNGeometryElement *yElement = [SCNGeometryElement geometryElementWithData:indexData primitiveType:SCNGeometryPrimitiveTypeLine primitiveCount:2 bytesPerIndex:sizeof(int)];
    SCNGeometry *yAxis = [SCNGeometry geometryWithSources:@[ySource] elements:@[yElement]];
    SCNNode *yNode = [SCNNode nodeWithGeometry:yAxis];
    [self.gameView.scene.rootNode addChildNode:yNode];

    // Z axis
    SCNVector3 zPositions[] = {SCNVector3Make(0, 0, -50), SCNVector3Make(0, 0, 50)};
    SCNGeometrySource *zSource = [SCNGeometrySource geometrySourceWithVertices:zPositions count:2];
    SCNGeometryElement *zElement = [SCNGeometryElement geometryElementWithData:indexData primitiveType:SCNGeometryPrimitiveTypeLine primitiveCount:2 bytesPerIndex:sizeof(int)];
    SCNGeometry *zAxis = [SCNGeometry geometryWithSources:@[zSource] elements:@[zElement]];
    SCNNode *zNode = [SCNNode nodeWithGeometry:zAxis];
    [self.gameView.scene.rootNode addChildNode:zNode];

我的问题是我想改变它们的颜色,这样我就可以分辨出哪个是哪个。有没有办法可以做到这一点,或者我必须以另一种方式创建我的线条?我正在制作一个 Mac 应用程序,而不是 iOS 应用程序,仅供参考。另外,我知道重复代码,我只是累了,我会解决这个问题。

【问题讨论】:

    标签: objective-c cocoa scenekit


    【解决方案1】:

    使用-firstMaterial 或(如果是复杂对象)-materials 数组更改几何体的材质。

    SCNMaterial *redMaterial = [SCNMaterial new];
    redMaterial.diffuse.contents = [NSColor redColor];
    xAxis.firstMaterial = redMaterial;
    

    或者更简洁:

    yAxis.firstMaterial.diffuse.contents = [NSColor magentaColor];
    zAxis.firstMaterial.diffuse.contents = [NSColor yellowColor];
    

    【讨论】:

    • 我可以把它改成红蓝还是绿?我真的不明白那是做什么的。对不起。
    • 当然,选择你喜欢的任何颜色。 contents 属性属于SCNMaterialProperty 类,它可以是颜色、图像、Core Animation 层或树,或 SpriteKit 纹理或场景。请参阅 SCNMaterialSCNMaterialProperty 的参考文档。
    • 我最终使用了emission 而不是diffuse。就我而言,我正在使用为 ARKit 项目启用的sceneView.autoenablesDefaultLighting,而diffuse 并没有改变我节点的颜色。
    猜你喜欢
    • 2021-03-16
    • 2016-10-14
    • 1970-01-01
    • 2020-12-06
    • 2014-12-17
    • 2019-10-16
    • 2020-07-01
    • 2018-03-26
    • 2023-04-01
    相关资源
    最近更新 更多