Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.
                    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"];

                // Draw all the instance copies in a single call.
                foreach (EffectPass pass in effect.CurrentTechnique.Passes)

                    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"]): enter image description here

share|improve this question

closed as too localized by Byte56, Sean Middleditch, bummzack, API-Beast, Anko Apr 7 '13 at 16:30

This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.If this question can be reworded to fit the rules in the help center, please edit the question.

up vote 2 down vote accepted

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;


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);
share|improve this answer
Sorry. Now I encounter an error using your example .. Have you any idea? I added a picture in my description of the error – Mehdi Bugnard Mar 12 '13 at 16:16
My "meshPart.Effect" contains only 7 effects of parameter collection. FogColor, ForNear, Forfar does not exist .... sniff – Mehdi Bugnard Mar 12 '13 at 16:22
You need to add them into your shader file. Whats happening is that you are telling the effect to set particular parameters that will be found inside the corresponding effect shader file. – Jason Coombes Mar 13 '13 at 6:14
You would load an effect file usually in the LoadContent method, something like this: Effect myEffect = Content.Load<Effect>("Shaders/SampleShader"); The file would be named "SampleShader.fx" if you browse to that folder – Jason Coombes Mar 13 '13 at 7:46
Attempting to answer would put this out of context as it becomes specific to you. – Jason Coombes Mar 13 '13 at 22:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.