Shader "JZ/CusustomMaskLightShader"
{
Properties
{
[Enable]_ScaleX("缩放X",float) = 1
[Enable]_ScaleY("缩放Y",float) = 1
_OffsetCenterX("亮点X",Range(0,1))=0.5
_OffsetCenterY("亮点Y",Range(0,1))=0.5
_BaseColor("背景色",Color) = (0,0,0,1)
_SmoothStep("柔和度",Range(0,0.5))=0
_BaseMul("范围(相乘)",float)=100
_BasePow("范围(对比度pow)",float)=100
_BaseAdd("整体范围(相加)",Range(0,1))=0
_CirWidth("圆形的高",float)=1.5
_CirHeight("圆形的宽",float)=1.5
_FinalAlpha("整体透明值",Range(0,1))=1
}
SubShader
{
Tags { "Queue"="Transparent+1999" "RenderType" = "Transparent" "IgnoreProjector" = "True" "RenderPipeline" = "UniversalPipeline" }
LOD 100
Pass
{
Name "CustomShader"
Blend SrcAlpha OneMinusSrcAlpha
ZTest Always
HLSLPROGRAM
// Required to compile gles 2.0 with standard srp library
#pragma vertex vert
#pragma fragment frag
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
struct Attributes
{
float4 positionOS : POSITION;
float2 uv : TEXCOORD0;
};
struct Varyings
{
float4 positionCS : SV_POSITION;
float2 uv : TEXCOORD0;
};
CBUFFER_START(UnityPerMaterial)
half4 _BaseColor;
float2 _LightPos;
float _SmoothStep;
float _BasePow;
float _BaseMul;
float _BaseAdd;
float _CirWidth;
float _CirHeight;
float _FinalAlpha;
float _OffsetCenterX;
float _OffsetCenterY;
float _ScaleX;
float _ScaleY;
CBUFFER_END
float4x4 Scale(float4 scale)
{
return float4x4(scale.x, 0.0, 0.0, 0.0,
0.0, scale.y, 0.0, 0.0,
0.0, 0.0, scale.z, 0.0,
0.0, 0.0, 0.0, 1.0);
}
Varyings vert(Attributes v)
{
Varyings o = (Varyings)0;
v.positionOS = mul(Scale(float4(_ScaleX,1,_ScaleY,1)), v.positionOS);
float4 ori_pos=mul(UNITY_MATRIX_MV,float4(0,0,0,1));
float4 positionOS= v.positionOS;
positionOS.y=positionOS.z;
positionOS.z=0;
positionOS.xyz+=ori_pos.xyz;
o.positionCS=mul(UNITY_MATRIX_P,positionOS*1);
// o.positionCS = TransformObjectToHClip(v.positionOS.xyz);
o.uv=v.uv;
return o;
}
half4 frag(Varyings i) : SV_Target
{
float2 screen_pos=i.uv;
float2 light_pos =float2(_OffsetCenterX,_OffsetCenterY);
float Width=_ScaleX/_ScaleY;
float Height=_ScaleY/_ScaleX;
float light_distance=0;
if (_ScaleX>_ScaleY)
{
screen_pos.x/=_CirHeight;
screen_pos.y/=_CirWidth;
screen_pos.x*=Width;
light_pos=float2((light_pos.x*Width)/_CirHeight ,(light_pos.y)/_CirWidth);
light_distance=length(screen_pos-light_pos);
light_distance=smoothstep(_SmoothStep,1-_SmoothStep,light_distance);
light_distance=saturate(pow(max((light_distance*_BaseMul),0),_BasePow)+_BaseAdd);
}
else
{
screen_pos.x/=_CirHeight;
screen_pos.y/=_CirWidth;
screen_pos.y*=Height;
light_pos=float2((light_pos.x)/_CirHeight ,(light_pos.y*Height)/_CirWidth);
light_distance=length(screen_pos-light_pos);
light_distance=smoothstep(_SmoothStep,1-_SmoothStep,light_distance);
light_distance=saturate(pow(max((light_distance*_BaseMul),0),max(_BasePow,0))+_BaseAdd);
}
half4 c =_BaseColor;
light_distance=lerp(light_distance,_FinalAlpha,light_distance);
return float4(c.xyz,light_distance);
}
ENDHLSL
}
}
}