This code example shows how to add fog to the environment. There are 3 different types of fog, Linear, Squared and Exponential. The fog is initially set to be Linear, however, in the example you can change the fog type to Squared by pressing the S key, Exponential by pressing the E key or Linear by pressing the L key. The example also shows how to use a keyboard listener to detect the E, S and L keys being pressed. All the code and assets are in the projects Scripts directory. You can download the project using the link above.
// Turn on debug so that Chat.printDebug() and Chat.dump() work
Chat.debugOutput = true;
// Display the stats panel in the bottom left corner of screen
Application.statsVisible = true;
// Turn on collisions, but allow the user to turn it off
Actor.setCollisionSetting( true, false );
// Turn on gravity, but allow the user to turn it off
Actor.setGravitySetting( true, false );
// Hide the chat panel
Application.chatPaneVisible = true;
// Set ambient light for the scene
Scene.ambientLightColor = Color(0.5, 0.5, 0.5, 0);
// Create a point light
var oPointLight = Scene.createPointLight( "SceneLight" );
oPointLight.position = Vector( 0, 600, -500 );
// Create a Skydome
var oSkyDome = Scene.createSkyDome();
oSkyDome.materialName = "CloudySky";
oSkyDome.curvature = 5;
oSkyDome.tiling = 8;
oSkyDome.distance = 4000;
oSkyDome.enable = true;
// Create the floor
var oGround = Scene.createGround( "MainFloor", "RustySteel", 3000.0, 3000.0, true );
// Add the Ogre head
oHeadEntity = Scene.createEntity( "OgreHead", "ogrehead.mesh" );
oHeadNode = Scene.createSceneNode( "OgreHeadNode" );
oHeadNode.attachObject( oHeadEntity );
oHeadNode.position = Vector( 0, 175, -500 );
// Display instructions in chat window
Chat.print( "Press E for Exponential fog" );
Chat.print( "Press S for Squared fog" );
Chat.print( "Press L for Linear fog" );
Chat.print( "Defaulting to Fog Linear mode" );
// Create the fog
Fog.dropOff = Fog.LINEAR;
Fog.near = 450;
Fog.far = 1000;
Fog.color = Color( 0.8, 0.8, 1, 1 );
Fog.active = true;
// Create a keyboard listener
this.oKeyListener = new Object();
this.oKeyListener.onKeyDown = function()
{
// If the E key is pressed
if ( Key.isKeyDown( Key.KEY_E ) )
{
Chat.print( "Switching to Fog Exponential mode" );
Fog.dropOff = Fog.EXPONENTIAL;
Fog.density = 0.002;
}
// If the S key is pressed
if ( Key.isKeyDown( Key.KEY_S ) )
{
Chat.print( "Switching to Fog Squared mode" );
Fog.dropOff = Fog.SQUARED;
Fog.density = 0.002;
}
// If the L key is pressed
if ( Key.isKeyDown( Key.KEY_L ) )
{
Chat.print( "Switching to Fog Linear mode" );
Fog.dropOff = Fog.LINEAR;
Fog.near = 450;
Fog.far = 1000;
}
}
// Add the key listener object
Key.addListener( this.oKeyListener );