Sign in to follow this  
arkangel2803

Float to Float4, Float4 to Float Problems

Recommended Posts

Hi all Im testing this code i found in this forum to encode some floats to a single float value and viceversa.
float4 FloatToFloat4( float rawValue )
{	
	float4 packFactors = float4( 256.0 * 256.0 * 256.0, 256.0 * 256.0, 256.0, 1.0);
	float4 bitMask = float4(1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0, 1.0 / 256.0);

	float4 packedValue = float4(frac(packFactors*rawValue));
	packedValue -= packedValue.xxyz * bitMask;
	return packedValue;
}

float Float4ToFloat(float4 encodedValue)
{
	float4 unpackFactors = float4( 1.0 / (256.0 * 256.0 * 256.0), 1.0 / (256.0 * 256.0), 1.0 / 256.0, 1.0 );

	return dot(encodedValue,unpackFactors);
}
I tryed to encode float4(150.0f,100.0f,50.0f,25.0f) and the results when i try to decode the single float is float4(0.0f,159.0f,100.0f,204.0f) Ofcourse something wrong is happening, but i dont know what, even this, i dont know if i can use this code to encode float4 -> float -> float4. Im trying to pack values from material specular (power, exponent, fresnel_min, fresnel_max) to a 32bits float value, and then unpack it again on a post process. Knows you a better way to achive this ? or i need to pass each value on a color channel in a Render Target ? Thanks for your time and help :) LLORENS

Share this post


Link to post
Share on other sites
Your trying to pack 128bits of data into 32bits...
Looking at it, it seems this is will pack 4 8bit normalized floats.
Try numbers like 1.0 0.75 0.5 0.25.

You'll still get some errors cause a float only has 24bits of precission.

Share this post


Link to post
Share on other sites
Hi Ibasa, and thx for your answer.

I tested float4(1.0f,0.75f,0.5f,0.25f) and the result is float4(0.0f,191.0f,128.0f,191.0f);

It's strange, but i cant get it working, i dont know if precision is making these to fail or what...

Any ideas? Or any other methodology to pack some float values in a 32bits Render Target channel ?

Thx in advance :)

LLORENS

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