How to linearly interpolate animation curve in unity 3D? - unity3d

After researching for hours and hours I am just not able to linearly interpolate two animation curve,I need something like Mathf.lerp for animation curve,I had try with inTangent and OutTangent with keyframes of the animation curve but it does not store the values,am I missing something ?
Need guidance,this would help me a lot in my project,thanks.

Seems to me you are looking to "morph" one animation curve into another. This imho depends on your use case.
Assuming both curves are defined between [0.0, 1.0], I'd calculate the curve value for t in both, and interpolate linearly both results over the same time.
/*
* Evaluates two AnimationCurve objects, and interpolates linearly
* between the results, effectively "morphing" from curve A to curve
* B in a linear fashion.
* Both curves must be defined between 0.0 and 1.0, and t must be
* within the same 0.0-1.0 range too.
*/
public float EvaluateLerpTwoCurves(AnimationCurve a, AnimationCurve b, float t) {
float valueA = a.Evaluate (t);
float valueB = b.Evaluate (t);
float result = Mathf.Lerp(valueA, valueB, t);
return result;
}
You may wish to interpolate between both AnimationCurve using a different approach (not linearly like Mathf.Lerp does), you could even use a third AnimationCurve to define how to morph between both curves.
Note that the suggested code above requires both curves to be defined between 0.0 and 1.0, and t must be within the same 0.0-1.0 range too.

Related

Do two floats in a compute shader being added or subtracted not give the same value 100% of the time?

I have a function I call to generate some randomness in my hlsl compute shader code
float rand3dTo1d(float3 value, float3 dotDir = float3(12.9898, 78.233, 37.719)){
//make value smaller to avoid artefacts
float3 smallValue = sin(value);
//get scalar value from 3d vector
float random = dot(smallValue, dotDir);
//make value more random by making it bigger and then taking the factional part
random = frac(sin(random) * 43758.5453);
return random;
}
If I pass in an incoming vectors location, all is fine, but if I try to pass in the center point of three vectors using this function into the randomness:
float3 GetTriangleCenter3d(float3 a, float3 b, float3 c) {
return (a + b + c) / 3.0;
}
Then ocassionally SOME of my points are not the same from frame to frame (shown by the color I paint the triangles with using this code). I get flickering of color.
float3 color = lerp(_ColorFrom, _ColorTo, rand1d);
I am at a total loss. I was able to at least get consitant results by using the thread id as the seed for the randomness, but not being able to use the centerpoint of the triangle is really weird to me and I have no idea what I am doing wrong or what I am missing. Any help would be great.

HLSL: Unitys Vector3.RotateTowards(...)

I need to rotate a direction vector towards another with a maximum angle in a compute shader, just like the Vector3.RotateTowards(from, to, maxAngle, 0) function does. This needs to happen inside the compute shader, since I cannot send the needed values from and to the GPU for performance reasons. Any suggestions on how to implement this?
This is adapted from a combination of this post on the Unity forums by vc1001 and this shadertoy entry by demofox. I haven't tested this and it has been a while since I've done HLSL/cg coding, sop lease let me know if there are bugs--especially syntax errors.
float3 slerp(float3 current, float3 target, float maxAngle)
{
// Dot product - the cosine of the angle between 2 vectors.
float dot = dot(current, target);
// Clamp it to be in the range of Acos()
// This may be unnecessary, but floating point
// precision can be a fickle mistress.
dot = clamp(dot, -1, 1);
// Acos(dot) returns the angle between start and end,
// And multiplying that by percent returns the angle between
// start and the final result.
float delta = acos(dot);
float theta = min(1.0f, maxAngle / delta);
float3 relativeVec = normalize(target - current*dot); // Orthonormal basis
float3 slerped = ((start*cos(theta)) + (relativeVec*sin(theta)));
}

Cheapest way to find Vector magnitude from a given point and angle

