this happens even if the value of float3 is just close to zero. Meanwhile, Vector3 handles zero vectors well.
This seems like the correct behaviour you’d expect from a maths library for normalize.
Though I’m not sure about cross product. I thought (0,0,0)x(0,0,0) was actually 0. Been a while though.
-edit- yeah wiki seems to say that as well
If the cross product of two vectors is the zero vector (i.e. a Ă— b = 0), then either one or both of the inputs is the zero vector
Normalizing zero vector is undefined though and returning NaN seems to be the correct behaviour. Whether it is more useful to return 0 is another matter.
Indeed, math.cross of two vectors is zero vector. sorry for my mistake.
There exists math.safeNormalize.
It’s neither in the package source code nor on github. is it in the next release?
It’s normalizeSafe and it’s in experimental namespace, take a look here:
https://github.com/Unity-Technologies/Unity.Mathematics/blob/master/src/Unity.Mathematics/mathexperimental.cs
I know this is 2 years late, but I spent 2 weeks looking for an answer to this lol. Maybe add this to the documentation?
The math library is pretty well documented. Here’s the snippet.
/// <summary>Returns a normalized version of the float2 vector x by scaling it by 1 / length(x).</summary>
public static float2 normalize(float2 x) { return rsqrt(dot(x, x)) * x; }
/// <summary>Returns a normalized version of the double2 vector x by scaling it by 1 / length(x).</summary>
public static double2 normalize(double2 x) { return rsqrt(dot(x, x)) * x; }
/// <summary>
/// Returns a safe normalized version of the float2 vector x by scaling it by 1 / length(x).
/// Returns the given default value when 1 / length(x) does not produce a finite number.
/// </summary>
static public float2 normalizesafe(float2 x, float2 defaultvalue = new float2())
{
float len = math.dot(x, x);
return math.select(defaultvalue, x * math.rsqrt(len), len > FLT_MIN_NORMAL);
}
I can’t for the life of me find that.
I hit F12 (Go to Declaration or Usages) in rider to open the source code, and it’s all there
https://docs.unity3d.com/Packages/com.unity.mathematics@1.1/manual/index.html
also take a look at source in Packages path of Project. The code is pretty well documented.
Damn, now I have to get Rider, just for this.
I’m not sure why you linked to a page that doesn’t have any documentation about safeNormalize. I think that was the whole point of contention of martygrof3708.
Strange that Visual Studio doesn’t link to source, but a codegen meta version of it. Very strange.
It’s all lowercase.
You can also search for normalizesafe
in the search field (top right).
One issue: the docs are so horribly large that browsers struggle to render it in a timely manner.
If you get math through the package manager it is precompiled as a dll in Library/ScriptAssemblies and you don’t have access to the source. You have to get it from the git in order to read the source code.
https://github.com/Unity-Technologies/Unity.Mathematics
I hope they give the option of downloading the source through the package manager in future when it is available, the convenience of having access to it far outweighs the added build time.
F12 works in Visual Studio Code as well. Visual Studio and MonoDevelop should have similar functions. It may be called definition instead of declaration.
Source is all there mate
Library\PackageCache\com.unity.mathematics@1.2.1\
To further elaborate, Library/ScriptAssemblies is just the compiled assemblies in the editor. Unity does actually compile package assemblies from source on users’ machines.
VS Code doesn’t link to those files. What a shame.
It is! I wonder if there is a way to get visual studio code to refer to it.
VS Code Do link to math.
- Add “Visual Studio Code Editor” package to you project
- In Preferences=>External Tools. Select Visual Studio Code as External Script Editor.
- Click the “Regenerate project files” button
- change this line in your “.vscode/settings.json” => “[Ll]library/”:true, to “[Ll]ibrary/[^P]*”:true,
You Should be able to see package source in vscode and F12 jump into it.