public void SetSkyBox( bool enable,
string materialName,
float distance,
bool drawFirst,
Quaternion orientation,
string groupName )
{
// enable the skybox?
this.isSkyBoxEnabled = enable;
if ( enable )
{
Material m = (Material)MaterialManager.Instance[ materialName ];
if ( m == null )
{
this.isSkyBoxEnabled = false;
throw new AxiomException( string.Format( "Could not find skybox material '{0}'", materialName ) );
}
// Make sure the material doesn't update the depth buffer
m.DepthWrite = false;
// Ensure loaded
m.Load();
// ensure texture clamping to reduce fuzzy edges when using filtering
m.GetTechnique( 0 ).GetPass( 0 ).GetTextureUnitState( 0 ).SetTextureAddressingMode( TextureAddressing.Clamp );
this.isSkyBoxDrawnFirst = drawFirst;
if ( this.skyBoxNode == null )
{
this.skyBoxNode = this.CreateSceneNode( "SkyBoxNode" );
}
else
{
this.skyBoxNode.DetachAllObjects();
}
// need to create 6 plane entities for each side of the skybox
for ( int i = 0; i < 6; i++ )
{
Mesh planeModel = this.CreateSkyboxPlane( (BoxPlane)i, distance, orientation, groupName );
string entityName = "SkyBoxPlane" + i;
if ( this.skyBoxEntities[ i ] != null )
{
this.RemoveEntity( this.skyBoxEntities[ i ] );
}
// create an entity for this plane
this.skyBoxEntities[ i ] = CreateEntity( entityName, planeModel.Name );
// skyboxes need not cast shadows
this.skyBoxEntities[ i ].CastShadows = false;
// Have to create 6 materials, one for each frame
// Used to use combined material but now we're using queue we can't split to change frame
// This doesn't use much memory because textures aren't duplicated
Material boxMaterial = (Material)MaterialManager.Instance[ entityName ];
if ( boxMaterial == null )
{
// Create new by clone
boxMaterial = m.Clone( entityName );
boxMaterial.Load();
}
else
{
// Copy over existing
//must not copy over the name, otherwise the sky box entity will be set to use the origional m material instead of the copy for each entity which each uses a different frame
m.CopyTo( boxMaterial, false );
boxMaterial.Load();
}
// set the current frame
boxMaterial.GetTechnique( 0 ).GetPass( 0 ).GetTextureUnitState( 0 ).CurrentFrame = i;
this.skyBoxEntities[ i ].MaterialName = boxMaterial.Name;
// Attach to node
this.skyBoxNode.AttachObject( this.skyBoxEntities[ i ] );
} // for each plane
}
}