Game Development Community

dev|Pro Game Development Curriculum

Scatter Sky Colorization

by Tim Dix (Raverix) · 08/08/2010 (5:40 pm) · 9 comments

img412.imageshack.us/img412/7703/screenshot00200000.jpg

Overview

This resource modifies the Scatter Sky to have more color rendering options. The changes are 100% backwards compatible, and offer a great deal more flexibility with sky color options.

I created it because I have a bunch of sky boxes which are basically the same as scatter sky, just in various other hues. Now, I can drop all of my skyboxes for this.

Alright, the real core of this change is a very simple modification to the scatterSkyV.hlsl. It's a bit simplistic, but it gets the job done for me. Feel free to expand to fit your needs. First, it desaturates the existing ScatterSky, and then it colorizes it based on the color you specify.

Code

scatterSkyV.hlsl

Add the following above the function main:
//Raverix - SSC -->
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;
// <-- Raverix - SSC

Add the following before return out;
//Raverix - SSC -->
#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
 // <-- Raverix - SSC

The rest of the code just handles passing colorize to the shader.

scatterSky.h

Place this after all of the GFXShaderConstHandle declarations:
//Raverix - SSC -->
   F32 mColorizeAmt;
   ColorF mColorize;

   GFXShaderConstHandle *mColorizeSC;
   // <-- Raverix - SSC

scatterSky.cpp

ScatterSky::ScatterSky() - Add the following anywhere:
//Raverix - SSC -->
   mColorizeAmt = 0;
   mColorize.set(0,0,0);
   // <-- Raverix - SSC

ScatterSky::initPersistFields() - I chose to add the following fields within the ScatterSky group, but it could really go anywhere:
//Raverix - SSC -->
	  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." );
	  // <-- Raverix - SSC

ScatterSky::packUpdate() - Find stream->write( mFogScale ); and add below:
//Raverix - SSC -->
      stream->write( mColorizeAmt );
	   stream->write( mColorize );
      // <-- Raverix - SSC

ScatterSky::unpackUpdate() - find stream->read( &mFogScale );
and add below:
//Raverix - SSC -->
      F32 colorizeAmt;
      stream->read( &colorizeAmt );

      if(mColorizeAmt != colorizeAmt) {
         mColorizeAmt = colorizeAmt;
         mShader = NULL; //forces shader refresh
      }

      stream->read( &mColorize );
      // <-- Raverix - SSC

ScatterSky::_initShader() - comment out mShader = shaderData->getShader();, and add below:
//Raverix - SSC -->
   Vector<GFXShaderMacro> macros;
   if ( mColorizeAmt )   
      macros.push_back( GFXShaderMacro( "USE_COLORIZE" ) );

   mShader = shaderData->getShader( macros );
   // <-- Raverix - SSC

ScatterSky::_initShader() - Add before the function returns:
//Raverix - SSC -->
   mColorizeSC = mShader->getShaderConstHandle( "$colorize" );
   // <-- Raverix - SSC

ScatterSky::_render() - Add after the rest of the mShaderConsts->setSafe calls:
//Raverix - SSC -->
   mShaderConsts->setSafe( mColorizeSC, mColorize*mColorizeAmt );
   // <-- Raverix - SSC

Usage

Obviously, you need to have a ScatterSky in your level...

  • colorizeAmt - Controls how much the the alpha component of colorize brigthens the sky. Setting to 0 returns default behavior.
  • colorize - Tints the sky the color specified, the alpha controls the brigthness. The brightness is multipled by the value of colorizeAmt.

I've found that I leave colorizeAmt at 10, and then use the alpha component of the colorize the sky and get me all the different color variations.


Results

img22.imageshack.us/img22/4357/screenshot00100001.jpgimg819.imageshack.us/img819/4114/screenshot00100002.jpgimg208.imageshack.us/img208/6060/screenshot00100003.jpgimg718.imageshack.us/img718/6395/screenshot00100004.jpg

img265.imageshack.us/img265/214/screenshot00300000.jpgimg408.imageshack.us/img408/6154/screenshot00300001.jpgimg231.imageshack.us/img231/2859/screenshot00300002.jpgimg189.imageshack.us/img189/4027/screenshot00300003.jpgimg840.imageshack.us/img840/1843/screenshot00300004.jpgimg69.imageshack.us/img69/710/screenshot00300005.jpg




Enjoy!

tp.cantanogames.com/counter.php?Scatter_Sky_Colorization

#1
08/08/2010 (6:55 pm)
Nice trick... I like it a lot. Thanks!

EDIT: beware giving the colorblind guy color controls :D
#2
08/08/2010 (8:43 pm)
Wow, that's some colours! Be good for alien worlds and stuff. :)
#3
08/08/2010 (9:08 pm)
Pssh, blue skies are so overdone.
#4
08/09/2010 (6:28 pm)
nice code ty for share:)
#5
08/10/2010 (10:17 pm)
Very nice job
#6
08/10/2010 (10:21 pm)
What a great resource.
#7
08/11/2010 (5:56 am)
I really liked the color choices there. I kind of feel like someone that is an artist at heart picked them out.
#8
02/26/2011 (1:07 am)
Wow! This is fantastic! Thank you!
#9
01/10/2013 (7:15 pm)
changing sky color does not affect other environment's color.
specially sunscale and ambient scale.
to make it realistic, level designer have to adjust sunscale and ambient scale too.that is ok for one time adjustment.

but if any programmer try to utilize it to create a dynamic environment from script he have no way to utilize it to create a realistic environment without looking into visual output.

so i was thinking if anyway this color changing also will automatically adjust sunscale and ambient scale to make it more realistic for script side work.

i know current implementation gives more control.
so a checkbox with title "adjust with sun and ambient scale" will make them both exists.
no shader knowledge.so no idea how to make it happen