More decal shader reorganization

This commit is contained in:
Andrew Cassidy 2024-07-29 22:32:11 -07:00
parent ce55f07150
commit 946975249d
20 changed files with 182 additions and 296 deletions

View File

@ -0,0 +1,156 @@
Shader "ConformalDecals/Decal/{% block shader_name %}UNKNOWN{% endblock %}"
{
Properties
{
// Shader-specific properties
{% block properties %}
{% endblock %}
// Common decal properties
_Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
_DecalOpacity("Opacity", Range(0,1) ) = 1
_Background("Background Color", Color) = (0.9,0.9,0.9,0.7)
[Enum(UnityEngine.Rendering.CullMode)] _Cull ("Cull", int) = 2
[Toggle] _ZWrite ("ZWrite", Float) = 1.0
[Toggle(DECAL_PREVIEW)] _Preview ("Preview", int) = 0
[Header(Effects)]
[PerRendererData]_Opacity("_Opacity", Range(0,1) ) = 1
[PerRendererData]_Color("_Color", Color) = (1,1,1,1)
[PerRendererData]_RimFalloff("_RimFalloff", Range(0.01,5) ) = 0.1
[PerRendererData]_RimColor("_RimColor", Color) = (0,0,0,0)
[PerRendererData]_UnderwaterFogFactor ("Underwater Fog Factor", Range(0,1)) = 0
}
SubShader
{
Tags
{
"Queue" = "Geometry+100" "IgnoreProjector" = "true" "DisableBatching" = "true"
}
Cull [_Cull]
Pass
{
Name "FORWARD"
Tags
{
"LightMode" = "ForwardBase"
}
ZWrite [_ZWrite]
ZTest LEqual
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert
#pragma fragment frag_forward
#pragma multi_compile_fwdbase
#pragma skip_variants LIGHTMAP_ON DIRLIGHTMAP_COMBINED DYNAMICLIGHTMAP_ON LIGHTMAP_SHADOW_MIXING
#pragma multi_compile_local __ DECAL_PREVIEW
{% block body %}
#error No body provided
{% endblock %}
ENDCG
}
Pass
{
Name "FORWARD"
Tags
{
"LightMode" = "ForwardAdd"
}
ZWrite Off
ZTest LEqual
Blend SrcAlpha One
Offset -1, -1
CGPROGRAM
#pragma vertex vert
#pragma fragment frag_forward
#pragma multi_compile DIRECTIONAL SPOT POINT
#pragma multi_compile __ LIGHTPROBE_SH
#pragma multi_compile_local __ DECAL_PREVIEW
{{ self.body() }}
ENDCG
}
Pass
{
Name "DEFERRED_PREPASS"
Tags
{
"LightMode" = "Deferred"
}
ZWrite Off
ZTest LEqual
Offset -1, -1
Blend 1 Zero OneMinusSrcColor, Zero OneMinusSrcAlpha
Stencil
{
Ref 1
Comp Equal
Pass Keep
}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag_deferred_prepass
#pragma target 3.0
#pragma multi_compile_local __ DECAL_PREVIEW
{{ self.body() }}
{% block pragmas_deferred_prepass %}
{% endblock %}
ENDCG
}
Pass
{
Name "DEFERRED"
Tags
{
"LightMode" = "Deferred"
}
ZWrite Off
ZTest LEqual
Offset -1, -1
Blend 0 SrcAlpha OneMinusSrcAlpha, Zero One
Blend 1 One One
Blend 2 SrcAlpha OneMinusSrcAlpha, Zero One
Blend 3 SrcAlpha OneMinusSrcAlpha, Zero One
Stencil
{
Ref 1
Comp Equal
Pass Keep
}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag_deferred
#pragma target 3.0
#pragma multi_compile __ LIGHTPROBE_SH
#pragma multi_compile __ UNITY_HDR_ON
#pragma multi_compile_local __ DECAL_PREVIEW
{{ self.body() }}
ENDCG
}
}
}

View File

