Discontinuity reduction by Mercury et al.

This commit is contained in:
Emmanuel BENOîT 2017-10-05 17:03:31 +02:00
parent 290555ad53
commit 6bb35ae40e
3 changed files with 81 additions and 7 deletions

View file

@ -1,4 +1,5 @@
//! type library //! type library
//! include lib/utils.glsl
vec2 RM_Map( in vec3 pos ); vec2 RM_Map( in vec3 pos );
@ -88,3 +89,60 @@ vec3 RM_Advanced(
return vec3( cDist , -1 , steps ); return vec3( cDist , -1 , steps );
} }
float RM_TanPhi(
in vec2 uv ,
in float pixelRadius )
{
// 1 / tan( FOV * PI / 360 )
// u_NearPlane = 2 * tan( PI * ( 180 - FOV ) / 360 );
// atan( u_NearPlane / 2 ) = PI * ( 180 - FOV ) / 360
// = PI / 2 - PI * FOV / 360
// atan( u_NearPlane / 2 ) - PI / 2 = - PI * FOV / 360
// PI / 2 - atan( u_NearPlane / 2 ) = PI * FOV / 360
// ...
// ...
// or we could just pass it as a fucking uniform
const float fov = PI * .5 - atan( u_NearPlane * .5 );
vec2 e = ( abs( uv.x ) > abs( uv.y ) )
? vec2( uv.x < 0 ? -1 : 1 , 0 )
: vec2( 0 , uv.y < 0 ? -1 : 1 );
vec3 d = normalize( vec3( uv , -1 / tan( fov ) ) ) ,
a = vec3( ( e + e.yx ) * pixelRadius + uv ,
-1 / tan( fov ) ) ,
b = vec3( ( e - e.yx ) * pixelRadius + uv ,
-1 / tan( fov ) ) ,
p2ca = a / dot( a , d ) ,
p2cb = a / dot( b , d ) ,
diff = p2cb - p2ca ,
de = mix( p2ca , p2cb , M_Saturate(
dot( d - p2ca , diff ) / M_LengthSqr( diff ) ) );
float sinPhiSquared = M_LengthSqr( cross( d , normalize( de ) ) );
return sqrt( sinPhiSquared / ( 1 - sinPhiSquared ) );
}
float RM_ErrFunc(
in vec3 origin ,
in vec3 pos )
{
return 0; // FIXME
}
vec3 RM_ReduceDiscontinuity(
in vec3 origin ,
in vec3 rayDir ,
in float hitDist ,
in float tanPhi ,
in int iterations )
{
vec3 hitPos = origin + hitDist * rayDir;
for ( int i = 0 ; i < iterations ; i ++ ) {
const float err = length( origin - hitPos ) * tanPhi;
hitPos = hitPos + rayDir * (
RM_Map( hitPos ).x - tanPhi
);
}
return hitPos;
}

View file

@ -17,14 +17,28 @@ float M_Hash(
// ----------------------------------------------------------------------------- // -----------------------------------------------------------------------------
float M_Luminosity( float M_Luminosity(
in vec3 color ) in vec3 color )
{ {
return dot( color , vec3( .299 , .587 , .114 ) ); return dot( color , vec3( .299 , .587 , .114 ) );
} }
vec3 M_NormalizeColor( vec3 M_NormalizeColor(
in vec3 color ) in vec3 color )
{ {
const float l = M_Luminosity( color ); const float l = M_Luminosity( color );
return l > 0 ? ( color / l ) : vec3( 1 ); return l > 0 ? ( color / l ) : vec3( 1 );
} }
// -----------------------------------------------------------------------------
float M_Saturate(
in float x )
{
return clamp( x , 0 , 1 );
}
float M_LengthSqr(
in vec3 x )
{
return dot( x , x );
}

View file

@ -94,24 +94,26 @@ void main( )
+ uv.x * side * u_Resolution.x / u_Resolution.y + uv.x * side * u_Resolution.x / u_Resolution.y
+ uv.y * up ); + uv.y * up );
float tanPhi = RM_TanPhi( uv , u_Render.z );
vec3 r = RM_Advanced( u_CamPos , rayDir , vec3 r = RM_Advanced( u_CamPos , rayDir ,
int( u_Render.x ) , u_Render.y , int( u_Render.x ) , u_Render.y ,
u_Render.z , .001 , u_Render.w ); /*u_Render.z */ tanPhi , .001 , u_Render.w );
vec3 hitPos = u_CamPos + rayDir * r.x;
const vec3 background = vec3( .005 ); const vec3 background = vec3( .005 );
vec3 bc = background; vec3 bc = background;
if ( r.y >= 0. ) { if ( r.y >= 0. ) {
const int midx = int( r.y ); const vec3 hitPos = RM_ReduceDiscontinuity(
u_CamPos , rayDir , r.x ,
tanPhi , 10 );
const vec3 normal = RM_GetNormal( hitPos ); const vec3 normal = RM_GetNormal( hitPos );
const int midx = int( r.y );
const vec3 sunlightDir = normalize( vec3( 0 , 1 , 1 ) ); const vec3 sunlightDir = normalize( vec3( 0 , 1 , 1 ) );
const vec3 sunlightColor = vec3( 1 , 1 , .99 ) * .1; const vec3 sunlightColor = vec3( 1 , 1 , .99 ) * .1;
const vec3 pl1Pos = vec3( 3 , 3 , 5 ); const vec3 pl1Pos = vec3( 3 , 3 , 5 );
const vec3 pl1Color = vec3( 1 , .7 , .8 ); const vec3 pl1Color = vec3( 1 , .7 , .8 );
const float pl1Power = 20; const float pl1Power = 20;
const vec3 pl2Pos = vec3( -3 , -3 , 5 ); const vec3 pl2Pos = vec3( -3 , -3 , 5 );
const vec3 pl2Color = vec3( .6 , 1 , .8 ); const vec3 pl2Color = vec3( .6 , 1 , .8 );
const float pl2Power = 20; const float pl2Power = 20;