Ground fog.

Quick question… I want to have fog in my game that is not distance based (there should be some of that too) but based on height on the Y axis. Cliffs fading off into the great grey oblivion below and hiding the fact that they only go down 20 meters.

I don’t yet know how to go about this, but my intuition tells me I am going to need a custom shader. Also that that shader I want is probably ridiculously simple. Also that I am simpler still, when it comes to coding shaders. Anyone know how to do this/ already done it?

“Volumetric fog” is the magic word, and yes, you’ll need a custom shader (if it’s possible).

The challenge is that it’s not simply a Y position you need to take into account, but a whole range of them (from the camera’s origin to the pixel being rendered.) You could probably fake it with just one position, or by averaging or taking the minimum of the two.

The way it’s usually done (in a custom shader):

You have eye position. And you have the position of vertex (or pixel) being rendered. And you know the altitude of your fog. So you compute the length of the line that goes from eye to the vertex/pixel, that is below the fog altitude. That gives the distance that is “in fog”. From that distance, you compute the fog density (using linear, exponential or whatever fog function you want).

Doing this in a vertex shader would be faster, but for large polygons this can be inaccurate. Doing this in a pixel shader would be slower, but more accurate.

Aras that sounds very cool, and you’re essentially saying it could be done.

I’ve only just started looking at volumetric fog, and it does add a lot of realism.

Maybe something for a future unity demo?

Cheers
AaronC

This thread might be dead, but what the heck.
It sounds like the cliff doesn’t move, so you could just add a trans → gray gradient onto the texture in the editor of your choice. And what do you know, it fades into an endless gray… 20 meters. Add a bottom or gray skybox and you’re good to go.

We do this using the fixed function pipeline: encode the Y depth into the alpha channel of your lightmap.

you can do something like

SubShader {
	Blend AppSrcAdd AppDstAdd
	Fog { Color [_AddFog] }

	// Ambient pass
	Pass {
		Name "BASE"
		Tags {"LightMode" = "PixelOrNone"}
		Color [_PPLAmbient]
		BindChannels {
			Bind "Vertex", vertex
			Bind "normal", normal
			Bind "texcoord", texcoord0 // main uses 1st uv
			Bind "texcoord1", texcoord1 // lightmap uses 2nd uv
			Bind "texcoord1", texcoord2 // lightmap uses 2nd uv
		}
		SetTexture [_MainTex] {
			constantColor [_Color]
			combine texture * constant
		}
		
		SetTexture [_LightMap] {
			combine texture * previous DOUBLE
		}
		
		SetTexture [_LightMap] {
			constantColor [_GradientColor]
			combine constant lerp (texture) previous
		}
		
	}
}

Where gradientcolor is your vertical fog color.
we also still do regular fog ontop of this.

It doesn’t work for dynamic objects, but if you’re looking for the effects of things going down really far, this works just fine.

Bye, Lucas

I really want to try this out in my scene, not sure how to implament it?
Do i add it to a shader, is it java?

More necroposting! Hooray. This thread has been inactive for 3 years.#

Also, it’s Unity 2 shader code. And Unity does NOT USE JAVA.

It uses Unityscript, which in turn is JavaScript (note: NOT JAVA), just modified really for Unity.