-2
\$\begingroup\$

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

\$\endgroup\$

1 Answer 1

2
\$\begingroup\$

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);
}
\$\endgroup\$
8
  • \$\begingroup\$ Sorry. Now I encounter an error using your example .. Have you any idea? I added a picture in my description of the error \$\endgroup\$ Mar 12, 2013 at 16:16
  • \$\begingroup\$ My "meshPart.Effect" contains only 7 effects of parameter collection. FogColor, ForNear, Forfar does not exist .... sniff \$\endgroup\$ Mar 12, 2013 at 16:22
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ Mar 13, 2013 at 6:14
  • 1
    \$\begingroup\$ 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 \$\endgroup\$ Mar 13, 2013 at 7:46
  • 1
    \$\begingroup\$ Attempting to answer would put this out of context as it becomes specific to you. \$\endgroup\$ Mar 13, 2013 at 22:29

Not the answer you're looking for? Browse other questions tagged .