Solar Wind 3D Sample

This sample for Silverilght 5 uses the new 3D features to draw the Earth with day and night transitions, atmosphere layers, and population density overlays. It demonstrates advanced concepts like mipmaps, texture blending, multiple drawing passes, sampler states...

C# (10.4 MB)
 
 
 
 
 
4.4 Star
(8)
9,128 times
Add to favorites
9/12/2011
E-mail Twitter del.icio.us Digg Facebook
// ===================================================================================
//  Copyright (c) Microsoft Corporation.  All rights reserved.
//  THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY
//  OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT
//  LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
//  FITNESS FOR A PARTICULAR PURPOSE.
// ===================================================================================
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Media.Imaging;
using System.Windows.Resources;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Primitives3D;

namespace SolarWind
{
    /// <summary>
    /// Represents and draws the atmosphere layer above the Earth
    /// </summary>
    public class EarthAtmosphereLayer
    {
        /// <summary>
        /// The sphere mesh representing the Earth
        /// </summary>
        SpherePrimitive mesh;

        // Lower atmosphere shaders
        VertexShader lowerAtmosphereVertexShader;
        PixelShader lowerAtmospherePixelShader;
        PixelShader cloudsPixelShader;

        // Upper atmosphere shaders
        VertexShader upperAtmosphereVertexShader;
        PixelShader upperAtmospherePixelShader;

        // Textures
        Texture2D cloudTexture;
        Texture2D atmosphereTexture;

        public EarthAtmosphereLayer(SpherePrimitive mesh)
        {
            this.mesh = mesh;
        }

        /// <summary>
        /// Loads shaders and textures
        /// </summary>
        public void LoadContent()
        {
            //
            // Load shaders
            //
            cloudsPixelShader = ContentManager.LoadPixelShader("Shaders/Clouds_ps.ps");
            lowerAtmosphereVertexShader = ContentManager.LoadVertexShader("Shaders/AtmosphereLower_vs.vs");
            lowerAtmospherePixelShader = ContentManager.LoadPixelShader("Shaders/AtmosphereLower_ps.ps");
            upperAtmosphereVertexShader = ContentManager.LoadVertexShader("Shaders/AtmosphereUpper_vs.vs");
            upperAtmospherePixelShader = ContentManager.LoadPixelShader("Shaders/AtmosphereUpper_ps.ps");

            //
            // Load textures
            //
            cloudTexture = ContentManager.LoadTexture("Textures/Earth/EarthClouds.png", true);
            atmosphereTexture = ContentManager.LoadTexture("Textures/Earth/EarthAtmosphere.png", true);
        }

        /// <summary>
        /// Draws the atmosphere overlay. Shader constants are the same as earlier passes.
        /// </summary>
        /// <param name="device">Graphics device</param>
        public void Draw(GraphicsDevice device)
        {
            // Pass 1 - Earth (already drawn)

            //
            // Pass 2 - Clouds
            //
            device.Textures[0] = cloudTexture;
            device.SamplerStates[0] = SamplerState.AnisotropicClamp;
            device.SetVertexShader(lowerAtmosphereVertexShader);
            device.SetPixelShader(cloudsPixelShader);
            device.BlendState = BlendState.AlphaBlend;               
            mesh.Draw(device);                

            //
            // Pass 3 - Lower atmosphere
            //                
            device.Textures[0] = atmosphereTexture;
            device.SamplerStates[0] = SamplerState.AnisotropicClamp;
            device.SetPixelShader(lowerAtmospherePixelShader);
            device.BlendState = BlendState.Additive;
            mesh.Draw(device);

            //
            // Pass 4 - Upper atmosphere
            //
            device.SetVertexShader(upperAtmosphereVertexShader);
            device.SetPixelShader(upperAtmospherePixelShader);
            RasterizerState originalRasterizerState = device.RasterizerState;
            device.RasterizerState = RasterizerState.CullClockwise;
            mesh.Draw(device);
            device.RasterizerState = originalRasterizerState;
        }
    }
}