Types conversion in Unity3D

Hey there!
Recently I needed to store floats as integers and vice versa (used it to easily xor floats in my Anti-Cheat Toolkit) and I came across few ways of doing this.

Unsafe pointers – fastest one:

public unsafe int FloatToInt(float value)
{
	return *(int*)&value;
}

public unsafe float IntToFloat(int value)
{
	return *(float*)&value;
}

Easy, right?
Please, note, it requires /unsafe compiler option. To leverage unsafe operations in unity, you have two commonly used options to choose from:
1. Use it in a separate dll, compiled with /unsafe option.
2. Set /unsafe option right in your project using “Global Custom Defines”, putting it in .rsp file in your Assets/ root. See bottom of this page for details.
And keep in mind unsafe code is not supported in some build targets, like Web Player and Flash Player.

Unions (Explicitly layouted structs) – slower than pointers, but works in Web Player (not in Flash Player, d’oh!):

[StructLayout(LayoutKind.Explicit)]
internal struct FloatIntUnion
{
	[FieldOffset(0)]
	public float f;

	[FieldOffset(0)]
	public int i;
}

public int FloatToInt(float value)
{
	var u = new FloatIntUnion();
	u.f = value;
	return u.i;
}

public float IntToFloat(int value)
{
	var u = new FloatIntUnion();
	u.i = value;
	return u.f;
}

Pretty easy to use, safe and works on more build targets comparing to pointers.
Note [StructLayout(LayoutKind.Explicit)] attribute is used here in conjunction with [FieldOffset(*)] attribute. It allows you to set each struct field position in memory explicitly and read data stored there.

BitConverter class – pretend to be slowest:

public int FloatToInt(float value)
{
	return BitConverter.ToInt32(BitConverter.GetBytes(value), 0);
}

public float IntToFloat(int value)
{
	return BitConverter.ToSingle(BitConverter.GetBytes(value), 0);
}

Safest one though (works even in Flash Player).

I should mention there are so-called “safe pointers” in C#, used through Marshal class, but they are pretty complicated, require unmanaged memory allocations and constant control on memory at all (C# developers are usually rely on GC and do not bother on allocations), so I’ll not post Marshal review here for now, these 3 methods I described are usually enough for the types conversion.
I hop I’ll have some time to make performance tests on these methods in latest Unity 4.3 later and post results with tests sourcers here (in another blog post).

Questions? Ask in comments!

Found a typo? Please, highlight it and press Shift + Enter or click here to inform me!

Share Button

Alternativa3D 7 mouse intersection without mouse movement tip

Hi! If you’re foced to avoid mouse movement, but still have to find mouse intersection with a some object (e.g. if moves only object), here is a quick tip:

//place this code to the EnterFrame or Timer event handler
var origin:Vector3D = new Vector3D();
var direction:Vector3D = new Vector3D();
var concatenatedMatrix:Matrix3D;
var rayData:RayIntersectionData;

_camera.calculateRay(origin, direction, mouseX, mouseY);

// _containerSurface - object to intersect
concatenatedMatrix = _containerSurface.concatenatedMatrix;
concatenatedMatrix.invert();
origin = concatenatedMatrix.transformVector(origin);
direction = concatenatedMatrix.deltaTransformVector(direction);

rayData = _containerSurface.intersectRay(origin, direction);
if (rayData)
{
  // just highlighting intersected face
  rayData.face.material = new FillMaterial(0xFF0000);
}

Found a typo? Please, highlight it and press Shift + Enter or click here to inform me!

Share Button

Inverting animation in the Alternativa3D

Hi! If you wish to reverse (play from the end to the beginning) some of your animations in the Alternativa3D engine, you could try to set the AnimationClip‘s property speed to the negative value, but it could be harmful in most cases and don’t officially supported.

Unfortunately, there is no any information or suggests about inverting animations like so on the public yet and nothing about it in the official documentation. I think, current engine build (7.7) have no lightweight native implementation of this functionality. It could be nice to see something like boolean reverse property at the AnimationClip class, isn’t it? Bot there is nothing similar yet =(

So, with great tip from the Vladimir Babushkin (about rebuilding animation by keyframes with reversed time), I came to this piece of code:
Continue reading

Found a typo? Please, highlight it and press Shift + Enter or click here to inform me!

Share Button

FlashDevelop + Flash Pro IDE + AIR (quick tip)

Hello, friends!
I had to write some code in the FlashDevelop for my AIR app (builded from Flash Pro IDE) recently and there was a little tricky to force FD to recognize and work well with all AIR-related classes and code.
So, if you’re using FD for code writing and Flash IDE to publish you AIR app, you could create usual FD “Flash IDE Project”.
But that project type don’t support native AIR code and it could be annoying.
To add native AIR code support in your FlashDevelop Flash IDE Project, just add “+configname=air” to the Additional Compiler Options in the Compiler Options tab of your project properties dialog:

FlashDevelop project properties dialog

FlashDevelop project properties dialog

Hope it will be helpful for some of you.

Found a typo? Please, highlight it and press Shift + Enter or click here to inform me!

Share Button