forked from ipud2/Unity-Basic-Shader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBasicURP.shader
192 lines (153 loc) · 6.76 KB
/
BasicURP.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
Shader "BasicURP"
{
Properties
{
_BaseMap("Texture", 2D) = "white" {}
_BaseColor("Color", Color) = (1, 1, 1, 1)
}
SubShader
{
Tags { "RenderType" = "Opaque" "RenderPipeline" = "UniversalPipeline" }
Pass
{
Name "Unlit"
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
#pragma shader_feature _ALPHATEST_ON
#pragma shader_feature _ALPHAPREMULTIPLY_ON
// -------------------------------------
// Unity defined keywords
#pragma multi_compile_fog
#pragma multi_compile_instancing
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Lighting.hlsl"
TEXTURE2D(_BaseMap); SAMPLER(sampler_BaseMap);
CBUFFER_START(UnityPerMaterial)
float4 _BaseColor;
CBUFFER_END
struct Attributes
{
float4 positionOS : POSITION;
float3 normalOS : NORMAL;
float4 tangentOS : TANGENT;
float2 uv : TEXCOORD0;
UNITY_VERTEX_INPUT_INSTANCE_ID
};
struct Varyings
{
float2 uv : TEXCOORD0;
float4 normalWS : TEXCOORD1; // xyz: normal, w: viewDir.x
float4 tangentWS : TEXCOORD2; // xyz: tangent, w: viewDir.y
float4 bitangentWS : TEXCOORD3; // xyz: bitangent, w: viewDir.z
float3 viewDirWS : TEXCOORD4;
float4 positionCS : SV_POSITION;
UNITY_VERTEX_INPUT_INSTANCE_ID
UNITY_VERTEX_OUTPUT_STEREO
};
Varyings vert(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);
VertexNormalInputs normalInput = GetVertexNormalInputs(input.normalOS, input.tangentOS);
float3 viewDirWS = GetCameraPositionWS() - vertexInput.positionWS;
float3 vertexLight = VertexLighting(vertexInput.positionWS, normalInput.normalWS);
output.positionCS = vertexInput.positionCS;
output.uv = input.uv;
output.normalWS = float4(normalInput.normalWS, viewDirWS.x);
output.tangentWS = float4(normalInput.tangentWS, viewDirWS.y);
output.bitangentWS = float4(normalInput.bitangentWS, viewDirWS.z);
output.viewDirWS = viewDirWS;
return output;
}
float4 frag(Varyings input) : SV_Target
{
UNITY_SETUP_INSTANCE_ID(input);
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(input);
float2 uv = input.uv;
// float4 BaseColor = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, uv);
float3 N = normalize( input.normalWS.xyz);
float3 T = normalize(input.tangentWS.xyz);
float3 B = normalize(input.bitangentWS.xyz);
float3 V = normalize(input.viewDirWS.xyz);
float3 L = normalize(_MainLightPosition.xyz);
float3 H = normalize(V+L);
float NV = dot(N,V);
float NL = dot(N,L);
float NH = dot(N,H);
return NL;
}
ENDHLSL
}
// Used for rendering URP's shadowmaps
Pass
{
Name "ShadowCaster"
Tags{"LightMode" = "ShadowCaster"}
//we don't care about color, we just write to depth
ColorMask 0
HLSLPROGRAM
#pragma vertex ShadowCasterPassVertex
#pragma fragment ShadowCasterPassFragment
#include "SimpleURPToonLitOutlineExample_Shared.hlsl"
Varyings ShadowCasterPassVertex(Attributes input)
{
VertexShaderWorkSetting setting = GetDefaultVertexShaderWorkSetting();
setting.isOutline = false; //(you can delete this line, this line is just a note) the correct value is false here, else self shadow is not correct
setting.applyShadowBiasFixToHClipPos = true;//important for shadow caster pass, else shadow artifact will appear
return VertexShaderWork(input, setting);
}
half4 ShadowCasterPassFragment(Varyings input) : SV_TARGET
{
return BaseColorAlphaClipTest(input);
}
ENDHLSL
}
Pass
{
Tags{"LightMode" = "DepthOnly"}
ZWrite On
ColorMask 0
HLSLPROGRAM
// Required to compile gles 2.0 with standard srp library
#pragma prefer_hlslcc gles
#pragma exclude_renderers d3d11_9x
#pragma target 2.0
#pragma vertex DepthOnlyVertex
#pragma fragment DepthOnlyFragment
// -------------------------------------
// Material Keywords
#pragma shader_feature _ALPHATEST_ON
//--------------------------------------
// GPU Instancing
#pragma multi_compile_instancing
#include "Packages/com.unity.render-pipelines.universal/Shaders/UnlitInput.hlsl"
#include "Packages/com.unity.render-pipelines.universal/Shaders/DepthOnlyPass.hlsl"
ENDHLSL
}
// This pass it not used during regular rendering, only for lightmap baking.
Pass
{
Name "Meta"
Tags{"LightMode" = "Meta"}
Cull Off
HLSLPROGRAM
// Required to compile gles 2.0 with standard srp library
#pragma prefer_hlslcc gles
#pragma exclude_renderers d3d11_9x
#pragma vertex UniversalVertexMeta
#pragma fragment UniversalFragmentMetaUnlit
#include "Packages/com.unity.render-pipelines.universal/Shaders/UnlitInput.hlsl"
#include "Packages/com.unity.render-pipelines.universal/Shaders/UnlitMetaPass.hlsl"
ENDHLSL
}
}
FallBack "Hidden/Universal Render Pipeline/FallbackError"
CustomEditor "UnityEditor.Rendering.Universal.ShaderGUI.UnlitShader"
}