unity-shader-tutorial-planarReflection

buildin效果图

urp效果图

PlanarReflection.shader

PlanarReflection.shader
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
Shader "HHF/Tutorial/PlanarReflection"
{
Properties
{
_BaseColor("Base Color",color) = (1,1,1,1)
_DistortMap("DistortMap",2D) ="white"{}
_Distort("Distort",range(0,0.05)) = 0.2
}

SubShader
{
Tags { "Queue"="Transparent" "RenderPipeline" = "UniversalPipeline" }
// Blend One One
Blend SrcAlpha OneMinusSrcAlpha

Pass
{
HLSLPROGRAM
// Required to compile gles 2.0 with standard srp library
#pragma prefer_hlslcc gles
#pragma exclude_renderers d3d11_9x
#pragma vertex vert
#pragma fragment frag
#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/Lighting.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl"

struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};

struct v2f
{
float4 positionCS : SV_POSITION;
float2 uv : TEXCOORD0;
};

CBUFFER_START(UnityPerMaterial)
half4 _BaseColor;
half4 _DistortMap_ST;
half _Distort;
CBUFFER_END
TEXTURE2D (_ReflectionRT);SAMPLER(sampler_ReflectionRT);
TEXTURE2D (_DistortMap);SAMPLER(sampler_DistortMap);

v2f vert(appdata v)
{
v2f o = (v2f)0;

o.positionCS = TransformObjectToHClip(v.vertex.xyz);
o.uv = v.uv * _DistortMap_ST.xy + _DistortMap_ST.zw+_Time.x;
return o;
}

half4 frag(v2f i) : SV_Target
{
half4 c;
float2 screenUV = i.positionCS.xy / _ScaledScreenParams.xy;
half4 distortMap = SAMPLE_TEXTURE2D(_DistortMap, sampler_DistortMap, i.uv)*2-1;
screenUV = lerp(screenUV.xy, distortMap.xy, _Distort);
half4 baseMap = SAMPLE_TEXTURE2D(_ReflectionRT, sampler_ReflectionRT, screenUV);

c = baseMap * _BaseColor;
return c;
}
ENDHLSL
}

}

SubShader
{
Tags { "Queue"="Transparent" }
Blend SrcAlpha OneMinusSrcAlpha

Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma shader_feature _CALCMODE_MANUAL _CALCMODE_BUILD

#include "UnityCG.cginc"

struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};

struct v2f
{
float4 positionCS : SV_POSITION;
float2 uv : TEXCOORD0;
};

half4 _BaseColor;
sampler2D _ReflectionRT; half4 _ReflectionRT_ST;
sampler2D _DistortMap; half4 _DistortMap_ST;
half _Distort;

v2f vert(appdata v)
{
v2f o = (v2f)0;

o.positionCS = UnityObjectToClipPos(v.vertex.xyz);
o.uv = v.uv * _DistortMap_ST.xy + _DistortMap_ST.zw+_Time.x;
return o;
}

half4 frag(v2f i) : SV_Target
{
half4 c;
float2 screenUV = i.positionCS.xy / _ScreenParams.xy;
half4 distortMap = tex2D(_DistortMap, i.uv) * 2 - 1;
screenUV = lerp(screenUV, distortMap, _Distort);
half4 baseMap = tex2D(_ReflectionRT, screenUV);

c = baseMap * _BaseColor;
return c;
}

ENDCG
}
}
}

PlanarMeshReflection.shader

