151 lines
6.4 KiB
GLSL
151 lines
6.4 KiB
GLSL
//Created by Wans Wu
|
|
//2025,05,08
|
|
Shader "WansShader/Decal/Unlit"
|
|
{
|
|
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(PBRIntput, _, off, off)] _PBRIntput ("基础属性", Float) = 1
|
|
[Tex(PBRIntput,_BaseColor)][MainTexture] _BaseMap("Base贴图", 2D) = "white" {}
|
|
[HideInInspector][MainColor][HDR] _BaseColor("Base颜色", Color) = (1,1,1,1)
|
|
[Sub(PBRIntput)] _AddColor("叠加 颜色", Color) = (0,0,0,0)
|
|
[Sub(PBRIntput)] _Intensity("亮度", Float) =1.0
|
|
[Sub(PBRIntput)] _Opacity("透明度", Range(0.0, 1.0)) =1.0
|
|
[Sub(PBRIntput)] _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
|
|
}
|
|
|
|
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);
|
|
#if defined (_EMISSION)
|
|
TEXTURE2D(_EmissionMap);
|
|
SAMPLER(sampler_EmissionMap);
|
|
#endif
|
|
CBUFFER_START(UnityPerMaterial)
|
|
float4 _ReflectionCubeMap_HDR;
|
|
half4 _BaseColor;
|
|
half4 _AddColor;
|
|
half4 _EmissionColor;
|
|
half _EmissionStrength;
|
|
half4 _Tiliing;
|
|
half _Opacity;
|
|
half _Fade;
|
|
half _Intensity;
|
|
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
|
|
float3 worldPos = ComputeWorldSpacePosition(ScreenUV, depth, UNITY_MATRIX_I_VP);
|
|
return worldPos;
|
|
}
|
|
|
|
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 positionCSNDC = float4(posCS.xyz/ posCS.w,1.0);
|
|
float4 screenPos = ComputeScreenPos(positionCSNDC);
|
|
float screenDepth = SampleSceneDepth(screenPos.xy);
|
|
float3 ReconstPos = ReconstructWorldPos(screenPos.xy,screenDepth);
|
|
float3 ReconstPosOS = TransformWorldToObject(float4(ReconstPos,0));
|
|
|
|
float3 ReconstPosOSOffset = ReconstPosOS + half3(0,0.5,0);
|
|
float3 PosStep = step(-0.5,ReconstPosOSOffset) * (1 - step(0.5,ReconstPosOSOffset));
|
|
float BoundingBox = PosStep.x * PosStep.y * PosStep.z;
|
|
|
|
|
|
float2 decaluv = (ReconstPosOS.xz + 0.5 ) * _Tiliing.xy + _Tiliing.zw;
|
|
|
|
half decalFade = LinearStep(0,_Fade,-ReconstPosOS.y);
|
|
|
|
float4 BaseColorAlpha = SAMPLE_TEXTURE2D(_BaseMap,sampler_BaseMap,decaluv) ;
|
|
float3 BaseColor = BaseColorAlpha.rgb *_BaseColor.rgb * _Intensity + _AddColor.rgb;
|
|
float4 EmissonMap = 0;
|
|
#if defined (_EMISSION)
|
|
EmissonMap = (SAMPLE_TEXTURE2D(_EmissionMap,sampler_EmissionMap,decaluv)* _EmissionColor )* _EmissionStrength;
|
|
#else
|
|
EmissonMap = half4(0,0,0,0);
|
|
#endif
|
|
float4 EmissonColor = EmissonMap;
|
|
float csz = input.positionCS.z * input.positionCS.w;
|
|
real fogFactor = ComputeFogFactor(csz);
|
|
half finalAlpha = saturate(BaseColorAlpha.a * _Opacity * BoundingBox * decalFade);
|
|
float4 color = half4(BaseColor + EmissonColor.rgb ,finalAlpha);
|
|
color.rgb = MixFog(color.rgb,fogFactor) ;
|
|
return color;
|
|
|
|
}
|
|
ENDHLSL
|
|
}
|
|
}
|
|
FallBack "Hidden/Universal Render Pipeline/FallbackError"
|
|
CustomEditor "LWGUI.LWGUI"
|
|
}
|