Skip to content

Commit

Permalink
Finish water effects & bump version
Browse files Browse the repository at this point in the history
  • Loading branch information
xxmicloxx committed Feb 5, 2021
1 parent e51e16a commit 7305de1
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 20 deletions.
2 changes: 1 addition & 1 deletion assets/volumetricshading/shaders/ssrout.fsh
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ vec4 raytrace(vec3 fragpos, vec3 rvector) {
vec3 fragpos0 = vec3(pos.st, texture(gDepth, pos.st).r);
fragpos0 = nvec3(invProjectionMatrix * nvec4(fragpos0 * 2.0 - 1.0));
float err = distance(fragpos,fragpos0);
bool isFurther = fragpos0.z < start.z + 0.01;
bool isFurther = fragpos0.z < start.z;
if(err < pow(length(rvector), 1.175) && isFurther) {
hit = true;
hitFragpos0 = fragpos0;
Expand Down
11 changes: 7 additions & 4 deletions assets/volumetricshading/shaders/ssrtopsoil.fsh
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#version 330 core

uniform sampler2D terrainTex;
uniform float rainStrength = 1.0;

in vec3 worldPosition;
in vec4 fragPosition;
Expand All @@ -17,16 +18,18 @@ layout(location = 2) out vec4 outTint;

#include noise3d.ash

void main()
void main()
{
if (normal.y <= 0) discard; // we only want top

vec4 color = texture(terrainTex, uv);
if (color.a < 0.02) discard;

float noise = gnoise(worldPosition * 10);
noise += cnoise(worldPosition * 50) * 0.5;
float alpha = 0.0;
float alpha = 1.0 - pow(rainStrength, 0.7);

if (normal.y <= 0) {
alpha = 1.0 - (1.0 - alpha) * 0.3;
}

vec3 normal = gnormal.xyz;
normal += vec3(noise * 0.05);
Expand Down
2 changes: 1 addition & 1 deletion modinfo.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"name": "Volumetric Shading",
"authors": [ "xxmicloxx" ],
"description": "Adds simple volumetric shading that hopefully looks good.",
"version": "0.2.7",
"version": "0.2.8",
"side": "Client",
"dependency": {
"game": ""
Expand Down
9 changes: 9 additions & 0 deletions src/ModSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,15 @@ public static bool SSDOEnabled
set => ClientSettings.Inst.Bool["volumetricshading_SSDO"] = value;
}

public static bool SSRRainReflectionsEnabled
{
get => ClientSettings.Inst.GetBoolSetting("volumetricshading_SSRRainReflections");
set => ClientSettings.Inst.Bool["volumetricshading_SSRRainReflections"] = value;
}

public static bool SSRRainReflectionsEnabledSet =>
ClientSettings.Inst.Bool.Exists("volumetricshading_SSRRainReflections");

public static int SSRWaterTransparency
{
get => ClientSettings.Inst.GetIntSetting("volumetricshading_SSRWaterTransparency");
Expand Down
64 changes: 53 additions & 11 deletions src/ScreenSpaceReflections.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Reflection;
using OpenTK.Graphics.OpenGL;
using Vintagestory.API.Client;
Expand All @@ -14,6 +15,7 @@ public class ScreenSpaceReflections : IRenderer
private readonly VolumetricShadingMod _mod;

private bool _enabled;
private bool _rainEnabled;

private FrameBufferRef _ssrFramebuffer;
private FrameBufferRef _ssrOutFramebuffer;
Expand All @@ -32,6 +34,10 @@ public class ScreenSpaceReflections : IRenderer

private int _fbWidth;
private int _fbHeight;

private float _currentRain;
private float _targetRain;
private float _rainAccumulator;

private readonly float[] _invProjectionMatrix;
private readonly float[] _invModelViewMatrix;
Expand All @@ -49,7 +55,10 @@ public ScreenSpaceReflections(VolumetricShadingMod mod)
mod.Events.PreFinalRender += OnSetFinalUniforms;

_enabled = ModSettings.ScreenSpaceReflectionsEnabled;
_rainEnabled = ModSettings.SSRRainReflectionsEnabled;

mod.CApi.Settings.AddWatcher<bool>("volumetricshading_screenSpaceReflections", OnEnabledChanged);
mod.CApi.Settings.AddWatcher<bool>("volumetricshading_SSRRainReflections", OnRainReflectionsChanged);

mod.CApi.Event.RegisterRenderer(this, EnumRenderStage.Opaque, "ssrWorldSpace");
mod.CApi.Event.RegisterRenderer(this, EnumRenderStage.AfterOIT, "ssrOut");
Expand Down Expand Up @@ -89,6 +98,11 @@ private void OnEnabledChanged(bool enabled)
{
_enabled = enabled;
}

private void OnRainReflectionsChanged(bool enabled)
{
_rainEnabled = enabled;
}

private bool ReloadShaders()
{
Expand Down Expand Up @@ -181,6 +195,7 @@ public void OnRenderFrame(float deltaTime, EnumRenderStage stage)

if (stage == EnumRenderStage.Opaque)
{
OnPreRender(deltaTime);
OnRenderSsrChunks();
}
else if (stage == EnumRenderStage.AfterOIT)
Expand All @@ -189,6 +204,29 @@ public void OnRenderFrame(float deltaTime, EnumRenderStage stage)
}
}

private void OnPreRender(float dt)
{
_rainAccumulator += dt;
if (_rainAccumulator > 5f)
{
_rainAccumulator = 0f;
var climate = _game.BlockAccessor.GetClimateAt(_game.EntityPlayer.Pos.AsBlockPos,
EnumGetClimateMode.NowValues);

var rainMul = GameMath.Clamp((climate.Temperature + 1f) / 4f, 0f, 1f);
_targetRain = climate.Rainfall * rainMul;
}

if (_targetRain > _currentRain)
{
_currentRain = Math.Min(_currentRain + dt * 0.15f, _targetRain);
}
else if (_targetRain < _currentRain)
{
_currentRain = Math.Max(_currentRain - dt * 0.01f, _targetRain);
}
}

private void OnRenderSsrOut()
{
if (_ssrOutFramebuffer == null) return;
Expand Down Expand Up @@ -288,18 +326,22 @@ private void OnRenderSsrChunks()

shader.Stop();

shader = _ssrTopsoilShader;
shader.Use();
shader.UniformMatrix("projectionMatrix", _mod.CApi.Render.CurrentProjectionMatrix);
shader.UniformMatrix("modelViewMatrix", _mod.CApi.Render.CurrentModelviewMatrix);
pools = _chunkRenderer.poolsByRenderPass[(int) EnumChunkRenderPass.TopSoil];
for (var i = 0; i < textureIds.Length; ++i)
if (_rainEnabled)
{
shader.BindTexture2D("terrainTex", textureIds[i], 0);
pools[i].Render(cameraPos, "origin");
shader = _ssrTopsoilShader;
shader.Use();
shader.UniformMatrix("projectionMatrix", _mod.CApi.Render.CurrentProjectionMatrix);
shader.UniformMatrix("modelViewMatrix", _mod.CApi.Render.CurrentModelviewMatrix);
shader.Uniform("rainStrength", _currentRain);
pools = _chunkRenderer.poolsByRenderPass[(int) EnumChunkRenderPass.TopSoil];
for (var i = 0; i < textureIds.Length; ++i)
{
shader.BindTexture2D("terrainTex", textureIds[i], 0);
pools[i].Render(cameraPos, "origin");
}

shader.Stop();
}

shader.Stop();

shader = _ssrLiquidShader;
shader.Use();
Expand Down
16 changes: 16 additions & 0 deletions src/ScreenSpaceReflectionsGui.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ public ScreenSpaceReflectionsGui(ICoreClientAPI capi) : base(capi)
ToggleAction = ToggleSSR
});

RegisterOption(new ConfigOption
{
SwitchKey = "toggleRainReflections",
Text = "Enable wet grass",
Tooltip = "Enables reflecting grass when raining",
ToggleAction = ToggleRainReflections
});

RegisterOption(new ConfigOption
{
SliderKey = "dimmingSlider",
Expand Down Expand Up @@ -60,6 +68,7 @@ public ScreenSpaceReflectionsGui(ICoreClientAPI capi) : base(capi)
protected override void RefreshValues()
{
SingleComposer.GetSwitch("toggleSSR").SetValue(ModSettings.ScreenSpaceReflectionsEnabled);
SingleComposer.GetSwitch("toggleRainReflections").SetValue(ModSettings.SSRRainReflectionsEnabled);
SingleComposer.GetSlider("dimmingSlider").SetValues(ModSettings.SSRReflectionDimming, 1, 400, 1);
SingleComposer.GetSlider("transparencySlider").SetValues(ModSettings.SSRWaterTransparency, 0, 100, 1);
SingleComposer.GetSlider("tintSlider").SetValues(ModSettings.SSRTintInfluence, 0, 100, 1);
Expand All @@ -76,6 +85,13 @@ private void ToggleSSR(bool on)
capi.Shader.ReloadShaders();
RefreshValues();
}

private void ToggleRainReflections(bool on)
{
ModSettings.SSRRainReflectionsEnabled = on;

RefreshValues();
}

private bool OnDimmingSliderChanged(int value)
{
Expand Down
6 changes: 3 additions & 3 deletions src/ShadowTweaksGui.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ public ShadowTweaksGui(ICoreClientAPI capi) : base(capi)
Text = "Near base width",
Tooltip = "Sets the base width of the near shadow map. Increases sharpness of near shadows," +
"but decreases sharpness of mid-distance ones. Unmodified game value is 30.",
SlideAction = OnShadowBaseWidthSliderChanged
SlideAction = OnShadowBaseWidthSliderChanged,
InstantSlider = true
});

RegisterOption(new ConfigOption
Expand Down Expand Up @@ -50,7 +51,6 @@ protected override void RefreshValues()
private bool OnShadowBaseWidthSliderChanged(int value)
{
ModSettings.NearShadowBaseWidth = value;
capi.Shader.ReloadShaders();
return true;
}

Expand All @@ -60,7 +60,7 @@ private bool OnNearPeterPanningChanged(int value)
capi.Shader.ReloadShaders();
return true;
}

private bool OnFarPeterPanningChanged(int value)
{
ModSettings.FarPeterPanningAdjustment = value;
Expand Down
5 changes: 5 additions & 0 deletions src/VolumetricShadingMod.cs
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ private static void SetConfigDefaults()
{
ModSettings.FarPeterPanningAdjustment = 5;
}

if (!ModSettings.SSRRainReflectionsEnabledSet)
{
ModSettings.SSRRainReflectionsEnabled = true;
}
}

private bool OnConfigurePressed(KeyCombination cb)
Expand Down

0 comments on commit 7305de1

Please sign in to comment.