While I usually prefer RGB color space over HSV, I'd like SKEmitterNode to emit particles with max saturation and value and have hue value to range across all colors. Is that possible?
I'm new to XCode, Swift and SpriteKit, and am unable to open .sks files due to a bug in XCode, so I don't know what can be done that way, but as far as I learned by looking through docs, the closest I can get is setting RGB range as in:
emitter.particleColorRedRange = 0.5;
emitter.particleColorGreenRange = 0.5;
emitter.particleColorBlueRange = 0.5;
Is there something like this:
emitter.particleColorHueRange = 0.5;
emitter.particleColorSaturationRange = 0.0;
emitter.particleColorValueRange = 0.0;
I could make a bunch of SKEmitterNode instances, with each of them having set specific UIColor, but that seems like very inefficient and ugly hack.
Thanks, Mirac7
SKEmitterNode does not provide an API for working within an HSV colorspace. One possible alternative would be to specify an explicit sequence of colors using the particleColorSequence property.
Related
I don't know the numbers. I am bad with CFrames. What I need is to know how to make a BodyGiro that would make sure that the bottom face of a part is always facing down. I know the MaxTorque would be Vector3.new(math.huge, 0, math.huge). But what would the CFrame be?
The way I understand it (from reading the reference), the CFrame property on BodyGyro is only used for its angular components. So basically it gives you the ability to set up a desired orientation of your part, by initializing it with x,y and z angles. Then you use MaxTorque to specify the amount of force you want to be allowed for each angle.
So for example:
-- create a part and float it in the air
local part = Instance.new("Part", game.Workspace)
local inTheAirHolder = Instance.new("BodyPosition", part)
inTheAirHolder.Position = Vector3.new(5,5,5)
-- apply strong force on a desired x and z orientation
local gyro = Instance.new("BodyGyro", part)
gyro.MaxTorque = Vector3.new(math.huge, 0, math.huge)
Since the orientation by default is 0/0/0 it would float with the bottom facing down as you want.
Lets say you wanted it 10 degrees tilted, you would use the CFrame like that:
gyro.CFrame = CFrame.Angles(math.rad(10), 0, 0)
My SCNNode is a compass that I want to rotate towards north.
compassNode = scene?.rootNode.childNodes[0]
I am successfully getting heading data and converting it to radians and storing it in a variable called angle.
angle = newHeading.trueHeading.toRadians
I have tried three methods now:
rotate = SCNAction.rotate(by: CGFloat(angle), around: SCNVector3(0, 1, 0), duration: 0.5)
compassNode?.runAction(rotate)
and
compassNode?.rotation = SCNVector4Make(0, 1, 0, Float(angle))
and
let oldTransform = compassNode?.transform
let newTransform = SCNMatrix4MakeRotation(Float(angle), 0, 1, 0)
SCNTransaction.begin()
SCNTransaction.animationDuration = 0.5
compassNode?.transform = SCNMatrix4Mult(newTransform, oldTransform!)
SCNTransaction.commit()
None of them makes any noticeable transformation to the compass. It appears to be still.
Running out of comment characters... Sry - have to put it in answer section.
Convert 90 degrees to radians, rotateTo that value. Rotating models can be confusing in 3D (for me anyway), sometimes it's easier to just use an SCNBox at first - it will have the correct rotation by default provided you're looking down -Z if you didn't change the camera view. You can turn on the the 3D outlines and sometimes that helps provide a better visual OR just texture map the box with different colors so that Red is the top, Green is front or whatever.
Then you can reshape the box, rotated it with different angles until you get the hang of rotateTo, rotateBy, rotateAround. I'm not a math guru, so unfortunately trial and error for me sometimes, but once I understand what's happening, then I can put the model in and know that my math works - then if I have to do some work on the model, such as pivot point or need to change the model - set Z-UP or whatever (this post: 59766878), then I'm focused on one thing at a time and not having a combination of problems to figure out.
I've also used lookAt and placed various (hidden) nodes at particular positions, so that I can see what X,Y,Z rotations look like at various points in a game and from different view points.
I am trying to replicate the behavior of CGContextClipToMask on iOS without any luck so far. Does anyone know how CGContextClipToMask works internally? I have read the documentation and it says it simply multiplies the image alpha value by the mask alpha value, so that is what I am doing in my custom function, however when I draw the result image using normal blending on to a CGContext multiple times, the result gets darker and darker, whereas with CGContextClipToMask the result is correct and does not get darker.
My theory is that CGContextClipToMask somehow uses the destination context, in addition to the image and mask to produce a correct result, but I just don't know enough about core graphics to be sure.
I've also read this question:
How to get the real RGBA or ARGB color values without premultiplied alpha?
and it's possible I am running into this problem, but then how does CGContextClipToMask get around the problem of precision loss with 8 bit alpha?
I found the problem. When multiplying the mask, I had to do a ceilf call on the RGB values like so:
float alphaPercent = (float)maskAlpha / 255.0f;
pixelDest->A = maskAlpha;
pixelDest->R = ceilf((float)pixelDest->R * alphaPercent);
pixelDest->G = ceilf((float)pixelDest->G * alphaPercent);
pixelDest->B = ceilf((float)pixelDest->B * alphaPercent);
Amazingly, this solves the problem...
I personally have no idea about the internals of CGContextClipToMask, but maybe you can find out something interesting by having a look at how it is implemented in GNUStep.
PS:
thinking more about it, when you write "multiplies the image alpha value by the mask alpha value", there is something that strikes my attention.
Indeed, as far as I know, a mask on iOS has no alpha channel and all my attempts to use alpha-channeled masks have given wrong results. Could this allow you to move further a bit?
Maybe the trick is multiplying the image by the mask (which is defined in the gray color space, and has got just one component), and leave the image alpha channel unchanged...
how does that sounds to you?
I am trying to add a CCLabelTTF to my Cocos2d project and have the text be an inverted version of the graphics behind it.
I am having a hard time figuring out what blend fund to use.
I have to admit I do not really understand the concepts behind this, so I am basically just trying different modes.
I have tried several types:
This one inverts the background of the text, but leaves the text white:
[fontLabel setBlendFunc:(ccBlendFunc){GL_ONE_MINUS_DST_COLOR, GL_SRC_ALPHA}];
Can you help me in the right direction?
I want the text to be inverted, and the background to be invisible.
You can visually experiment with the various blendfunc methods with the aptly named Visual glBlendFunc tool.
You should also be aware that CCLabelTTF uses 8-Bit (alpha mask, kCCTexture2DPixelFormat_A8) textures on 1st and 2nd generation devices, and 16-Bit (alpha+intensity mask, kCCTexture2DPixelFormat_AI88) textures on 3rd generation and newer devices. This may or may not affect the blend mode results, or even make it impossible because the textures don't contain color information, only alpha.
It can not be done using glBlendFunc. Blending equation looks like this:
result = A * front_color OP B * back_color;
OpenGL allows you to configure A, B - glBlendFunc(A, B);
and OP (operation) - glBlendEquation(OP);
To invert colors, you need
result = 1 - back_color;
You can do that by setting A = 1, B = 1, OP = FUNC_SUBTRACT, but you will have to set front_color to (1,1,1,1) in fragment shader.
P.S. I might be wrong, so write a comment below and I will change my answer.
I have made a quad curve path using the method CGPathAddQuadCurveToPoint. I got the path perfectly. But, I want to know all the coordinate points which are participated in the path.
Is there a way to retrieve all the coordinate points in a path?
If not do u have any other solution for retrieving all the points in a curve mathematically.
Thanks in advance,
Vamshi
You can do this using the wykobi C++ library routine for cubic bezier curves. Wykobi's library supports quadratic Bezier curves also.
Of course as someone pointed out you don't want all the points (although not impossible, it would just take infinite time :). Wykobi makes it easy to get a certain number of points -- if your start, c1, c2, and end points (where c1, c2 are the control points) are exactly the same as the ones given to CGContextAddCurveToPoint then the points will lie perfectly on the line drawn by core graphics -- so you can do things like draw a pattern at several points on the path.
See: http://www.codeproject.com/Articles/22568/Computational-Geometry-C-and-Wykobi
Also, after I started using wykobi I heard that there is a similar, maybe even better library that is part of Boost, but have not checked it out yet.
I created a C++ Class WPoint as a bridge between wykobi points and CGPoints (C++ fun!). Here's some code (without WPoint, but you can imagine that it is exactly the same layout as a CGPoint so if you do the right cast you can convert easily.
NSMutableArray* result = [[NSMutableArray alloc] init];
wykobi::cubic_bezier<CGFloat,2> bezier;
bezier[0] = (WPoint)p1; // start point, in CG we did a CGMoveToPoint
bezier[1] = (WPoint)b1i; // control 1
bezier[2] = (WPoint)b2i; // control 2
bezier[3] = (WPoint)p2; // end point
std::vector<WPoint> point_list;
int numPoints = p2.dist(p3) * pointDensity;
// *** here's the magic ***
wykobi::generate_bezier(bezier,std::back_inserter(point_list), numPoints);
for (int i=0; i<numPoints; i++) {
CGPoint p = (CGPoint)(point_list[i]);
[result addObject:[NSValue valueWithCGPoint:p]];
}
// result has your points!
Here's a link to the Boost geometry library:
http://www.boost.org/doc/libs/1_47_0/libs/geometry/doc/html/geometry/introduction.html
Use CGContextSetLineDash
The purpose of this function is to create a dashed line, but You can use it to get smaller segments.
starting point of each segment can be treated as points.
CGSize bbSize = CGPathGetBoundingBox(path).size;
UIGraphicsBeginImageContext(bbSize);
CGContextRef ctx = UIGraphicsGetCurrentContext();
CGContextSetLineWidth(ctx, 1.0);
CGContextAddPath(ctx, path);
CGContextSetLineDash(ctx, phase, lengths, count);
CGContextReplacePathWithStrokedPath(ctx);
result = CGContextCopyPath(ctx);
UIGraphicsEndImageContext();
If you want to work on the moveto, lineto, and curveto elements of the path, use CGPathApply. You pass this a pointer to a function in your program, and it calls that function once per element of the path.
Unfortunately, there's no way to just ask for each element like there is with AppKit's NSBezierPath. The function is the only way.
If you want to determine every pixel intersected by the path, too bad—that's not practical, and I can't even think of why you'd want that information. Some contexts, such as PDF contexts, don't even have pixels; in those cases, any question involving pixels is a non sequitur.
A quadratic curve is just that -- a curve. It's impossible to get a list of all of the points on it because there are infinitely many points, and it's not a simple line segment.
See Getting Information about Quartz Paths for a list of the functions you can use to query a CGPath object. Unfortunately, it seems like the most useful information you're going to get is with CGPathContainsPoint(), which only tells you if a given point is contained within the area of the path.
If not do u have any other solution for retrieving all the points in a curve mathematically.
What do you need them for, i.e. what problem are you trying to solve? If it is to intersect two curves, you can do this mathematically. Just set the two curve equations equal to each other and solve for the unknown.
I guess you're after something equivalent to the Java2D FlatteningPathIterator class. For example Java2D's path.getPathIterator(null, 1.0) returns an iterator of only 'lineTo' segments even if the original path had curveTo and quadTo, the double argument controls the 'flatness', giving you an easy way to calculate any point on the curve.
I'm searching for the same thing in Cocoa, but have found nothing. If you find a solution please let me know.
There are curve implmentations around (e.g. http://sourceforge.net/projects/curves/) that could be ported, but there's always a risk that if you don't use the same algorithm as Cocoa then there could be errors between your interpolation and the stroked NSBezierPath/CGPath.