在上一篇文章中,我们实现了能量罩外发光的效果。但是,这样看我们的能量罩十分单调不好看。在这篇文章中,我们给能量罩加上花纹、扭曲流光,使其变好看。
_MainTex(“MainTex”,2D) = “white”{}
TEXTURE2D(_MainTex);SAMPLER(sampler_MainTex);float4 _MainTex_ST;
o.uv.zw = TRANSFORM_TEX(v.uv,_MainTex);
float4 mainTex = SAMPLE_TEXTURE2D(_MainTex,sampler_MainTex,i.uv.zw);
col *= mainTex;
o.uv.xy = v.uv;
col *= frac(i.uv.y * _Flow + _Time.y);
TEXTURE2D(_CameraOpaqueTexture);SAMPLER(sampler_CameraOpaqueTexture);
float4 distortTex = SAMPLE_TEXTURE2D(_MainTex,sampler_MainTex,i.uv.zw + float2(0,_Time.x));
float2 distortUV = lerp(uv,distortTex.rr,_Distort);
half4 opaqueTex = SAMPLE_TEXTURE2D(_CameraOpaqueTexture,sampler_CameraOpaqueTexture,distortUV);
half4 distort = opaqueTex;
col *= distort;
return col;
//流光扭曲花纹
Shader "MyShader/URP/P4_3_7"
{
Properties
{
[Header(MainTex)]
_MainTex("MainTex",2D) = "white"{}
[Header(HighLight)]
_HighLightColor("HighLightColor",Color) = (0,0,0,0)
_HighLightFade("HighLight",Float) = 1.0
[Header(Fresnel)]
_FresnelIntensity("FresnelIntensity",Range(1,15)) = 1.0
_FresnelColor("FresnelColor",Color) = (0,0,0,0)
[Header(Flow)]
_Flow("Flow",Float)=0
_Distort("Distort",Range(0,1)) = 0.3
}
SubShader
{
Tags
{
//告诉引擎,该Shader只用于 URP 渲染管线
"RenderPipeline"="UniversalPipeline"
//渲染类型
"RenderType"="Transparent"
//渲染队列
"Queue"="Transparent"
}
Pass
{
Blend One One
ZWrite Off
Name "Unlit"
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
// Pragmas
#pragma target 2.0
// Includes
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/Color.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Input.hlsl"
CBUFFER_START(UnityPerMaterial)
half4 _HighLightColor;
half _HighLightFade;
half _FresnelIntensity;
half4 _FresnelColor;
half _Flow;
half _Distort;
CBUFFER_END
TEXTURE2D(_MainTex);SAMPLER(sampler_MainTex);float4 _MainTex_ST;
TEXTURE2D(_CameraDepthTexture);SAMPLER(sampler_CameraDepthTexture);
TEXTURE2D(_CameraOpaqueTexture);SAMPLER(sampler_CameraOpaqueTexture);
//struct appdata
//顶点着色器的输入
struct Attributes
{
float3 positionOS : POSITION;
float2 uv : TEXCOORD0;
half3 normalOS : NORMAL;
};
//struct v2f
//片元着色器的输入
struct Varyings
{
float4 positionCS : SV_POSITION;
float4 uv : TEXCOORD0;
float4 screenPos : TEXCOORD1;
float3 positionWS : TEXCOORD2;
float3 positionVS : TEXCOORD3;
half3 normalWS : TEXCOORD4;
};
//v2f vert(Attributes v)
//顶点着色器
Varyings vert(Attributes v)
{
Varyings o = (Varyings)0;
o.positionWS = TransformObjectToWorld(v.positionOS);
o.positionVS = TransformWorldToView(o.positionWS);
o.positionCS = TransformWViewToHClip(o.positionVS);
o.screenPos = ComputeScreenPos(o.positionCS);
o.normalWS = TransformObjectToWorldNormal(v.normalOS);
o.uv.xy = v.uv;
o.uv.zw = TRANSFORM_TEX(v.uv,_MainTex);
return o;
}
//fixed4 frag(v2f i) : SV_TARGET
//片元着色器
half4 frag(Varyings i) : SV_TARGET
{
half4 col=0;
//深度图
//float2 uv = i.screenPos.xy / i.screenPos.w;
float2 uv = i.positionCS.xy/ _ScreenParams.xy;
float4 cameraDepthTex = SAMPLE_TEXTURE2D(_CameraDepthTexture,sampler_CameraDepthTexture,uv);
float depthTex = LinearEyeDepth(cameraDepthTex.r,_ZBufferParams);
float depth = depthTex + i.positionVS.z;
float4 highLight = 1 - depth;
highLight = pow(saturate(highLight),_HighLightFade);
highLight *= _HighLightColor;
col += saturate(highLight);
//fresnel外发光
//pow(max(0,dot(N,V)),Intensity)
half3 N = normalize(i.normalWS);
half3 V = normalize(_WorldSpaceCameraPos - i.positionWS);
half NdotV = dot(N,V);
half4 fresnel = pow(max(0,1 - NdotV),_FresnelIntensity);
fresnel *= _FresnelColor;
col += fresnel;
//能量罩花纹
float4 mainTex = SAMPLE_TEXTURE2D(_MainTex,sampler_MainTex,i.uv.zw);
col += mainTex * 0.15;
//流光
half flowMask = frac(i.uv.y * _Flow + _Time.y);
col *= flowMask;
//抓屏流光扭曲
float4 distortTex = SAMPLE_TEXTURE2D(_MainTex,sampler_MainTex,i.uv.zw + float2(0,_Time.x));
float2 distortUV = lerp(uv,distortTex.rr,_Distort);
half4 opaqueTex = SAMPLE_TEXTURE2D(_CameraOpaqueTexture,sampler_CameraOpaqueTexture,distortUV);
half4 distort = opaqueTex;
col *= distort;
return col;
}
ENDHLSL
}
}
}