Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deferred fixes/improvements #24

Merged
merged 3 commits into from
Nov 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file modified GameData/KerbalKonstructs/Shaders/kkshaders.linux
Binary file not shown.
Binary file modified GameData/KerbalKonstructs/Shaders/kkshaders.osx
Binary file not shown.
Binary file modified GameData/KerbalKonstructs/Shaders/kkshaders.windows
Binary file not shown.
16 changes: 11 additions & 5 deletions Unity/Shaders/Diffuse_Multiply_Random_Normal.shader
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ Shader "KK/Diffuse_Multiply_Random"

Stencil
{
Ref 4
Ref 3
Comp Always
Pass Replace
}
Expand All @@ -34,10 +34,11 @@ Shader "KK/Diffuse_Multiply_Random"
#pragma exclude_renderers gles

// Physically based Standard lighting model, and enable shadows on all light types
#pragma surface surf BlinnPhong fullforwardshadows nofog
#pragma surface surf BlinnPhongKSP fullforwardshadows nofog

#pragma target 3.0

#include "LightingKSP.cginc"
#include "KSP-include.cginc"
#include "TileRandom2-include.cginc"
//#include "TileRandom-include.cginc"
Expand All @@ -55,7 +56,6 @@ Shader "KK/Diffuse_Multiply_Random"
//float4 _EmissiveColor;

//standard shader params for adjusting color/etc
float4 _Color;
float _MakeGrayScale;

struct Input
Expand Down Expand Up @@ -125,11 +125,17 @@ Shader "KK/Diffuse_Multiply_Random"
o.Emission *= fog.a;
o.Specular = (0);
o.Normal = normal;
// o.SpecularColor = (0,0,0,0);

#if UNITY_PASS_DEFERRED
// In deferred rendering do not use the flat ambient because Deferred adds its own ambient as a composite of flat ambient and probe
unity_SHAr = 0.0.xxxx;
unity_SHAg = 0.0.xxxx;
unity_SHAb = 0.0.xxxx;
#endif
}

ENDCG

}
Fallback "Standard"
}
}
17 changes: 12 additions & 5 deletions Unity/Shaders/Ground_Multi_NoUV_Normal.shader
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,23 @@ Shader "KK/Ground_Multi_NoUV"

Stencil
{
Ref 4
Ref 3
Comp Always
Pass Replace
}

CGPROGRAM
// Upgrade NOTE: excluded shader from OpenGL ES 2.0 because it uses non-square matrices
#pragma exclude_renderers gles
//
#pragma surface surf BlinnPhong fullforwardshadows nofog


#pragma surface surf BlinnPhongKSP fullforwardshadows nofog

// Use shader model 3.0 target, to get nicer looking lighting
#pragma target 3.0

//#include "TileRandom-include.cginc"
#include "TileRandom2-include.cginc"
#include "LightingKSP.cginc"
#include "KSP-include.cginc"

sampler2D _BlendMaskTexture;
Expand Down Expand Up @@ -306,7 +306,14 @@ Shader "KK/Ground_Multi_NoUV"
o.Albedo = fog.rgb;
o.Emission *= fog.a;
o.Specular = (0);
// o.SpecularColor = (0,0,0,0);


#if UNITY_PASS_DEFERRED
// In deferred rendering do not use the flat ambient because Deferred adds its own ambient as a composite of flat ambient and probe
unity_SHAr = 0.0.xxxx;
unity_SHAg = 0.0.xxxx;
unity_SHAb = 0.0.xxxx;
#endif
}

ENDCG
Expand Down
26 changes: 0 additions & 26 deletions Unity/Shaders/KSP-include.cginc
Original file line number Diff line number Diff line change
@@ -1,33 +1,7 @@
float4 _LocalCameraPos;
float4 _LocalCameraDir;
float4 _UnderwaterFogColor;
float _UnderwaterMinAlphaFogDistance;
float _UnderwaterMaxAlbedoFog;
float _UnderwaterMaxAlphaFog;
float _UnderwaterAlbedoDistanceScalar;
float _UnderwaterAlphaDistanceScalar;
float _UnderwaterFogFactor;


float _Opacity;
float4 _TemperatureColor;
float4 _RimColor;
float _RimFalloff;



