amerkoleci / vortice.mathematics Goto Github PK
View Code? Open in Web Editor NEWCross platform .NET math library.
License: MIT License
Cross platform .NET math library.
License: MIT License
Hi,
I found a trivial mistake in Color.cs
.
/// <summary>
/// The alpha component of the color.
/// </summary>
[FieldOffset(2)] // <- 3?
public byte A;
Thank you for your great work.
Suggest if you are happy for any contributions also, creating a to do list on your API. Happy to contribute and grow it.
I know your time is limited and the community would be happy to do add to your code base.
Feel free to close once reviewed.
I've been doing a fair few hours work around why unproject seems to lose accuracy as you get further from 0. The obvious answer is floating point accuracy.
I did play around with double, but essentially the issue is that due to the Matrices also being float, you simply can't just create data out of thin air. (float is substantially more accurate closer to 0 anyway obviously)
I did though figure out a novel approach that will work in most circumstances that drastically improved the accuracy.
What the approach does below is translate the point to the origin, maintaining the orientation (scale other than 1 will not work atm).
Once the unproject is complete, I add the translation back on, which improves the accuracy substantially.
The view matrix needs to be generated without translations, and you can't eliminate from the WVP obviously.
Proposal, that a new unproject in the view class is created (I can do the pull request) that takes in Projection, View and ScreenPosition and returns the world position.
The accuracy is improved by about 90% once your world coordinates get > 1000. I've tested it multiple times and found that the ray projection resolution is now closer to the screen resolution, before, its sub 10% at high world coordinates.
///camera function
public Matrix4x4 GenerateNonTranslatedWVP()
{
Matrix4x4 viewMatrix = Matrix4x4.CreateLookAt(_position, _targetPosition, Constants.s_upVector);
viewMatrix.Translation = Vector3.Zero;
Matrix4x4 projectionMatrix = Matrix4x4.CreatePerspectiveFieldOfView(c_gameFov, _viewport.AspectRatio, 0.01f, _unitDepth);
return viewMatrix * projectionMatrix;
}
/// test code
public static Vector3 Determine3dPointFrom2d(Camera3d camera, in Vector3 screenSpacePoint)
{
Matrix4x4 nonTranslated = camera.GenerateNonTranslatedWVP();
Matrix4x4.Invert(nonTranslated, out Matrix4x4 inverse);
Vector3 nonTrans_ = camera.ViewPort.Unproject(screenSpacePoint, inverse);
nonTrans_ += camera.Position;
return nonTrans_;// Unproject(camera.ViewPort, screenSpacePoint, inverse);
}
Thoughts?
Rect rect = new ();
Console.WriteLine(rect);
Rect { X = 0, Y = 0 }, { Width = 0, Height = 0 }
Rect { X = 0, { Y = 0, { Width = 0, Height = 0 }
A Matrix5x4 instance cannot be created because the constructor does not exist.
Just like Matrix3x3 and other Matrix structs, I want the following constructors.
Matrix5x4(float value)
Matrix5x4(float m11, float m12, ...)
Like this constructor:
Vortice.Mathematics/src/Vortice.Mathematics/Matrix4x3.cs
Lines 82 to 115 in d874d93
Error CS0433: The type 'Half' exists in both 'Vortice.Mathematics, Version=1.4.0.0, Culture=neutral, PublicKeyToken=5431ec61a7e925da' and 'System.Runtime, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
Hi, for functional parity, is there consideration of pulling the Ray class from Sharpdx across into Vortice Mathematics?
+-*/~%^&| << >> Length() AsBytes(Span) ToArray(Span) xyzw get;set;
MCG0017: MCG0017:NotWinRTType Cannot marshal generic Windows Runtime types with a non Windows Runtime type as a generic type argument. Check the generic arguments of type 'System.Nullable`1<Vortice.Mathematics.Color4>'
It runs in debug mode, but won't compile in release mode
Line 68 of BoundingBox.cs should read _min = value;
This lead to some hilarious bugs on my end ๐
Should have field offset of 4 and 6 for Z and W respectively.
Great library so far! Having fun converting from sharpdx
Hi, long time since we spoke ๐!
Wanted to share this small nitpick with you.
The method public readonly bool Intersects( in Vector3 v0, in Vector3 v1, in Vector3 v2, out Vector3 pointInTriangle)
on the Ray
struct has the following disclaimer:
This ONLY works on CW wound triangles.
But what is clock-wise depends on the unit system the user is using. As a DirectX user, I assumed Z-
is forward. But it looks like this code uses the OpenGL convention in which Z+
is forward. Which means that every triangle that I believe to be clockwise, is actually counter-clockwise.
A small proof:
[Fact]
public void RayTriangleIntersectionTest()
{
var position = new Vector3(2.75f, 1000.0f, 1.5f);
var direction = new Vector3(0.0f, -1.0f, 0.0f);
var ray = new Ray(position, direction);
// nw---ne
// \ |
// \ |
// \ se
var nw = new Vector3(2, 18, 1);
var ne = new Vector3(3, 18, 1);
var se = new Vector3(3, 18, 2);
var hitCW = ray.Intersects(nw, ne, se, out var pitCW);
var hitCCW = ray.Intersects(se, ne, nw, out var pitCCW);
True(hitCW);
False(hitCCW);
}
It would be nice to add a small clarification, so that users double check what convention they are using.
We've noticed that your package on NuGet.org is missing a README file.
Our customer research indicates that one of the top problems that package consumers face is insufficient package documentation such as README files. Adding a README file to your package will help users quickly understand what the package is and what it does. Since your README will be the first impression for users when they view your package on NuGet.org, it is crucial for authors to write and include high-quality READMEs for their packages.
If you're new to NuGet READMEs, follow guidance on how to add one to your package.
Follow a blog post with README best practices and a template file to help you get started.
We value your feedback. If you encounter any issues or have suggestions, please reply to this issue.
Thank you for your contribution to the NuGet community.
Hi,
Using the unproject to determine a raycast into the screen (0, 1) range.
I get values starting near 0 to a very large value. The implementation differs from the Sharpdx version where I call
Vector3 screenSpacePoint = new Vector3(mousex, mousey, 0);
Matrix4x4.Invert(camera.WVPMatrix, out Matrix4x4 inverse);
Vector3 point = Unproject(camera.ViewPort, screenSpacePoint, inverse);
I tested this against the vortice implementation and getting very different result.
The underlying code for the unproject is:
public static Vector3 Unproject(Viewport vp, Vector3 source, Matrix4x4 matrix)
{
Vector3 vector_ = new Vector3();
vector_.X = (((source.X - vp.X) / (vp.Width)) * 2f) - 1f;
vector_.Y = -((((source.Y - vp.Y) / (vp.Height)) * 2f) - 1f);
vector_.Z = (source.Z - vp.MinDepth) / (vp.MaxDepth - vp.MinDepth);
float a = (((vector_.X * matrix.M14) + (vector_.Y * matrix.M24)) + (vector_.Z * matrix.M34)) + matrix.M44;
//Vector3.Transform(ref vector, ref matrix, out vector);
vector_ = Vector3.Transform(vector_, matrix);
if (!IsOne(a))
{
vector_ = (vector_ / a);
}
return vector_;
}
public const float ZeroTolerance = 1e-6f; // Value a 8x higher than 1.19209290E-07F
public static bool IsOne(float a)
{
return IsZero(a - 1.0f);
}
public static bool IsZero(float a)
{
return Math.Abs(a) < ZeroTolerance;
}
Happy to provide more samples, but this maybe down to my use fundamentally of the current unproject call.
Vortice.Mathematics uses System.Runtime.CompilerServices.Unsafe in version 6.0.0 as a dependency. Would it be possible to downgrade the version to 4.7.0?
This will increase compatibility with other products that also use System.Runtime.CompilerServices.Unsafe as a dependency.
In the versions <= 1.9.143 the library System.Runtime.CompilerServices.Unsafe was not used at all. There the compatibility had been higher.
I suspect the Box members are reversed from what they should be. Height should be Bottom - Top, not Top - Bottom. And Depth should be Back - Front, not Front - Back.
At least if you want to match the D3D box structure: https://docs.microsoft.com/en-us/windows/win32/api/d3d12/ns-d3d12-d3d12_box
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.