|
| 1 | +// WHAT IS THIS FILE? |
| 2 | +// this file provides a replacement for the LightingKSP.cginc file that ships with part tools for writing custom shaders. |
| 3 | +// This version enables support for the Deferred mod |
| 4 | +// |
| 5 | +// HOW DO I USE IT? |
| 6 | +// Step 1) |
| 7 | +// replace LightingKSP.cginc in your shader folder with this file, if present. If you aren't using LightingKSP.cginc |
| 8 | +// in your shader, add the following below `CGPROGRAM`: |
| 9 | +// `#include "../LightingKSP.cginc"` |
| 10 | +// |
| 11 | +// Step 2) |
| 12 | +// add the following above `CGPROGRAM`, respect the stencil values described in |
| 13 | +// https://github.com/LGhassen/Deferred?tab=readme-ov-file#stencil-buffer-usage |
| 14 | +// ``` |
| 15 | +// Stencil |
| 16 | +// { |
| 17 | +// Ref 1 |
| 18 | +// Comp Always |
| 19 | +// Pass Replace |
| 20 | +// } |
| 21 | +// ``` |
| 22 | +// |
| 23 | +// Step 3) |
| 24 | +// there should be a line in your shader that looks like this: |
| 25 | +// `#pragma surface surf BlinnPhongSmooth keepalpha` |
| 26 | +// Remove the `keepalpha` if it's there. the part after `surf` is the name of the lighting function your shader uses now. |
| 27 | +// If the lighting function is `BlinnPhong` or `BlinnPhongSmooth`, change it to `BlinnPhongKSP` |
| 28 | +// If the lighting function is `Standard`, change it to `StandardKSP` |
| 29 | +// If the lighting function is `StandardSpecular`, change it to `StandardSpecularKSP` |
| 30 | + |
| 31 | +#ifndef LIGHTING_KSP_INCLUDED |
| 32 | +#define LIGHTING_KSP_INCLUDED |
| 33 | + |
| 34 | +#include "UnityPBSLighting.cginc" |
| 35 | + |
| 36 | +#define blinnPhongShininessPower 0.215 |
| 37 | + |
| 38 | +// An exact conversion from blinn-phong to PBR is impossible, but the look can be approximated perceptually |
| 39 | +// and by observing how blinn-phong looks and feels at various settings, although it can never be perfect |
| 40 | +// 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 |
| 41 | +// 2) Blinn-phong shininess doesn't stop feeling shiny unless at very low values, like below 0.04 |
| 42 | +// while the PBR smoothness feels more linear -> map shininess to smoothness accordingly using a function |
| 43 | +// 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 |
| 44 | +// I tried various mappings from the literature but nothing really worked as well as this |
| 45 | +// 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 |
| 46 | +// specularColor but high shininess and specularMap, so I multiplied the smoothness by the sqrt of the specularColor and that caps |
| 47 | +// the smoothness when specularColor is low |
| 48 | +void GetStandardSpecularPropertiesFromLegacy(float legacyShininess, float specularMap, out float3 specular, |
| 49 | + out float smoothness) |
| 50 | +{ |
| 51 | + float3 legacySpecularColor = saturate(_SpecColor); |
| 52 | + |
| 53 | + smoothness = pow(legacyShininess, blinnPhongShininessPower) * specularMap; |
| 54 | + smoothness *= sqrt(length(legacySpecularColor)); |
| 55 | + |
| 56 | + specular = legacySpecularColor * UNITY_INV_PI; |
| 57 | +} |
| 58 | + |
| 59 | +float4 _Color; |
| 60 | + |
| 61 | +// LEGACY BLINN-PHONG LIGHTING FUNCTION FOR KSP WITH PBR CONVERSION FOR DEFERRED |
| 62 | + |
| 63 | +inline float4 LightingBlinnPhongKSP(SurfaceOutput s, half3 viewDir, UnityGI gi) |
| 64 | +{ |
| 65 | + return LightingBlinnPhong(s,viewDir, gi); |
| 66 | +} |
| 67 | + |
| 68 | +inline float4 LightingBlinnPhongKSP_Deferred(SurfaceOutput s, float3 worldViewDir, UnityGI gi, |
| 69 | + out float4 outDiffuseOcclusion, out float4 outSpecSmoothness, |
| 70 | + out float4 outNormal) |
| 71 | +{ |
| 72 | + SurfaceOutputStandardSpecular ss; |
| 73 | + ss.Albedo = s.Albedo; |
| 74 | + ss.Normal = s.Normal; |
| 75 | + ss.Emission = s.Emission; |
| 76 | + ss.Occlusion = 1; |
| 77 | + ss.Alpha = saturate(s.Alpha); |
| 78 | + GetStandardSpecularPropertiesFromLegacy(s.Specular, s.Gloss, ss.Specular, ss.Smoothness); |
| 79 | + |
| 80 | + return LightingStandardSpecular_Deferred(ss, worldViewDir, gi, outDiffuseOcclusion, outSpecSmoothness, outNormal); |
| 81 | +} |
| 82 | + |
| 83 | +inline void LightingBlinnPhongKSP_GI(inout SurfaceOutput s, UnityGIInput gi_input, inout UnityGI gi) |
| 84 | +{ |
| 85 | + #ifndef UNITY_PASS_DEFERRED |
| 86 | + gi = UnityGlobalIllumination(gi_input, 1.0, s.Normal); |
| 87 | + #endif |
| 88 | +} |
| 89 | + |
| 90 | +// STANDARD UNITY LIGHTING FUNCTION FOR KSP |
| 91 | + |
| 92 | +inline float4 LightingStandardKSP(SurfaceOutputStandard s, float3 worldViewDir, UnityGI gi) |
| 93 | +{ |
| 94 | + return LightingStandard(s, worldViewDir, gi); // no change |
| 95 | +} |
| 96 | + |
| 97 | +inline float4 LightingStandardKSP_Deferred(SurfaceOutputStandard s, float3 worldViewDir, UnityGI gi, |
| 98 | + out float4 outDiffuseOcclusion, |
| 99 | + out float4 outSpecSmoothness, out float4 outNormal) |
| 100 | +{ |
| 101 | + return LightingStandard_Deferred(s, worldViewDir, gi, outDiffuseOcclusion, outSpecSmoothness, outNormal); |
| 102 | +} |
| 103 | + |
| 104 | +inline void LightingStandardKSP_GI(inout SurfaceOutputStandard s, UnityGIInput gi_input, inout UnityGI gi) |
| 105 | +{ |
| 106 | + #ifndef UNITY_PASS_DEFERRED |
| 107 | + LightingStandard_GI(s, gi_input, gi); |
| 108 | + #endif |
| 109 | +} |
| 110 | + |
| 111 | +// STANDARD SPECULAR UNITY LIGHTING FUNCTION FOR KSP |
| 112 | + |
| 113 | +inline float4 LightingStandardSpecularKSP(SurfaceOutputStandardSpecular s, float3 worldViewDir, UnityGI gi) |
| 114 | +{ |
| 115 | + return LightingStandardSpecular(s, worldViewDir, gi); // no change |
| 116 | +} |
| 117 | + |
| 118 | +inline float4 LightingStandardSpecularKSP_Deferred(SurfaceOutputStandardSpecular s, float3 worldViewDir, UnityGI gi, |
| 119 | + out float4 outDiffuseOcclusion, |
| 120 | + out float4 outSpecSmoothness, out float4 outNormal) |
| 121 | +{ |
| 122 | + return LightingStandardSpecular_Deferred(s, worldViewDir, gi, outDiffuseOcclusion, outSpecSmoothness, outNormal); |
| 123 | +} |
| 124 | + |
| 125 | +inline void LightingStandardSpecularKSP_GI(inout SurfaceOutputStandardSpecular s, UnityGIInput gi_input, |
| 126 | + inout UnityGI gi) |
| 127 | +{ |
| 128 | + #ifndef UNITY_PASS_DEFERRED |
| 129 | + LightingStandardSpecular_GI(s, gi_input, gi); |
| 130 | + #endif |
| 131 | +} |
| 132 | + |
| 133 | +float4 _LocalCameraPos; |
| 134 | +float4 _LocalCameraDir; |
| 135 | +float4 _UnderwaterFogColor; |
| 136 | +float _UnderwaterMinAlphaFogDistance; |
| 137 | +float _UnderwaterMaxAlbedoFog; |
| 138 | +float _UnderwaterMaxAlphaFog; |
| 139 | +float _UnderwaterAlbedoDistanceScalar; |
| 140 | +float _UnderwaterAlphaDistanceScalar; |
| 141 | +float _UnderwaterFogFactor; |
| 142 | + |
| 143 | +float4 UnderwaterFog(float3 worldPos, float3 color) |
| 144 | +{ |
| 145 | + // skip fog in deferred mode |
| 146 | + #ifdef UNITY_PASS_DEFERRED |
| 147 | + return float4(color, 1); |
| 148 | + #endif |
| 149 | + |
| 150 | + float3 toPixel = worldPos - _LocalCameraPos.xyz; |
| 151 | + float toPixelLength = length(toPixel); ///< Comment out the math--looks better without it. |
| 152 | + |
| 153 | + float underwaterDetection = _UnderwaterFogFactor * _LocalCameraDir.w; ///< sign(1 - sign(_LocalCameraPos.w)); |
| 154 | + float albedoLerpValue = underwaterDetection * (_UnderwaterMaxAlbedoFog * saturate( |
| 155 | + toPixelLength * _UnderwaterAlbedoDistanceScalar)); |
| 156 | + float alphaFactor = 1 - underwaterDetection * (_UnderwaterMaxAlphaFog * saturate( |
| 157 | + (toPixelLength - _UnderwaterMinAlphaFogDistance) * _UnderwaterAlphaDistanceScalar)); |
| 158 | + |
| 159 | + return float4(lerp(color, _UnderwaterFogColor.rgb, albedoLerpValue), alphaFactor); |
| 160 | +} |
| 161 | + |
| 162 | +#endif |
0 commit comments