|
I wrote an artcile about rendering light map in XNA in my blog as below:
http://tw.myblog.yahoo.com/akira32-akira32/article?mid=290&prev=-1&next=287
Complete source code and max file in the web: http://cid-fbeb6373d9321a7f.skydrive.live.com/self.aspx/BlogYahoo/XNA/LightMapWindows%7C_.rar
Part1: Create 3D Models and their light map and render them with shader
Step1: (file: Content\step1,original material.max)
Add Box and Cylinder
Assign texture to them
Add directional light (Target Directional and enlarge the Hotspot/Beam)
shadows [V] on
"Ray Traced Shadows" or "Shadow Map"
Directinal Parameter:
use proper Hotspot/Beam
Step2: (file: Content\step2,render t texture.max)
Selected Box and Cylinder
Rendering>Render to Texture
Output Page
Add,LightingMap<=Add Elements
Taregt Map Slot: Self-Illumination
Backed Material Page
Output Into Source[V]
Objects to Bake Page
Mapping Coordinates Block
Object==>Use Automomatic Unwrap
Channel=2
Render<===click
Delete the light
Step3: (file: Content\step3,use shader.max)
Change Mterial to be Direct Shader and "Keep old material as sub-material"
Select the Shader of "Content\LightMapEffect.fx"
Assign the Diffuse Map and its Light Map and Select the "Technique1"
At Last,you can export the model by Panda plugin(http://www.andytather.co.uk/Panda/directxmax.aspx), the second uv coordinate will be wrote into FVFData block in XFile. Remember must enable the "Include .fx file" and "Include .fx parameters".
PS:
If you want to see the light map in 3ds max with Shader,you must use two optional steps as below:
Optional Step:
Materail Editor
Show Hardware Maps in Viewport
Optional Step:
Material
DirectX Manager
[V] Enable Plugin Material==>Light Map
DirectX Shader-LightMap
BasTexture: RedB.jpg [V]
Mapping channel=1
light map: cylinder01lightingmap.tga [V]
Mapping channel=2
Part2: LightMapWindows,
Step1:
Load model and measure its size
Add camera
namespace LightMapWindows
{
/// <summary>
/// This is the main type for your game
/// </summary>
public class LightMap : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
//add
ContentManager content;
BasicModel basicModel;
Camera camera;
public LightMap()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
//
content = new ContentManager(Services);
content.RootDirectory = "Content";
IsMouseVisible = true;
}
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
// TODO: use this.Content to load your game content here
camera = new Camera(this);
Components.Add(camera);
basicModel = new BasicModel();
basicModel.LoadModel(content, @"pillar");
basicModel.MeasureModel();
camera.SetSpeed(basicModel.GetModelRadius() / 100.0f);
float modelRadius = basicModel.GetModelRadius();
Vector3 modelCenter = basicModel.GetModelCenter();
Vector3 eyePosition = modelCenter;
eyePosition.Z += modelRadius * 2;
eyePosition.Y += modelRadius;
float aspectRatio = (float)Window.ClientBounds.Width / (float)Window.ClientBounds.Height;
float nearClip = modelRadius / 100;
float farClip = modelRadius * 100;
camera.SetCamera(eyePosition, modelCenter, Vector3.Up);
camera.SetFOV(MathHelper.PiOver4, aspectRatio, nearClip, farClip);
}
Step2: Render model with its effect instance
protected override void Draw(GameTime gameTime)
{
GraphicsDevice.Clear(Color.CornflowerBlue);
// TODO: Add your drawing code here
basicModel.DrawLightMap(camera);
//basicModel.Draw(camera);//basic effect
base.Draw(gameTime);
}
namespace AkBasicModel
{
class BasicModel
{
......
public void DrawLightMap(Camera camera)
{
//Set transfroms
Matrix[] transforms = new Matrix[m_model.Bones.Count];
m_model.CopyAbsoluteBoneTransformsTo(transforms);
//Loop through meshes and their effects
foreach (ModelMesh mesh in m_model.Meshes)
{
foreach (Effect effect in mesh.Effects)
{
//Set BasicEffect information
effect.CurrentTechnique = effect.Techniques["Technique1"];
effect.Begin();
effect.Parameters["World"].SetValue(GetWorld() * transforms[mesh.ParentBone.Index]);
effect.Parameters["View"].SetValue(camera.view);
effect.Parameters[" rojection"].SetValue(camera.projection);
effect.End();
}
//Draw
mesh.Draw();
}
} |
|