Skip to content

Commit

Permalink
Addition of FogVolumes, FogShaders, FogMaterial, and overhaul of Volu…
Browse files Browse the repository at this point in the history
…metricFog

Co-authored-by: Brian Semrau <brian.semrau@gmail.com>
  • Loading branch information
clayjohn and briansemrau committed Oct 4, 2021
1 parent 66ab3ce commit e7cacfc
Show file tree
Hide file tree
Showing 47 changed files with 2,925 additions and 770 deletions.
33 changes: 20 additions & 13 deletions doc/classes/Environment.xml
Original file line number Diff line number Diff line change
Expand Up @@ -248,23 +248,38 @@
<member name="tonemap_white" type="float" setter="set_tonemap_white" getter="get_tonemap_white" default="1.0">
The white reference value for tonemapping. Only effective if the [member tonemap_mode] isn't set to [constant TONE_MAPPER_LINEAR].
</member>
<member name="volumetric_fog_density" type="float" setter="set_volumetric_fog_density" getter="get_volumetric_fog_density" default="0.01">
<member name="volumetric_fog_albedo" type="Color" setter="set_volumetric_fog_albedo" getter="get_volumetric_fog_albedo" default="Color(1, 1, 1, 1)">
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.
</member>
<member name="volumetric_fog_anisotropy" type="float" setter="set_volumetric_fog_anisotropy" getter="get_volumetric_fog_anisotropy" default="0.2">
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.
</member>
<member name="volumetric_fog_density" type="float" setter="set_volumetric_fog_density" getter="get_volumetric_fog_density" default="0.05">
The base density of the volumetric fog. Set this to the lowest density you want to have globally.
</member>
<member name="volumetric_fog_detail_spread" type="float" setter="set_volumetric_fog_detail_spread" getter="get_volumetric_fog_detail_spread" default="2.0">
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.
</member>
<member name="volumetric_fog_emission" type="Color" setter="set_volumetric_fog_emission" getter="get_volumetric_fog_emission" default="Color(0, 0, 0, 1)">
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.
</member>
<member name="volumetric_fog_emission_energy" type="float" setter="set_volumetric_fog_emission_energy" getter="get_volumetric_fog_emission_energy" default="1.0">
The brightness of the emitted light from the volumetric fog.
</member>
<member name="volumetric_fog_enabled" type="bool" setter="set_volumetric_fog_enabled" getter="is_volumetric_fog_enabled" default="false">
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.
</member>
<member name="volumetric_fog_gi_inject" type="float" setter="set_volumetric_fog_gi_inject" getter="get_volumetric_fog_gi_inject" default="0.0">
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.
</member>
<member name="volumetric_fog_length" type="float" setter="set_volumetric_fog_length" getter="get_volumetric_fog_length" default="64.0">
</member>
<member name="volumetric_fog_light" type="Color" setter="set_volumetric_fog_light" getter="get_volumetric_fog_light" default="Color(0, 0, 0, 1)">
</member>
<member name="volumetric_fog_light_energy" type="float" setter="set_volumetric_fog_light_energy" getter="get_volumetric_fog_light_energy" default="1.0">
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.
</member>
<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">
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.
</member>
<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">
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.
</member>
</members>
<constants>
Expand Down Expand Up @@ -349,13 +364,5 @@
</constant>
<constant name="SDFGI_Y_SCALE_50_PERCENT" value="2" enum="SDFGIYScale">
</constant>
<constant name="VOLUMETRIC_FOG_SHADOW_FILTER_DISABLED" value="0" enum="VolumetricFogShadowFilter">
</constant>
<constant name="VOLUMETRIC_FOG_SHADOW_FILTER_LOW" value="1" enum="VolumetricFogShadowFilter">
</constant>
<constant name="VOLUMETRIC_FOG_SHADOW_FILTER_MEDIUM" value="2" enum="VolumetricFogShadowFilter">
</constant>
<constant name="VOLUMETRIC_FOG_SHADOW_FILTER_HIGH" value="3" enum="VolumetricFogShadowFilter">
</constant>
</constants>
</class>
34 changes: 34 additions & 0 deletions doc/classes/FogMaterial.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="FogMaterial" inherits="Material" version="4.0">
<brief_description>
[Material] used with a [FogVolume] to draw things with the volumetric fog effect.
</brief_description>
<description>
A [Material] resource that can be used by [FogVolume]s to draw volumetric effects.
</description>
<tutorials>
</tutorials>
<members>
<member name="albedo" type="Color" setter="set_albedo" getter="get_albedo" default="Color(1, 1, 1, 1)">
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].
</member>
<member name="density" type="float" setter="set_density" getter="get_density" default="1.0">
Sets the density of the [FogVolume]. Denser objects are more opaque, but may suffer from under-sampling artifacts that look like stripes.
</member>
<member name="density_texture" type="Texture3D" setter="set_density_texture" getter="get_density_texture">
Sets a 3D texture that is used to scale the [member density] of the [FogVolume].
</member>
<member name="edge_fade" type="float" setter="set_edge_fade" getter="get_edge_fade" default="0.1">
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.
</member>
<member name="emission" type="Color" setter="set_emission" getter="get_emission" default="Color(0, 0, 0, 1)">
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.
</member>
<member name="height_falloff" type="float" setter="set_height_falloff" getter="get_height_falloff" default="0.0">
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.
</member>
<member name="height_offset" type="float" setter="set_height_offset" getter="get_height_offset" default="0.0">
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.
</member>
</members>
</class>
23 changes: 23 additions & 0 deletions doc/classes/FogVolume.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8" ?>
<class name="FogVolume" inherits="VisualInstance3D" version="4.0">
<brief_description>
A node used to add local fog with the volumetric fog effect.
</brief_description>
<description>
[FogVolume]s are used to add localized fog into the global volumetric fog effect.
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.
</description>
<tutorials>
</tutorials>
<members>
<member name="extents" type="Vector3" setter="set_extents" getter="get_extents" default="Vector3(1, 1, 1)">
Sets the size of the [FogVolume] when [member shape] is [constant RenderingServer.FOG_VOLUME_SHAPE_ELLIPSOID] or [constant RenderingServer.FOG_VOLUME_SHAPE_BOX].
</member>
<member name="material" type="Material" setter="set_material" getter="get_material">
Sets the [Material] to be used by the [FogVolume]. Can be either a [FogMaterial] or a custom [ShaderMaterial].
</member>
<member name="shape" type="int" setter="set_shape" getter="get_shape" enum="RenderingServer.FogVolumeShape" default="1">
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].
</member>
</members>
</class>
73 changes: 64 additions & 9 deletions doc/classes/RenderingServer.xml
Original file line number Diff line number Diff line change
Expand Up @@ -1121,27 +1121,61 @@
<argument index="0" name="env" type="RID" />
<argument index="1" name="enable" type="bool" />
<argument index="2" name="density" type="float" />
<argument index="3" name="light" type="Color" />
<argument index="4" name="light_energy" type="float" />
<argument index="5" name="length" type="float" />
<argument index="6" name="p_detail_spread" type="float" />
<argument index="7" name="gi_inject" type="float" />
<argument index="8" name="temporal_reprojection" type="bool" />
<argument index="9" name="temporal_reprojection_amount" type="float" />
<argument index="3" name="albedo" type="Color" />
<argument index="4" name="emission" type="Color" />
<argument index="5" name="emission_energy" type="float" />
<argument index="6" name="anisotropy" type="float" />
<argument index="7" name="length" type="float" />
<argument index="8" name="p_detail_spread" type="float" />
<argument index="9" name="gi_inject" type="float" />
<argument index="10" name="temporal_reprojection" type="bool" />
<argument index="11" name="temporal_reprojection_amount" type="float" />
<description>
</description>
</method>
<method name="environment_set_volumetric_fog_filter_active">
<return type="void" />
<argument index="0" name="active" type="bool" />
<description>
Enables filtering of the volumetric fog scattering buffer. This results in much smoother volumes with very few under-sampling artifacts.
</description>
</method>
<method name="environment_set_volumetric_fog_volume_size">
<return type="void" />
<argument index="0" name="size" type="int" />
<argument index="1" name="depth" type="int" />
<description>
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.
</description>
</method>
<method name="fog_volume_create">
<return type="RID" />
<description>
Creates a new fog volume and allocates an RID.
</description>
</method>
<method name="fog_volume_set_extents">
<return type="void" />
<argument index="0" name="fog_volume" type="RID" />
<argument index="1" name="extents" type="Vector3" />
<description>
Sets the size of the fog volume when shape is [constant FOG_VOLUME_SHAPE_ELLIPSOID] or [constant FOG_VOLUME_SHAPE_BOX].
</description>
</method>
<method name="fog_volume_set_material">
<return type="void" />
<argument index="0" name="fog_volume" type="RID" />
<argument index="1" name="material" type="RID" />
<description>
Sets the [Material] of the fog volume. Can be either a [FogMaterial] or a custom [ShaderMaterial].
</description>
</method>
<method name="fog_volume_set_shape">
<return type="void" />
<argument index="0" name="fog_volume" type="RID" />
<argument index="1" name="shape" type="int" enum="RenderingServer.FogVolumeShape" />
<description>
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].
</description>
</method>
<method name="force_draw">
Expand Down Expand Up @@ -1422,6 +1456,13 @@
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].
</description>
</method>
<method name="instance_set_ignore_culling">
<return type="void" />
<argument index="0" name="instance" type="RID" />
<argument index="1" name="enabled" type="bool" />
<description>
</description>
</method>
<method name="instance_set_layer_mask">
<return type="void" />
<argument index="0" name="instance" type="RID" />
Expand Down Expand Up @@ -3408,7 +3449,10 @@
<constant name="SHADER_SKY" value="3" enum="ShaderMode">
Shader is a sky shader.
</constant>
<constant name="SHADER_MAX" value="4" enum="ShaderMode">
<constant name="SHADER_FOG" value="4" enum="ShaderMode">
Shader is a fog shader.
</constant>
<constant name="SHADER_MAX" value="5" enum="ShaderMode">
Represents the size of the [enum ShaderMode] enum.
</constant>
<constant name="MATERIAL_RENDER_PRIORITY_MIN" value="-128">
Expand Down Expand Up @@ -3771,6 +3815,15 @@
</constant>
<constant name="PARTICLES_COLLISION_HEIGHTFIELD_RESOLUTION_MAX" value="6" enum="ParticlesCollisionHeightfieldResolution">
</constant>
<constant name="FOG_VOLUME_SHAPE_ELLIPSOID" value="0" enum="FogVolumeShape">
[FogVolume] will be shaped like an ellipsoid.
</constant>
<constant name="FOG_VOLUME_SHAPE_BOX" value="1" enum="FogVolumeShape">
[FogVolume] will be shaped like a box.
</constant>
<constant name="FOG_VOLUME_SHAPE_WORLD" value="2" enum="FogVolumeShape">
[FogVolume] will have no shape, will cover the whole world and will not be culled.
</constant>
<constant name="VIEWPORT_UPDATE_DISABLED" value="0" enum="ViewportUpdateMode">
Do not update the viewport.
</constant>
Expand Down Expand Up @@ -4147,7 +4200,9 @@
</constant>
<constant name="INSTANCE_VISIBLITY_NOTIFIER" value="11" enum="InstanceType">
</constant>
<constant name="INSTANCE_MAX" value="12" enum="InstanceType">
<constant name="INSTANCE_FOG_VOLUME" value="12" enum="InstanceType">
</constant>
<constant name="INSTANCE_MAX" value="13" enum="InstanceType">
Represents the size of the [enum InstanceType] enum.
</constant>
<constant name="INSTANCE_GEOMETRY_MASK" value="14" enum="InstanceType">
Expand Down
3 changes: 3 additions & 0 deletions doc/classes/Shader.xml
Original file line number Diff line number Diff line change
Expand Up @@ -61,5 +61,8 @@
<constant name="MODE_SKY" value="3" enum="Mode">
Mode used for drawing skies. Only works with shaders attached to [Sky] objects.
</constant>
<constant name="MODE_FOG" value="4" enum="Mode">
Mode used for setting the color and density of volumetric fog effect.
</constant>
</constants>
</class>
5 changes: 4 additions & 1 deletion doc/classes/VisualShader.xml
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,10 @@
</constant>
<constant name="TYPE_SKY" value="8" enum="Type">
</constant>
<constant name="TYPE_MAX" value="9" enum="Type">
<constant name="TYPE_FOG" value="9" enum="Type">
A compute shader that runs for each froxel of the volumetric fog map.
</constant>
<constant name="TYPE_MAX" value="10" enum="Type">
Represents the size of the [enum Type] enum.
</constant>
<constant name="NODE_ID_INVALID" value="-1">
Expand Down
4 changes: 2 additions & 2 deletions drivers/vulkan/rendering_device_vulkan.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8052,9 +8052,9 @@ void RenderingDeviceVulkan::compute_list_dispatch(ComputeListID p_list, uint32_t
ERR_FAIL_COND_MSG(p_x_groups > limits.maxComputeWorkGroupCount[0],
"Dispatch amount of X compute groups (" + itos(p_x_groups) + ") is larger than device limit (" + itos(limits.maxComputeWorkGroupCount[0]) + ")");
ERR_FAIL_COND_MSG(p_y_groups > limits.maxComputeWorkGroupCount[1],
"Dispatch amount of Y compute groups (" + itos(p_x_groups) + ") is larger than device limit (" + itos(limits.maxComputeWorkGroupCount[0]) + ")");
"Dispatch amount of Y compute groups (" + itos(p_y_groups) + ") is larger than device limit (" + itos(limits.maxComputeWorkGroupCount[1]) + ")");
ERR_FAIL_COND_MSG(p_z_groups > limits.maxComputeWorkGroupCount[2],
"Dispatch amount of Z compute groups (" + itos(p_x_groups) + ") is larger than device limit (" + itos(limits.maxComputeWorkGroupCount[0]) + ")");
"Dispatch amount of Z compute groups (" + itos(p_z_groups) + ") is larger than device limit (" + itos(limits.maxComputeWorkGroupCount[2]) + ")");

