Sign in to follow this  
adt7

[SlimDX] Efficient Vector4 to Vector3 conversion

Recommended Posts

I suppose this is more of a .Net/C# question than a SlimDX one, but I want to make sure the SlimDX guys see it to get their input. The "problem" I'm having is that 90% of the time I use Vector3.Transform() and the other Vector3 methods, I want a Vector3 as the output (much like you get in XNA), however, since SlimDX wraps DirectX it outputs a Vector4, just like the D3DX functions. So my first thought was to write a quick ToVector3() extension method for Vector4, but then I couldn't figure out which was the best way to write it. Originally I had the following:
public static Vector3 ToVector3(this Vector4 in)
{
    return new Vector3(in.X, in.Y, in.Z);
}

Thinking about it this morning though, I don't want to be allocating new Vector3s each frame (or does it not really matter?) Ideally the Vector3 methods would return Vector3s, or I could downcast between Vector types but I know both of those are not going to happen. Is there another way of doing this? Or a more efficient way? Is it worth writting an extension overload for transform that returns a Vector3 instead? (although I'm guessing I'd lose out on all the optomisation the wrapped SlimDX methods have?)

Share this post


Link to post
Share on other sites
Look at it this way: you have a Vector4 but you want to make it into something new. I think the code above is just fine.

If you really wish to cut down on allocations you should take a step back and ask yourself why you're given a Vector4 when you really want a Vector3.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this