Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Pack depth into RGB


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
1 reply to this topic

#1 skytiger   Members   -  Reputation: 262

Like
3Likes
Like

Posted 22 December 2011 - 03:06 AM

I came up with this approach, which seems to fix problems with other approaches on the net, and reduces errors across the whole [0,1] range to 0
Furthermore this should exactly match the 24 bit fixed precision of a standard D24S8 depth buffer

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Pack Unit Float [0,1] into RGB24
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
float3 UnitToColor24New(in float depth) {

	// Constants
	const float3 scale	= float3(1.0, 256.0, 65536.0);
	const float2 ogb	= float2(65536.0, 256.0) / 16777215.0;
	const float normal	= 256.0 / 255.0;
	
	// Avoid Precision Errors
	float3 unit			= (float3)depth;
	unit.gb				-= floor(unit.gb / ogb) * ogb;
	
	// Scale Up
	float3 color		= unit * scale;
	
	// Use Fraction to emulate Modulo
	color				= frac(color);
	
	// Normalize Range
	color				*= normal;
	
	// Mask Noise
	color.rg			-= color.gb / 256.0;

	return color;
	
}//function

// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// Unpack RGB24 into Unit Float [0,1]
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
float ColorToUnit24New(in float3 color) {
	const float3 scale = float3(65536.0, 256.0, 1.0) / 65793.0;
	return dot(color, scale);
}//function



Sponsor:

#2 EsorU   Members   -  Reputation: 112

Like
0Likes
Like

Posted 23 December 2011 - 03:24 PM

Nice work, thanks for sharing!




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS