141 lines
6.0 KiB
GLSL
141 lines
6.0 KiB
GLSL
// Created by Wans Wu
|
|
// Modified: Convert to Lit, support darker blending, alpha mask
|
|
Shader "WansShader/Decal/Lit_test"
|
|
{
|
|
Properties
|
|
{
|
|
[Enum(UnityEngine.Rendering.CullMode)] _CullMode("剔除模式", Float) = 2.0
|
|
[Enum(UnityEngine.Rendering.CompareFunction)]_ZTes("Ztest", Float) = 4.0
|
|
_Tiliing("全局Tiliing", vector) = (1, 1, 0, 0)
|
|
[Main(PBRInput, _, off, off)] _PBRInput("基础属性", Float) = 1
|
|
[Tex(PBRInput,_BaseColor)][MainTexture]_BaseMap("Base贴图", 2D) = "white" {}
|
|
[HideInInspector][MainColor][HDR]_BaseColor("Base颜色", Color) = (1,1,1,1)
|
|
[Sub(PBRInput)]_AddColor("叠加颜色", Color) = (0,0,0,0)
|
|
[Sub(PBRInput)]_Opacity("透明度", Range(0.0, 1.0)) = 1.0
|
|
[Sub(PBRInput)]_Fade("Fade", Range(0.0, 1.0)) = 1.0
|
|
[Main(Emission, _EMISSION, off)]_Emission("发光", Float) = 0
|
|
[Tex(Emission,_EmissionColor)]_EmissionMap("发光贴图", 2D) = "white" {}
|
|
[HideInInspector][HDR]_EmissionColor("发光颜色", Color) = (0,0,0,1)
|
|
[Sub(Emission)]_EmissionStrength("发光强度", Range(0.0, 1.0)) = 1.0
|
|
_AlphaMask("遮罩贴图", 2D) = "white" {}
|
|
}
|
|
|
|
SubShader
|
|
{
|
|
Tags {"RenderType" = "Transparent" "Queue" = "Transparent" "RenderPipeline" = "UniversalPipeline" }
|
|
Pass
|
|
{
|
|
Tags {"LightMode" = "UniversalForward"}
|
|
Cull[_CullMode]
|
|
ZWrite Off
|
|
ZTest[_ZTes]
|
|
Blend SrcAlpha OneMinusSrcAlpha
|
|
|
|
HLSLPROGRAM
|
|
#pragma target 3.5
|
|
#pragma shader_feature_local _EMISSION
|
|
#pragma multi_compile_fog
|
|
|
|
#pragma vertex LitPassVertex
|
|
#pragma fragment LitPassFragment
|
|
|
|
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
|
|
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/UnityInput.hlsl"
|
|
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/DeclareDepthTexture.hlsl"
|
|
|
|
struct Attributes {
|
|
float4 positionOS : POSITION;
|
|
float2 texcoord : TEXCOORD0;
|
|
UNITY_VERTEX_INPUT_INSTANCE_ID
|
|
};
|
|
|
|
struct Varyings {
|
|
float2 uv : TEXCOORD0;
|
|
float3 positionWS : TEXCOORD1;
|
|
float4 positionCS : SV_POSITION;
|
|
float4 positionOS : TEXCOORD2;
|
|
UNITY_VERTEX_OUTPUT_STEREO
|
|
};
|
|
|
|
TEXTURE2D(_BaseMap); SAMPLER(sampler_BaseMap);
|
|
TEXTURE2D(_AlphaMask); SAMPLER(sampler_AlphaMask);
|
|
#if defined (_EMISSION)
|
|
TEXTURE2D(_EmissionMap); SAMPLER(sampler_EmissionMap);
|
|
#endif
|
|
|
|
CBUFFER_START(UnityPerMaterial)
|
|
float4 _BaseColor;
|
|
float4 _AddColor;
|
|
float4 _EmissionColor;
|
|
float4 _Tiliing;
|
|
float _Opacity;
|
|
float _Fade;
|
|
float _EmissionStrength;
|
|
CBUFFER_END
|
|
|
|
float3 ReconstructWorldPos(float2 ScreenUV, float rawdepth) {
|
|
#if UNITY_REVERSED_Z
|
|
real depth = rawdepth;
|
|
#else
|
|
real depth = lerp(UNITY_NEAR_CLIP_VALUE, 1, rawdepth);
|
|
#endif
|
|
return ComputeWorldSpacePosition(ScreenUV, depth, UNITY_MATRIX_I_VP);
|
|
}
|
|
|
|
float LinearStep(float Min, float Max, float Value) {
|
|
return clamp((Value - Min) / (Max - Min), 0, 1);
|
|
}
|
|
|
|
Varyings LitPassVertex(Attributes input) {
|
|
Varyings output = (Varyings)0;
|
|
UNITY_SETUP_INSTANCE_ID(input);
|
|
UNITY_TRANSFER_INSTANCE_ID(input, output);
|
|
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output);
|
|
VertexPositionInputs vertexInput = GetVertexPositionInputs(input.positionOS.xyz);
|
|
output.uv = input.texcoord;
|
|
output.positionWS = vertexInput.positionWS;
|
|
output.positionCS = vertexInput.positionCS;
|
|
output.positionOS = input.positionOS;
|
|
return output;
|
|
}
|
|
|
|
float4 LitPassFragment(Varyings input) : SV_Target {
|
|
UNITY_SETUP_INSTANCE_ID(input);
|
|
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
|
|
|
|
float4 posCS = TransformObjectToHClip(float4(input.positionOS.xyz, 0));
|
|
float4 screenPos = ComputeScreenPos(posCS / posCS.w);
|
|
float screenDepth = SampleSceneDepth(screenPos.xy);
|
|
float3 worldPos = ReconstructWorldPos(screenPos.xy, screenDepth);
|
|
float3 localPos = TransformWorldToObject(float4(worldPos, 1)).xyz + float3(0, 0.5, 0);
|
|
|
|
float3 inBox = step(-0.5, localPos) * (1 - step(0.5, localPos));
|
|
float boxMask = inBox.x * inBox.y * inBox.z;
|
|
|
|
float2 uv = (localPos.xz - float2(-0.5, -0.5)) * _Tiliing.xy + _Tiliing.zw;
|
|
float fade = LinearStep(0, _Fade, -localPos.y);
|
|
|
|
float4 baseCol = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, uv);
|
|
float alphaMask = SAMPLE_TEXTURE2D(_AlphaMask, sampler_AlphaMask, uv).r;
|
|
float alpha = baseCol.a * _Opacity * boxMask * fade * alphaMask;
|
|
float3 baseColor = baseCol.rgb * _BaseColor.rgb + _AddColor.rgb * baseCol.a;
|
|
|
|
float3 emission = 0;
|
|
#if defined (_EMISSION)
|
|
emission = SAMPLE_TEXTURE2D(_EmissionMap, sampler_EmissionMap, uv).rgb * _EmissionColor.rgb * _EmissionStrength;
|
|
#endif
|
|
|
|
float csz = input.positionCS.z * input.positionCS.w;
|
|
float fogFactor = ComputeFogFactor(csz);
|
|
|
|
float4 color = float4(baseColor + emission, saturate(alpha));
|
|
color.rgb = MixFog(color.rgb, fogFactor);
|
|
return color;
|
|
}
|
|
ENDHLSL
|
|
}
|
|
}
|
|
FallBack "Hidden/Universal Render Pipeline/FallbackError"
|
|
CustomEditor "LWGUI.LWGUI"
|
|
}
|