I have small textures (8×8, 16×16, 20×20, also 10×20, 24×32, 64×16 and other various sizes) which I'd like to fill a certain rectangular area on the screen, repeating and not stretching.
Problem is if I just draw enough sprites one after another by doing multiple draw calls, it just consumes a lot of cpu. Isn't there some other way to do it that wouldn't require so many draw calls?
Using pre-made textures of repeating sprites isn't a solution — target rectangular areas have different sizes and are randomly generated each game.
At the time you create your random rectangular areas you could create a RenderTarget2D along with each rectangle. Then you draw your tiles into that RenderTarget2D once. After that you could use the RenderTarget2D (which can be drawn just like a Texture2D) to draw ALL your tiles in a single Draw call.
Related
From Apples class reference for SKShapeNode's but also from many developers i hear that using SKShapeNode for drawing custom shapes you want to draw often on the view is a bad idea because it performs bad.
Its true, a simple app with some custom shapes is spiking my CPU up to 80% and using like 80MB RAM (on devices its a bit better).
So then, how do i draw shapes like arrows without using SKShapeNode, because i like the idea to draw with bezierpaths since i do not need to care about display size.
How to draw an arrow with a texture without getting a bad quality, since it would stretch my image when i move my touch stretching the arrow. Doing this with SKShapeNode works perfect but performs bad.
You can always have one SKShapeNode node available that you use for creating shapes, and use the path property to design the shape. Then create a texture out of the SKShapeNode from the scenes view with let texture = scene.view.textureFromNode(shapeNode) and place the new texture into an SKSpriteNode
I have to draw a number sprites and also a very large circle, and I'm wondering if is it better to draw that circle using ccDrawCircle, or to make a circle sprite and draw it as a regular sprite? In terms of memory, I'd be using up a lot to store basically nothing but a giant circle; but is it faster to do all the drawing as one batch? What if I had to split it up into two batches, one for the circle and another for the other sprites? Is it faster to draw a primitive shape or to draw a sprite?
ccDraw* methods aren't batch-drawn and meant for debugging, they are not particularly efficient.
CCDrawNode is a much better option as it can batch-draw primitives, but it doesn't allow primitives to be removed without recreating them all.
Batched sprites are usually the best option. How much this affects fps depends on your use case and targeted devices. Problem is they don't "scale" well, so it's best to have each required size of a shape as a separate image. On the other hand you can tint (colorize) a grayscale image in order to have more color variety without having to have each shape with different colors as separate images.
I am trying to put together ray tracing routine and the width/shade of every pixel in a single ray will change over the length of the line. Is there a way in SpriteKit to draw a single pixel on the screen? or should I be doing this using UIImage?
SpriteKit isn't a pixel drawing API. It's more of a "moving pictures around on the screen" API. There are a couple of cases, though, where it makes sense to do custom drawing, and SpriteKit (as of iOS 8 and OS X 10.10) has a few facilities for this.
If you want to create custom art to apply to sprites, with that art being mostly static (that is, not needing to be redrawn frequently as SpriteKit animates the scene), just use the drawing API of your choice — Core Graphics, OpenGL, emacs, whatever — to create an image. Then you can stick that image in an SKTexture object to apply to sprites in your scene.
To directly munge the bits inside a texture, use SKMutableTexture. This doesn't really provide drawing facilities — you just get to work with the raw image data. (And while you could layer drawing tools on top of that — for example, by creating a CG image context from the data and rewriting the data with CG results — that'd slow you down too much to keep up with the animation framerate.)
If you need high-framerate, per-pixel drawing, your best bet is to do that entirely on the GPU. Use SKShader for that.
If your application is redrawing every frame, then you can use an offscreen buffer to dynamically update pixels, aka SKMutableTexture. I'm not sure how slow it will be.
I'm ok with iOS drawing. I've had no problem drawing circles, lines, etc onto a view. In my latest project I would like to restrict my drawing to an irregular area on my view. Basically I have a paper doll outline (jpg) of a person. I want to be able to draw within that outline but have drawing stop when I reach the border. I'm honestly not really sure what my approach can be to accomplish this. Do I have to do hit testing to see if I'm within this irregular region? I don't think that is realistic if I start with a JPG. Do I need to use a "special" color outside my region and test for that color under my brush? I'm worried that won't be accurate as I'm using a big fat fuzzy brush to draw.
Is it possible to restrict drawing within an irregular boundary?
Of course it's possible!
If you are drawing with CoreGraphics (Quartz), you could use a clipping path, or a bitmap mask.
If you are using CoreAnimation, then try a mask layer.
(It sounds like a bitmap mask is what you want, since you're talking about using an arbitrary JPEG image.)
I have lines that are programmatically defined by my program. what I want to do is render a brush stroke along them.
the way I think the type of brush I want works is, it simply has a texture, mostly transparent, and what you do is, render this texture centered on EVERY PIXEL in the path, and they blend together to create the stroke.
now assuming this even works, I'm going to make a bet that it will be WAY too expensive (targeting the ipad and other mobile chips, which HATE fillrate and alpha blending)
so, what other options are there?
if it could be done in realtime (that is, the path spline updating every frame) that would be ideal. but if not, within a fraction of a second on the ipad would be good too (the splines connect nodes, the user can drag nodes around thus transforming the spline, but it would be acceptable to revert to a simpler fill for the spline while it was moving around, then recalculate the brush once they release it)
for those wondering, I'm trying to get it so the thick lines look like they have been made with a pencil. it should look as real life as possible.
I considered just rendering the brushed spline to a texture, but as the spline can be any length, in any direction, dedicating a WHOLE rectangular texture to encompass the whole spline would be way to costly...
the spline is inevitably broken up into quads for rendering, so I thought of initially rendering the brush to a texture, then generating an optimized texture with each of the quads separated and packed as neatly as possible into the texture.
but two renders to texture... algorithm to create the optimized texture, making it so the quads still seamlessly blend with each other... sounds like a nightmare, and thats not even making it realtime.
so yeah, any ideas on how to draw thick, pencil like lines that follow a spline in real time on the ipad in openGL?
From my point of view, what you want is to render a line that:
is textured
has the edges fade off (i.e. no sharp edge to it)
follows a spline
To achieve these goals I would first of all break the spline up into a series of line segments that closely approximate the curve (you can make it more or less fine-grained depending on how accurate you want it to be versus how fast you want it to render).
Once you have these, you will need to make each segment into 3 quads, one that goes over the middle of the line segment that serves as the fully opaque part of the line and one on each edge of the line that will fade out to be totally transparent.
You will need to use a little bit of math to make sure that you extrude the quads along a vector that bisects 2 segments equally (i.e. so that the angle between the each segment and the extrusion vector are equal). This will ensure that you don't have gaps in the obtuse part of the join and overlaps in the acute parts.
After all of this, you just need to use the vertex positions as the UV co-ordinates (probably scaled though) and allow the texture to wrap around.
Using this method, you should end up with a mesh that has a solid thick line running through the middle of your spline with "fins" that taper off into complete transparency. This should approximate the effect you want quite closely while only rendering to relevant pixels (i.e. no giant areas of completely transparent pixels) and with very litter memory overhead.
I've been a little vague here as its kind of hard to explain with text alone and without writing an in depth tutorial. If you need more info, just comment on what your stuck on and I'll elaborate further.