Tasharen Entertainment Forum

Support => Other Packages => Topic started by: Anselm on December 16, 2013, 08:32:31 PM

Title: [Fog of War] Make fog not overlay in clear areas
Post by: Anselm on December 16, 2013, 08:32:31 PM
I've been using Tasharen FOW and been liking it a lot. Currently I'm running in to a bit of an issue though. Fog cuts through objects in circumstances when I'd like the object to draw over it.

Here's an image of an un-revealed area, working fine:
(http://i44.tinypic.com/4j5dsp.png)

And here's an image of a revealed area, yet again this is how I'd like it to appear from above:
(http://i44.tinypic.com/2mxx51.png)

However, here's the issue. When I zoom in to the planet, you can see that the fog unrealistically cuts through the planet, even though the area the camera is in is technically clear of fog.
(http://i41.tinypic.com/2hheqza.png)

Is there any way to fix this? Or any adjustment I could make to the shader to help this? I realize this isn't a glitch with the system, I'm just trying to figure out how I could adjust the system to work for my needs.
I'm not exactly sure how I could procedurally tell when to draw the fog over the planet, and when not to.
Title: Re: [Fog of War] Make fog not overlay in clear areas
Post by: ArenMook on December 17, 2013, 12:36:43 PM
By default, FoW system simply uses a post-process step to "fog" your geometry. This involves determining whether each pixel on the screen is covered or not by calculating its world-space position from screen coordinates and depth.

The key here, is writing to depth. If the shader doesn't write to depth (such as any transparent shader), then the correct position of the pixel cannot be determined. My guess is that's exactly what happens in your case.

So to fix it, you need to either use a shader that writes to depth, or have a second pass do a depth write after your planet has been drawn.
Title: Re: [Fog of War] Make fog not overlay in clear areas
Post by: Anselm on December 17, 2013, 02:46:06 PM
So I'd either need to set the planet (and any other shaders for objects I want to not have that error on them) to write to depth? Or have a second pass in each shader that writes to depth?
Title: Re: [Fog of War] Make fog not overlay in clear areas
Post by: Anselm on December 18, 2013, 11:57:24 AM
Hmm, I have ZWrite set to on in my planetary shader. Would adding in a second pass have a different effect?

The exact settings are:
Cull Back
ZWrite On
ZTest LEqual
ColorMask RGBA
Title: Re: [Fog of War] Make fog not overlay in clear areas
Post by: ArenMook on December 18, 2013, 01:05:01 PM
Turning on ZWrite may not work, depending on the shader. If you're using "Transparent" queues, there won't be any depth writes.

I suggest adding a second pass to the shader.
Title: Re: [Fog of War] Make fog not overlay in clear areas
Post by: Anselm on December 18, 2013, 01:25:51 PM
Alright, I haven't done a ton with shaders, what would I need too add to the second pass in order to make it write correctly?
Title: Re: [Fog of War] Make fog not overlay in clear areas
Post by: ArenMook on December 18, 2013, 05:58:03 PM
http://docs.unity3d.com/Documentation/Components/SL-Pass.html
http://docs.unity3d.com/Documentation/Components/SL-SubshaderTags.html

but more importantly: http://docs.unity3d.com/Documentation/Components/SL-CullAndDepth.html

The last link shows several multi-pass shaders.
Title: Re: [Fog of War] Make fog not overlay in clear areas
Post by: Anselm on December 18, 2013, 10:10:35 PM
So I'd create a second pass that writes to geometry or some such with ZWrite on?
Title: Re: [Fog of War] Make fog not overlay in clear areas
Post by: ArenMook on December 19, 2013, 12:36:48 PM
Yes. Same geometry, just an additional pass that writes to depth.
Title: Re: [Fog of War] Make fog not overlay in clear areas
Post by: Anselm on December 20, 2013, 03:00:10 PM
So I can't seem to get anything working. I have the fog rendering on a second camera that writes to a depth higher than the planet camera, could this be causing it? The fog doesn't draw at all when I put the script on the same camera as the planet.
Title: Re: [Fog of War] Make fog not overlay in clear areas
Post by: Anselm on December 20, 2013, 03:04:22 PM
Actually I just confirmed it is a problem with the second camera. The only problem is I need the second camera for 3DGUI, and the fog doesn't draw at all when I put it on the lower depth camera
Title: Re: [Fog of War] Make fog not overlay in clear areas
Post by: ArenMook on December 21, 2013, 02:54:47 AM
The fog needs to be on the camera that draws the objects -- so your game camera. UI camera is completely separate, and should be drawn after your game camera.
Title: Re: [Fog of War] Make fog not overlay in clear areas
Post by: Anselm on December 22, 2013, 11:13:15 AM
The fog doesn't show at all when it's not on the UI camera. That's my main trouble right now.

The Main camera is perspective Skybox with depth -1. The UI camera is perspective depth only with depth 0. It only renders the UI elements.
Title: Re: [Fog of War] Make fog not overlay in clear areas
Post by: ArenMook on December 22, 2013, 12:33:08 PM
That makes no sense to me. The fog should always be on the game camera, not on the UI camera. I'd be surprised if it worked at all while on the UI camera. Try adding a UI to the built-in example.
Title: Re: [Fog of War] Make fog not overlay in clear areas
Post by: Anselm on December 22, 2013, 01:01:51 PM
Yeah it surprised me as well. It ONLY works when it's on the UI camera. If it's on the main camera and the UI camera is ON, it doesn't show. If I turn OFF the UI camera the fog can show from the main camera.

Here is a comparison shot of my two cameras:
(http://i41.tinypic.com/2wfqsma.png)

When FOW Effect script is on right camera with both cameras enabled, it draws fog with the error above.
When it's on the left camera, with both cameras enabled, it doesn't draw fog at all.
When it's on left camera, with right camera disabled, it draws fog perfectly how I want it to.
Title: Re: [Fog of War] Make fog not overlay in clear areas
Post by: ArenMook on December 22, 2013, 07:45:00 PM
Move the Boom effect to your game camera as well.
Title: Re: [Fog of War] Make fog not overlay in clear areas
Post by: Anselm on December 23, 2013, 10:58:39 AM
Due to the way Unity handles cameras if I move the bloom effect to the camera on the left it won't draw the effect.

I suppose this could explain the fog not drawing either. Due to Unity's silly way of handling camera rendering.
Title: Re: [Fog of War] Make fog not overlay in clear areas
Post by: ArenMook on December 23, 2013, 02:07:54 PM
Handles cameras? What do you mean? Lower depth cameras are drawn first, which in your case is your main camera. UI camera can be last, drawn after all the post processing effects. It should work just fine.
Title: Re: [Fog of War] Make fog not overlay in clear areas
Post by: Anselm on December 23, 2013, 05:40:05 PM
The way Unity has it's camera system set up post processing effects on cameras with a lower depth do not draw at all. You have to have the post processing effect on the highest depth camera in order for the effect to render properly, according to the Unity forums
Title: Re: [Fog of War] Make fog not overlay in clear areas
Post by: ArenMook on December 24, 2013, 01:56:09 PM
That's not true. Windward (http://www.tasharen.com/windward/) has the game camera with the FoW, and the UI camera is drawn afterwards.

P.S. Both your cameras are on the "Default" layer, which is wrong. The 3D UI camera should be on the 3D UI layer.
Title: Re: [Fog of War] Make fog not overlay in clear areas
Post by: Anselm on December 27, 2013, 12:18:34 PM
I'll set up the cameras to be on the correct layers.

And that's very odd, in my projects whenever I have two cameras camera effects only work when you put them on the last drawn camera. I even looked at the Unity forums and they said this was a problem with the way Unity handles cameras. I'll try switching the layers on my cameras and see if that fixes it
Title: Re: [Fog of War] Make fog not overlay in clear areas
Post by: Anselm on December 27, 2013, 12:31:19 PM
Nope, still doesn't fix it. I moved everything off the 3DGUI camera and changed it's layer, and the fog still only draws when the 3DGUI camera is turned off.

What settings did you have on the two cameras in Windward?
Title: Re: [Fog of War] Make fog not overlay in clear areas
Post by: ArenMook on December 27, 2013, 04:29:52 PM
Main Camera (depth 0, Skybox clear flags, perspective projection)
- Grayscale effect
- FOWEffect

UICamera (depth 1, depth only clear flags, orthographic projection)
- UICamera script
Title: Re: [Fog of War] Make fog not overlay in clear areas
Post by: Anselm on December 27, 2013, 06:17:43 PM
Interesting, it seems that the problem is caused by the 3DGUI camera being perspective. When I switch it to orthographic the fog draws fine.