URPShader
Template
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
Shader "URP/unlit"
{
Properties
{
_BaseColor ("Base Color", color) = (1, 1, 1, 1)
_BaseMap ("BaseMap", 2D) = "white" { }
}
SubShader
{
Tags { "Queue" = "Geometry" "RenderType" = "0paque" "IgnoreProjector" = "True" "RenderPipeline" = "UniversalPipeline" }
LOD 100
Pass
{
Name "Unlit"
HLSLPROGRAM
// Pragmas
#pragma target 2.0
#pragma multi_compile_instancing
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fog
// 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/Lighting.hlsl"
struct appdata
{
float4 positionOS : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f
{
float4 positionCS : SV_POSITION;
float2 uv : TEXCOORD0;
};
CBUFFER_START(UnityPerMaterial)
half4 _BaseColor;
float4 _BaseMap_ST;
CBUFFER_END
TEXTURE2D(_BaseMap);
SAMPLER(sampler_BaseMap);
v2f vert(appdata v)
{
v2f o;
o.positionCS = TransformObjectToHClip(v.positionOS.xyz);
o.uv = TRANSFORM_TEX(v.uv, _BaseMap);
return o;
}
half4 frag(v2f i) : SV_Target
{
half4 c;
half4 baseMap = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, i.uv);
c = baseMap * _BaseColor;
return c;
}
ENDHLSL
}
}
}
SRP Batcher & CBuffer
- Static Batching - Bake - same material different mesh
- Dynamic Batching - restrict on mesh - same material
- GPU Instancing - same mesh
- SRP Batcher - different material same shader. static mesh without rig
1
2
3
CBUFFER_START(UnityPerMaterial) // 除了贴图,全放在同一CBuffer//UnityPerDraw -> 内部变量
half4 _Color;
CBUFFER_END
- 同一shader只batch一次
Texture & Sampler
1
2
sampler2D _MainTex
tex2D(_MainTex, i.uv)
1
2
TEXTURE2D(_MainTex); // 纹理的定义,如果是编绎到GLES2.0平台,则相当于sampler2D_MainTex;否则就相当于Texture2D_MainTex;
SAMPLER(samlper_MainTex); //采样器的定义,如果是编绎到GLES2.0平台,就相当于空,否则就相当于SamplerState sampler_MainTex;
与Built in不同,Texture与Sampler分离定义(多个贴图用同个sampler),不能被用于GLES2
SAMPLE_TEXTURE2D
1
2
3
4
5
6
7
half4 frag(Varyings i) : SV_TARGET
{
half4 c;
half4 mainTex = SAMPLE_TEXTURE2D(_MainTex, sampler_MainTex, i.uv);
c = mainTex * _Color;
return c;
}
- sampler setting in inspector
1
2
#define smp SamplerState_Point_Repeat
SAMPLER(SamplerState_Point_Repeat)