Combining two or more SCNGeometries

I made this method to capture UIImage and turn it into a 3D model. However, this means that I am adding to many nodes. I thought maybe it could be optimized by adding all the geometries to one node. Is there any way to do this?

Here is the code

static inline SCNNode* S2NNode(const UIImage* sprite) {
  SCNNode *spriteNode = [SCNNode node];
  CFDataRef pixelData = CGDataProviderCopyData(CGImageGetDataProvider(sprite.CGImage));
  const UInt8* data = CFDataGetBytePtr(pixelData);
  for (int x = 0; x < sprite.size.width; x++)
  {
    for (int y = 0; y < sprite.size.height; y++)
    {
      int pixelInfo = ((sprite.size.width * y) + x) * 4;
      UInt8 alpha = data[pixelInfo + 3];
      if (alpha > 3)
      {
        UInt8 red   = data[pixelInfo];
        UInt8 green = data[pixelInfo + 1];
        UInt8 blue  = data[pixelInfo + 2];
        UIColor *color = [UIColor colorWithRed:red/255.0f green:green/255.0f blue:blue/255.0f alpha:alpha/255.0f];
        SCNNode *pixel = [SCNNode node];
        pixel.geometry = [SCNBox boxWithWidth:1.001 height:1.001 length:1.001 chamferRadius:0];
        pixel.geometry.firstMaterial.diffuse.contents = color;
        pixel.position = SCNVector3Make(x - sprite.size.width / 2.0,
                                        y - sprite.size.height / 2.0,
                                        0);
        [spriteNode addChildNode:pixel];
      }
    }
  }
  CFRelease(pixelData);
  //The image is upside down and I have no idea why.
  spriteNode.rotation = SCNVector4Make(1, 0, 0, M_PI);
  return spriteNode;
}
+4
source share
2 answers

This answer addresses the initially asked question - how to combine geometries. However, merging geometries does not seem to be the solution to your real problem - how to get a pixel sprite with a certain thickness, with decent performance. See my other answer for this.


SCNNode flattenedClone node node . DrawGL OpenNL... , , . , , , , , , , GPU, .

, , , , .

+3

, , , , , , - , , , .


, , , , , 2D- 3D - , , :

chunky sprite

- . ( , " " ):

artifacts

, . , flattenedClone , - , , GPU, , ( ), ( ). :

408 draws, 4.9K polygons, 14.7K vertices

408 , 4.9K , 14.7K . iPad Air 14 - yuck.

, SCNShape. . ( , , - .)

, . , ( SpriteKit, SpriteKit iOS 8/OS X 10.10): .

, , SCNShape, - ( , ), :

UIImage *image = [UIImage imageNamed:@"sprite"];
UIBezierPath *path = PathFromImage(image); // Make or load your path here
SCNNode *node = [SCNNode nodeWithGeometry:[SCNShape shapeWithPath:path extrusionDepth:1]];
SCNMaterial *face = [SCNMaterial material];
face.diffuse.contents = image;
face.diffuse.magnificationFilter = SCNFilterModeNone;
SCNMaterial *side = [SCNMaterial material];
side.diffuse.contents = [UIColor blackColor];
side.specular.contents = [UIColor whiteColor];
node.geometry.materials = @[ face, side, side ];

SCNShape:

3 draws, 276 polygons, 828 vertices

, - 60 , .

+2

All Articles