I am trying to determine a players depth position on a plane, which defines the walkable ground in a 2D brawler game. The problem is depictured in the following drawing:
C represents the players current position. I need to find the magnitude of vector V. Since I am not strong on linear algebra, the one thing I can think of is: determining the intersection point P of L1 and L2, and then take the magnitude from AP. However, I get the feeling there must be an easier way to find V, since I already know the angle the vector should have, given by vector from AB.
Any input would be appreciated, since I am looking forward to step up my linear algebra game.
Edit: As it is unclear thanks to my lack of drawing skills: the geometry depicted above is a parallelogram. The vector V I am looking for is parallel to the left and right side of the parallelogram. Depth does not mean, that I am looking for the vector perpendicular to the top side, but it refers to the fake depth of a purely 2D game. The parallelogram is therefore used as a means for creating the feeling of walking along a z axis.
The depth of your player (length of V) as measured from the top line in your drawing, is just the difference between A.y and C.y. This is seperate from the slant in the parralelogram, as we're just looking at depth.
example:
float v;
Vector2 a = new Vector2(100, 100); //The point you're measuring from
Vector2 c = new Vector2(150, 150); //Your character position
v = c.y - a.y; // This is the length of V.
//In numbers: 50 = 150 - 100
Illustrated: image not to scale
This works for any coördinate in your plane.
Now if you'd want to get the length of AC is when you'd need to apply some pythagoras, which is a² + b² = c². In the example that would mean in code:
Vector2 a = new Vector2(100, 100);
Vector2 c = new Vector2(150, 150);
float ac1 = Mathf.Sqrt(Mathf.Pow(c.x - a.x, 2) + Mathf.Pow(c.y - a.y, 2));
Now that is quite a sore to have to type out every time, and looks quite scary. But Unity has you covered! There is a Vector method called Distance
float ac2 = Vector2.Distance(a, c);
Which both return 70.71068 which is the length of AC.
This works because for any point c in your area you can draw a right angled triangle from a to c.
Edit as per comment:
If you want your "depth" vector to be parallel with the sides of the paralellogram we can just create a triangle in the parallelogram of which we calculate the hypotenuse.
Since we want the new hypotenuse of our triangle to be parallel to the parallelogram we can use the same angle θ as point B has in your drawing (indicated by pink in mine), of which I understood you know the value.
We also know the length of the adjacent (indicated in blue) side of this new triangle, as that is the height we calculated earlier (c.y - a.y).
Using these two values we can use cosine to find the length of hypotenuse (indicated in red) of the triangle, which is equal to the vector V, in parallel with the parallelogram.
the formula for that is: hypotenuse = adjacent/cos(θ)
Now if we were to put some numbers in this, and for my example I took 55 for the angle θ. It would look like this
float v = 50/(cos(55));
image not to scale
Let's call the lower right vertex of the parallelogram D.
If the long sides of the parallelogram are horizontal, you can find magnitude of V vector by:
V.magnitude = (c.y - a.y) / sin(BAD)
Or if you prefer:
V.magnitude = AB.magnitude * (c.y - a.y)/(b.y - a.y)

Moving object continuously using translate

I want to move an object from A to B continuously,like 1st A to B then B to A then again A to B and so on,thanks in advance. I've tried this.
float speed X = 1; float speed Y = 0; float speed Z = 0;
// Use this for initialization
void Start () {
}
// Update is called once per frame
void Update () {
transform.Translate (new Vector 3 (speed X, speed Y, speed Z) * Time . delta time );
}
You should need to use Vector3.Lerp:
Linearly interpolates between two vectors.
Interpolates between the vectors a and b by the interpolant t. The
parameter t is clamped to the range [0, 1]. This is most commonly used
to find a point some fraction of the way along a line between two
endpoints (e.g. to move an object gradually between those points.(read this)
While for your actual answer here is code you can see:
http://answers.unity3d.com/questions/14279/make-an-object-move-from-point-a-to-point-b-then-b.html
http://answers.unity3d.com/questions/905966/moving-an-object-constantly-between-two-points-wit.html
How to move an object between two points in Unity?
http://answers.unity3d.com/questions/690884/how-to-move-an-object-along-x-axis-between-two-poi.html

OpenGL ES 2. Shader with parameters of 3 neighbor points which generates 2 points?

I have an array of polyline's points (x, y). Each time I shoud process 3 neighbour points and generate 2 output points.
For example, something like this:
void someFunc(float x0, float y0, float x1, float y1, float x2, float y2, float *pXout1, float *pYout1, float *pXout2, float *pYout2)
{
*xout1 = x1 - 1;
*xout2 = x1 + 1;
*yout1 = MIN(y0, y2);
*yout2 = MAX(y0, y2);
}
So I have 2 problems which I should implement in my vertex shader:
1)How to input several neighbor points at once?
2)How to output several points at once?
1)How to input several neighbor points at once?
Vertex shader has no ability to input neighbouring vertex data. If you really need this, then you can implement this through redundant vertex attributes by supplying neighbouring vertex data (the same way you would supply colour, normals, etc.).
2)How to output several points at once?
Do you mean passing multiple vertex data to fragment shader? If so then the same rule applies as for question #1, only you should use redundant varyings.