Rotate a CCTexture2D - ios

So the premise of my problem is that I apply an alpha mask with a fragment shader to a circular CCSprite. Then I want to rotate the CCSprite while keeping the alpha mask in the same position. If I apply the mask and then rotate the sprite, the mask will rotate with the sprite. If I rotate the sprite and then apply the mask, the mask will be in the rotated position because I base it off the texture of the Sprite which never rotates.
Is there a way I can get the cctexture2d to rotate in a way that the mask will stay in place while the sprite is rotated or do I need to apply a custom vertex shader to the mask or the ccpsrite texture? If custom, what would the matrix transformations look like?

Related

Texture Brush (Drawing Application ) Using Metal

I am trying to implement a metal-backed drawing application where brushstrokes are drawn on an MTKView by textured square repeatedly along a finger position.
I am drawing this with alpha 0.2. When the squares are overlapped the color is added. How can I draw with alpha 0.2.
I think you need to draw the brush squares to a separate texture, initially cleared to transparent, without blending. Then draw that whole texture to your view with blending.
If you draw the brush squares directly to the view, then they will accumulate. After you draw square 1, it's part of the image. Metal can no longer distinguish it from anything else that was already there. So, when you draw square 2 overlapping it, it will blend with what's already there, including square 1.

Pixelated circles when scaling with SKSpriteNode

The perimeter around a circle gets pixelated when scaling down the image.
The embedded circle image has a radius of 100 pixels. (The circle is white so click around the blank space, and you'll get the image.) Scaling down using SpriteKit causes the border to get very blurry and pixelated. How to scale up/down and preserve sharp borders in SpriteKit? The goal is to use a base image for a circle and create circle images of different sizes with this one base image.
// Create dot
let dot = SKSpriteNode(imageNamed: "dot50")
// Position dot
dot.position = scenePoint
// Size dot
let scale = radius / MasterDotRadius
println("Dot size and scale: \(radius) and \(scale)")
dot.setScale(scale)
dot.texture!.filteringMode = .Nearest
It seems you should use SKTextureFilteringLinear instead of SKTextureFilteringNearest:
SKTextureFilteringNearest:
Each pixel is drawn using the nearest point in the texture. This mode
is faster, but the results are often pixelated.
SKTextureFilteringLinear:
Each pixel is drawn by using a linear filter of multiple texels in the
texture. This mode produces higher quality results but may be slower.
You can use SKShapeNode which will act better while scale animation, but end result (when dot is scaled to some value) will be almost pixelated as when using SKSpriteNode and image.

Spritekit textured line drawing

How to draw a texture line using SKShapenode in SpriteKit?
For example, how to draw chalk-like textured line on touch move?
Is the following method correct?
[lineNode setStrokeTexture:[SKTexture textureWithImageNamed:#"texture.png"]];
But it shows nothing and the line is empty.
A possible solution for your task is to use SKCropNode with the line node set as the mask node of the crop node and a texture node added as a child to the crop node. Keep in mind however, that SKCropNode does not use alpha value of the mask image pixels to "smoothly" mask out the target image. It just checks if the alpha of the mask image is greater than 0.05 and if so, displays the corresponding target image pixel and if not, it completely masks out the pixel. So the result may be somewhat pixelated.

Open GLES 2.0 draw in texture

I'm trying to load an image into a texture, then draw into the texture.
Am I right in thinking that to display a texture you always need an array of vertices and texture coordinates? So, to draw a square texture I would need to draw 2 triangles to make a square and attach the texture to them?
Yes, that's right. For a square texture, the alternative is to use point sprites with GL_POINTS.

How to distort a Sprite into a trapezoid?

I am trying to transform a Sprite into a trapezoid, I don't really care about the interpolation even though I know without it my image will lose detail. All I really want to do is Transform my rectangular Sprite into a trapezoid like this:
/ \
/ \
/__________\
Has anyone done this with CGAffineTransforms or with cocos2d?
The transformation you're proposing is not affine. Affine transformations have to be undoable. So they can typically:
Scale
Rotate
Shear (make lopsided, like square -> parallelogram)
Translate
But they cannot "squeeze". Notice that the left and right sides of your trapezoid, if extended, would intersect at a particular spot. They're not parallel anymore. So you couldn't "undo" the transformation, because if there was anything to transform at that spot, you couldn't decide where they would transform to. In other words, if a transformation doesn't preserve parallelism, it pinches space, can't be undone, and isn't affine.
I don't know that much about transformations in Core Animation, so I hope that mathy stuff helps you find an alternative.
But I do know how you could do it in OpenGL, but it would require you to start over on how you draw your application:
If I'm envisioning the result you want correctly, you want to build your rectangle in 3D, use an affine transformation to rotate it away a little bit, and use a (non-affine) projection transformation to flatten it into a 2D image.
If you're not looking for a 3D effect, but you really just want to pinch in the corners, then you can specify a GL_RECT with the points of your trapezoid and map your sprite onto it as a texture.
The easiest thing might be to pre-squeeze your image in a photo editor, save it as a .png with transparency, and draw a rectangle with that image.
You need to apply a CATransform3D to the layer of the UIView.
To find out the right one, it is easier to use AGGeometryKit.
#import <AGGeometryKit/AGGeometryKit.h>
UIView *view = ...; // create a view
view.layer.anchorPoint = CGPointZero;
AGKQuad quad = view.layer.quadrilateral;
quad.tl.x += 20; // shift top left x-value with 20 pixels
quad.tr.x -= 20; // shift top right x-value with 20 pixels
view.layer.quadrilateral = quad; // the quad is converted to CATransform3D and applied

Resources