Skip to content

Commit

Permalink
Merge pull request #139 from SaulMyers/master
Browse files Browse the repository at this point in the history
Cleaning up the shader for the 'forest portals'
  • Loading branch information
kirides committed Aug 16, 2023
2 parents 6629ce0 + d68b7c9 commit 8913913
Showing 1 changed file with 25 additions and 31 deletions.
56 changes: 25 additions & 31 deletions D3D11Engine/Shaders/PS_PortalDiffuse.hlsl
@@ -1,5 +1,5 @@
//--------------------------------------------------------------------------------------
// World/VOB-Pixelshader for G2D3D11 by SaulMyers
// World/VOB-Pixelshader for Gothic 1 D3D11 by SaulMyers
//--------------------------------------------------------------------------------------
#include <DS_Defines.h>
#include <AtmosphericScattering.h>
Expand All @@ -8,19 +8,14 @@
// Textures and Samplers
//--------------------------------------------------------------------------------------
SamplerState SS_Linear : register(s0);
SamplerState SS_samMirror : register(s1);
Texture2D TX_Texture0 : register(t0);
Texture2D TX_Texture1 : register(t1);
Texture2D TX_Texture2 : register(t2);
TextureCube TX_ReflectionCube : register(t4);

//--------------------------------------------------------------------------------------
// Input / Output structures
//--------------------------------------------------------------------------------------
struct PS_INPUT
{
float2 vTexcoord : TEXCOORD0;
float2 vTexcoord2 : TEXCOORD1;
float4 vDiffuse : TEXCOORD2;
float3 vNormalVS : TEXCOORD4;
float3 vViewPosition : TEXCOORD5;
Expand All @@ -34,29 +29,28 @@ DEFERRED_PS_OUTPUT PSMain(PS_INPUT Input) : SV_TARGET
{
//how far is the nameless hero from the object?
float distFromCamera = distance(Input.vViewPosition, Input.vPosition);

//start / end distances for fading
float startFade = 6000.0f;
float completeFade = 5000.0f;

//how much to fade the object by
float percentageFade = (distFromCamera - completeFade) / (startFade - completeFade);

//keep the fade in bounds
if (percentageFade < 0) {percentageFade = 0.0f;}
if (percentageFade > 1) {percentageFade = 1.0f;}

//darken the portals depending on where the sun is in the sky
float darknessFactor = 2.0f;
if (AC_LightPos.y <= 0.05f) { darknessFactor += (1 - AC_LightPos.y) * 6; }
else if (AC_LightPos.y > 0.05f) { darknessFactor = 7.5f - (1 + AC_LightPos.y) * 3; }

//sample the texture we want to fade out and apply relevant darkness factor for day / night cycle
float4 color = TX_Texture0.Sample(SS_Linear, Input.vTexcoord) / darknessFactor;

//apply fade
DEFERRED_PS_OUTPUT output;
output.vDiffuse = float4(color.rgb, percentageFade);

return output;

//correct issue with transparency of forest portals for certain camera angles
if (Input.vViewPosition.x > 0) { distFromCamera = distFromCamera + (Input.vViewPosition.x * clamp(Input.vViewPosition.x / 9000, 0, 1)); }

//start / end distances for fading
float startFade = 6500.0f;
float completeFade = 5500.0f;

//how much to fade the object by
float percentageFade = clamp((distFromCamera - completeFade) / (startFade - completeFade), 0, 1);

//darken the portals depending on where the sun is in the sky
float darknessFactor = 4.0f;
if (AC_LightPos.y <= 0.05f) { darknessFactor += (1 - AC_LightPos.y) * 6; }
else if (AC_LightPos.y > 0.05f) { darknessFactor = 7.5f - (1 + AC_LightPos.y) * 3; }

//sample the texture we want to fade out and apply relevant darkness factor for day / night cycle
float4 color = TX_Texture0.Sample(SS_Linear, Input.vTexcoord) / darknessFactor;

//apply fade
DEFERRED_PS_OUTPUT output;
output.vDiffuse = float4(color.rgb, percentageFade);

return output;
}

0 comments on commit 8913913

Please sign in to comment.