ERR_FAIL_COND_MSG(!cl->validation.active, "Submitted Compute Lists can no longer be modified.");
#endif
Expand Down
4 changes: 4 additions & 0 deletions editor/editor_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7028,6 +7028,10 @@ EditorNode::EditorNode() {
physical_sky_mat_convert.instantiate();
resource_conversion_plugins.push_back(physical_sky_mat_convert);

Ref<FogMaterialConversionPlugin> fog_mat_convert;
fog_mat_convert.instantiate();
resource_conversion_plugins.push_back(fog_mat_convert);

Ref<VisualShaderConversionPlugin> vshader_convert;
vshader_convert.instantiate();
resource_conversion_plugins.push_back(vshader_convert);
Expand Down
38 changes: 38 additions & 0 deletions editor/plugins/material_editor_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

#include "editor/editor_scale.h"
#include "scene/gui/subviewport_container.h"
#include "scene/resources/fog_material.h"
#include "scene/resources/particles_material.h"
#include "scene/resources/sky_material.h"

Expand Down Expand Up @@ -477,3 +478,40 @@ Ref<Resource> PhysicalSkyMaterialConversionPlugin::convert(const Ref<Resource> &
smat->set_name(mat->get_name());
return smat;
}

String FogMaterialConversionPlugin::converts_to() const {
return "ShaderMaterial";
}

bool FogMaterialConversionPlugin::handles(const Ref<Resource> &p_resource) const {
Ref<FogMaterial> mat = p_resource;
return mat.is_valid();
}

Ref<Resource> FogMaterialConversionPlugin::convert(const Ref<Resource> &p_resource) const {
Ref<FogMaterial> mat = p_resource;
ERR_FAIL_COND_V(!mat.is_valid(), Ref<Resource>());

Ref<ShaderMaterial> smat;
smat.instantiate();

Ref<Shader> shader;
shader.instantiate();

String code = RS::get_singleton()->shader_get_code(mat->get_shader_rid());

shader->set_code(code);

smat->set_shader(shader);

List<PropertyInfo> params;
RS::get_singleton()->shader_get_param_list(mat->get_shader_rid(), &params);

for (const PropertyInfo &E : params) {
Variant value = RS::get_singleton()->material_get_param(mat->get_rid(), E.name);
smat->set_shader_param(E.name, value);
}

smat->set_render_priority(mat->get_render_priority());
return smat;
}
9 changes: 9 additions & 0 deletions editor/plugins/material_editor_plugin.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,4 +152,13 @@ class PhysicalSkyMaterialConversionPlugin : public EditorResourceConversionPlugi
virtual Ref<Resource> convert(const Ref<Resource> &p_resource) const override;
};

class FogMaterialConversionPlugin : public EditorResourceConversionPlugin {
GDCLASS(FogMaterialConversionPlugin, EditorResourceConversionPlugin);

public:
virtual String converts_to() const override;
virtual bool handles(const Ref<Resource> &p_resource) const override;
virtual Ref<Resource> convert(const Ref<Resource> &p_resource) const override;
};

#endif // MATERIAL_EDITOR_PLUGIN_H
Loading

0 comments on commit e7cacfc

Please sign in to comment.