Scatter Sky Community Fixes

Fixes and Extends Features in the Scatter Sky Object and Shaders
This commit is contained in:
Ron Kapaun 2013-04-22 14:25:15 -05:00 committed by DavidWyand-GG
parent 88a319179b
commit 808f1a5beb
6 changed files with 188 additions and 95 deletions

View file

@ -169,6 +169,7 @@ ScatterSky::ScatterSky()
mNightColor.set( 0.0196078f, 0.0117647f, 0.109804f, 1.0f );
mNightFogColor = mNightColor;
mUseNightCubemap = false;
mSunSize = 1.0f;
mMoonMatInst = NULL;
@ -179,6 +180,9 @@ ScatterSky::ScatterSky()
mMatrixSet = reinterpret_cast<MatrixSet *>(dMalloc_aligned(sizeof(MatrixSet), 16));
constructInPlace(mMatrixSet);
mColorizeAmt = 0;
mColorize.set(0,0,0);
}
ScatterSky::~ScatterSky()
@ -318,10 +322,16 @@ void ScatterSky::initPersistFields()
addField( "skyBrightness", TypeF32, Offset( mSkyBrightness, ScatterSky ),
"Global brightness and intensity applied to the sky and objects in the level." );
addField( "mieScattering", TypeF32, Offset( mMieScattering, ScatterSky ),
"Affects the size and intensity of light scattering around the sun." );
addField( "sunSize", TypeF32, Offset( mSunSize, ScatterSky ),
"Affects the size of the sun's disk." );
addField( "colorizeAmount", TypeF32, Offset( mColorizeAmt, ScatterSky ),
"Controls how much the the alpha component of colorize brigthens the sky. Setting to 0 returns default behavior." );
addField( "colorize", TypeColorF, Offset( mColorize, ScatterSky ),
"Tints the sky the color specified, the alpha controls the brigthness. The brightness is multipled by the value of colorizeAmt." );
addField( "rayleighScattering", TypeF32, Offset( mRayleighScattering, ScatterSky ),
"Controls how blue the atmosphere is during the day." );
@ -436,7 +446,9 @@ U32 ScatterSky::packUpdate(NetConnection *con, U32 mask, BitStream *stream)
mMieScattering4PI = mMieScattering * 4.0f * M_PI_F;
stream->write( mMieScattering4PI );
stream->write( mSunSize );
stream->write( mSkyBrightness );
stream->write( mMiePhaseAssymetry );
@ -460,6 +472,8 @@ U32 ScatterSky::packUpdate(NetConnection *con, U32 mask, BitStream *stream)
stream->write( mAmbientScale );
stream->write( mSunScale );
stream->write( mFogScale );
stream->write( mColorizeAmt );
stream->write( mColorize );
stream->write( mExposure );
@ -513,7 +527,9 @@ void ScatterSky::unpackUpdate(NetConnection *con, BitStream *stream)
stream->read( &mMieScattering );
stream->read( &mMieScattering4PI );
stream->read( &mSunSize );
stream->read( &mSkyBrightness );
stream->read( &mMiePhaseAssymetry );
@ -539,6 +555,16 @@ void ScatterSky::unpackUpdate(NetConnection *con, BitStream *stream)
stream->read( &mAmbientScale );
stream->read( &mSunScale );
stream->read( &mFogScale );
F32 colorizeAmt;
stream->read( &colorizeAmt );
if(mColorizeAmt != colorizeAmt) {
mColorizeAmt = colorizeAmt;
mShader = NULL; //forces shader refresh
}
stream->read( &mColorize );
if ( tmpColor != mWavelength )
{
@ -663,8 +689,12 @@ bool ScatterSky::_initShader()
Con::warnf( "ScatterSky::_initShader - failed to locate shader ScatterSkyShaderData!" );
return false;
}
Vector<GFXShaderMacro> macros;
if ( mColorizeAmt )
macros.push_back( GFXShaderMacro( "USE_COLORIZE" ) );
mShader = shaderData->getShader( macros );
mShader = shaderData->getShader();
if ( !mShader )
return false;
@ -698,6 +728,7 @@ bool ScatterSky::_initShader()
mInverseWavelengthSC = mShader->getShaderConstHandle( "$invWaveLength" );
mNightInterpolantAndExposureSC = mShader->getShaderConstHandle( "$nightInterpAndExposure" );
mUseCubemapSC = mShader->getShaderConstHandle( "$useCubemap" );
mColorizeSC = mShader->getShaderConstHandle( "$colorize" );
return true;
}
@ -792,68 +823,78 @@ void ScatterSky::_initCurves()
// Takes time of day (0-2) and returns
// the night interpolant (0-1) day/night factor.
// moonlight = 0, sunlight > 0
mCurves[0].clear();
mCurves[0].addPoint( 0.0f, 0.5f );
mCurves[0].addPoint( 0.1f, 1.0f );
mCurves[0].addPoint( 0.9f, 1.0f );
mCurves[0].addPoint( 1.0f, 0.5f );
mCurves[0].addPoint( 1.1f, 0.0f );
mCurves[0].addPoint( 1.9f, 0.0f );
mCurves[0].addPoint( 2.0f, 0.5f );
//mCurves[0].addPoint( 0.0f, 0.25f );
//mCurves[0].addPoint( 0.05f, 0.5f );
//mCurves[0].addPoint( 0.1f, 1.0f );
//mCurves[0].addPoint( 0.6f, 1.0f );
//mCurves[0].addPoint( 0.98f, 0.895f );
//mCurves[0].addPoint( 1.0f, 0.15f );
//mCurves[0].addPoint( 1.15f, 0.0f );
//mCurves[0].addPoint( 1.9f, 0.0f );
//mCurves[0].addPoint( 2.0f, 0.15f );
mCurves[0].addPoint( 0.0f, 0.5f );// Sunrise
mCurves[0].addPoint( 0.025f, 1.0f );//
mCurves[0].addPoint( 0.975f, 1.0f );//
mCurves[0].addPoint( 1.0f, 0.5f );//Sunset
mCurves[0].addPoint( 1.02f, 0.0f );//Sunlight ends
mCurves[0].addPoint( 1.98f, 0.0f );//Sunlight begins
mCurves[0].addPoint( 2.0f, 0.5f );// Sunrise
// Takes time of day (0-2) and returns
// the moon light brightness.
// Takes time of day (0-2) and returns mieScattering factor
// Regulates the size of the sun's disk
mCurves[1].clear();
mCurves[1].addPoint( 0.0f, 0.0f );
mCurves[1].addPoint( 1.0f, 0.0f );
mCurves[1].addPoint( 1.1f, 0.0f );
mCurves[1].addPoint( 1.2f, 0.5f );
mCurves[1].addPoint( 1.3f, 1.0f );
mCurves[1].addPoint( 1.8f, 0.5f );
mCurves[1].addPoint( 1.9f, 0.0f );
mCurves[1].addPoint( 2.0f, 0.0f );
mCurves[1].addPoint( 0.0f, 0.0006f );
mCurves[1].addPoint( 0.01f, 0.00035f );
mCurves[1].addPoint( 0.03f, 0.00023f );
mCurves[1].addPoint( 0.1f, 0.00022f );
mCurves[1].addPoint( 0.2f, 0.00043f );
mCurves[1].addPoint( 0.3f, 0.00062f );
mCurves[1].addPoint( 0.4f, 0.0008f );
mCurves[1].addPoint( 0.5f, 0.00086f );// High noon
mCurves[1].addPoint( 0.6f, 0.0008f );
mCurves[1].addPoint( 0.7f, 0.00062f );
mCurves[1].addPoint( 0.8f, 0.00043f );
mCurves[1].addPoint( 0.9f, 0.00022f );
mCurves[1].addPoint( 0.97f, 0.00023f );
mCurves[1].addPoint( 0.99f, 0.00035f );
mCurves[1].addPoint( 1.0f, 0.0006f );
mCurves[1].addPoint( 2.0f, 0.0006f );
// Takes time of day and returns brightness
// Controls sunlight and moonlight brightness
mCurves[2].clear();
mCurves[2].addPoint( 0.0f, 0.4f );
mCurves[2].addPoint( 0.25f, 1.0f );
mCurves[2].addPoint( 0.5f, 1.0f );
mCurves[2].addPoint( 0.75f, 0.9f );
mCurves[2].addPoint( 1.0f, 0.3f );
mCurves[2].addPoint( 1.02877f, 0.0f );
mCurves[2].addPoint( 1.05f, 0.0f );
mCurves[2].addPoint( 1.15f, 0.0f );
mCurves[2].addPoint( 1.2f, 0.0f );
mCurves[2].addPoint( 1.3f, 0.3f );
mCurves[2].addPoint( 1.85f, 0.4f );
mCurves[2].addPoint( 1.9f, 0.0f );
mCurves[2].addPoint( 2.0f, 0.0f );
mCurves[2].addPoint( 0.0f, 0.2f );// Sunrise
mCurves[2].addPoint( 0.1f, 1.0f );
mCurves[2].addPoint( 0.9f, 1.0f );// Sunset
mCurves[2].addPoint( 1.008f, 0.0f );//Adjust end of sun's reflection
mCurves[2].addPoint( 1.02001f, 0.0f );
mCurves[2].addPoint( 1.05f, 0.5f );// Turn brightness up for moonlight
mCurves[2].addPoint( 1.93f, 0.5f );
mCurves[2].addPoint( 1.97999f, 0.0f );// No brightness when sunlight starts
mCurves[2].addPoint( 1.992f, 0.0f );//Adjust start of sun's reflection
mCurves[2].addPoint( 2.0f, 0.2f ); // Sunrise
// Interpolation of day/night color sets
// 0/1 ambient/nightcolor
// 0 = day colors only anytime
// 1 = night colors only anytime
// between 0 and 1 renders both color sets anytime
mCurves[3].clear();
mCurves[3].addPoint( 0.0f, 0.01f );
mCurves[3].addPoint( 0.05f, 0.0f );
mCurves[3].addPoint( 0.0f, 0.8f );//Sunrise
mCurves[3].addPoint( 0.1f, 0.0f );
mCurves[3].addPoint( 0.6f, 0.0f );
mCurves[3].addPoint( 0.98f, 0.75f );
mCurves[3].addPoint( 1.0f, 1.0f );
mCurves[3].addPoint( 1.02877f, 1.0f );
mCurves[3].addPoint( 1.05f, 1.0f );
mCurves[3].addPoint( 1.2f, 1.0f );
mCurves[3].addPoint( 1.3f, 1.0f );
mCurves[3].addPoint( 1.85f, 1.0f );
mCurves[3].addPoint( 1.9f, 1.0f );
mCurves[3].addPoint( 2.0f, 1.0f );
mCurves[3].addPoint( 0.99f, 0.0f );
mCurves[3].addPoint( 1.0f, 0.8f );// Sunset
mCurves[3].addPoint( 1.01999f, 1.0f );//
mCurves[3].addPoint( 1.98001f, 1.0f );// Sunlight begins with full night colors
mCurves[3].addPoint( 2.0f, 0.8f ); //Sunrise
// Takes time of day (0-2) and returns smoothing factor
// Interpolates between mMoonTint color and mNightColor
mCurves[4].clear();
mCurves[4].addPoint( 0.0f, 1.0f );
mCurves[4].addPoint( 0.96f, 1.0f );
mCurves[4].addPoint( 1.01999f, 0.5f );
mCurves[4].addPoint( 1.02001f, 0.5f );
mCurves[4].addPoint( 1.08f, 1.0f );
mCurves[4].addPoint( 1.92f, 1.0f );
mCurves[4].addPoint( 1.97999f, 0.5f );
mCurves[4].addPoint( 1.98001f, 0.5f );
mCurves[4].addPoint( 2.0f, 1.0f );
}
void ScatterSky::_updateTimeOfDay( TimeOfDay *timeOfDay, F32 time )
@ -869,9 +910,6 @@ void ScatterSky::_render( ObjectRenderInst *ri, SceneRenderState *state, BaseMat
GFXTransformSaver saver;
//mLightDir = -mLight->getDirection();
//mLightDir.normalize();
if ( mVB.isNull() || mPrimBuffer.isNull() )
_initVBIB();
@ -896,14 +934,6 @@ void ScatterSky::_render( ObjectRenderInst *ri, SceneRenderState *state, BaseMat
MatrixF proj( true );
frust.getProjectionMatrix( &proj );
//MatrixF camMat = state->getCameraTransform();
//camMat.inverse();
//MatrixF tmp( true );
//tmp = camMat;
//tmp.setPosition( Point3F( 0, 0, 0 ) );
//proj.mul( tmp );
Point3F camPos2 = state->getCameraPosition();
MatrixF xfm(true);
xfm.setPosition(camPos2);//-Point3F( 0, 0, 200000.0f));
@ -922,6 +952,7 @@ void ScatterSky::_render( ObjectRenderInst *ri, SceneRenderState *state, BaseMat
mShaderConsts->setSafe( mNightColorSC, mNightColor );
mShaderConsts->setSafe( mInverseWavelengthSC, invWavelength );
mShaderConsts->setSafe( mNightInterpolantAndExposureSC, Point2F( mExposure, mNightInterpolant ) );
mShaderConsts->setSafe( mColorizeSC, mColorize*mColorizeAmt );
if ( GFXDevice::getWireframe() )
{
@ -1120,7 +1151,15 @@ void ScatterSky::_interpolateColors()
mSunColor *= mSunScale;
mFogColor *= mFogScale;
mFogColor.interpolate( mFogColor, mNightFogColor, mCurves[3].getVal( mTimeOfDay ) );//mNightInterpolant );
mMieScattering = (mCurves[1].getVal( mTimeOfDay) * mSunSize ); //Scale the size of the sun's disk
ColorF moonTemp = mMoonTint;
ColorF nightTemp = mNightColor;
moonTemp.interpolate( mNightColor, mMoonTint, mCurves[4].getVal( mTimeOfDay ) );
nightTemp.interpolate( mMoonTint, mNightColor, mCurves[4].getVal( mTimeOfDay ) );
mFogColor.interpolate( mFogColor, mNightFogColor, mCurves[3].getVal( mTimeOfDay ) );//mNightInterpolant );
mFogColor.alpha = 1.0f;
mAmbientColor.interpolate( mAmbientColor, mNightColor, mCurves[3].getVal( mTimeOfDay ) );//mNightInterpolant );
@ -1175,11 +1214,6 @@ void ScatterSky::_getAmbientColor( ColorF *outColor )
if ( count > 0 )
(*outColor) /= count;
//Point3F pColor( outColor->red, outColor->green, outColor->blue );
//F32 len = pColor.len();
//if ( len > 0 )
// (*outColor) /= len;
mMieScattering = oldMieScattering;
}
@ -1232,14 +1266,6 @@ void ScatterSky::_getFogColor( ColorF *outColor )
F32 ScatterSky::_vernierScale( F32 fCos )
{
/*
F32 x5 = x * 5.25;
F32 x5p6 = (-6.80 + x5);
F32 xnew = (3.83 + x * x5p6);
F32 xfinal = (0.459 + x * xnew);
F32 xfinal2 = -0.00287 + x * xfinal;
F32 outx = mExp( xfinal2 );
return 0.25 * outx;*/
F32 x = 1.0 - fCos;
return 0.25f * exp( -0.00287f + x * (0.459f + x * (3.83f + x * ((-6.80f + (x * 5.25f))))) );
}
@ -1322,18 +1348,10 @@ void ScatterSky::_getColor( const Point3F &pos, ColorF *outColor )
F32 g = -0.991f;
F32 g2 = g * g;
F32 miePhase = _getMiePhase( fCos, fCos2, g, g2 );
//F32 rayleighPhase = _getRayleighPhase( fCos2 );
Point3F color = rayleighColor + (miePhase * mieColor);
ColorF tmp( color.x, color.y, color.z, color.y );
//if ( !tmp.isValidColor() )
//{
// F32 len = color.len();
// if ( len > 0 )
// color /= len;
//}
Point3F expColor( 0, 0, 0 );
expColor.x = 1.0f - exp(-mExposure * color.x);
expColor.y = 1.0f - exp(-mExposure * color.y);

View file

@ -60,7 +60,6 @@ class MatrixSet;
GFXDeclareVertexFormat( ScatterSkyVertex )
{
// .xyz = coords
Point3F point;
VectorF normal;
ColorF color;
@ -152,7 +151,7 @@ protected:
static const F32 smAtmosphereRadius;
static const F32 smViewerHeight;
#define CURVE_COUNT 4
#define CURVE_COUNT 5
FloatCurve mCurves[CURVE_COUNT];
@ -161,7 +160,7 @@ protected:
F32 mRayleighScattering;
F32 mRayleighScattering4PI;
F32 mSunSize;
F32 mMieScattering;
F32 mMieScattering4PI;
@ -246,6 +245,10 @@ protected:
GFXShaderConstHandle *mInverseWavelengthSC;
GFXShaderConstHandle *mNightInterpolantAndExposureSC;
GFXShaderConstHandle *mUseCubemapSC;
F32 mColorizeAmt;
ColorF mColorize;
GFXShaderConstHandle *mColorizeSC;
};
#endif // _SCATTERSKY_H_

View file

@ -55,6 +55,14 @@ struct Conn
float3 pos : TEXCOORD3;
};
float3 desaturate(const float3 color, const float desaturation)
{
const float3 gray_conv = float3 (0.30, 0.59, 0.11);
return lerp(color, dot(gray_conv , color), desaturation);
}
uniform float4 colorize;
Conn main( Vert In,
uniform float4x4 modelView : register(C0),
uniform float4 misc : register(C4),
@ -139,6 +147,16 @@ Conn main( Vert In,
Out.v3Direction = newCamPos - v3Pos.xyz;
Out.pos = In.position.xyz;
#ifdef USE_COLORIZE
Out.rayleighColor.rgb = desaturate(Out.rayleighColor.rgb, 1) * colorize.a;
Out.rayleighColor.r *= colorize.r;
Out.rayleighColor.g *= colorize.g;
Out.rayleighColor.b *= colorize.b;
#endif
return Out;
}

View file

@ -55,6 +55,14 @@ struct Conn
float3 pos : TEXCOORD3;
};
float3 desaturate(const float3 color, const float desaturation)
{
const float3 gray_conv = float3 (0.30, 0.59, 0.11);
return lerp(color, dot(gray_conv , color), desaturation);
}
uniform float4 colorize;
Conn main( Vert In,
uniform float4x4 modelView : register(C0),
uniform float4 misc : register(C4),
@ -139,6 +147,16 @@ Conn main( Vert In,
Out.v3Direction = newCamPos - v3Pos.xyz;
Out.pos = In.position.xyz;
#ifdef USE_COLORIZE
Out.rayleighColor.rgb = desaturate(Out.rayleighColor.rgb, 1) * colorize.a;
Out.rayleighColor.r *= colorize.r;
Out.rayleighColor.g *= colorize.g;
Out.rayleighColor.b *= colorize.b;
#endif
return Out;
}

View file

@ -55,6 +55,14 @@ struct Conn
float3 pos : TEXCOORD3;
};
float3 desaturate(const float3 color, const float desaturation)
{
const float3 gray_conv = float3 (0.30, 0.59, 0.11);
return lerp(color, dot(gray_conv , color), desaturation);
}
uniform float4 colorize;
Conn main( Vert In,
uniform float4x4 modelView : register(C0),
uniform float4 misc : register(C4),
@ -139,6 +147,16 @@ Conn main( Vert In,
Out.v3Direction = newCamPos - v3Pos.xyz;
Out.pos = In.position.xyz;
#ifdef USE_COLORIZE
Out.rayleighColor.rgb = desaturate(Out.rayleighColor.rgb, 1) * colorize.a;
Out.rayleighColor.r *= colorize.r;
Out.rayleighColor.g *= colorize.g;
Out.rayleighColor.b *= colorize.b;
#endif
return Out;
}

View file

@ -55,6 +55,14 @@ struct Conn
float3 pos : TEXCOORD3;
};
float3 desaturate(const float3 color, const float desaturation)
{
const float3 gray_conv = float3 (0.30, 0.59, 0.11);
return lerp(color, dot(gray_conv , color), desaturation);
}
uniform float4 colorize;
Conn main( Vert In,
uniform float4x4 modelView : register(C0),
uniform float4 misc : register(C4),
@ -139,6 +147,16 @@ Conn main( Vert In,
Out.v3Direction = newCamPos - v3Pos.xyz;
Out.pos = In.position.xyz;
#ifdef USE_COLORIZE
Out.rayleighColor.rgb = desaturate(Out.rayleighColor.rgb, 1) * colorize.a;
Out.rayleighColor.r *= colorize.r;
Out.rayleighColor.g *= colorize.g;
Out.rayleighColor.b *= colorize.b;
#endif
return Out;
}