Skip to content

Instantly share code, notes, and snippets.

@stilllisisi
Created March 11, 2020 09:36
Show Gist options
  • Save stilllisisi/7f181b38daf48114c5acd8db3ac3fd92 to your computer and use it in GitHub Desktop.
Save stilllisisi/7f181b38daf48114c5acd8db3ac3fd92 to your computer and use it in GitHub Desktop.
【游戏-Shader】Shader灯光遮罩(水纹,散焦等)效果实现
//原理
//1. 利用灯光投射范围
//2. 利用法线RG偏移贴图
Shader "QQ/LightWave"
{
Properties
{
_Color("Color",Color) = (0,0,0,1)
_MainTex("Texture", 2D) = "white" {}
_WaveTex("Wave",2D) = "white" {}
_BumpTex("Bump", 2D) = "white" {}
_HeatForce("HeatForce",float) = 1
}
SubShader
{
Tags { "RenderType" = "Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#pragma multi_compile_fog
#pragma target 3.0
struct a2v
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float4 vertex : SV_POSITION;
float2 uv: TEXCOORD0;
UNITY_FOG_COORDS(1)
};
fixed4 _Color;
sampler2D _MainTex;
float4 _MainTex_ST;
v2f vert(a2v v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = TRANSFORM_TEX(v.uv, _MainTex);
UNITY_TRANSFER_FOG(o, o.vertex);
return o;
}
fixed4 frag(v2f i) : SV_Target
{
fixed4 col = tex2D(_MainTex, i.uv)*_Color;
UNITY_APPLY_FOG(i.fogCoord, col);
return col;
}
ENDCG
}
Pass{
Tags{"LightMode" = "ForwardAdd"}
Blend One One
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#define UNITY_PASS_FORWARDADD
#include "UnityCG.cginc"
#include "AutoLight.cginc"
#pragma multi_compile_fwdadd_fullshadows
#pragma multi_compile_fog
#pragma target 3.0
struct a2v
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
float3 normal:NORMAL;
};
struct v2f
{
float4 vertex : SV_POSITION;
UNITY_FOG_COORDS(0)
LIGHTING_COORDS(1, 2)
float2 uv[2] : TEXCOORD3;
float atten : Texcoord5;
};
float4 _LightColor0;
sampler2D _WaveTex;
sampler2D _BumpTex;
float4 _WaveTex_ST;
float4 _BumpTex_ST;
float _HeatForce;
v2f vert(a2v v)
{
v2f o;
o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
float4 wPos = mul(unity_ObjectToWorld,v.vertex);
o.atten = max(0, dot(UnityObjectToWorldNormal(v.normal), UnityWorldSpaceLightDir(wPos)));
_WaveTex_ST.zw *= _Time.x;
_BumpTex_ST.zw *= _Time.x;
o.uv[0] = TRANSFORM_TEX(v.uv, _WaveTex);
o.uv[1] = TRANSFORM_TEX(v.uv, _BumpTex);
UNITY_TRANSFER_FOG(o,o.vertex);
TRANSFER_VERTEX_TO_FRAGMENT(o)
return o;
}
fixed4 frag(v2f i) : SV_Target
{
i.uv[0] += tex2D(_BumpTex, i.uv[1]).rg*_HeatForce;
fixed4 col = tex2D(_WaveTex, i.uv[0]);
col *= i.atten*_LightColor0*LIGHT_ATTENUATION(i)*col.a;
UNITY_APPLY_FOG(i.fogCoord, col);
return col;
}
ENDCG
}
}
}
@stilllisisi
Copy link
Author

@stilllisisi
Copy link
Author

1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment