I can't get fog to work on Windows Phone

I read around that fog doesn’t work on Windows phone and to use the shader here: http://docs.unity3d.com/Documentation/Manual/windowsstore-gettingstarted.html

I have tried that and still can’t get the fog to work.

Has anyone else gotten fog to work? Without it my underwater game looks horrible without the blue “watery” fog.

I wrote a a blog post about how to create a simple custom shader with linear or exponential fog for WP8.

I’m using it in the game Mombari - Microsoft Apps

Stephan

I saw that post while searching but I can’t seem to get it to work for me, which is probably my fault.

I created a shader by right clicking → create shader → copy pasted your final shader in and then created a new material and attached that shader to it.

Now I’m not sure what to do with it…in my game I have fog starting at a certain distance so the fog is in the background and I have to change the distance in code depending on the camera selected by the player. I don’t see what I have to attach this shader to do to get environmental fog.

Here is my game on blackberry I’m trying to port over: https://appworld.blackberry.com/webstore/content/35073888/?lang=en&countrycode=US

If you look at the images (2nd and 3rd) you will see what I am trying to do fog wise on windows phone.

Thank you.

Every GameObject that should be affected by the fog needs that shader/material (or a similar one) applied. Be sure to use the fog settings in the Unity editor (Edit/Render Settings).

I could create a small test project for you the next days if that would be helpful for you.

Stephan

Yes, that would be lovely if you don’t mind.

I have shaders from a toon pack on the assest store applied to all my fish so they look cel shaded. If I replace that shader with the fog one I would lose the cel shadedness. I think that is why I’m confused. I feel like I need the air to be foggy.

Ah, ok. So fog is already shown if you apply the custom fog shader to your fishes? But without the cel shading effect.

Unfortunately it works differently.

All the GameObjects need to be rendered with some custom fog shader code. For each vertex the distance to the camera is calculated and based that the amount of fog color to apply is determined.

In your case you would need to add the custom fog handling to the toon shader code.

Stephan

I don’t believe mine is working when I apply it to the fish.

When you get a chance if you could create the test scene like you mentioned that would be helpful.

Thanks.

Just added a download link for the test project demonstrating fog on WP8 to my blog post:

Nothing fancy, only a small test scene.

Let me know if it works on your device.

Stephan

When I open the project in visual studio, the project is empty.

Nope, it is not empty.

I made a quick screencast how to download and install the project on WP8:
http://www.software7.com/wp-content/uploads/tmp/fogtestinstall/index.html

Hope it helps.

Stephan

http://www.software7.com

Ah, I was opening the sln file with visual studio 2013 and nothing was coming up. I didn’t realize it was a unity project that needed to be compiled.

I ran it and the fog showed up. I’ll use this example and see if I can use it in my scenario.

Thank you.

Hi Stephan,

I am trying to use your customer fog shader on Windows Phone 8. However my scene has trees in it and I don’t know how to make your shader work with transparent items. Could you help?

Thanks
Mark

You can use the recipe from my blog post also for other shaders.

It’s often helpful to start by modifying the Unity standard shaders. You can download the sources for the built-in shaders from here:
https://unity3d.com/unity/download/archive

There are different transparent shaders.

Example:
If you would like to modify the “Transparent/Cutout/Diffuse” shader you would copy the file ‘AlphaTest-Diffuse.shader’ from the above mentioned list of build-in-shaders with a different name into your project.

By applying the recipe from my blog post the resulting shader would look like shown below.

And voilà: you now have a transparent cutout shader with custom fog :slight_smile:

Shader "Custom/FogCutout" {
Properties {
	_Color ("Main Color", Color) = (1,1,1,1)
	_MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
	_Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
}

SubShader {
	Tags {"Queue"="AlphaTest" "IgnoreProjector"="True" "RenderType"="TransparentCutout"}
	Fog {Mode Off}
	LOD 200
	
CGPROGRAM
#pragma surface surf Lambert alphatest:_Cutoff vertex:fogVertex finalcolor:fogColor

	sampler2D _MainTex;
	fixed4 _Color;

	uniform half4 unity_FogColor;
	uniform half4 unity_FogStart;
	uniform half4 unity_FogEnd;
	uniform half4 unity_FogDensity;

	struct Input {
		float2 uv_MainTex;
		half fogFactor;
	};

	void fogVertex(inout appdata_full v, out Input data) 
	{
		UNITY_INITIALIZE_OUTPUT(Input, data);
		float cameraVertDist = length(mul(UNITY_MATRIX_MV, v.vertex).xyz);
		float f = cameraVertDist * unity_FogDensity;
		data.fogFactor = saturate(1 / pow(2.71828,  f * f));
	}

	void fogColor(Input IN, SurfaceOutput o, inout fixed4 color)
	{
		color.rgb = lerp(unity_FogColor.rgb, color.rgb, IN.fogFactor);
	}

	void surf (Input IN, inout SurfaceOutput o) {
		fixed4 c = tex2D(_MainTex, IN.uv_MainTex) * _Color;
		o.Albedo = c.rgb;
		o.Alpha = c.a;
	}
	ENDCG
}

Fallback "Transparent/Cutout/VertexLit"
}

Thank you!! I was somehow getting the alpha part all messed up and over engineered, yet it was really simple :-).

I now have trees that work with fog on WP!