XNA Shader Programming – Tutorial 6, Shader demo

XNA Shader Programming
Tutorial 6 – Shader demo
 
 
Welcome back to my tutorial. Today, we are not going to learn anything new( almost ), but just put together a scene that uses some different shaders in order to see how powerfull shaders can be.
 
I was not sure if I wanted to release the source code for this. The scene/code is hacked together really fast so don’t follow the source code too closely, but rather focus on the shaders. Alot can be made better performance wise, but if proves the point.
 
To move around, use the sticks on the controller. To turn shaders off or on, press A/X.
 
To run this scene, you will need the XBox360 controlles connected to the USB port, or just edit the code so you can move the camera with the keyboard instead.
 
 
The skysphere
This scene uses a simple sphere as a skysphere, slowly rotating to make the scene look alive. To make this even better, one can add more skyes on a different sphere and rotate it in a different speed to get the illution of a more real sky.
 
 
The island
The island is a 3D model created by a friend of mine, Espen Nordahl. The island use a normal map and the same shader covered in tutorial 4, to make the model look more detailed.
 
 
The ocean
The ocean is a plane built up by many vertexes. We use the technique described in tutorial 5( Deform shader ) to create the ocean waves, and using normal map from tutorial 4 to make tinier waves on top of this. I added two normal maps moving in different directions to make the small detail waves ripple on the waves:
Normal = (Normal + Normal2)/2;
 
Here I take the two normals, adding them together and using the avg. of these normals when calculating lighting, diffuse and specular( Tutorial 3 ).
 
I also move the texture coordinates on the color map, making it look like there is some stream in the water.
 
NOTE:
You might have noticed that I have not used effect.commitChanges(); in this code. If you are rendering many objects using this shader, you should add this code in the pass.Begin() part so the changed will get affected in the current pass, and not in the next pass. This should be done if you set any shader paramteres inside the pass.
 
 
 
 
 
Advertisements
This entry was posted in XNA Shader Tutorial. Bookmark the permalink.

11 Responses to XNA Shader Programming – Tutorial 6, Shader demo

  1. Steffen says:

    Hmm when i try to run this sample it doesn\’t work i\’m just stuck viewing the skybox circling around. The ocean shader doesn\’t seem to work either ( Game studio 3.0)

  2. Petri says:

    Do you have the XNA controller put into your USB port? You need to move around with the thumbsticks.And, to turn on the shader on and off, press A/X on the controller.Tell me if this solves your issues.

  3. Daniel says:

    Normals are not working on the xbox in this scene either?

  4. Petri says:

    You need to add the tangent setting in properties on the asset of the 3d models to get it working. It\’s described in tutorial 4. 🙂 Tell me if you keep having problems

  5. Giuseppe says:

    It gives me an InvalidOperationException on DrawIndexedPrimitives of the model at line 444. What\’s going wrong?

  6. Pingback: XNA Shader Programming – Tutorial 6, Shader demo | digitalerr0r « Siam Source Code – สยามซอร์สโค้ด

  7. mihay says:

    it is not working.I modified it so it responds to keyboard, because i don’t have xbox and when i turn shader on there is so mutch light that you can’t even see the color of the ocean. all it’s white

  8. jorma says:

    seconding what mihay says. the water is all white when the water shader is turned on.

  9. jorma says:

    ok, got it working by changing effectOcean.Parameters as follows:
    Vector4 vLightDirection = new Vector4(0.5f, 1.0f, 0.0f, 1.0f);
    effectOcean.Parameters[“A”].SetValue(0.5f);

  10. T0715 ink says:

    It’s an amazing piece of writing in favor of all the internet viewers; they will obtain benefit from it I am sure.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s