Skip to content

Commit e7cacfc

Browse files
clayjohnbriansemrau
andcommitted
Addition of FogVolumes, FogShaders, FogMaterial, and overhaul of VolumetricFog
Co-authored-by: Brian Semrau <brian.semrau@gmail.com>
1 parent 66ab3ce commit e7cacfc

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+2925
-770
lines changed

doc/classes/Environment.xml

+20-13
Original file line numberDiff line numberDiff line change
@@ -248,23 +248,38 @@
248248
<member name="tonemap_white" type="float" setter="set_tonemap_white" getter="get_tonemap_white" default="1.0">
249249
The white reference value for tonemapping. Only effective if the [member tonemap_mode] isn't set to [constant TONE_MAPPER_LINEAR].
250250
</member>
251-
<member name="volumetric_fog_density" type="float" setter="set_volumetric_fog_density" getter="get_volumetric_fog_density" default="0.01">
251+
<member name="volumetric_fog_albedo" type="Color" setter="set_volumetric_fog_albedo" getter="get_volumetric_fog_albedo" default="Color(1, 1, 1, 1)">
252+
The [Color] of the volumetric fog when interacting with lights. Mist and fog have an albedo close to [code]Color(1, 1, 1, 1)[/code] while smoke has a darker albedo.
253+
</member>
254+
<member name="volumetric_fog_anisotropy" type="float" setter="set_volumetric_fog_anisotropy" getter="get_volumetric_fog_anisotropy" default="0.2">
255+
The direction of scattered light as it goes through the volumetric fog. A value close [code]1[/code] means almost all light is scattered forward. A value close to [code]0[/code] means light is scattered equally in all directions. A value close to [code]-1[/code] means light is scattered mostly backward. Fog and mist scatter light slightly forward, while smoke scatters light equally in all directions.
256+
</member>
257+
<member name="volumetric_fog_density" type="float" setter="set_volumetric_fog_density" getter="get_volumetric_fog_density" default="0.05">
258+
The base density of the volumetric fog. Set this to the lowest density you want to have globally.
252259
</member>
253260
<member name="volumetric_fog_detail_spread" type="float" setter="set_volumetric_fog_detail_spread" getter="get_volumetric_fog_detail_spread" default="2.0">
261+
The distribution of size down the length of the froxel buffer. A higher value compresses the froxels closer to the camera and places more detail closer to the camera.
262+
</member>
263+
<member name="volumetric_fog_emission" type="Color" setter="set_volumetric_fog_emission" getter="get_volumetric_fog_emission" default="Color(0, 0, 0, 1)">
264+
The emitted light from the volumetric fog. Even with emission, volumetric fog will not cast light onto other surfaces. Emission is useful to establish an ambient color. As the volumetric fog effect uses single-scattering only, fog tends to need a little bit of emission to soften the harsh shadows.
265+
</member>
266+
<member name="volumetric_fog_emission_energy" type="float" setter="set_volumetric_fog_emission_energy" getter="get_volumetric_fog_emission_energy" default="1.0">
267+
The brightness of the emitted light from the volumetric fog.
254268
</member>
255269
<member name="volumetric_fog_enabled" type="bool" setter="set_volumetric_fog_enabled" getter="is_volumetric_fog_enabled" default="false">
270+
Enables the volumetric fog effect. Volumetric fog uses a screen-aligned froxel buffer to calculate accurate volumetric scattering in the short to medium range. Volumetric fog interacts with [FogVolume]s and lights to calculate localized and global fog. Volumetric fog uses a PBR single-scattering model based on extinction, scattering, and emission which it exposes to users as density, albedo, and emission.
256271
</member>
257272
<member name="volumetric_fog_gi_inject" type="float" setter="set_volumetric_fog_gi_inject" getter="get_volumetric_fog_gi_inject" default="0.0">
273+
Scales the strength of Global Illumination used in the volumetric fog. A value of [code]0[/code] means that Global Illumination will not impact the volumetric fog.
258274
</member>
259275
<member name="volumetric_fog_length" type="float" setter="set_volumetric_fog_length" getter="get_volumetric_fog_length" default="64.0">
260-
</member>
261-
<member name="volumetric_fog_light" type="Color" setter="set_volumetric_fog_light" getter="get_volumetric_fog_light" default="Color(0, 0, 0, 1)">
262-
</member>
263-
<member name="volumetric_fog_light_energy" type="float" setter="set_volumetric_fog_light_energy" getter="get_volumetric_fog_light_energy" default="1.0">
276+
The distance over which the volumetric fog is computed. Increase to compute fog over a greater range, decrease to add more detail when a long range is not needed. For best quality fog, keep this as low as possible.
264277
</member>
265278
<member name="volumetric_fog_temporal_reprojection_amount" type="float" setter="set_volumetric_fog_temporal_reprojection_amount" getter="get_volumetric_fog_temporal_reprojection_amount" default="0.9">
279+
The amount by which to blend the last frame with the current frame. A higher number results in smoother volumetric fog, but makes "ghosting" much worse. A lower value reduces ghosting but can result in the per-frame temporal jitter becoming visible.
266280
</member>
267281
<member name="volumetric_fog_temporal_reprojection_enabled" type="bool" setter="set_volumetric_fog_temporal_reprojection_enabled" getter="is_volumetric_fog_temporal_reprojection_enabled" default="true">
282+
Enables temporal reprojection in the volumetric fog. Temporal reprojection blends the current frame's volumetric fog with the last frame's volumetric fog to smooth out jagged edges. The performance cost is minimal, however it does lead to moving [FogVolume]s and [Light3D]s "ghosting" and leaving a trail behind them. When temporal reprojection is enabled, try to avoid moving [FogVolume]s or [Light3D]s too fast.
268283
</member>
269284
</members>
270285
<constants>
@@ -349,13 +364,5 @@
349364
</constant>
350365
<constant name="SDFGI_Y_SCALE_50_PERCENT" value="2" enum="SDFGIYScale">
351366
</constant>
352-
<constant name="VOLUMETRIC_FOG_SHADOW_FILTER_DISABLED" value="0" enum="VolumetricFogShadowFilter">
353-
</constant>
354-
<constant name="VOLUMETRIC_FOG_SHADOW_FILTER_LOW" value="1" enum="VolumetricFogShadowFilter">
355-
</constant>
356-
<constant name="VOLUMETRIC_FOG_SHADOW_FILTER_MEDIUM" value="2" enum="VolumetricFogShadowFilter">
357-
</constant>
358-
<constant name="VOLUMETRIC_FOG_SHADOW_FILTER_HIGH" value="3" enum="VolumetricFogShadowFilter">
359-
</constant>
360367
</constants>
361368
</class>

