Author Topic: [Fog of War] Make fog not overlay in clear areas  (Read 9455 times)

Anselm

  • Guest
[Fog of War] Make fog not overlay in clear areas
« 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:


And here's an image of a revealed area, yet again this is how I'd like it to appear from above:


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.


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.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: [Fog of War] Make fog not overlay in clear areas
« Reply #1 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.

Anselm

  • Guest
Re: [Fog of War] Make fog not overlay in clear areas
« Reply #2 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?

Anselm

  • Guest
Re: [Fog of War] Make fog not overlay in clear areas
« Reply #3 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

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: [Fog of War] Make fog not overlay in clear areas
« Reply #4 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.

Anselm

  • Guest
Re: [Fog of War] Make fog not overlay in clear areas
« Reply #5 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?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile

Anselm

  • Guest
Re: [Fog of War] Make fog not overlay in clear areas
« Reply #7 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?

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: [Fog of War] Make fog not overlay in clear areas
« Reply #8 on: December 19, 2013, 12:36:48 PM »
Yes. Same geometry, just an additional pass that writes to depth.

Anselm

  • Guest
Re: [Fog of War] Make fog not overlay in clear areas
« Reply #9 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.

Anselm

  • Guest
Re: [Fog of War] Make fog not overlay in clear areas
« Reply #10 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

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: [Fog of War] Make fog not overlay in clear areas
« Reply #11 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.

Anselm

  • Guest
Re: [Fog of War] Make fog not overlay in clear areas
« Reply #12 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.

ArenMook

  • Administrator
  • Hero Member
  • *****
  • Thank You
  • -Given: 337
  • -Receive: 1171
  • Posts: 22,128
  • Toronto, Canada
    • View Profile
Re: [Fog of War] Make fog not overlay in clear areas
« Reply #13 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.

Anselm

  • Guest
Re: [Fog of War] Make fog not overlay in clear areas
« Reply #14 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:


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.