一次绘制多个skshapenode? [英] Multiple skshapenode in one draw?

查看:25
本文介绍了一次绘制多个skshapenode?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我已经绘制了连接图表中节点的 skshapenodes,用于视觉 a* 表示.

Hi I have drawn skshapenodes connecting the nodes in my graph for my visual a* representation.

for(int x=0; x<64; x++)
        {
            for(int y=0; y<48; y++)
            {
                PathFindingNode *node = [[gridToDraw objectAtIndex:x] objectAtIndex:y];
                for(int i=0; i< node.connections.count; i++)
                {
                    PathFindingNode *neighbor = [node.connections objectAtIndex:i];

                    SKShapeNode *line = [SKShapeNode node];
                    CGMutablePathRef pathToDraw = CGPathCreateMutable();
                    CGPathMoveToPoint(pathToDraw, NULL, node.position.x, node.position.y);
                    CGPathAddLineToPoint(pathToDraw, NULL, neighbor.position.x, neighbor.position.y);
                    line.path = pathToDraw;
                    line.lineWidth = 0.1f;
                    [line setStrokeColor:[UIColor blueColor]];
                    line.alpha = 0.1f;
                    [self addChild:line];
                }
            }
        }

我的图表中有很多节点,它绘制了近 22,000 个形状.有没有一种方法可以在一次绘制调用中绘制这些形状,因为它们都是相同的,唯一的区别是它们的开始和结束位置.

There are lots of nodes in my graph and this draws almost 22,000 shapes. Is there a way I can draw these shapes in one draw call, as they are all the same, the only difference is the start and end location of them.

如果我改用纹理,它会被加载一次,我怎么能改变它的旋转来像上面一样连接我的所有节点.

If I used a texture instead, which would be loaded in once, how could I change the rotation of it to join all of my nodes up like above.

问候,

更新:

SKShapeNode *line = [SKShapeNode node];
        CGMutablePathRef pathToDraw = CGPathCreateMutable();

        for(int x=0; x<64; x++)
        {
            for(int y=0; y<48; y++)
            {
                PathFindingNode *node = [[gridToDraw objectAtIndex:x] objectAtIndex:y];
                for(int i=0; i< node.connections.count; i++)
                {
                    PathFindingNode *neighbor = [node.connections objectAtIndex:i];
                    CGPathMoveToPoint(pathToDraw, NULL, node.position.x, node.position.y);
                    CGPathAddLineToPoint(pathToDraw, NULL, neighbor.position.x, neighbor.position.y);
                }
            }
        }

        line.path = pathToDraw;
        line.lineWidth = 0.1f;
        [line setStrokeColor:[UIColor blueColor]];
        line.alpha = 0.1f;
        [self addChild:line];

我已经更新了我的代码看起来像上面,这个绘制了一个 sknode,但它绘制了 185 次,这是为什么?

I have updated my code to look like the above, this draws one sknode, but it draws 185 times, why is this?

推荐答案

我昨天打算回答一个类似的问题,但它消失了.

I was going to answer this yesterday on a similar question but it disappeared.

您可以创建一个路径并将所有内容绘制为一个 SKShapeNode 使用 CGPathAddPath 来合并路径.

You can create one path and draw everything as one SKShapeNode using CGPathAddPath to merge the paths as you go.

这是一个例子:

SKEffectNode *effectNode = [[SKEffectNode alloc]init];
SKShapeNode  *shape = [[SKShapeNode alloc] init];
CGMutablePathRef myPath = CGPathCreateMutable();
CGPathAddArc(myPath, NULL, 0,0, 15, 0, M_PI*2, YES);

CGMutablePathRef pathTwo = CGPathCreateMutable();
CGPathAddArc(pathTwo, NULL, 50,0, 15, 0, M_PI*2, YES);     
CGPathAddPath(myPath, NULL, pathTwo);

CGMutablePathRef pathThree = CGPathCreateMutable();
CGPathAddArc(pathThree, NULL, 100,0, 15, 0, M_PI*2, YES);
CGPathAddPath(myPath, NULL, pathThree);

shape.path = myPath;

[effectNode addChild:shape];
[self addChild:effectNode];

effectNode.shouldRasterize = YES;  // if you don't rasterize it's horrifically slow.

这只是一个示例,说明如何合并不同的路径以创建一个 SKShapeNode,其中包含您正在寻找的所有视觉元素.您需要将此概念应用到您的代码中.

This was just an example of how to merge the different paths to create one SKShapeNode that has all visual elements you are seeking. You'll need to apply this concept to your code.

我将生成的 SKShapeNode 添加到 SKEffectNode 以便我可以利用 shouldRasterize 属性来缓存形状,否则您的帧速率将是太可怕了.

I added the resulting SKShapeNode to an SKEffectNode so that I could utilize the shouldRasterize property to cache the shape, otherwise your framerate will be horrible.

这篇关于一次绘制多个skshapenode?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