We're all one little world... as shown with the Silverlight 5 3D sample, Solar Wind
- Posted: Jun 01, 2011 at 6:00 AM
- 7,639 Views
- 4 Comments
Loading User Information from Channel 9
Something went wrong getting user information from Channel 9
Loading User Information from MSDN
Something went wrong getting user information from MSDN
Loading Visual Studio Achievements
Something went wrong getting the Visual Studio Achievements
What I liked about this sample was that it was 'simple,' focusing on one primary thing and yet was pretty cool looking. And there's just something about seeing our little ball of dirt and rock from space that appeals to the frustrated space voyager in me. [Insert quip, "Look! You can see my house from here!" ]
This sample for Silverlight 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, and lighting.
Note the "Building the Sample" section (which of course I didn't until I tried to run it the first time... Note to self: RTFP [Read The Fine Post]... ![]()
Building the Sample
To compile the solution you will need to install the DirectX SDK and the HLSL Shader Build Task. Afterwards you can open the solution in Visual Studio to build.
Here's another shot, showing off some of the other capabilities, like the rotation and Magnetic Field overlay
The solution is pretty straight forward, with more files for the shaders and textures than for code...
Here's the Earth class;
public Earth()
{
Transform = Matrix.CreateWorld(new Vector3(), Vector3.Forward, Vector3.Up);
AtmosphereVisible = true;
MagneticFieldVisible = false;
PopulationDensityVisible = false;
ShowWireframe = false;
}
Now lets load the shaders and textures.
void LoadEarthContent()
{
// Load mesh
mesh = new SpherePrimitive(10.0f, 50);
//
// Load shaders
//
earthVertexShader = ContentManager.LoadVertexShader("Shaders/Earth_vs.vs");
earthPixelShader = ContentManager.LoadPixelShader("Shaders/Earth_ps.ps");//
// Load textures
//
dayTexture = ContentManager.LoadTexture("Textures/Earth/EarthDay.png", true);
nightTexture = ContentManager.LoadTexture("Textures/Earth/EarthNight.png", true);
nightLightsTexture = ContentManager.LoadTexture("Textures/Earth/EarthNightLights.png", true);
normalTexture = ContentManager.LoadTexture("Textures/Earth/EarthNormal.png", true);
maskTexture = ContentManager.LoadTexture("Textures/Earth/EarthMask.png", true);}
Here's a snap of those textures;
Drawing the world...
public void Draw(GraphicsDevice device, SceneTime time, Camera camera)
{
// All Earth pass shaders use the same constants so update them each frame
UpdateEarthShaderConstants(device, time, camera);
//
// Pass 1 - Draw Earth
//
// Set textures in order of samplers
device.Textures[0] = dayTexture;
device.Textures[1] = nightTexture;
device.Textures[2] = nightLightsTexture;
device.Textures[3] = normalTexture;
device.Textures[4] = maskTexture;
// Set states to control sampling
device.SamplerStates[0] = SamplerState.AnisotropicClamp;
device.SamplerStates[1] = SamplerState.AnisotropicClamp;
device.SamplerStates[2] = SamplerState.AnisotropicClamp;
device.SamplerStates[3] = SamplerState.AnisotropicClamp;
device.SamplerStates[4] = SamplerState.AnisotropicClamp;
// Set device states
device.BlendState = BlendState.Opaque;
device.RasterizerState = rasterizerState;
// Configure shading pipeline
device.SetVertexShader(earthVertexShader);
device.SetPixelShader(earthPixelShader);
// Draw the main earth pass
mesh.Draw(device);
// Draw atmosphere layer
if (AtmosphereVisible)
atmosphereLayer.Draw(device);
// Draw population layer
if (PopulationDensityVisible)
populationLayer.Draw(device);
// Draw magnetic field
if (MagneticFieldVisible)
field.Draw(device, time, camera);
}
And finally updating it as the world rotates.
void UpdateEarthShaderConstants(GraphicsDevice device, SceneTime time, Camera camera)
{
// Get the inverse transpose matrix
Matrix WorldInverseTransposeMatrix = Matrix.Invert(Transform);
WorldInverseTransposeMatrix = Matrix.Transpose(WorldInverseTransposeMatrix);
// Set the primary vertex constants
earthConstants.WorldMatrix = Transform;
earthConstants.WorldInverseTransposeMatrix = Matrix.Transpose(Matrix.Invert(Transform));
earthConstants.WorldViewProjectionMatrix = Transform * camera.ViewTransform * camera.ProjectionTransform;
earthConstants.ViewInverseMatrix = Matrix.Invert(camera.ViewTransform);
earthConstants.TotalSeconds.X = (float)time.TotalTime.TotalSeconds;
device.SetVertexShaderConstantFloat4(0, ref earthConstants);
// Set the pixel shader constants (to achieve solid lighting in wireframe mode)
if(ShowWireframe)
device.SetPixelShaderConstantFloat4(0, ref showWireframeValue);
else
device.SetPixelShaderConstantFloat4(0, ref hideWireframeValue);
}
If you're looking at Silverlight 5's 3D capabilities, then this, or other samples from the MIX11 Silverlight 3D session, should be worth a peek...
Here’s a few more links you might find interesting:
Page thumbnail, Day 432 / 365 - It's a small world, curtsey of xJasonRogersx
Comments have been closed since this content was published more than 30 days ago, but if you'd like to continue the conversation,
please create a new thread in our Forums,
or
Contact Us and let us know.
Follow the Discussion
Oops, something didn't work.
What does this mean?
Following an item on Channel 9 allows you to watch for new content and comments that you are interested in. You need to be signed in to Channel 9 to use this feature.What does this mean?
Following an item on Channel 9 allows you to watch for new content and comments that you are interested in and view them all on your notifications page.sign up for email notifications?
I have a question re Silverlight 3D, i hope maybe you could help. Is it relateded to DirectX? and more importantly, is it supported on the Mac?
Thx
@fanbaby: Silverlight is cross platform so yes to supported on the mac. For DirectX, what do you mean?
@Clint: I meant: is 3d supported on the Mac. I think the answer is not right now, and I wanted more info about this, hence the question about directx
great demo! 3ku
Remove this comment
Remove this thread
close