I work on a small XNA games. I generate a large map type voxel. a floor of 1000 cubic long by 1000 cubes wide. This is why I use the method "MODEL instancing" with hardware instancing to generate a large number of identical model ..
My problem is how to add fog to the cubic field? I know it is very easy to utiler fog with the class Microsoft.Xna.Framework.Graphics "BasicEffect". unfortunately i can not use this class because of the instancing model ...
I use Microsoft.Xna.Framework.Graphics "Effect" and does not contain a property "FogEnable, FogColor etc ..."
Do you have a solution to bring me to allow me to draw a fog on my models instantiated?
Thank you so much.
Here is my code that draws models
foreach (ModelMesh mesh in model.Meshes)
{
foreach (ModelMeshPart meshPart in mesh.MeshParts)
{
// Tell the GPU to read from both the model vertex buffer plus our instanceVertexBuffer.
Game.GraphicsDevice.SetVertexBuffers(
new VertexBufferBinding(meshPart.VertexBuffer, meshPart.VertexOffset, 0),
new VertexBufferBinding(instanceVertexBuffer, 0, 1)
);
Game.GraphicsDevice.Indices = meshPart.IndexBuffer;
// Set up the instance rendering effect.
Effect effect = meshPart.Effect;
//effect.CurrentTechnique = effect.Techniques["HardwareInstancing"];
effect.Parameters["World"].SetValue(modelBones[mesh.ParentBone.Index]);
effect.Parameters["View"].SetValue(view);
effect.Parameters["Projection"].SetValue(projection);
effect.Parameters["Texture"].SetValue(texture);
// Draw all the instance copies in a single call.
foreach (EffectPass pass in effect.CurrentTechnique.Passes)
{
pass.Apply();
Game.GraphicsDevice.DrawInstancedPrimitives(PrimitiveType.TriangleList, 0, 0,
meshPart.NumVertices, meshPart.StartIndex,
meshPart.PrimitiveCount, instances.Length);
}
}
}
My error if i use the technique (effect..Parameters["FogColor"]):
Answer
You need to add fog as parameters in a gpu shader file.
For example, set various parameters in your code:
float FOGNEAR = 250.0f;
float FOGFAR = 300.0f;
effect.Parameters["FogColor"].SetValue(Color.SkyBlue.ToVector4());
effect.Parameters["FogNear"].SetValue(FOGNEAR);
effect.Parameters["FogFar"].SetValue(FOGFAR);
And then, in the effect shader file that is chosen, use them such as:
float FogNear;
float FogFar;
float4 FogColor;
float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
{
float fog = saturate((input.Distance - FogNear) / (FogNear-FogFar));
return lerp(FogColor, color, fog);
}
No comments:
Post a Comment