doc/classes/FogMaterial.xml

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<class name="FogMaterial" inherits="Material" version="4.0">
3+
<brief_description>
4+
[Material] used with a [FogVolume] to draw things with the volumetric fog effect.
5+
</brief_description>
6+
<description>
7+
A [Material] resource that can be used by [FogVolume]s to draw volumetric effects.
8+
</description>
9+
<tutorials>
10+
</tutorials>
11+
<members>
12+
<member name="albedo" type="Color" setter="set_albedo" getter="get_albedo" default="Color(1, 1, 1, 1)">
13+
Sets the single-scattering [Color] of the [FogVolume]. Internally [member albedo] is converted into single-scattering which is additively blended with other [FogVolume]s and the [member Environment.volumetric_fog_albedo].
14+
</member>
15+
<member name="density" type="float" setter="set_density" getter="get_density" default="1.0">
16+
Sets the density of the [FogVolume]. Denser objects are more opaque, but may suffer from under-sampling artifacts that look like stripes.
17+
</member>
18+
<member name="density_texture" type="Texture3D" setter="set_density_texture" getter="get_density_texture">
19+
Sets a 3D texture that is used to scale the [member density] of the [FogVolume].
20+
</member>
21+
<member name="edge_fade" type="float" setter="set_edge_fade" getter="get_edge_fade" default="0.1">
22+
Sets the hardness of the edges of the [FogVolume]. A higher number will result in softer edges while a lower number will result in harder edges.
23+
</member>
24+
<member name="emission" type="Color" setter="set_emission" getter="get_emission" default="Color(0, 0, 0, 1)">
25+
Sets the [Color] of the light emitted by the [FogVolume]. Emitted light will not cast light or shadows on other objects, but can be useful for modulating the [Color] of the [FogVolume] independently from light sources.
26+
</member>
27+
<member name="height_falloff" type="float" setter="set_height_falloff" getter="get_height_falloff" default="0.0">
28+
Sets the rate by which the height-based fog decreases in density as height increases. A high falloff will result in a sharp transition, while a low falloff will result in a smoother transition. A value of [code]0[/code] results in uniform-density fog.
29+
</member>
30+
<member name="height_offset" type="float" setter="set_height_offset" getter="get_height_offset" default="0.0">
31+
Sets the threshold at which the height-based fog starts to decrease in density. Relies on [member height_falloff] to have any effect. Offset is specified in world coordinates and should be roughly equal to or slightly above your ground plane.
32+
</member>
33+
</members>
34+
</class>

