Questions in topic: "tangent"
http://answers.unity.com/questions/topics/single/74.html
The latest questions for the topic "tangent"Finding a tangent vector from a given point and circle
http://answers.unity.com/questions/1617078/finding-a-tangent-vector-from-a-given-point-and-ci.html
Hi all
Im working on some AI navigation, and I would like the vehicle to aim up for the tangent *b* (reference picture at the bottom). I got most of the stuff figured out at the moment (thanks to [this][1] great answer), but I'm still struggling with the maths in C#.
I have the point B, the direction of vector w, the length shouldn't matter.
I also have point A, and the radius of the circle. What I'm missing is the point E, which is the intersecting point between the tangent of the circle, and the circle itself, and the direction of vector *b*.
Approximations are also alright, if they grant a performance boost - I'm not looking for accuracy, just a decent behaving ai :)
All help is appreciated!
![this][2]
[1]: https://gamedev.stackexchange.com/questions/162030/how-can-i-maneuver-an-ai-pirate-ship-for-a-sea-battle
[2]: /storage/temp/135494-tangent.pngmathvectortangentdirectionstangentsFri, 29 Mar 2019 00:34:45 GMTasger1231tangent space viewDir in a SurfaceShader
http://answers.unity.com/questions/1523286/tangent-space-viewdir-in-a-surfaceshader.html
There is probably not much more I can say about that. I'm trying to get the tangent space view direction in a surface shader (in the surf() function) but I can't seem to figure out how, so after searching for a solution for quite some time I'm coming back here to ask you guys.
------
The last thing I got (after a few other iterations that didn't work) is this:
struct Input {
// [...]
float3 worldTangentDir : TEXCOORD0;
};
void vert (inout appdata_full v, out Input o) {
// [...]
TANGENT_SPACE_ROTATION; // create rotation matrix
o.worldTangentDir = mul( rotation, ObjSpaceViewDir( v.vertex ) );
}
void surf (Input IN, inout SurfaceOutputStandard o) {
// [...]
o.Albedo = abs(IN.worldTangentDir);
}
What I expect would be to see different colors on my mesh representing the tangent view direction *(kind of like when you map the normals to the output Albedo so each pixel gets colored accordingly)*. But what I get is just black. Any ideas on this one?
---
Thanks in advance!shadersurface shaderhlsltangentviewdirThu, 28 Jun 2018 19:41:39 GMTDrayI Don't Need Tangent Space Normals
http://answers.unity.com/questions/1517769/i-dont-need-tangent-space-normals.html
Unity Standard Surface Shader uses "Tangent Space Normals" but I'm creating a custom bump solution that doesn't need tangents and I want to remove the configuration from the Standard Shader if possible.
My bump solution uses custom bump maps and works perfectly when applied in an Unlit Shader using "Normal Space Normals".
Is there a way to simply get the Surface Shader to use "Normal Space Normals" without losing the other features like Tessellation?normalsnormalmapsurface shadertangentWed, 13 Jun 2018 15:30:55 GMTWowmaniacHow to make One More Line game?
http://answers.unity.com/questions/1475615/how-to-make-one-more-line.html
How to hook the player to the Anchor point and release the ball according to the direction it is rotating?rotationvelocitydirectionjointstangentFri, 02 Mar 2018 11:24:13 GMTabishekjreubenNormal map issue in Calculating Tangent Space
http://answers.unity.com/questions/1370326/normal-map-issue-in-calculating-tangent-space.html
Hello evryone,
I am having a problem regarding the calculation of the tangents. Am using 3Dsmax and Substance painter 2 for the workflow. Inside SP2 the model look fine with all the maps , but in Unity i get these strange artifacts on the Normal map after calculating the tangents . I found out tha my normal map has a small degree of color inclination. Normal color(127,127,255 ) with inclinations of (126, 127,255) in some areas, producing these effects on my model.
What could i do to fix it?
Thank you![alt text][1]
[1]: /storage/temp/96616-issues.jpgtexturenormalsbakingshadingtangentMon, 26 Jun 2017 11:18:10 GMTPolls47Detect a collision point, but allow pass through collider.
http://answers.unity.com/questions/1313739/detect-a-collision-point-but-allow-pass-through-co.html
I have a Unity project in which there is a 2D game world that consists of static circle colliders. The player is dynamic. I need collision point to check whether player's trajectory(path from which he came from) tangent to circle collider or not, so I can't use Trigger. But I also need to allow player pass through collider.
How can I solve the problem?2dcollisioncollidertriggertangentFri, 17 Feb 2017 09:14:07 GMTiamdenayHow can I detect a collision point, but allow player to pass through collider.
http://answers.unity.com/questions/1314110/how-can-i-detect-a-collision-point-but-allow-playe.html
I have a Unity project in which there is a 2D game world that consists of static circle colliders. The player is dynamic. ***I need collision point*** to check whether player's trajectory(path from which he came from) ***tangent*** to circle collider or not, so I can't use Trigger. But I also need to allow player ***pass through collider***. I cannot use Trigger, because I need collision point. How can I solve the problem? Thanks!2dcollisioncollidertriggertangentThu, 16 Feb 2017 13:44:36 GMTiamdenayTangent Space Normal Map to World Space Normal Vector in C# Script
http://answers.unity.com/questions/1308536/tangent-space-normal-map-to-world-space-normal-vec.html
I am writing a path tracer in C# using Physics.Raycast, and I want to implement normal maps. When an object is hit by a raycast, I can correctly sample its normal map at the proper uv, then I convert the rgb pixel into an xyz vector. Once I have this normal vector, which is measured in the object's tangent space, I need to transform it into world space. I know this involves finding the tangent and binormal vectors to the surface, but I am unsure how to do so. Is there any consistent way of determining these vectors so that the results will match a normal-mapped shader?raycastingvectornormalmaptangentSat, 04 Feb 2017 09:50:16 GMTtaylorulesFinding distance of player to a path in 3D
http://answers.unity.com/questions/1201367/finding-distance-of-player-to-a-path-in-3d.html
I have a path in 3D (sort of a travel in space situation - Go from planet A to planet B). At the beginning of the game I show the path to be followed by the player using a thin line. The player is supposed to stay as close to the line as possible. If they move more than a certain distance x from the path, the player should automatically start moving towards the center.
For example - if the path is circular, and the player moves away from the circle, I want to calculate how far the player is from the path, then find the normal direction of the player to the circle, and push the player along that vector.
How do I find the distance of the player from the path? Keep in mind I need the shortest distance. I can't really find the distance of the player to each point on the path and then use the shortest one; as it will be very expensive. Is there a way to do this? Also, how do I find the direction vector?
Thank youdistancenormalspathfollowingtangentSun, 12 Jun 2016 07:02:24 GMTshreypareek1991How to get tangential velocity of revolving object?
http://answers.unity.com/questions/1167692/how-to-get-tangential-velocity-of-revolving-object.html
So I have this empty game object which is rotating. Now this has a sphere object as its child. So when the parent point object rotates, the child sphere appears to be revolving around it. I want to know the velocity of that sphere, specifically, its tangential velocity.
When I debug, I find all the components of the sphere rigid body velocity/angular velocity to be (0,0,0). Can anyone please tell me how can I attain this..rotationvelocitysphereangularvelocitytangentFri, 08 Apr 2016 16:46:04 GMTjajan_psContinuing rotation of circle from new point on screen by touch
http://answers.unity.com/questions/1155410/continuing-rotation-of-circle-from-new-point-on-sc.html
Hello,I have circle in my game that i use to rotate while draging my finger on the screen.When i take finger off the screen,and then drag from another place,the circle should continue rotating from previous rotation instead of instantly changing rotation.It kinda works but my problem is that sometimes - I can't tell when that happens exactly,the circle jumps to new rotation instead of continuation.Can someone tell me where i have error in my script?
Here's the exact part of the script :
if (Input.touchCount > 0)
{
Vector3 pos = Camera.main.ScreenToWorldPoint(Input.GetTouch(0).position);
Vector2 touchPos = new Vector2(pos.x, pos.y);
if (Input.GetTouch(0).phase == TouchPhase.Began)
{
startAngle = Mathf.Atan2(touchPos.y,touchPos.x) * Mathf.Rad2Deg;
if (startAngle <= 0)
{
startAngle += 360;
}
}
angle = Mathf.Atan2(touchPos.y, touchPos.x) * Mathf.Rad2Deg;
if(angle < 0)
angle += 360;
distanceAngle = startAngle - endAngle;
angle -= distanceAngle;
if (angle < 0)
angle += 360;
else if (angle > 360)
angle -= 360;
if (Input.GetTouch(0).phase == TouchPhase.Ended)
{
endAngle = angle;
}
circle.rotation = Quaternion.Euler(0f, 0f,angle);rotationtouchanglecircletangentTue, 15 Mar 2016 16:04:40 GMTBartol0ll0Asset Bundle and Mesh Missing Tangents
http://answers.unity.com/questions/1103799/asset-bundle-and-mesh-missing-tangents.html
I just tracked down a problem in a game we are making, that turned out to be caused by a mesh loaded from an asset bundle not having any tangents.
The symptom was that, on some devices (Nexus 6 and iPad Mini 1st gen, as examples), a mesh was not rendering properly - the lighting was wrong, and the mesh just looked bad. Upon some investigation, we discovered that the problem would go away if we did not load the mesh from an asset bundle, and had the mesh built directly into the build. This, however, was not a valid solution, as the build was already close to the 100MB IPA limit. Upon further investigation, I discovered that the mesh did not have any tangents loaded, but only when built into the asset bundle. The lack of tangents was visible in editor (which did not have the rendering problem), as well as on the problematic devices. I confirmed this both by checking the Mesh.tangents array size, as well as hacking the shader to output the tangents as the color, whereby they were completely black when loaded from an asset bundle, but seemed reasonable when loaded straight from the main package. The fbx in both cases is identical, as well as the import settings, where Tangents is set to Calculate.
I was able to work around the problem by adding code to calculate tangents on the meshes loaded from asset bundles, however this is less than ideal as it requires the meshes to be set to "Read/Write Enabled", as well as just requiring extra load time.
The question then is: Is this a known problem in Unity 4.6? Is there something wrong with the way we are building or loading the asset bundles. Is there a better way to work around the problem?
For building the asset bundle, we are using BuildPipeline.BuildAssetBundle with a potentially different asset as the main asset (this bundle has a few meshes in it, two of which have the problem), and the mesh's FBX is in the assets list. We are using the options: CollectDependencies | DeterministicAssetBundle. I have also tried adding CompleteAssets to that list, however it had no effect. The download of the asset bundle is occurring with WWW.LoadFromCacheOrDownload, and the load then uses AssetBundle.LoadAll().
We have are using Unity 4.6.8f1 and 4.6.7f1 for Android, and 4.6.8p3 (the version distributed on the forums) for iOS.assetbundleunity 4.6unity 4tangenttangentsFri, 27 Nov 2015 11:04:29 GMTChris KaynorExtending tangent length
http://answers.unity.com/questions/1075638/extending-tangent-length.html
Is there a way to extend a curve's tangent line? Currently unity has it fixed at this weird length that you can't seem to change.
![weird tangent length][1]
[1]: /storage/temp/55448-screen-shot-2015-10-02-at-195745.pnganimatorcurvestangentSat, 03 Oct 2015 01:58:36 GMTi_am_qixNormal mapping mirrored UVs?
http://answers.unity.com/questions/1007165/normal-mapping-mirrored-uvs.html
I have an object that has been mirrored in Blender with the mirror modifier. UV's have been correctly mirrored as normal, but *applying a normal map to the object in Unity causes only one half of the object to be bump mapped*. So after doing some research on the topic I noticed it had something to do incorrect tangents. However, after disabling Unity's tangent importer, I was left with an *unshaded objects*. (No tangents had been imported)
I thought the problem was that .blend files didn't contain tangents, so I exported an FBX with "tangent space" checked. And sure enough, there was bump mapping on both halves of the object. But something is still not right, there is an *obvious seam and the bump mapping on one half looks opposite to the other half*:
![alt text][1]
On the left is the original .blend (with only half bump mapped) and the right is the .fbx with imported tangents (noticeable seam with inverted bump maps). What is going on here?
Side note: previously when I ran into this problem I would just *apply the mirror and recalculate UV's*. This worked when I had **tiled textures** (as I could just scale up UV mapping to compensate for the extra UV coordinates) but now I'm using a **non-tileable** texture, which means I *can't just dump the current UVs*! The mapping is now dependent on the texture being correct in mirrored mode. Thanks in advance!
[1]: http://i58.tinypic.com/35ar8k7.pngshaderimportnormalmapbumpmaptangentTue, 14 Jul 2015 22:08:31 GMTSomeGuy22Scripting Curves, How To Maintain The Shape ?
http://answers.unity.com/questions/949859/scripting-curves-how-to-maintain-the-shape.html
Hello guys, I have a little problem. I am trying to learn how to script AnimationCurves, in order to change my Color Correction Curves Image Effect in runtime. I've figured out the most of it, but still I have a minor glitch to solve. First, here is the default value and the desired value of my red channel :
![alt text][1]
So I wrote this script:
using UnityEngine;
using System.Collections;
using UnityStandardAssets.ImageEffects;
public class IS_ActionImageEffects : MonoBehaviour {
public Transform t_EffectsHolder;
public float f_LerpSpeedRed;
public float f_LerpSpeedRedBack;
public bool b_WaitFirst;
public float f_WaitFirst;
public float f_WaitToLerpBack;
private ColorCorrectionCurves _CorrectionCurves;
private Vector2 v2_OriginalRedKey;
void Awake()
{
_CorrectionCurves = t_EffectsHolder.GetComponent<ColorCorrectionCurves>();
}
void OnEnable()
{
StartCoroutine(LerpRedChannel());
}
IEnumerator LerpRedChannel()
{
if (b_WaitFirst)
yield return new WaitForSeconds(f_WaitFirst);
float i = 0.0f;
float rate = 1.0f / f_LerpSpeedRed;
float f_CurrentTime = _CorrectionCurves.redChannel.keys[1].time;
float f_CurrentValue = _CorrectionCurves.redChannel.keys[1].value;
v2_OriginalRedKey.x = f_CurrentTime;
v2_OriginalRedKey.y = f_CurrentValue;
float f_KeyTime;
float f_KeyValue;
while (i < 1.0f)
{
i += Time.deltaTime * rate;
f_KeyTime = Mathf.Lerp(f_CurrentTime, 0.6f, i);
f_KeyValue = Mathf.Lerp(f_CurrentValue, 0.3f, i);
_CorrectionCurves.redChannel.MoveKey(1, new Keyframe(f_KeyTime, f_KeyValue));
_CorrectionCurves.redChannel.SmoothTangents(1, 1);
_CorrectionCurves.UpdateParameters();
yield return null;
}
yield return new WaitForSeconds(f_WaitToLerpBack);
i = 0.0f;
float rate2 = 1.0f / f_LerpSpeedRedBack;
float f_CurrentTime2 = _CorrectionCurves.redChannel.keys[1].time;
float f_CurrentValue2 = _CorrectionCurves.redChannel.keys[1].value;
while (i < 1.0f)
{
i += Time.deltaTime * rate2;
f_KeyTime = Mathf.Lerp(f_CurrentTime2, v2_OriginalRedKey.x, i);
f_KeyValue = Mathf.Lerp(f_CurrentValue2, v2_OriginalRedKey.y, i);
_CorrectionCurves.redChannel.MoveKey(1, new Keyframe(f_KeyTime, f_KeyValue));
_CorrectionCurves.redChannel.SmoothTangents(1, 1);
_CorrectionCurves.UpdateParameters();
yield return null;
}
}
}
Which lerps 2 values in order to make the change smooth, waits, and lerps those values back. But the problem is, curves get, lets say, broken. I can't maintain the shape, here is the visual of the curve after the first while loop and after the second one :
![alt text][2]
It seems like something to do with my tries to maintain the things by using SmoothTangent, but I've tried:
SmoothTangent(1,0);
SmoothTangent(1,0.5f);
SmoothTangent(1,1);
I've got slightly different results but none of them helped me solve the issue. So, what am I missing? Thanks in advance :)
[1]: /storage/temp/44763-attach1.png
[2]: /storage/temp/44764-attach2.pngsmoothkeysframestangentSat, 18 Apr 2015 19:00:59 GMTInan-EvinCalculating an angle to rotate some tangent around an axis
http://answers.unity.com/questions/934403/calculating-an-angle-to-rotate-some-tangent-around.html
I don't even know how to Google my question to find an answer so I drew a picture, below. I have a weapon who's Field of Fire I'm trying to rotate so a target comes into view.
I've previously been calculating Theta (below) from my weapon's exit aperture and rotating my parent GameObject by that angle, Theta.
![alt text][1]
After drawing it out, I now understand that's not enough since my exit aperture is on the outside radius of (what is effectively) a circle and I always end up with some "error" Theta' (prime).
**I would love a math wiz to help me calculate the angle my circle needs to rotate in order for a target to come into my field of view.** My domain is 2D - everything is on the same y-plane. The center of the circle, target (x,y), radius, exit aperture (x,y) and min/max directions are all known.
[1]: /storage/temp/43476-tangent-around-axis.jpgtangentSat, 28 Mar 2015 01:54:38 GMTsaporterHow can I set my Animation Curve's Tangents to Flat in script?
http://answers.unity.com/questions/870283/how-can-i-set-my-animation-curves-tangents-to-flat.html
I have an animation that I've generated through script with three keyframes. The first is just the resting position, the second moves it down along the y-axis some ways, then the third frame moves it back to the resting position.
In my code, when I create the middle keyframe I am using AnimationCurve.AddKey() which by default will smooth the animation tangents for you. This makes the tangents look like so.
![alt text][1]
The problem with this is that the animation of my y-axis curves underneath my destination which doesn't seem like a lot here but when the animation changes in scale it can dip well underneath half my requested position. What I would like for the tangents to look like is this.
![alt text][2]
I know I can just go into the Animation Window and right click the key frame and sort this all out, but that isn't the question here so pretty please don't recommend that! Thanks for taking the time to read this and I hope you can help me!
Edit : Here is an image where this is REALLY getting out of hand and Unity just takes the flag and runs with it lol.
![alt text][3]
[1]: /storage/temp/38320-notsoflat.png
[2]: /storage/temp/38321-flattangents.png
[3]: http://i.imgur.com/2H8xOVo.pnganimationanimationcurvetangentflatTue, 06 Jan 2015 18:11:40 GMTdepressGenerate cylindrical mesh around 3D Catmull-rom spline
http://answers.unity.com/questions/867023/generate-cylindrical-mesh-around-3d-catmull-rom-sp.html
The spline is curve is defined by 4 points: p0,p1,p2,p3 and drawn between p1 and p2 with a for loop, looping through the number of segments I want. The point on the curve at time t can is being found with this function:
public Vector3 GetSpline(Vector3 p0, Vector3 p1, Vector3 p2, Vector3 p3, float t) {
Vector3 ret = new Vector3();
float t2 = t * t;
float t3 = t * t * t;
ret.x = 0.5f * ((2.0f * p1.x) +
(-p0.x + p2.x) * t +
(2.0f * p0.x - 5.0f * p1.x + 4 * p2.x - p3.x) * t2 +
(-p0.x + 3.0f * p1.x - 3.0f * p2.x + p3.x) * t3);
ret.y = 0.5f * ((2.0f * p1.y) +
(-p0.y + p2.y) * t +
(2.0f * p0.y - 5.0f * p1.y + 4 * p2.y - p3.y) * t2 +
(-p0.y + 3.0f * p1.y - 3.0f * p2.y + p3.y) * t3);
ret.z = 0.5f * ((2.0f * p1.z) +
(-p0.z + p2.z) * t +
(2.0f * p0.z - 5.0f * p1.z + 4 * p2.z - p3.z) * t2 +
(-p0.z + 3.0f * p1.z - 3.0f * p2.z + p3.z) * t3);
return ret;
}
I think I can figure out all the mesh generation stuff but I just can't figure out how to get a "normal" vector from the "direction vector" (segmentEnd-segmentStart). I feel like this is stupidly simple.. but my 3d maths isn't that great.
Any idea?
Thanks!vectornormalsplinetangentFri, 02 Jan 2015 07:24:52 GMTDMevilleConverting vortex effect from legacy to Shuriken
http://answers.unity.com/questions/864157/converting-vortex-effect-from-legacy-to-shuriken.html
I'm trying to convert a legacy particle effect to Shuriken. The effect currently swirls particles inwards from a MeshEmitter using tangent velocity, negative normal velocity and tweaking emitter/animator settings with scripting to give it style.
I did some research and the answers I found implied that it couldn't be done easily, or perhaps at all. My experiments were not encouraging. However many of the results I found were years old and implied that the problems were just a beta issue. I haven't found any evidence of significant changes to Shuriken since (I'm currently on 4.5.5).
Is there currently a way to specify tangent velocity and negative normal velocities, or perhaps some other way to achieve the same effect? If not, should I just stick with legacy?
Thanks,
JTparticlesshurikentangentMon, 29 Dec 2014 01:18:47 GMTjefferytitanTrying to set Mesh.tangents = Vector4[] gives error of "An object reference is required to access non-static member `Create_Mesh.mesh'"
http://answers.unity.com/questions/858689/trying-to-set-meshtangents-vector4-gives-error-of.html
hello everyone, so im following [this][1] tutorial on creating a deformable mesh, and aproximately 36 mitutes in the guy creates a function to calculate the tangents, everything seems to work fine for him but i get this error that says "An object reference is required to access non-static member `Create_Mesh.mesh'" clueless of what did I do wrong i thought you guys might have an idea on what it could be. this is the part where the problem seems to be:
private static void RecalculateTangents (Mesh objMesh)
{
int [] triangles = objMesh.triangles;
Vector3 [] vertices = objMesh.vertices;
Vector2 [] uv = objMesh.uv;
Vector3 [] normals = objMesh.normals;
int triangleCount = triangles.Length;
int vertexCount = vertices.Length;
Vector3 [] tan1 = new Vector3[vertexCount];
Vector3 [] tan2 = new Vector3[vertexCount];
Vector4 [] tangents = new Vector4[vertexCount];
for (long a = 0 ; a < triangleCount ; a++)
{
long i1 = triangles[a + 0];
long i2 = triangles[a + 1];
long i3 = triangles[a + 2];
Vector3 v1 = vertices[i1];
Vector3 v2 = vertices[i2];
Vector3 v3 = vertices[i3];
Vector2 w1 = uv[i1];
Vector2 w2 = uv[i2];
Vector2 w3 = uv[i3];
float x1 = v2.x - v1.x;
float x2 = v3.x - v1.x;
float y1 = v2.y - v1.y;
float y2 = v3.y - v1.y;
float z1 = v2.z - v1.z;
float z2 = v3.z - v1.z;
float s1 = w2.x - w1.x;
float s2 = w3.x - w1.x;
float t1 = w2.y - w1.y;
float t2 = w3.y - w1.y;
float div = s1 * t2 - s2 * t1;
float r = div == 0.0f ? 0.0f : 1.0f / div;
Vector3 sdir = new Vector3 ((t2 * x1 - t1 * x2) * r, (t2 * y1 + t1 * y2) * r, (t2 * z1 - t1 * z2) * r);
Vector3 tdir = new Vector3 ((s1 * x2 - s2 * x1) * r, (s1 * y2 - s2 * y1) * r, (s1 * z2 - s2 * z1) * r);
tan1[i1] += sdir;
tan1[i2] += sdir;
tan1[i3] += sdir;
tan2[i1] += tdir;
tan2[i2] += tdir;
tan2[i3] += tdir;
}
for (long a = 0; a < vertexCount; a ++)
{
Vector3 n = normals[a];
Vector3 t = tan1[a];
Vector3.OrthoNormalize(ref n, ref t);
tangents[a].x = t.x;
tangents[a].y = t.y;
tangents[a].z = t.z;
tangents[a].w = (Vector3.Dot(Vector3.Cross(n, t), tan2[a]) < 0.0f) ? -1.0f : 1.0f;
}
mesh.tangents = tangents;
}
[1]: https://www.youtube.com/watch?v=hL0TXUY6Vl0&index=66&list=WL
I apreciate your time and thanks a lot in advanced.tangentFri, 19 Dec 2014 23:34:02 GMTthenachotech1113Procedural mesh generation - problem with normals.
http://answers.unity.com/questions/830117/procedural-mesh-generation-problem-with-normals-1.html
I'm trying to make mesh figure generated procedurally but some triangles clearly have broken normals. What is weird, everything is okay during play in editor:
![alt text][1]
But after build game and run it from exe file,has become a strange light on some triangles.
![alt text][2]
On pictures we have exactly same place, but we can see, that some triangles on lava mesh ignore light.
My mesh needs to have normal maps. It of course require tangents so to achieve that, I'm using script avaible at http://www.terathon.com/code/tangent.html and converted to javascript by nootz (http://forum.unity3d.com/threads/how-to-calculate-mesh-tangents.38984/):
/*
Derived from
Lengyel, Eric. “Computing Tangent Space Basis Vectors for an Arbitrary Mesh”. Terathon Software 3D Graphics Library, 2001.
http://www.terathon.com/code/tangent.html
*/
static function TangentSolver(theMesh : Mesh)
{
vertexCount = theMesh.vertexCount;
vertices = theMesh.vertices;
normals = theMesh.normals;
texcoords = theMesh.uv;
triangles = theMesh.triangles;
triangleCount = triangles.length/3;
tangents = new Vector4[vertexCount];
tan1 = new Vector3[vertexCount];
tan2 = new Vector3[vertexCount];
tri = 0;
for ( i = 0; i < (triangleCount); i++)
{
i1 = triangles[tri];
i2 = triangles[tri+1];
i3 = triangles[tri+2];
v1 = vertices[i1];
v2 = vertices[i2];
v3 = vertices[i3];
w1 = texcoords[i1];
w2 = texcoords[i2];
w3 = texcoords[i3];
x1 = v2.x - v1.x;
x2 = v3.x - v1.x;
y1 = v2.y - v1.y;
y2 = v3.y - v1.y;
z1 = v2.z - v1.z;
z2 = v3.z - v1.z;
s1 = w2.x - w1.x;
s2 = w3.x - w1.x;
t1 = w2.y - w1.y;
t2 = w3.y - w1.y;
r = 1.0 / (s1 * t2 - s2 * t1);
sdir = new Vector3((t2 * x1 - t1 * x2) * r, (t2 * y1 - t1 * y2) * r, (t2 * z1 - t1 * z2) * r);
tdir = new Vector3((s1 * x2 - s2 * x1) * r, (s1 * y2 - s2 * y1) * r, (s1 * z2 - s2 * z1) * r);
tan1[i1] += sdir;
tan1[i2] += sdir;
tan1[i3] += sdir;
tan2[i1] += tdir;
tan2[i2] += tdir;
tan2[i3] += tdir;
tri += 3;
}
for (i = 0; i < (vertexCount); i++)
{
n = normals[i];
t = tan1[i];
// Gram-Schmidt orthogonalize
Vector3.OrthoNormalize( n, t );
tangents[i].x = t.x;
tangents[i].y = t.y;
tangents[i].z = t.z;
// Calculate handedness
tangents[i].w = ( Vector3.Dot(Vector3.Cross(n, t), tan2[i]) < 0.0 ) ? -1.0 : 1.0;
}
theMesh.tangents = tangents;
return theMesh;
}
Turning off that script fix the problem with broken normals. So there is my question: where lies the problem in attached script and is there other possibility to calculate mesh tangents?
[1]: /storage/temp/35129-normal+normals.jpg
[2]: /storage/temp/35130-broken+normals.jpglightingnormalsprocedural meshtangentWed, 12 Nov 2014 17:43:10 GMTPolak149What is the length of the Keyframe tangent?
http://answers.unity.com/questions/777792/what-is-the-length-of-the-keyframe-tangent.html
Hi guys
I'm trying to make sense of the AnimationCurve and KeyFrame objects.
I've been able to figure out the in/out tangent angles, but I'm not sure how Unity assigns lengths to the tangents.
I had hoped I could simply take 1/3 of the length of the line from the current Keyframe to the next, but this doesn't seem to be correct:
**AnimationCurve editor window (goes from 0,0 to 1,1 - the aspect ratio is just weird)**
![alt text][1]
**My reconstruction**
![alt text][2]
The co-ordinates and tangent angles are all correct, the problem however is the shape of the curve.
Does anyone know what length Unity assigns to Keyframe tangents? Or perhaps it's some function of the angle?
Thanks,
Ves
[1]: http://i.gyazo.com/4b0c8bcc4dbcd906454552bbe3d038e7.png
[2]: http://i.gyazo.com/9154b0779cde549a94f37df1f2c18023.pnganimationcurvekeyframelengthbeziertangentSun, 24 Aug 2014 22:18:32 GMTVesuvianPrimeHow can I get an accurate bounding rect for an AnimationCurve
http://answers.unity.com/questions/762280/how-can-i-get-an-accurate-bounding-rect-for-an-ani.html
Hi guys
I'm currently using the following to get my curve bounding rect:
public static Rect GetBounds(this AnimationCurve extends)
{
if (extends.length == 0)
return new Rect();
float xMin = extends.keys.First().time;
float xMax = extends.keys.Last().time;
float yMin = extends.keys.First().value;
float yMax = yMin;
foreach (Keyframe frame in extends.keys)
{
if (frame.value < yMin)
yMin = frame.value;
if (frame.value > yMax)
yMax = frame.value;
}
return Rect.MinMaxRect(xMin, yMax, xMax, yMin);
}
The problem is that this method doesn't consider the tangents of each KeyFrame.
I presume Unity already has some hidden method for getting the bounding Rect since it is able to focus curves in the curve editor.
Does anyone know how I can get an accurate bounding Rect for my AnimationCurves?
Thanks,
VesboundsrectanimationcurvetangentSat, 02 Aug 2014 18:35:46 GMTVesuvianPrimeHow do I scale an animation curve by time and value while retaining my tangent values?
http://answers.unity.com/questions/707390/how-do-i-scale-an-animation-curve-by-time-and-valu.html
I have an artist defined animationCurve that is supposed to describe how an object comes to a stop. The first key frame is at 0, 1 and the last is at 1, 0. I need to scale that curve's time by stopTime and its value by maxSpeed.
Here is a test I did:
///////////////////////////
using UnityEngine;
using System.Collections;
public class AnimCurveScaleTest : MonoBehaviour {
public AnimationCurve artistCurve;
public AnimationCurve scaledCurve;
Keyframe[] tempKeys;
float speed = 1000f;
float stopTime = 0.5f;
// Use this for initialization
void Start () {
tempKeys = new Keyframe[artistCurve.length];
for ( int i = 0; i < artistCurve.length; i++ ) {
tempKeys[i] = new Keyframe(
artistCurve[i].time * stopTime,
artistCurve[i].value * speed,
artistCurve[i].inTangent,
artistCurve[i].outTangent
);
}
scaledCurve = new AnimationCurve( tempKeys );
}
}
//////////////////////////////
And this is the result:
![alt text][1]
[1]: https://dl.dropboxusercontent.com/u/36289/xfer/2014/Unity_Answers_AnimationCurve_Tangents.jpgc#animationcurvekeyframetangentTue, 13 May 2014 23:57:59 GMTMatt-Ostgardfind the tangent of a box / circle collider
http://answers.unity.com/questions/707106/find-the-tangent-of-a-box-circle-collider.html
I am trying to find the tangent of a box collier to add an explosion force, which isn't built in to unity 2D
It should find either the ray, or the point where the tangent hits.
Any ideas on how to do that?
Thanks in advance.2d2d-physicsboxcollidertangentTue, 13 May 2014 16:31:48 GMTOrbitGamesWeird Animation clip curves tangents
http://answers.unity.com/questions/670994/weird-animation-clip-curves-tangents.html
When i create an animation curve with custom keyframes in C#, the result curve has strange tangents. Here's an image which rapresents the problem:
![alt text][1]
The final result show be: ![alt text][2]
I want to know how can i solve this problem... Thanks for your replies...
**EDIT:**
This is a piece of code:
currKeyframe = -frameStep; nextKeyframe();
Keyframe[] keysPosX = new Keyframe[totalFrames], keysPosY = new Keyframe[totalFrames], keysPosZ = new Keyframe[totalFrames];
Keyframe[] keysRotX = new Keyframe[totalFrames], keysRotY = new Keyframe[totalFrames], keysRotZ = new Keyframe[totalFrames], keysRotW = new Keyframe[totalFrames];
float frameRate = animation.clip.frameRate;
for(int k = 0; k < totalFrames; k++)
{
keysPosX[k] = new Keyframe(frameStep * k / frameRate, currTransform.localPosition.x);
keysPosY[k] = new Keyframe(frameStep * k / frameRate, currTransform.localPosition.y);
keysPosZ[k] = new Keyframe(frameStep * k / frameRate, currTransform.localPosition.z);
keysRotX[k] = new Keyframe(frameStep * k / frameRate, currTransform.localRotation.x);
keysRotY[k] = new Keyframe(frameStep * k / frameRate, currTransform.localRotation.y);
keysRotZ[k] = new Keyframe(frameStep * k / frameRate, currTransform.localRotation.z);
keysRotW[k] = new Keyframe(frameStep * k / frameRate, currTransform.localRotation.w);
nextKeyframe();
}
AnimationCurve cPosX = new AnimationCurve(keysPosX), cPosY = new AnimationCurve(keysPosY), cPosZ = new AnimationCurve(keysPosZ), cRotX = new AnimationCurve(keysRotX), cRotY = new AnimationCurve(keysRotY), cRotZ = new AnimationCurve(keysRotZ), cRotW = new AnimationCurve(keysRotW);
animation.clip.SetCurve(AnimationUtility.CalculateTransformPath(currTransform, transform), typeof(Transform), "localPosition.x", cPosX);
animation.clip.SetCurve(AnimationUtility.CalculateTransformPath(currTransform, transform), typeof(Transform), "localPosition.y", cPosY);
animation.clip.SetCurve(AnimationUtility.CalculateTransformPath(currTransform, transform), typeof(Transform), "localPosition.z", cPosZ);
animation.clip.SetCurve(AnimationUtility.CalculateTransformPath(currTransform, transform), typeof(Transform), "localRotation.x", cRotX);
animation.clip.SetCurve(AnimationUtility.CalculateTransformPath(currTransform, transform), typeof(Transform), "localRotation.y", cRotY);
animation.clip.SetCurve(AnimationUtility.CalculateTransformPath(currTransform, transform), typeof(Transform), "localRotation.z", cRotZ);
animation.clip.SetCurve(AnimationUtility.CalculateTransformPath(currTransform, transform), typeof(Transform), "localRotation.w", cRotW);
[1]: /storage/temp/24097-image.png
[2]: /storage/temp/24099-image2.pnganimationclipcurvetangentSun, 23 Mar 2014 11:10:05 GMTqweeWorld Space Normal -> Tangent Space Normal is surface shader
http://answers.unity.com/questions/651356/world-space-normal-tangent-space-normal-is-surface.html
I use a shader that calculates a normal vector trough a noise function, but i want to transform that world normal to a tangent normal so it will work properly with deferred lighting and other image effects; I tried passing a TANGENT_SPACE_ROTATION matrix to the surface function but it gave me an error:
> "too many interpolators used"
**Deferred Rendering with World Normals**
![alt text][1]
**Forward Rendering with World Normals and world light direction **
![alt text][2]
Here is my shader:
Shader "LostInMind/Planet/Ground/Bumped Specular" {
Properties {
_Tess ("Tessellation", Range(1,32)) = 4
_MainTex ("Texture", 2D) = "white" {}
_TerrainHeight("Terrain Height",Float) = 128
_TerrainBump("Terrain Bump",Float) = 2
_TerrainBumpSample("Terrain Bump Sample",Float) = 1
_Frequency("Frequency",Float) = 0.01
}
SubShader {
Tags { "RenderType" = "Opaque" }
CGPROGRAM
#pragma surface surf SimpleLambert vertex:vert
#include "ImprovedPerlinNoise3D.cginc"
#pragma glsl
#pragma target 5.0
struct SurfaceOutputCustom {
fixed3 Albedo;
fixed3 Normal;
fixed3 Emission;
half Specular;
fixed Gloss;
fixed Alpha;
fixed3 LightDir;
};
half4 LightingSimpleLambert (SurfaceOutputCustom s, half3 lightDir, half atten) {
half NdotL = dot (s.Normal, s.LightDir);
half4 c;
c.rgb = s.Albedo * _LightColor0.rgb * (NdotL * atten * 2);
c.a = s.Alpha;
return c;
}
half4 LightingSimpleLambert_PrePass (SurfaceOutputCustom s, half4 light) {
half4 c;
c.rgb = s.Albedo + light.rgb;
c.a = s.Alpha;
return c;
}
float _TerrainHeight;
float3 _LightDir;
float _TerrainBump;
float _TerrainBumpSample;
struct Input {
float2 uv_MainTex;
float3 Pos;
float3 Normal;
};
float Noise(float3 P,int o)
{
return 1 - turbulence(P,o);
}
float3 GetNormal(float3 N,float3 P)
{
float e = _TerrainBumpSample;
int o = 4;
float F = Noise(float3(P.x,P.y,P.z),o) * _TerrainBump;
float Fx = Noise(float3(P.x+e,P.y,P.z),o) * _TerrainBump;
float Fy = Noise(float3(P.x,P.y+e,P.z),o) * _TerrainBump;
float Fz = Noise(float3(P.x,P.y,P.z+e),o) * _TerrainBump;
float3 dF = float3((Fx-F)/e, (Fy-F)/e, (Fz-F)/e);
return normalize(N-dF);
}
void vert (inout appdata_full v,out Input o)
{
UNITY_INITIALIZE_OUTPUT(Input,o);
TANGENT_SPACE_ROTATION;
o.Normal = mul(_Object2World, normalize(v.vertex));
o.Pos = normalize(mul(_Object2World, v.vertex)).xyz*12720;
}
sampler2D _MainTex;
void surf (Input IN, inout SurfaceOutputCustom o) {
o.LightDir = normalize(_WorldSpaceLightPos0).xyz;
o.Albedo = tex2D (_MainTex, IN.uv_MainTex).rgb;
float3 n = GetNormal(IN.Normal,IN.Pos);
o.Normal = n;
}
ENDCG
}
Fallback "Diffuse"
}
[1]: http://forum.unity3d.com/attachment.php?attachmentid=88701&d=1393288752
[2]: http://forum.unity3d.com/attachment.php?attachmentid=88702&d=1393288754spacenormalstangentdefferedThu, 27 Feb 2014 15:23:50 GMTSimeonHow to convert cubic Bezier curve into AnimationCurve
http://answers.unity.com/questions/623318/how-to-convert-cubic-bezier-curve-into-animationcu.html
I need to transfer animation from 3d party animation software ([spine][1]) in to unity.
Here is how it looks in spine
![alt text][2]
according to [this page][3]
> The Bézier curve array has 4 elements which define the control points: cx1, cy1, cx2, cy2. The X axis is from 0 to 1 and represents the percent of time between the two keyframes. The Y axis is from 0 to 1 and represents the percent of the difference between the keyframe’s values.
here is what I see in input data
[ 0.135, 0.74, 0.918, 0.17 ]
and this is cooridnates for **P1** = (0.135, 0.74) and **P2** = (0.918, 0.17).
for all cases **P0** = (0,0), **P3** = (1,1)
At unity side, i need to provide **outTangent** for P0 and **inTangent** for P3 which is **keyframe[0]** and **keframe[1]** in case of curve has just 2 points.
here is my calculation (which doesn't show correct result):
**i** and **nextI** is a keyframe indexes of **P0** and **P3** in AnimationCurve,
**tangentArray** is json array contains data specified above **[cx1,cy1,cx2,cy2]**
**parseFloat** custom method, i can't use default (float)cx1 or float.parse(cx1)
public static void setCustomTangents(AnimationCurve curve, int i, int nextI, JsonData tangentArray){
float cx1 = parseFloat(tangentArray[0]);
float cy1 = parseFloat(tangentArray[1]);
float cx2 = parseFloat(tangentArray[2]);
float cy2 = parseFloat(tangentArray[3]);
float time = (float)(curve.keys[nextI].time - curve.keys[i].time);
float value = (float)(curve.keys[nextI].value - curve.keys[i].value);
Keyframe thisKeyframe = curve[i];
Keyframe nextKeyframe = curve[nextI];
float outTangent = (cy1 * value)/ (cx1 * time);
float inTangent = ((1 - cy2) * value)/ ((1 - cx2) * time);
thisKeyframe.outTangent = outTangent;
nextKeyframe.inTangent = inTangent;
curve.MoveKey(i, thisKeyframe);
curve.MoveKey(nextI, nextKeyframe);
}
This is result in unity
![alt text][4]
I've already seen some similar bezier questions like [The algorithm of curve in Shuriken Particle System][5]. But 2d day can't figure out how to solve this problem, probably because my math skills is low =).
Thanks.
[1]: http://esotericsoftware.com
[2]: /storage/temp/21030-spinecurve.png
[3]: http://esotericsoftware.com/spine-json-format/
[4]: /storage/temp/21031-unity_curve.png
[5]: http://answers.unity3d.com/questions/438407/the-algorithm-of-curve-in-shuriken-particle-system.htmlanimationcurvebeziertangentThu, 23 Jan 2014 08:03:21 GMTnicloayProblem with tangents / normals on UV seam
http://answers.unity.com/questions/584338/problem-with-tangents-normals-on-uv-seam.html
** Partial solve. Turns out this is a problem with 3ds Max Render to Texture. I used xNormal and it created more accurate normal maps for Unity **
Hey guys, I was wondering if anyone had a solution to this problem when importing models into Unity from 3ds Max.
In the attached image I have a visible line on the model where there is a UV seam, but it is only visible in Unity. I assume this is a problem with the tangent calculation, but exporting tangents from 3ds max looks far worse when imported.
I've tried every possible combination of export/import/calculate/split tangents from FBX format and can't find a perfect solution.
Does anyone have a solution to this?
Thanks,
-Alex![alt text][1]
[1]: /storage/temp/18558-uv_seam_tangents.jpgmodeluvnormaltangentseamTue, 26 Nov 2013 23:35:48 GMTMojFind tangent points on circle
http://answers.unity.com/questions/552279/find-tangent-points-on-circle.html
I need to find the tangent point on each side of a circle from a point. So If I have 2 game objects. object1 has a circle of radius 10, object2 is off to the side and looking at object1. Draw 2 lines from object1 to object2 that are tangent to the circle on each side. How can I find these points?vectorcircletangentWed, 09 Oct 2013 18:36:39 GMTmthicke2