//stock fog function
fixed4 UnderwaterFog(fixed3 worldPos, fixed3 color)
{
fixed3 toPixel = worldPos - _LocalCameraPos.xyz;
fixed toPixelLength = length(toPixel);

fixed underwaterDetection = _UnderwaterFogFactor * _LocalCameraDir.w; ///< sign(1 - sign(_LocalCameraPos.w));
fixed albedoLerpValue = underwaterDetection * (_UnderwaterMaxAlbedoFog * saturate(toPixelLength * _UnderwaterAlbedoDistanceScalar));
fixed alphaFactor = 1 - underwaterDetection * (_UnderwaterMaxAlphaFog * saturate((toPixelLength - _UnderwaterMinAlphaFogDistance) * _UnderwaterAlphaDistanceScalar));

return fixed4(lerp(color, _UnderwaterFogColor.rgb, albedoLerpValue), alphaFactor);
}

inline half3 stockEmit (float3 viewDir, float3 normal, half4 rimColor, half rimFalloff, half4 tempColor)
{
Expand Down
162 changes: 162 additions & 0 deletions Unity/Shaders/LightingKSP.cginc
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
// WHAT IS THIS FILE?
// this file provides a replacement for the LightingKSP.cginc file that ships with part tools for writing custom shaders.
// This version enables support for the Deferred mod
//
// HOW DO I USE IT?
// Step 1)
// replace LightingKSP.cginc in your shader folder with this file, if present. If you aren't using LightingKSP.cginc
// in your shader, add the following below `CGPROGRAM`:
// `#include "../LightingKSP.cginc"`
//
// Step 2)
// add the following above `CGPROGRAM`, respect the stencil values described in
// https://github.com/LGhassen/Deferred?tab=readme-ov-file#stencil-buffer-usage
// ```
// Stencil
// {
// Ref 1
// Comp Always
// Pass Replace
// }
// ```
//
// Step 3)
// there should be a line in your shader that looks like this:
// `#pragma surface surf BlinnPhongSmooth keepalpha`
// Remove the `keepalpha` if it's there. the part after `surf` is the name of the lighting function your shader uses now.
// If the lighting function is `BlinnPhong` or `BlinnPhongSmooth`, change it to `BlinnPhongKSP`
// If the lighting function is `Standard`, change it to `StandardKSP`
// If the lighting function is `StandardSpecular`, change it to `StandardSpecularKSP`

#ifndef LIGHTING_KSP_INCLUDED
#define LIGHTING_KSP_INCLUDED

#include "UnityPBSLighting.cginc"

#define blinnPhongShininessPower 0.215

// An exact conversion from blinn-phong to PBR is impossible, but the look can be approximated perceptually
// and by observing how blinn-phong looks and feels at various settings, although it can never be perfect
// 1) The specularColor can be used as is in the PBR specular flow, just needs to be divided by PI so it sums up to 1 over the hemisphere
// 2) Blinn-phong shininess doesn't stop feeling shiny unless at very low values, like below 0.04
// while the PBR smoothness feels more linear -> map shininess to smoothness accordingly using a function
// that increases very quickly at first then slows down, I went with something like x^(1/4) or x^(1/6) then made the power configurable
// I tried various mappings from the literature but nothing really worked as well as this
// 3) Finally I noticed that some parts still looked very shiny like the AV-R8 winglet while in stock they looked rough thanks a low
// specularColor but high shininess and specularMap, so I multiplied the smoothness by the sqrt of the specularColor and that caps
// the smoothness when specularColor is low
void GetStandardSpecularPropertiesFromLegacy(float legacyShininess, float specularMap, out float3 specular,
out float smoothness)
{
float3 legacySpecularColor = saturate(_SpecColor);

smoothness = pow(legacyShininess, blinnPhongShininessPower) * specularMap;
smoothness *= sqrt(length(legacySpecularColor));

specular = legacySpecularColor * UNITY_INV_PI;
}

float4 _Color;

// LEGACY BLINN-PHONG LIGHTING FUNCTION FOR KSP WITH PBR CONVERSION FOR DEFERRED

inline float4 LightingBlinnPhongKSP(SurfaceOutput s, half3 viewDir, UnityGI gi)
{
return LightingBlinnPhong(s,viewDir, gi);
}

inline float4 LightingBlinnPhongKSP_Deferred(SurfaceOutput s, float3 worldViewDir, UnityGI gi,
out float4 outDiffuseOcclusion, out float4 outSpecSmoothness,
out float4 outNormal)
{
SurfaceOutputStandardSpecular ss;
ss.Albedo = s.Albedo;
ss.Normal = s.Normal;
ss.Emission = s.Emission;
ss.Occlusion = 1;
ss.Alpha = saturate(s.Alpha);
GetStandardSpecularPropertiesFromLegacy(s.Specular, s.Gloss, ss.Specular, ss.Smoothness);

return LightingStandardSpecular_Deferred(ss, worldViewDir, gi, outDiffuseOcclusion, outSpecSmoothness, outNormal);
}

inline void LightingBlinnPhongKSP_GI(inout SurfaceOutput s, UnityGIInput gi_input, inout UnityGI gi)
{
#ifndef UNITY_PASS_DEFERRED
gi = UnityGlobalIllumination(gi_input, 1.0, s.Normal);
#endif
}

// STANDARD UNITY LIGHTING FUNCTION FOR KSP

inline float4 LightingStandardKSP(SurfaceOutputStandard s, float3 worldViewDir, UnityGI gi)
{
return LightingStandard(s, worldViewDir, gi); // no change
}

inline float4 LightingStandardKSP_Deferred(SurfaceOutputStandard s, float3 worldViewDir, UnityGI gi,
out float4 outDiffuseOcclusion,
out float4 outSpecSmoothness, out float4 outNormal)
{
return LightingStandard_Deferred(s, worldViewDir, gi, outDiffuseOcclusion, outSpecSmoothness, outNormal);
}

inline void LightingStandardKSP_GI(inout SurfaceOutputStandard s, UnityGIInput gi_input, inout UnityGI gi)
{
#ifndef UNITY_PASS_DEFERRED
LightingStandard_GI(s, gi_input, gi);
#endif
}

// STANDARD SPECULAR UNITY LIGHTING FUNCTION FOR KSP

inline float4 LightingStandardSpecularKSP(SurfaceOutputStandardSpecular s, float3 worldViewDir, UnityGI gi)
{
return LightingStandardSpecular(s, worldViewDir, gi); // no change
}

inline float4 LightingStandardSpecularKSP_Deferred(SurfaceOutputStandardSpecular s, float3 worldViewDir, UnityGI gi,
out float4 outDiffuseOcclusion,
out float4 outSpecSmoothness, out float4 outNormal)
{
return LightingStandardSpecular_Deferred(s, worldViewDir, gi, outDiffuseOcclusion, outSpecSmoothness, outNormal);
}

inline void LightingStandardSpecularKSP_GI(inout SurfaceOutputStandardSpecular s, UnityGIInput gi_input,
inout UnityGI gi)
{
#ifndef UNITY_PASS_DEFERRED
LightingStandardSpecular_GI(s, gi_input, gi);
#endif
}

float4 _LocalCameraPos;
float4 _LocalCameraDir;
float4 _UnderwaterFogColor;
float _UnderwaterMinAlphaFogDistance;
float _UnderwaterMaxAlbedoFog;
float _UnderwaterMaxAlphaFog;
float _UnderwaterAlbedoDistanceScalar;
float _UnderwaterAlphaDistanceScalar;
float _UnderwaterFogFactor;

float4 UnderwaterFog(float3 worldPos, float3 color)
{
// skip fog in deferred mode
#ifdef UNITY_PASS_DEFERRED
return float4(color, 1);
#endif

float3 toPixel = worldPos - _LocalCameraPos.xyz;
float toPixelLength = length(toPixel); ///< Comment out the math--looks better without it.

float underwaterDetection = _UnderwaterFogFactor * _LocalCameraDir.w; ///< sign(1 - sign(_LocalCameraPos.w));
float albedoLerpValue = underwaterDetection * (_UnderwaterMaxAlbedoFog * saturate(
toPixelLength * _UnderwaterAlbedoDistanceScalar));
float alphaFactor = 1 - underwaterDetection * (_UnderwaterMaxAlphaFog * saturate(
(toPixelLength - _UnderwaterMinAlphaFogDistance) * _UnderwaterAlphaDistanceScalar));

return float4(lerp(color, _UnderwaterFogColor.rgb, albedoLerpValue), alphaFactor);
}

#endif
2 changes: 1 addition & 1 deletion Unity/Shaders/NormalFromTexture.shader
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

Stencil
{
Ref 4
Ref 3
Comp Always
Pass Replace
}
Expand Down