doc/classes/FogVolume.xml

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8" ?>
2+
<class name="FogVolume" inherits="VisualInstance3D" version="4.0">
3+
<brief_description>
4+
A node used to add local fog with the volumetric fog effect.
5+
</brief_description>
6+
<description>
7+
[FogVolume]s are used to add localized fog into the global volumetric fog effect.
8+
Performance of [FogVolume]s is directly related to their relative size on the screen and the complexity of their attached [FogMaterial]. It is best to keep [FogVolume]s relatively small and simple where possible.
9+
</description>
10+
<tutorials>
11+
</tutorials>
12+
<members>
13+
<member name="extents" type="Vector3" setter="set_extents" getter="get_extents" default="Vector3(1, 1, 1)">
14+
Sets the size of the [FogVolume] when [member shape] is [constant RenderingServer.FOG_VOLUME_SHAPE_ELLIPSOID] or [constant RenderingServer.FOG_VOLUME_SHAPE_BOX].
15+
</member>
16+
<member name="material" type="Material" setter="set_material" getter="get_material">
17+
Sets the [Material] to be used by the [FogVolume]. Can be either a [FogMaterial] or a custom [ShaderMaterial].
18+
</member>
19+
<member name="shape" type="int" setter="set_shape" getter="get_shape" enum="RenderingServer.FogVolumeShape" default="1">
20+
Sets the shape of the [FogVolume] to either [constant RenderingServer.FOG_VOLUME_SHAPE_ELLIPSOID], [constant RenderingServer.FOG_VOLUME_SHAPE_BOX], or [constant RenderingServer.FOG_VOLUME_SHAPE_ELLIPSOID] or [constant RenderingServer.FOG_VOLUME_SHAPE_WORLD].
21+
</member>
22+
</members>
23+
</class>

doc/classes/RenderingServer.xml

