Created
May 22, 2015 17:00
-
-
Save unitycoder/9d4e539e6cc03a2ed211 to your computer and use it in GitHub Desktop.
Pack 3-Floats [0-1] to 1-Float
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using UnityEngine; | |
using System.Collections; | |
// source: http://diaryofagraphicsprogrammer.blogspot.fi/2009/10/bitmasks-packing-data-into-fp-render.html | |
public class PackTest : MonoBehaviour { | |
void Start () { | |
var myFloat = Pack3PNForFP32(new Vector3(0.1f,0.2f,0.3f)); | |
Debug.Log("my f:"+myFloat); | |
var unPacked = Unpack3PNFromFP32(myFloat); | |
Debug.Log("my u:"+unPacked); | |
} | |
float Pack3PNForFP32(Vector3 channel) | |
{ | |
int uValue; | |
uValue = ((int)(channel.x * 65535.0f + 0.5f)); | |
uValue |= ((int)(channel.y * 255.0f + 0.5f)) << 16; | |
uValue |= ((int)(channel.z * 253.0f + 1.5f)) << 24; | |
return (float)(uValue); | |
} | |
Vector3 Unpack3PNFromFP32(float fFloatFromFP32) | |
{ | |
float a, b, c, d; | |
int uValue; | |
int uInputFloat = (int)(fFloatFromFP32); | |
a = ((uInputFloat) & 0xFFFF) / 65535.0f; | |
b = ((uInputFloat >> 16) & 0xFF) / 255.0f; | |
c = (((uInputFloat >> 24) & 0xFF) - 1.0f) / 253.0f; | |
return new Vector3(a, b, c); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment