Change color SCNNode / Geometry / Material

I am very new to SceneKit and am starting a test application. I have a small cube that moves. I would like to add an axis (X, Y, Z). I created them like that.

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

My problem is that I would like to change the color on them so that I can determine what is. Is there a way I can do this, or do I need to create my lines differently? I am making a mac application, not an iOS application, just fyi. Also, I know about code repetition, I'm just tired, and I will fix it.

+4
source share
1 answer

Modify the geometry material using -firstMaterialor (if it is a complex object) an array -materials.

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

Or more briefly:

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

Source: https://habr.com/ru/post/1616748/


All Articles