-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLitInput.hlsl
More file actions
175 lines (147 loc) · 3.95 KB
/
LitInput.hlsl
File metadata and controls
175 lines (147 loc) · 3.95 KB
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
#ifndef CUSTOM_LIT_INPUT_INCLUDED
#define CUSTOM_LIT_INPUT_INCLUDED
TEXTURE2D(_BaseMap);
SAMPLER(sampler_BaseMap);
TEXTURE2D(_EmissionMap);
TEXTURE2D(_MaskMap);
TEXTURE2D(_DetailMap);
SAMPLER(sampler_DetailMap);
TEXTURE2D(_DetailNormalMap);
TEXTURE2D(_NormalMap);
UNITY_INSTANCING_BUFFER_START(UnityPerMaterial)
UNITY_DEFINE_INSTANCED_PROP(float4, _BaseMap_ST)
UNITY_DEFINE_INSTANCED_PROP(float4, _BaseColor)
UNITY_DEFINE_INSTANCED_PROP(float4, _DetailMap_ST)
UNITY_DEFINE_INSTANCED_PROP(float, _DetailAlbedo)
UNITY_DEFINE_INSTANCED_PROP(float, _DetailSmoothness)
UNITY_DEFINE_INSTANCED_PROP(float, _DetailNormalScale)
UNITY_DEFINE_INSTANCED_PROP(float, _NormalScale)
UNITY_DEFINE_INSTANCED_PROP(float4, _EmissionColor)
UNITY_DEFINE_INSTANCED_PROP(float, _Cutoff)
UNITY_DEFINE_INSTANCED_PROP(float, _ZWrite)
UNITY_DEFINE_INSTANCED_PROP(float, _Metallic)
UNITY_DEFINE_INSTANCED_PROP(float, _Occlusion)
UNITY_DEFINE_INSTANCED_PROP(float, _Smoothness)
UNITY_DEFINE_INSTANCED_PROP(float, _Fresnel)
UNITY_INSTANCING_BUFFER_END(UnityPerMaterial)
#define INPUT_PROP(name) UNITY_ACCESS_INSTANCED_PROP(UnityPerMaterial, name)
// Incapsulate uv
struct InputConfig
{
Fragment fragment;
float2 baseUV;
float2 detailUV;
bool useMask;
bool useDetail;
};
InputConfig GetInputConfig(float4 positionSS, float2 baseUV, float2 detailUV = 0.0)
{
InputConfig c;
c.fragment = GetFragment(positionSS);
c.baseUV = baseUV;
c.detailUV = detailUV;
c.useMask = false;
c.useDetail = false;
return c;
}
// Transform textures uv
float2 TransformBaseUV(float2 baseUV)
{
float4 baseST = INPUT_PROP(_BaseMap_ST);
return baseUV * baseST.xy + baseST.zw;
}
float2 TransformDetailUV(float2 detailUV)
{
float4 detailST = INPUT_PROP(_DetailMap_ST);
return detailUV * detailST.xy + detailST.zw;
}
// Sample textures
float4 GetMask(InputConfig c)
{
if (c.useMask)
{
return SAMPLE_TEXTURE2D(_MaskMap, sampler_BaseMap, c.baseUV);
}
return 1.0;
}
float4 GetDetail(InputConfig c)
{
if (c.useDetail)
{
float4 map = SAMPLE_TEXTURE2D(_DetailMap, sampler_DetailMap, c.detailUV);
return map * 2.0 - 1.0;
}
return 0.0;
}
float4 GetBase(InputConfig c)
{
float4 map = SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, c.baseUV);
float4 color = INPUT_PROP(_BaseColor);
if (c.useDetail)
{
float detail = GetDetail(c).r * INPUT_PROP(_DetailAlbedo);
float mask = GetMask(c).b;
map.rgb = lerp(sqrt(map.rgb), detail < 0.0 ? 0.0 : 1.0, abs(detail) * mask);
map.rgb *= map.rgb;
}
return map * color;
}
float3 GetNormalTS(InputConfig c)
{
float4 map = SAMPLE_TEXTURE2D(_NormalMap, sampler_BaseMap, c.baseUV);
float scale = INPUT_PROP(_NormalScale);
float3 normal = DecodeNormal(map, scale);
if (c.useDetail)
{
map = SAMPLE_TEXTURE2D(_DetailNormalMap, sampler_DetailMap, c.detailUV);
scale = INPUT_PROP(_DetailNormalScale) * GetMask(c).b;
float3 detail = DecodeNormal(map, scale);
normal = BlendNormalRNM(normal, detail);
}
return normal;
}
float3 GetEmission(InputConfig c)
{
float4 map = SAMPLE_TEXTURE2D(_EmissionMap, sampler_BaseMap, c.baseUV);
float4 color = INPUT_PROP(_EmissionColor);
return map.rgb * color.rgb;
}
// Get properties
float GetCutoff(InputConfig c)
{
return INPUT_PROP(_Cutoff);
}
float GetMetallic(InputConfig c)
{
float metallic = INPUT_PROP(_Metallic);
metallic *= GetMask(c).r;
return metallic;
}
float GetSmoothness(InputConfig c)
{
float smoothness = INPUT_PROP(_Smoothness);
smoothness *= GetMask(c).a;
if (c.useDetail)
{
float detail = GetDetail(c).b * INPUT_PROP(_DetailSmoothness);
float mask = GetMask(c).b;
smoothness = lerp(smoothness, detail < 0.0 ? 0.0 : 1.0, abs(detail) * mask);
}
return smoothness;
}
float GetOcclusion(InputConfig c)
{
float strength = INPUT_PROP(_Occlusion);
float occlusion = GetMask(c).g;
occlusion = lerp(occlusion, 1.0, strength);
return occlusion;
}
float GetFresnel(InputConfig c)
{
return INPUT_PROP(_Fresnel);
}
float GetFinalAlpha (float alpha)
{
return INPUT_PROP(_ZWrite) ? 1.0 : alpha;
}
#endif