+64-9
Original file line numberDiff line numberDiff line change
@@ -1121,27 +1121,61 @@
11211121
<argument index="0" name="env" type="RID" />
11221122
<argument index="1" name="enable" type="bool" />
11231123
<argument index="2" name="density" type="float" />
1124-
<argument index="3" name="light" type="Color" />
1125-
<argument index="4" name="light_energy" type="float" />
1126-
<argument index="5" name="length" type="float" />
1127-
<argument index="6" name="p_detail_spread" type="float" />
1128-
<argument index="7" name="gi_inject" type="float" />
1129-
<argument index="8" name="temporal_reprojection" type="bool" />
1130-
<argument index="9" name="temporal_reprojection_amount" type="float" />
1124+
<argument index="3" name="albedo" type="Color" />
1125+
<argument index="4" name="emission" type="Color" />
1126+
<argument index="5" name="emission_energy" type="float" />
1127+
<argument index="6" name="anisotropy" type="float" />
1128+
<argument index="7" name="length" type="float" />
1129+
<argument index="8" name="p_detail_spread" type="float" />
1130+
<argument index="9" name="gi_inject" type="float" />
1131+
<argument index="10" name="temporal_reprojection" type="bool" />
1132+
<argument index="11" name="temporal_reprojection_amount" type="float" />
11311133
<description>
11321134
</description>
11331135
</method>
11341136
<method name="environment_set_volumetric_fog_filter_active">
11351137
<return type="void" />
11361138
<argument index="0" name="active" type="bool" />
11371139
<description>
1140+
Enables filtering of the volumetric fog scattering buffer. This results in much smoother volumes with very few under-sampling artifacts.
11381141
</description>
11391142
</method>
11401143
<method name="environment_set_volumetric_fog_volume_size">
11411144
<return type="void" />
11421145
<argument index="0" name="size" type="int" />
11431146
<argument index="1" name="depth" type="int" />
11441147
<description>
1148+
Sets the resolution of the volumetric fog's froxel buffer. [code]size[/code] is modified by the screen's aspect ratio and then used to set the width and height of the buffer. While [code]depth[/code] is directly used to set the depth of the buffer.
1149+
</description>
1150+
</method>
1151+
<method name="fog_volume_create">
1152+
<return type="RID" />
1153+
<description>
1154+
Creates a new fog volume and allocates an RID.
1155+
</description>
1156+
</method>
1157+
<method name="fog_volume_set_extents">
1158+
<return type="void" />
1159+
<argument index="0" name="fog_volume" type="RID" />
1160+
<argument index="1" name="extents" type="Vector3" />
1161+
<description>
1162+
Sets the size of the fog volume when shape is [constant FOG_VOLUME_SHAPE_ELLIPSOID] or [constant FOG_VOLUME_SHAPE_BOX].
1163+
</description>
1164+
</method>
1165+
<method name="fog_volume_set_material">
1166+
<return type="void" />
1167+
<argument index="0" name="fog_volume" type="RID" />
1168+
<argument index="1" name="material" type="RID" />
1169+
<description>
1170+
Sets the [Material] of the fog volume. Can be either a [FogMaterial] or a custom [ShaderMaterial].
1171+
</description>
1172+
</method>
1173+
<method name="fog_volume_set_shape">
1174+
<return type="void" />
1175+
<argument index="0" name="fog_volume" type="RID" />
1176+
<argument index="1" name="shape" type="int" enum="RenderingServer.FogVolumeShape" />
1177+
<description>
1178+
Sets the shape of the fog volume to either [constant RenderingServer.FOG_VOLUME_SHAPE_ELLIPSOID], [constant RenderingServer.FOG_VOLUME_SHAPE_BOX], or [constant RenderingServer.FOG_VOLUME_SHAPE_ELLIPSOID] or [constant RenderingServer.FOG_VOLUME_SHAPE_WORLD].
11451179
</description>
11461180
</method>
11471181
<method name="force_draw">
@@ -1422,6 +1456,13 @@
14221456
Sets a margin to increase the size of the AABB when culling objects from the view frustum. This allows you to avoid culling objects that fall outside the view frustum. Equivalent to [member GeometryInstance3D.extra_cull_margin].
14231457
</description>
14241458
</method>
1459+
<method name="instance_set_ignore_culling">
1460+
<return type="void" />
1461+
<argument index="0" name="instance" type="RID" />
1462+
<argument index="1" name="enabled" type="bool" />
1463+
<description>
1464+
</description>
1465+
</method>
14251466
<method name="instance_set_layer_mask">
14261467
<return type="void" />
14271468
<argument index="0" name="instance" type="RID" />
@@ -3408,7 +3449,10 @@
34083449
<constant name="SHADER_SKY" value="3" enum="ShaderMode">
34093450
Shader is a sky shader.
34103451
</constant>
3411-
<constant name="SHADER_MAX" value="4" enum="ShaderMode">
3452+
<constant name="SHADER_FOG" value="4" enum="ShaderMode">
3453+
Shader is a fog shader.
3454+
</constant>
3455+
<constant name="SHADER_MAX" value="5" enum="ShaderMode">
34123456
Represents the size of the [enum ShaderMode] enum.
34133457
</constant>
34143458
<constant name="MATERIAL_RENDER_PRIORITY_MIN" value="-128">
@@ -3771,6 +3815,15 @@
37713815
</constant>
37723816
<constant name="PARTICLES_COLLISION_HEIGHTFIELD_RESOLUTION_MAX" value="6" enum="ParticlesCollisionHeightfieldResolution">
37733817
</constant>
3818+
<constant name="FOG_VOLUME_SHAPE_ELLIPSOID" value="0" enum="FogVolumeShape">
3819+
[FogVolume] will be shaped like an ellipsoid.
3820+
</constant>
3821+
<constant name="FOG_VOLUME_SHAPE_BOX" value="1" enum="FogVolumeShape">
3822+
[FogVolume] will be shaped like a box.
3823+
</constant>
3824+
<constant name="FOG_VOLUME_SHAPE_WORLD" value="2" enum="FogVolumeShape">
3825+
[FogVolume] will have no shape, will cover the whole world and will not be culled.
3826+
</constant>
37743827
<constant name="VIEWPORT_UPDATE_DISABLED" value="0" enum="ViewportUpdateMode">
37753828
Do not update the viewport.
37763829
</constant>
@@ -4147,7 +4200,9 @@
41474200
</constant>
41484201
<constant name="INSTANCE_VISIBLITY_NOTIFIER" value="11" enum="InstanceType">
41494202
</constant>
4150-
<constant name="INSTANCE_MAX" value="12" enum="InstanceType">
4203+
<constant name="INSTANCE_FOG_VOLUME" value="12" enum="InstanceType">
4204+
</constant>
4205+
<constant name="INSTANCE_MAX" value="13" enum="InstanceType">
41514206
Represents the size of the [enum InstanceType] enum.
41524207
</constant>
41534208
<constant name="INSTANCE_GEOMETRY_MASK" value="14" enum="InstanceType">

