I developped a small XNA games in 3D. I would like to add a "fog of war" on my models instantiated with an effect in my (.fx) file. The problem is, I do not know how to change the function "PixelShaderFunction".

File [.fx]

Here is the declaration of my variables in my effect file (.fx):

// InstancedModel.fx
// Microsoft XNA Community Game Platform
// Copyright (C) Microsoft Corporation. All rights reserved.

// Camera settings.
float4x4 World;
float4x4 View;
float4x4 Projection;

//Fog settings
float FogNear;
float FogFar;
float4 FogColor;

And my function "PixelShaderFunction" in my effect file (.fx):

// Both techniques share this same pixel shader.
float4 PixelShaderFunction(VertexShaderOutput input) : COLOR0
    return tex2D(Sampler, input.TextureCoordinate) * input.Color;
  • \$\begingroup\$ What do you mean "fog of war"? Is it about putting fog where there are no units in a strategy game? or about 3D fog where things get fainter as they get farther from the point of view? \$\endgroup\$ – Panda Pajama Mar 14 '13 at 13:25
  • \$\begingroup\$ i think about the 3D fog where things get fainter as they get farther from the point of view \$\endgroup\$ – Mehdi Bugnard Mar 14 '13 at 13:52
  • \$\begingroup\$ You made the exact same question, and you even accepted it. If you got your question solved, why did you make a new one? gamedev.stackexchange.com/questions/50905 \$\endgroup\$ – Panda Pajama Mar 14 '13 at 13:58
  • \$\begingroup\$ Because the old answer was correct! I asked how to apply a fog of war! Now I wonder how to create a function in my file PixelToShader. fx \$\endgroup\$ – Mehdi Bugnard Mar 14 '13 at 14:08
  • \$\begingroup\$ And anyway I can not delete because someone called attention to a moderator ...super \$\endgroup\$ – Mehdi Bugnard Mar 14 '13 at 14:10

You could take a look in XNA's BasicEffect shader source and see how they have implemented Fog support there.

The source code is available here.

  • \$\begingroup\$ Yes I know it is much easier to integrate a fog with class "BasicEffect". However, for other special reasons, I have to use the class "Effect" with a file. "Fx". Because I use an importer of special model for the technique "Model Instancing".And your link does not work for info ... ^ ^ \$\endgroup\$ – Mehdi Bugnard Mar 14 '13 at 12:58
  • \$\begingroup\$ I understand that there might be numerous reasons to use your own custom shader instead of BasicEffect. The link I provided contains a downloadable zip which contains all XNA stock effect files in their raw HLSL format. I suggested that it might be a good starting point to look how they have implemented Fog effect. I cannot help other than that as I am not that familiar with Fog effect principles myself. \$\endgroup\$ – Jaanus Varus Mar 14 '13 at 13:21
  • \$\begingroup\$ Ok thank you. It's already help me a lot. However I do not see where is your link .. The link mentioned above does not work. (dead link?) \$\endgroup\$ – Mehdi Bugnard Mar 14 '13 at 13:55
  • \$\begingroup\$ I wonder why the link didn't work ... anyway, I have changed it so it points to direct download link of the sample (hope it isn't against the rules). Let me know if there are still problems. \$\endgroup\$ – Jaanus Varus Mar 14 '13 at 14:02
  • \$\begingroup\$ Super. Thank you very much! Everything works perfectly now. Vai I take a look at these data. And I come back to keep you informed ;-). \$\endgroup\$ – Mehdi Bugnard Mar 14 '13 at 14:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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