Skip to main content
GameDev.net gamedev.net
🔒 Locked 🎮 Unity

[SlimDX] Efficient Vector4 to Vector3 conversion

Started by adt7 Dec 23, 2009 at 5:04 AM 1 replies 4k views
Original Post
adt7
adt7
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?)
Scarabus2
Scarabus2
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.
visualnovelty.com - Novelty - Visual novel maker
jpetrie
jpetrie
The vector classes are value types, so they're stack-allocated in most cases, including your example. It will not be a serious performance issue.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.