doc/classes/Shader.xml

+3
Original file line numberDiff line numberDiff line change
@@ -61,5 +61,8 @@
6161
<constant name="MODE_SKY" value="3" enum="Mode">
6262
Mode used for drawing skies. Only works with shaders attached to [Sky] objects.
6363
</constant>
64+
<constant name="MODE_FOG" value="4" enum="Mode">
65+
Mode used for setting the color and density of volumetric fog effect.
66+
</constant>
6467
</constants>
6568
</class>

doc/classes/VisualShader.xml

+4-1
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,10 @@
176176
</constant>
177177
<constant name="TYPE_SKY" value="8" enum="Type">
178178
</constant>
179-
<constant name="TYPE_MAX" value="9" enum="Type">
179+
<constant name="TYPE_FOG" value="9" enum="Type">
180+
A compute shader that runs for each froxel of the volumetric fog map.
181+
</constant>
182+
<constant name="TYPE_MAX" value="10" enum="Type">
180183
Represents the size of the [enum Type] enum.
181184
</constant>
182185
<constant name="NODE_ID_INVALID" value="-1">

drivers/vulkan/rendering_device_vulkan.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -8052,9 +8052,9 @@ void RenderingDeviceVulkan::compute_list_dispatch(ComputeListID p_list, uint32_t
80528052
ERR_FAIL_COND_MSG(p_x_groups > limits.maxComputeWorkGroupCount[0],
80538053
"Dispatch amount of X compute groups (" + itos(p_x_groups) + ") is larger than device limit (" + itos(limits.maxComputeWorkGroupCount[0]) + ")");
80548054
ERR_FAIL_COND_MSG(p_y_groups > limits.maxComputeWorkGroupCount[1],
8055-
"Dispatch amount of Y compute groups (" + itos(p_x_groups) + ") is larger than device limit (" + itos(limits.maxComputeWorkGroupCount[0]) + ")");
8055+
"Dispatch amount of Y compute groups (" + itos(p_y_groups) + ") is larger than device limit (" + itos(limits.maxComputeWorkGroupCount[1]) + ")");
80568056
ERR_FAIL_COND_MSG(p_z_groups > limits.maxComputeWorkGroupCount[2],
8057-
"Dispatch amount of Z compute groups (" + itos(p_x_groups) + ") is larger than device limit (" + itos(limits.maxComputeWorkGroupCount[0]) + ")");
8057+
"Dispatch amount of Z compute groups (" + itos(p_z_groups) + ") is larger than device limit (" + itos(limits.maxComputeWorkGroupCount[2]) + ")");
80588058

