-
|
|
|
This is my first post on the forums, i've read along for a while but never got involved. But i have encountered a problem i cant solve.
Im trying to implement fog into my project. I dont really know where to start. I am using a HLSL effect file i wrote, it works fine. Well i would like to know how to implement fog in the effect file. Im using shader model 3, and i read that fog in SM 3 has to be done using shaders.
Any help would be greatly appreciated.
|
|
-
|
|
|
Not sure exacltly, but take a look at the particle 3d sample. Im sure you could adapt the smoke so its fog-like.
|
|
-
|
|
|
The particle3D sample...That thing gives me headaches. Ive tried to understand it, but i just dont get it.
I read the point sprites tutorial in the documentation, i got it to work and was able to tweak it some.
I guess i understand the particle3d sample a little but not enough to explain it to someone, or to use it in my own projects ( i dont like using code i cant explain, it doesnt help the learning process )
Im gonna keep looking over it however, probably until i get it, or until i delete it from frustration.
I think im gonna search the internet about particle systems, and emmitters.
|
|
-
|
|
|
VGMaker0507:
The particle3D sample...That thing gives me headaches. Ive tried to understand it, but i just dont get it.
I read the point sprites tutorial in the documentation, i got it to work and was able to tweak it some.
I guess i understand the particle3d sample a little but not enough to explain it to someone, or to use it in my own projects ( i dont like using code i cant explain, it doesnt help the learning process )
Im gonna keep looking over it however, probably until i get it, or until i delete it from frustration.
I think im gonna search the internet about particle systems, and emmitters.
Can you describe what it is you find confusing? A lot of folks around here can probably help you fill in whatever it is that you are missing. And sometimes I find that just trying to describe what I'm confused about to another person helps me figure it out.
Is it the concept of particles? The code of the engine itself? Point sprites?
Tom
|
|
-
|
|
|
DONT use the particle sample for fog.
You can set your shader up to use fog, add a variable to your vertex out structure called Fog with the symantic FOG like this
struct VS_OUTPUT { // Your stuff here float Fog : FOG; };
then in your vertex shader assign the value of the fog you require like this (do it how you need it, this is an example)
Output.Fog = 1.0f - ( length( Input.Position - EyePosition) / 50.0f);
then in your pass switch the fog on like this:
pass P0 { FOGENABLE = (3); FOGCOLOR = (float4(1,1,1,1)); // VShader // PShader }
Hope this helps.
|
|
-
|
|
|
Thanks Chazsoft that got fog working.
I noticed if i turn the fog color to black,it acutally makes the scene fade really good.
Besides that,
The fog sorta sticks to the ground, is there any way i can get it to hover above it.
|
|
-
|
|
|
Output.Fog only works in shader model 2, in general.
In shader model 3, you want to calculate the distance from the camera of the pixel, and then map that distance to a value between 0 and 1 using a fog function, and then lerp between the calculated pixel color (with texturing, lighting, etc), and the fog color, using that calculated value.
[source] // a simple linear-style fog function float fogNear : FOGNEAR; float fogFar : FOGFAR; float4 fogColor : FOGCOLOR; float3 cameraPos : CAMERAPOS;
// fragmentPos can be forwarded as an interpoland from the vertex shader float4 MyPixelShader(... float3 fragmentPos ...) { ... float d = length(fragmentPos - cameraPos); float l = saturate((d-fogNear)/(fogFar-fogNear)); return lerp(outColor, fogColor, l); } [/source]
This will fog everything in an even manner, based on distance from camera. You can make altitude-based fog by adjusting the fog factor based on elevation above ground through this little tweak:
[source] float fogAltitudeScale : FOGALTITUDESCALE = 20; ... // assuming fragmentPos.z is "altitude" float l = saturate((d - fogNear)/(fogFar - fogNear) / clamp(fragmentPos.z / FOGALTITUDESCALE + 1, 1, 10)); [/source]
You might want to change the "+1" and "10" values of that equation to tunable parameters, too; the former controls how high up the fog will start thinning out, and the 10 controls the maximum amount of thinning.
Jon Watte, Direct3D MVP
kW X-port 3ds Max .X exporter
kW Animation source code
|
|
-
|
|
|
Cool!
Jwatte, Can you apply the FOGALTITUDESCALE to SM2 as well??
|
|
-
|
|
|
You can calculate fog manually (which is what the example does) in any shader model as long as your calculations fit into the capabilities of the shader model. The semantic FOGALTITUDESCALE that Jon used in his example is just a way of identifying the shader parameter to his code - the parameters are being identified by semantic rather than by name. He could have called it JONSWKDFOGFACTOR or anything else. Unlike vertex component semantics (POSITION, TEXCOORD, etc.) which must be recognised by the graphics API, the semantics on shader parameters are entirely for your own use and don't mean anything to the graphics API.
So, yes that example should work in SM2 :)
Cheers, Leaf.
|
|
-
|
|
|
jwatte, do you have any good link or recommendation of a book where I can learn these things? it seems shaders just get deeper and deeper. lol.
|
|
-
|
|
|
Leaf,
Ahh, of course it is silly me :) I really should read things properly...
Have sent you a mail regarding this method, thanks for your help. Thanks for your comment on the blog too :)
|
|
-
|
|
|
On the thread of doing fog in the shader for SM3, can any of you guys give an example, it's just that I/most of us are new too all this and just telling us how to go about doing something does not cut it every time (probably just me here), and an example would be great, even if it is just for a simple Texture shader with the SM3 fog logic in it.
Thanks.
|
|
-
|