Float to Float4, Float4 to Float Problems

Started by
1 comment, last by arkangel2803 14 years, 10 months ago
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
Advertisement
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.
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

This topic is closed to new replies.

Advertisement