How to create a fog effect using shaders?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By henriquelalves
:warning: Old Version Published before Godot 3 was released.

I’m interested in simulating 3D fog using shaders, but the problem is that I have no clue on where the shader script should be; from my understanding, Shader scripts on 3D scenes are only available in materials. In which type of node this material should be? Can it be a Sprite3D node with the color I’ll set as fog?

On a side-note, I tried using a GlobalEnvironment node to create the fog effect, but for some reason the distances limitations weren’t enough for the game I’m creating.

Can’t you just scale down your whole game for your world staying in the limited area?

timoschwarzer | 2016-12-09 06:33

I don’t have experience with shaders, but I found it relatively easy to find a shader I like on shadertoy.com, and convert the code from there in Godot code. The conversion is not difficult but rather tedious.

MrMonk | 2016-12-09 06:35

Isn’t the fog effect built-in with the WorldEnvironment? (it’s a distance-correct one btw, which is nice)
What kind of distances your game has? Are you working in meters?

Zylann | 2016-12-09 18:17

I’m new so forgive me if this sounds silly. Is it possible to add a shader to viewport? That probably doesn’t help. What about particle emitter?

CraftyManiac | 2016-12-09 19:39

Thanks for all the replies!
@Timoshwarzer: I tried scaling the game to match the WorldEnvironment fog, but that wasn’t working too. I’m not too sure how WorldEvironment fog even work; the game is on my GitHub account, if you want to check it out: GitHub - henriquelalves/simple_dungeon_demo: A simple Dungeon Demo for Godot Engine.

@MrMonk: Thanks, I’ll check it out! I’m still unsure on where I should put the script, thought, haha.

@Zylann: Well, I’m working on normal “Godot units”, which in Blender should be a meter each. But I tested changing the scale of the game and the distance of the fog on the WorldEnvironment node, and now I’m just confused on how does it work. If you want to check it out, the game is on my GitHub: GitHub - henriquelalves/simple_dungeon_demo: A simple Dungeon Demo for Godot Engine.

@CraftyManiac: I didn’t thought on using viewports, actually; apparently I can add an Environment property to the viewport itself, so maybe I can use this to create the fog Effect that I want. A particle emitter probably wouldn’t work, because the shader would be on each particle material (I guess, I’m not even sure on this one haha).

henriquelalves | 2016-12-10 14:14

I kind of meant instead of using a shader you can use a particle emitter. Maybe it can be attached to the viewport or player camera.

CraftyManiac | 2016-12-10 16:27

@CraftyManiac
I just tested this idea and its actually working! Fortunately there are a lot of Particle Emitter characteristics that I can play around to actually have the effect that I have in mind, so thanks a lot man! The only thing that I’m worried about is how processing heavy is it, and I’m still curious if there is a way to do it as a shader, haha.

EDIT: I think I found a problem; apparently, particles don’t follow the particle emitter transformation - so when you change the camera fast, you will see the fog being created at a distance.

EDIT.2: Aaaand there is an option to use the local coordinates, hahaha. I’ll explore more of this option before I say anything else.

henriquelalves | 2016-12-11 00:40

Did you write a bug report on GitHub regarding the fog in the WorldEnvironment? I tried to set it up correctly and it does seem broken indeed.

Warlaan | 2016-12-12 10:41

Tried with the Platformer 3D demo (which has a dim fog enabled), if you set the attenuation low, you will get a more dense fog, but don’t know if is the kind of fog you want.

eons | 2016-12-16 23:26

:bust_in_silhouette: Reply From: CraftyManiac

Check this link out not sure if this info will help since it’s not Godot related. But it is however shader related. Sorry I can’t be of more help I’m super new to game dev. Messed around with unity and unreal for a bit. I really enjoy the process of game dev. It’s like a difficult puzzle. More enjoyable than playing most games.

Didn’t mean to put this as an answer. On mobile at the moment.

CraftyManiac | 2016-12-11 02:32

Hahaha, no worries. We were commenting possible solutions to the fog on the comment section above, but I’m still looking for a shader answer.

henriquelalves | 2016-12-11 02:48