PlanarMeshReflection.shader
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
Shader "HHF/Tutorial/PlanarMeshReflection"
{
Properties
{
_BaseColor("Base Color",color) = (1,1,1,1)
_BaseMap("BaseMap", 2D) = "white" {}
}

SubShader
{
Tags { "Queue"="Geometry" "RenderType" = "Opaque" "IgnoreProjector" = "True" "RenderPipeline" = "UniversalPipeline" }

Pass
{
Tags {"LightMode" = "SRPDefaultUnlit"}
HLSLPROGRAM
// Required to compile gles 2.0 with standard srp library
#pragma prefer_hlslcc gles
#pragma exclude_renderers d3d11_9x
#pragma vertex vert
#pragma fragment frag
#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/Lighting.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl"

struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
half3 normal : NORMAL;
};

struct v2f
{
float4 positionCS : SV_POSITION;
float2 uv : TEXCOORD0;
half3 normalWS : TEXCOORD3;
};

CBUFFER_START(UnityPerMaterial)
half4 _BaseColor;
float4 _BaseMap_ST;
CBUFFER_END
TEXTURE2D (_BaseMap);SAMPLER(sampler_BaseMap);

v2f vert(appdata v)
{
v2f o = (v2f)0;

o.positionCS = TransformObjectToHClip(v.vertex.xyz);
o.uv = TRANSFORM_TEX(v.uv, _BaseMap);
o.normalWS = TransformObjectToWorldNormal(v.normal);
return o;
}

half4 frag(v2f i) : SV_Target
{
half4 c;
Light light = GetMainLight();
half3 L = light.direction;
half NoV = saturate(dot(i.normalWS,L))*0.5+0.5;
return NoV;
}
ENDHLSL
}

Pass
{
Tags {"LightMode" = "UniversalForward"}
Cull Front
HLSLPROGRAM
// Required to compile gles 2.0 with standard srp library
#pragma prefer_hlslcc gles
#pragma exclude_renderers d3d11_9x
#pragma vertex vert
#pragma fragment frag
#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/Lighting.hlsl"
#include "Packages/com.unity.render-pipelines.core/ShaderLibrary/UnityInstancing.hlsl"

struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
half3 normal : NORMAL;
};

struct v2f
{
float4 positionCS : SV_POSITION;
half3 normalWS : TEXCOORD3;
};

CBUFFER_START(UnityPerMaterial)
half4 _BaseColor;
float4 _BaseMap_ST;
CBUFFER_END
TEXTURE2D (_BaseMap);SAMPLER(sampler_BaseMap);

v2f vert(appdata v)
{
v2f o = (v2f)0;
float3 positionWS = TransformObjectToWorld(v.vertex.xyz);
positionWS.y *= -1;
if(positionWS.y>0) positionWS.y=0;
o.positionCS = TransformWorldToHClip(positionWS);
o.normalWS = TransformObjectToWorldNormal(v.normal);
return o;
}

half4 frag(v2f i) : SV_Target
{
half4 c;
Light light = GetMainLight();
half3 L = light.direction;
half NoV = saturate(dot(i.normalWS,L))*0.5+0.5;
return NoV;
}
ENDHLSL
}
}

SubShader
{
Tags { "RenderType"="Opaque" }

Pass
{
// Tags {"LightMode" = "ForwardBase"}

CGPROGRAM
#pragma vertex vert
#pragma fragment frag

#include "UnityCG.cginc"
#include "Lighting.cginc"
#include "AutoLight.cginc"

struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
half3 normal : NORMAL;
};

struct v2f
{
float4 positionCS : SV_POSITION;
float2 uv : TEXCOORD0;
half3 normalWS : TEXCOORD3;
};

half4 _BaseColor;
sampler2D _BaseMap; float4 _BaseMap_ST;

v2f vert(appdata v)
{
v2f o = (v2f)0;

o.positionCS = UnityObjectToClipPos(v.vertex.xyz);
o.uv = TRANSFORM_TEX(v.uv, _BaseMap);
o.normalWS = UnityObjectToWorldNormal(v.normal);
return o;
}

half4 frag(v2f i) : SV_Target
{
half4 c;
half3 L = _WorldSpaceLightPos0;
half NoV = saturate(dot(i.normalWS, L))*0.5+0.5;
return NoV;
return 0;
}

ENDCG
}

Pass
{
// Tags {"LightMode" = "ForwardBase"}
Cull Front

CGPROGRAM
#pragma vertex vert
#pragma fragment frag

#include "UnityCG.cginc"
#include "Lighting.cginc"
#include "AutoLight.cginc"

struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
half3 normal : NORMAL;
};

struct v2f
{
float4 positionCS : SV_POSITION;
half3 normalWS : TEXCOORD3;
};

half4 _BaseColor;
sampler2D _BaseMap; float4 _BaseMap_ST;

v2f vert(appdata v)
{
v2f o = (v2f)0;
float3 positionWS = mul(UNITY_MATRIX_M, v.vertex); // TransformObjectToWorld(v.vertex.xyz);
positionWS.y *= -1;
if(positionWS.y>0) positionWS.y=0;
o.positionCS = mul(UNITY_MATRIX_VP, float4(positionWS, 1));
o.normalWS = UnityObjectToWorldNormal(v.normal);
return o;
}

half4 frag(v2f i) : SV_Target
{
half4 c;
half3 L = _WorldSpaceLightPos0;
half NoV = saturate(dot(i.normalWS,L))*0.5+0.5;
// return _LightColor0;
return NoV;
}

ENDCG
}

}

}

PlanarReflection.cs

PlanarReflection.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Rendering;

public class PlanarReflection : MonoBehaviour
{
// public Matrix4x4 reflectionM = Matrix4x4.identity;
public Vector3 T = Vector3.zero;
public Vector3 R = Vector3.zero;
public Vector3 S = Vector3.one;
private Camera reflectionCam;
private RenderTexture reflectionRT;

// Start is called before the first frame update
void Start()
{
GameObject go = new GameObject("ReflectionCamera", new System.Type[] { typeof(Camera) });
reflectionCam = go.GetComponent<Camera>();

reflectionRT = RenderTexture.GetTemporary(Screen.width, Screen.height, 0);
reflectionRT.name = "ReflectionRT";
reflectionRT.SetGlobalShaderProperty("_ReflectionRT");

RenderPipelineManager.beginCameraRendering += OnBeginCameraRendering;
}

void OnBeginCameraRendering(ScriptableRenderContext context, Camera camera)
{
if (camera == reflectionCam)
{
camera.CopyFrom(Camera.main);
camera.clearFlags = CameraClearFlags.SolidColor;
camera.backgroundColor = Color.clear;
camera.targetTexture = reflectionRT;
camera.cullingMask = LayerMask.GetMask("Reflection");

GL.invertCulling = true;
// 方法一(写死参数)
// Matrix4x4 reflectionM = Matrix4x4.identity;
// reflectionM.m11 = -1;
// reflectionM[1, 1] = -1;
// camera.worldToCameraMatrix = Camera.main.worldToCameraMatrix * reflectionM;

// 方法二(暴露给用户调)
// camera.worldToCameraMatrix = Camera.main.worldToCameraMatrix * Matrix4x4.TRS(T, Quaternion.Euler(R), S);

// 方法三(高级版)
Matrix4x4 reflectionM = Matrix4x4.identity;
Vector3 N = new Vector3(0, 1, 0);
float OdotN = 2 * Vector3.Dot(transform.position, N);
reflectionM.m00 = 1 - 2 * N.x * N.x;
reflectionM.m01 = -2 * N.x * N.y;
reflectionM.m02 = -2 * N.x * N.z;
reflectionM.m03 = OdotN * N.x;
reflectionM.m10 = -2 * N.x * N.y;
reflectionM.m11 = 1 - 2 * N.y * N.y;
reflectionM.m12 = -2 * N.y * N.z;
reflectionM.m13 = OdotN * N.y;
reflectionM.m20 = -2 * N.x * N.z;
reflectionM.m21 = -2 * N.y * N.z;
reflectionM.m22 = 1 - 2 * N.z * N.z;
reflectionM.m23 = OdotN * N.z;
reflectionM.m30 = 0;
reflectionM.m31 = 0;
reflectionM.m32 = 0;
reflectionM.m33 = 1;
camera.worldToCameraMatrix = Camera.main.worldToCameraMatrix * reflectionM;
}
else
{
GL.invertCulling = false;
}
}

// 此方法用于BuiltIn管线,注意此脚本要挂在Camera上面
void OnPreRender()
{
Debug.LogError(1);
}
}