@ -3,7 +3,7 @@
#include "AutoLight.cginc"
#include "Lighting.cginc"
#include "LightingKSPDeferred.cginc"
#include "../LightingKSPDeferred.cginc"
#define CLIP_MARGIN 0.05
#define EDGE_MARGIN 0.01

View File

@ -1,4 +1,4 @@
#include "../DecalsCommon.cginc"
#include "DecalsCommon.cginc"
#include "../SDF.cginc"
void surf(DecalSurfaceInput IN, inout SurfaceOutput o) {

View File

@ -1,9 +1,7 @@
Shader "{% block shader_name %}ConformalDecals/Decal/Standard{% endblock %}"
{
Properties
{
// Shader-specific properties
//{% block properties %}
{% extends "DecalBase.shader.template" %}
{% block shader_name %}Standard{% endblock %}
{% block properties %}
[Header(Decal)]
_Decal("Decal Texture", 2D) = "gray" {}
[Toggle(DECAL_SDF_ALPHA)] _Decal_SDF_Alpha ("SDF in Alpha", int) = 0
@ -17,7 +15,7 @@
[Header(Specularity)]
[Toggle(DECAL_SPECMAP)] _Decal_SpecMap ("Has SpecMap", int) = 0
_SpecMap ("Specular Map)", 2D) = "black" {}
_SpecMap ("Specular Map", 2D) = "black" {}
_SpecColor ("_SpecColor", Color) = (0.25, 0.25, 0.25, 1)
_Shininess ("Shininess", Range (0.03, 10)) = 0.3
@ -25,161 +23,18 @@
[Toggle(DECAL_EMISSIVE)] _Decal_Emissive ("Has Emissive", int) = 0
_Emissive("_Emissive", 2D) = "black" {}
_EmissiveColor("_EmissiveColor", Color) = (0,0,0,1)
//{% endblock %}
{% endblock %}
// Common decal properties
_Cutoff ("Alpha cutoff", Range(0,1)) = 0.5
_DecalOpacity("Opacity", Range(0,1) ) = 1
_Background("Background Color", Color) = (0.9,0.9,0.9,0.7)
[Enum(UnityEngine.Rendering.CullMode)] _Cull ("Cull", int) = 2
[Toggle] _ZWrite ("ZWrite", Float) = 1.0
[Toggle(DECAL_PREVIEW)] _Preview ("Preview", int) = 0
[Header(Effects)]
[PerRendererData]_Opacity("_Opacity", Range(0,1) ) = 1
[PerRendererData]_Color("_Color", Color) = (1,1,1,1)
[PerRendererData]_RimFalloff("_RimFalloff", Range(0.01,5) ) = 0.1
[PerRendererData]_RimColor("_RimColor", Color) = (0,0,0,0)
[PerRendererData]_UnderwaterFogFactor ("Underwater Fog Factor", Range(0,1)) = 0
}
SubShader
{
Tags
{
"Queue" = "Geometry+100" "IgnoreProjector" = "true" "DisableBatching" = "true"
}
Cull [_Cull]
Pass
{
Name "FORWARD"
Tags
{
"LightMode" = "ForwardBase"
}
ZWrite [_ZWrite]
ZTest LEqual
Blend SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert
#pragma fragment frag_forward
#pragma multi_compile DIRECTIONAL
#pragma multi_compile_local __ DECAL_PREVIEW
#pragma multi_compile_local __ DECAL_BASE_NORMAL DECAL_BUMPMAP
//{% block pragmas %}
{% block body %}
#pragma multi_compile_local __ DECAL_BASE_NORMAL
#pragma multi_compile_local __ DECAL_SPECMAP
#pragma multi_compile_local __ DECAL_EMISSIVE
#pragma multi_compile_local __ DECAL_SDF_ALPHA
//{% endblock %}
//{% block body %}
#include "StandardDecal.cginc"
//{% endblock %}
{% endblock %}
ENDCG
}
Pass
{
Name "FORWARD"
Tags
{
"LightMode" = "ForwardAdd"
}
ZWrite Off
ZTest LEqual
Blend SrcAlpha One
Offset -1, -1
CGPROGRAM
#pragma vertex vert
#pragma fragment frag_forward
#pragma multi_compile DIRECTIONAL SPOT POINT
#pragma multi_compile_local __ DECAL_PREVIEW
#pragma multi_compile_local __ DECAL_BASE_NORMAL DECAL_BUMPMAP
//{{ self.pragmas() }}
//{{ self.body() }}
ENDCG
}
Pass
{
Name "DEFERRED_PREPASS"
Tags
{
"LightMode" = "Deferred"
}
ZWrite Off
ZTest LEqual
Offset -1, -1
Blend 1 Zero OneMinusSrcColor, Zero OneMinusSrcAlpha
Stencil
{
Ref 1
Comp Equal
Pass Keep
}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag_deferred_prepass
#pragma target 3.0
#pragma multi_compile_local __ DECAL_PREVIEW
#pragma multi_compile_local __ DECAL_BASE_NORMAL
//{{ self.pragmas() }}
//{% block prepass_skip_variants %}
#pragma skip_variants DECAL_SPECMAP DECAL_EMISSIVE
//{% endblock %}
#define DECAL_PREPASS
//{{ self.body() }}
ENDCG
}
Pass
{
Name "DEFERRED"
Tags
{
"LightMode" = "Deferred"
}
ZWrite Off
ZTest LEqual
Offset -1, -1
Blend 0 SrcAlpha OneMinusSrcAlpha, Zero One
Blend 1 One One
Blend 2 SrcAlpha OneMinusSrcAlpha, Zero One
Blend 3 SrcAlpha OneMinusSrcAlpha, Zero One
Stencil
{
Ref 1
Comp Equal
Pass Keep
}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag_deferred
#pragma target 3.0
#pragma multi_compile __ UNITY_HDR_ON
#pragma multi_compile_local __ DECAL_PREVIEW
#pragma multi_compile_local __ DECAL_BASE_NORMAL DECAL_BUMPMAP
//{{ self.pragmas() }}
//{{ self.body() }}
ENDCG
}
}
}
{% block pragmas_deferred_prepass %}
#pragma skip_variants DECAL_SPECMAP DECAL_EMISSIVE DECAL_BUMPMAP
{% endblock %}

View File

@ -1,4 +1,4 @@
#include "../DecalsCommon.cginc"
#include "DecalsCommon.cginc"
#include "../SDF.cginc"
float4 _DecalColor;

View File

@ -1,5 +1,5 @@
{% extends "StandardDecal.shader.template" %}
{% block shader_name %}ConformalDecals/Decal/Text{% endblock %}
{% extends "DecalBase.shader.template" %}
{% block shader_name %}Text{% endblock %}
{% block properties %}
[Header(Decal)]
@ -27,13 +27,15 @@
_Shininess ("Shininess", Range (0.03, 10)) = 0.3
{% endblock %}
{% block pragmas %}
{% block body %}
#pragma multi_compile_local __ DECAL_BASE_NORMAL
#pragma multi_compile_local __ DECAL_SPECMAP
#pragma multi_compile_local __ DECAL_OUTLINE
#pragma multi_compile_local __ DECAL_FILL
#pragma skip_variants DECAL_BUMPMAP
#include "TextDecal.cginc"
{% endblock %}
{% block body %}
#include "TextDecal.cginc"
{% block pragmas_deferred_prepass %}
#pragma skip_variants DECAL_SPECMAP
{% endblock %}

View File

@ -63,12 +63,12 @@ Shader "ConformalDecals/Decal Back"
// 45° rotation
uv_MainTex.x = IN.uv_MainTex.x - IN.uv_MainTex.y;
uv_MainTex.y = IN.uv_MainTex.x + IN.uv_MainTex.y;
// stagger every other row
uv_MainTex.y *= 2;
int row = floor(uv_MainTex.y);
uv_MainTex.x += row * _RowOffset;
uv_MainTex.y *= 2;
float4 color = _Color * tex2D(_MainTex,(uv_MainTex));
float3 normal = UnpackNormal(tex2D(_BumpMap, uv_BumpMap));

View File

@ -1,25 +0,0 @@
// Taken from https://github.com/LGhassen/Deferred
//TODO: remove this debug settings
float blinnPhongShininessPower = 0.215f;
// 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, float3 legacySpecularColor,
out float smoothness, out float3 specular)
{
legacySpecularColor = saturate(legacySpecularColor);
smoothness = pow(legacyShininess, 0.215) * specularMap;
smoothness *= sqrt(length(legacySpecularColor));
specular = legacySpecularColor * (1 / UNITY_PI);
}

View File

@ -1,102 +0,0 @@
#ifndef LIGHTING_KSP_INCLUDED
#define LIGHTING_KSP_INCLUDED
inline fixed4 LightingBlinnPhongSmooth(SurfaceOutput s, fixed3 lightDir, half3 viewDir, fixed atten)
{
s.Normal = normalize(s.Normal);
half3 h = normalize(lightDir + viewDir);
fixed diff = max(0, dot(s.Normal, lightDir));
float nh = max(0, dot(s.Normal, h));
float spec = pow(nh, s.Specular*128.0) * s.Gloss;
fixed4 c;
c.rgb = (s.Albedo * _LightColor0.rgb * diff + _LightColor0.rgb * _SpecColor.rgb * spec) * (atten);
c.a = s.Alpha + _LightColor0.a * _SpecColor.a * spec * atten;
return c;
}
inline half4 LightingUnlit(SurfaceOutput s, half3 lightDir, half atten)
{
// half diff = max (0, dot (s.Normal, lightDir));
half4 c;
c.rgb = s.Albedo;
c.a = s.Alpha;
return c;
}
inline half4 LightingUnlit_PrePass(SurfaceOutput s, half4 light)
{
half4 c;
c.rgb = s.Albedo;
c.a = s.Alpha;
return c;
}
fixed4 LightingNoLighting(SurfaceOutput s, fixed3 lightDir, fixed atten) { return fixed4(0, 0, 0, 0); }
float4 _Color;
half _LightBoost;
half4 LightingLightWrapped(SurfaceOutput s, half3 lightDir, half3 viewDir, half atten) {
float3 w = _Color.rgb*0.5;
half3 NdotL = dot(s.Normal, lightDir);
//Specular term
half3 h = normalize(lightDir + viewDir);
s.Normal = normalize(s.Normal);
float NdotH = dot(s.Normal, h);
float spec = pow(max(NdotH, 0), s.Specular * 128.0) * s.Gloss;
fixed3 specColor = _SpecColor.rgb * _LightColor0.rgb;
half3 diff = NdotL * (1 - w) + w;
half4 c;
c.rgb = ((s.Albedo * _LightColor0.rgb * diff) + (specColor * spec)) * (atten * _LightBoost);
c.a = s.Alpha + (_LightColor0.a * _SpecColor.a * spec * atten);
return c;
}
float4 _LocalCameraPos;
float4 _LocalCameraDir;
float4 _UnderwaterFogColor;
float _UnderwaterMinAlphaFogDistance;
float _UnderwaterMaxAlbedoFog;
float _UnderwaterMaxAlphaFog;
float _UnderwaterAlbedoDistanceScalar;
float _UnderwaterAlphaDistanceScalar;
float _UnderwaterFogFactor;
float4 UnderwaterFog(float3 worldPos, float3 color)
{
float3 toPixel = worldPos - _LocalCameraPos.xyz;
float toPixelLength = length(toPixel); ///< Comment out the math--looks better without it.
//float angleDot = dot(_LocalCameraDir.xyz, toPixel / toPixelLength);
//angleDot = lerp(0.00000001, angleDot, saturate(sign(angleDot)));
//float waterDist = -_LocalCameraPos.w / angleDot;
//float dist = min(toPixelLength, waterDist);
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

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

After

Width:  |  Height:  |  Size: 74 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 48 KiB

Binary file not shown.