80598059
ERR_FAIL_COND_MSG(!cl->validation.active, "Submitted Compute Lists can no longer be modified.");
80608060
#endif

editor/editor_node.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -7028,6 +7028,10 @@ EditorNode::EditorNode() {
70287028
physical_sky_mat_convert.instantiate();
70297029
resource_conversion_plugins.push_back(physical_sky_mat_convert);
70307030

7031+
Ref<FogMaterialConversionPlugin> fog_mat_convert;
7032+
fog_mat_convert.instantiate();
7033+
resource_conversion_plugins.push_back(fog_mat_convert);
7034+
70317035
Ref<VisualShaderConversionPlugin> vshader_convert;
70327036
vshader_convert.instantiate();
70337037
resource_conversion_plugins.push_back(vshader_convert);

editor/plugins/material_editor_plugin.cpp

+38
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
#include "editor/editor_scale.h"
3434
#include "scene/gui/subviewport_container.h"
35+
#include "scene/resources/fog_material.h"
3536
#include "scene/resources/particles_material.h"
3637
#include "scene/resources/sky_material.h"
3738

@@ -477,3 +478,40 @@ Ref<Resource> PhysicalSkyMaterialConversionPlugin::convert(const Ref<Resource> &
477478
smat->set_name(mat->get_name());
478479
return smat;
479480
}
481+
482+
String FogMaterialConversionPlugin::converts_to() const {
483+
return "ShaderMaterial";
484+
}
485+
486+
bool FogMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const {
487+
Ref<FogMaterial> mat = p_resource;
488+
return mat.is_valid();
489+
}
490+
491+
Ref<Resource> FogMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const {
492+
Ref<FogMaterial> mat = p_resource;
493+
ERR_FAIL_COND_V(!mat.is_valid(), Ref<Resource>());
494+
495+
Ref<ShaderMaterial> smat;
496+
smat.instantiate();
497+
498+
Ref<Shader> shader;
499+
shader.instantiate();
500+
501+
String code = RS::get_singleton()->shader_get_code(mat->get_shader_rid());
502+
503+
shader->set_code(code);
504+
505+
smat->set_shader(shader);
506+
507+
List<PropertyInfo> params;
508+
RS::get_singleton()->shader_get_param_list(mat->get_shader_rid(), &params);
509+
510+
for (const PropertyInfo &E : params) {
511+
Variant value = RS::get_singleton()->material_get_param(mat->get_rid(), E.name);
512+
smat->set_shader_param(E.name, value);
513+
}
514+
515+
smat->set_render_priority(mat->get_render_priority());
516+
return smat;
517+
}

editor/plugins/material_editor_plugin.h

+9
Original file line numberDiff line numberDiff line change
@@ -152,4 +152,13 @@ class PhysicalSkyMaterialConversionPlugin : public EditorResourceConversionPlugi
152152
virtual Ref<Resource> convert(const Ref<Resource> &p_resource) const override;
153153
};
154154

155+
class FogMaterialConversionPlugin : public EditorResourceConversionPlugin {
156+
GDCLASS(FogMaterialConversionPlugin, EditorResourceConversionPlugin);
157+
158+
public:
159+
virtual String converts_to() const override;
160+
virtual bool handles(const Ref<Resource> &p_resource) const override;
161+
virtual Ref<Resource> convert(const Ref<Resource> &p_resource) const override;
162+
};
163+
155164
#endif // MATERIAL_EDITOR_PLUGIN_H

0 commit comments

Comments
 (0)