BlueGit

blue-star ggj-2020

Files

ggj-2020 / Assets / Oculus / Avatar / Resources / Materials / AvatarMaterialStateShader.cginc

AvatarMaterialStateShader.cginc

Project Import
timfa Tim Falken · pushed by timfa · 6 years ago
b77834a1 History
AvatarMaterialStateShader.cginc 9.0 KiB Download Raw
1#ifndef AVATAR_UTIL_CG_INCLUDED
2#define AVATAR_UTIL_CG_INCLUDED
3
4#include "UnityCG.cginc"
5
6#define SAMPLE_MODE_COLOR 0
7#define SAMPLE_MODE_TEXTURE 1
8#define SAMPLE_MODE_TEXTURE_SINGLE_CHANNEL 2
9#define SAMPLE_MODE_PARALLAX 3
10#define SAMPLE_MODE_RSRM 4
11
12#define MASK_TYPE_NONE 0
13#define MASK_TYPE_POSITIONAL 1
14#define MASK_TYPE_REFLECTION 2
15#define MASK_TYPE_FRESNEL 3
16#define MASK_TYPE_PULSE 4
17
18#define BLEND_MODE_ADD 0
19#define BLEND_MODE_MULTIPLY 1
20
21#ifdef LAYERS_1
22#define LAYER_COUNT 1
23#elif LAYERS_2
24#define LAYER_COUNT 2
25#elif LAYERS_3
26#define LAYER_COUNT 3
27#elif LAYERS_4
28#define LAYER_COUNT 4
29#elif LAYERS_5
30#define LAYER_COUNT 5
31#elif LAYERS_6
32#define LAYER_COUNT 6
33#elif LAYERS_7
34#define LAYER_COUNT 7
35#elif LAYERS_8
36#define LAYER_COUNT 8
37#endif
38
39#define DECLARE_LAYER_UNIFORMS(index) \
40 int _LayerSampleMode##index; \
41 int _LayerBlendMode##index; \
42 int _LayerMaskType##index; \
43 fixed4 _LayerColor##index; \
44 sampler2D _LayerSurface##index; \
45 float4 _LayerSurface##index##_ST; \
46 float4 _LayerSampleParameters##index; \
47 float4 _LayerMaskParameters##index; \
48 float4 _LayerMaskAxis##index;
49
50DECLARE_LAYER_UNIFORMS(0)
51DECLARE_LAYER_UNIFORMS(1)
52DECLARE_LAYER_UNIFORMS(2)
53DECLARE_LAYER_UNIFORMS(3)
54DECLARE_LAYER_UNIFORMS(4)
55DECLARE_LAYER_UNIFORMS(5)
56DECLARE_LAYER_UNIFORMS(6)
57DECLARE_LAYER_UNIFORMS(7)
58
59struct VertexOutput
60{
61 float4 pos : SV_POSITION;
62 float2 texcoord : TEXCOORD0;
63 float3 worldPos : TEXCOORD1;
64 float3 worldNormal : TEXCOORD2;
65 float3 viewDir : TEXCOORD3;
66 float4 vertColor : COLOR;
67
68#if NORMAL_MAP_ON || PARALLAX_ON
69 float3 worldTangent : TANGENT;
70 float3 worldBitangent : TEXCOORD5;
71#endif
72};
73
74float _Alpha;
75int _BaseMaskType;
76float4 _BaseMaskParameters;
77float4 _BaseMaskAxis;
78fixed4 _DarkMultiplier;
79fixed4 _BaseColor;
80sampler2D _AlphaMask;
81float4 _AlphaMask_ST;
82sampler2D _AlphaMask2;
83float4 _AlphaMask2_ST;
84sampler2D _NormalMap;
85float4 _NormalMap_ST;
86sampler2D _ParallaxMap;
87float4 _ParallaxMap_ST;
88sampler2D _RoughnessMap;
89float4 _RoughnessMap_ST;
90float4x4 _ProjectorWorldToLocal;
91
92VertexOutput vert(appdata_full v)
93{
94 VertexOutput o;
95 UNITY_INITIALIZE_OUTPUT(VertexOutput, o);
96
97 o.texcoord = v.texcoord.xy;
98 o.worldPos = mul(unity_ObjectToWorld, v.vertex);
99 o.vertColor = v.color;
100 o.viewDir = normalize(_WorldSpaceCameraPos.xyz - o.worldPos);
101 o.worldNormal = normalize(mul(unity_ObjectToWorld, float4(v.normal, 0.0)).xyz);
102
103#if NORMAL_MAP_ON || PARALLAX_ON
104 o.worldTangent = normalize(mul(unity_ObjectToWorld, float4(v.tangent.xyz, 0.0)).xyz);
105 o.worldBitangent = normalize(cross(o.worldNormal, o.worldTangent) * v.tangent.w);
106#endif
107
108 o.pos = UnityObjectToClipPos(v.vertex);
109 return o;
110}
111
112#ifndef NORMAL_MAP_ON
113#define COMPUTE_NORMAL IN.worldNormal
114#else
115#define COMPUTE_NORMAL normalize(mul(lerp(float3(0, 0, 1), surfaceNormal, normalMapStrength), tangentTransform))
116#endif
117
118float3 ComputeColor(
119 VertexOutput IN,
120 float2 uv,
121#if PARALLAX_ON || NORMAL_MAP_ON
122 float3x3 tangentTransform,
123#endif
124#ifdef NORMAL_MAP_ON
125 float3 surfaceNormal,
126#endif
127 sampler2D surface,
128 float4 surface_ST,
129 fixed4 color,
130 int sampleMode,
131 float4 sampleParameters
132) {
133 if (sampleMode == SAMPLE_MODE_TEXTURE) {
134 float2 panning = _Time.g * sampleParameters.xy;
135 return tex2D(surface, (uv + panning) * surface_ST.xy + surface_ST.zw).rgb * color.rgb;
136 }
137 else if (sampleMode == SAMPLE_MODE_TEXTURE_SINGLE_CHANNEL) {
138 float4 channelMask = sampleParameters;
139 float4 channels = tex2D(surface, uv * surface_ST.xy + surface_ST.zw);
140 return dot(channels, channelMask) * color.rgb;
141 }
142#ifdef PARALLAX_ON
143 else if (sampleMode == SAMPLE_MODE_PARALLAX) {
144 float parallaxMinHeight = sampleParameters.x;
145 float parallaxMaxHeight = sampleParameters.y;
146 float parallaxValue = tex2D(_ParallaxMap, TRANSFORM_TEX(uv, _ParallaxMap)).r;
147 float scaledHeight = lerp(parallaxMinHeight, parallaxMaxHeight, parallaxValue);
148 float2 parallaxUV = mul(tangentTransform, IN.viewDir).xy * scaledHeight;
149 return tex2D(surface, (uv * surface_ST.xy + surface_ST.zw) + parallaxUV).rgb * color.rgb;
150 }
151#endif
152 else if (sampleMode == SAMPLE_MODE_RSRM) {
153 float roughnessMin = sampleParameters.x;
154 float roughnessMax = sampleParameters.y;
155#ifdef ROUGHNESS_ON
156 float roughnessValue = tex2D(_RoughnessMap, TRANSFORM_TEX(uv, _RoughnessMap)).r;
157 float scaledRoughness = lerp(roughnessMin, roughnessMax, roughnessValue);
158#else
159 float scaledRoughness = roughnessMin;
160#endif
161
162#ifdef NORMAL_MAP_ON
163 float normalMapStrength = sampleParameters.z;
164#endif
165 float3 viewReflect = reflect(-IN.viewDir, COMPUTE_NORMAL);
166 float viewAngle = viewReflect.y * 0.5 + 0.5;
167 return tex2D(surface, float2(scaledRoughness, viewAngle)).rgb * color.rgb;
168 }
169 return color.rgb;
170}
171
172float ComputeMask(
173 VertexOutput IN,
174#ifdef NORMAL_MAP_ON
175 float3x3 tangentTransform,
176 float3 surfaceNormal,
177#endif
178 int maskType,
179 float4 layerParameters,
180 float3 maskAxis
181) {
182 if (maskType == MASK_TYPE_POSITIONAL) {
183 float centerDistance = layerParameters.x;
184 float fadeAbove = layerParameters.y;
185 float fadeBelow = layerParameters.z;
186 float3 objPos = mul(unity_WorldToObject, float4(IN.worldPos, 1.0)).xyz;
187 float d = dot(objPos, maskAxis);
188 if (d > centerDistance) {
189 return saturate(1.0 - (d - centerDistance) / fadeAbove);
190 }
191 else {
192 return saturate(1.0 - (centerDistance - d) / fadeBelow);
193 }
194 }
195 else if (maskType == MASK_TYPE_REFLECTION) {
196 float fadeStart = layerParameters.x;
197 float fadeEnd = layerParameters.y;
198#ifdef NORMAL_MAP_ON
199 float normalMapStrength = layerParameters.z;
200#endif
201 float power = layerParameters.w;
202 float3 viewReflect = reflect(-IN.viewDir, COMPUTE_NORMAL);
203 float d = max(0.0, dot(viewReflect, maskAxis));
204 return saturate(1.0 - (d - fadeStart) / (fadeEnd - fadeStart));
205 }
206 else if (maskType == MASK_TYPE_FRESNEL) {
207 float power = layerParameters.x;
208 float fadeStart = layerParameters.y;
209 float fadeEnd = layerParameters.z;
210#ifdef NORMAL_MAP_ON
211 float normalMapStrength = layerParameters.w;
212#endif
213 float d = saturate(1.0 - max(0.0, dot(IN.viewDir, COMPUTE_NORMAL)));
214 float p = pow(d, power);
215 return saturate(lerp(fadeStart, fadeEnd, p));
216 }
217 else if (maskType == MASK_TYPE_PULSE) {
218 float distance = layerParameters.x;
219 float speed = layerParameters.y;
220 float power = layerParameters.z;
221 float3 objPos = mul(unity_WorldToObject, float4(IN.worldPos, 1.0)).xyz;
222 float d = dot(objPos, maskAxis);
223 float theta = 6.2831 * frac((d - _Time.g * speed) / distance);
224 return saturate(pow((sin(theta) * 0.5 + 0.5), power));
225 }
226 else {
227 return 1.0;
228 }
229}
230
231float3 ComputeBlend(float3 source, float3 blend, float mask, int blendMode) {
232 if (blendMode == BLEND_MODE_MULTIPLY) {
233 return source * (blend * mask);
234 }
235 else {
236 return source + (blend * mask);
237 }
238}
239
240float4 ComputeSurface(VertexOutput IN)
241{
242#if PROJECTOR_ON
243 float3 projectorPos = mul(_ProjectorWorldToLocal, float4(IN.worldPos, 1.0)).xyz;
244 if (abs(projectorPos.x) > 1.0 || abs(projectorPos.y) > 1.0 || abs(projectorPos.z) > 1.0)
245 {
246 discard;
247 }
248 float2 uv = projectorPos.xy * 0.5 + 0.5;
249#else
250 float2 uv = IN.texcoord.xy;
251#endif
252
253 fixed4 c = _BaseColor;
254 IN.worldNormal = normalize(IN.worldNormal);
255
256#if PARALLAX_ON || NORMAL_MAP_ON
257 float3x3 tangentTransform = float3x3(IN.worldTangent, IN.worldBitangent, IN.worldNormal);
258#endif
259
260#ifdef NORMAL_MAP_ON
261 float3 surfaceNormal = UnpackNormal(tex2D(_NormalMap, TRANSFORM_TEX(uv, _NormalMap)));
262#endif
263
264#if PARALLAX_ON || NORMAL_MAP_ON
265#ifndef NORMAL_MAP_ON
266#define COLOR_INPUTS IN, uv, tangentTransform
267#define MASK_INPUTS IN
268#else
269#define COLOR_INPUTS IN, uv, tangentTransform, surfaceNormal
270#define MASK_INPUTS IN, tangentTransform, surfaceNormal
271#endif
272#else
273#define COLOR_INPUTS IN, uv
274#define MASK_INPUTS IN
275#endif
276
277#define LAYER_COLOR(index) ComputeColor(COLOR_INPUTS, _LayerSurface##index, _LayerSurface##index##_ST, _LayerColor##index, _LayerSampleMode##index, _LayerSampleParameters##index)
278#define LAYER_MASK(index) ComputeMask(MASK_INPUTS, _LayerMaskType##index, _LayerMaskParameters##index, _LayerMaskAxis##index##.xyz)
279#define LAYER_BLEND(index, c) ComputeBlend(c, LAYER_COLOR(index), LAYER_MASK(index), _LayerBlendMode##index)
280
281 c.rgb = LAYER_BLEND(0, c.rgb);
282#if LAYER_COUNT > 1
283 c.rgb = LAYER_BLEND(1, c.rgb);
284#endif
285#if LAYER_COUNT > 2
286 c.rgb = LAYER_BLEND(2, c.rgb);
287#endif
288#if LAYER_COUNT > 3
289 c.rgb = LAYER_BLEND(3, c.rgb);
290#endif
291#if LAYER_COUNT > 4
292 c.rgb = LAYER_BLEND(4, c.rgb);
293#endif
294#if LAYER_COUNT > 5
295 c.rgb = LAYER_BLEND(5, c.rgb);
296#endif
297#if LAYER_COUNT > 6
298 c.rgb = LAYER_BLEND(6, c.rgb);
299#endif
300#if LAYER_COUNT > 7
301 c.rgb = LAYER_BLEND(7, c.rgb);
302#endif
303
304#ifdef VERTALPHA_ON
305 float scaledValue = IN.vertColor.a * 2.0;
306 float alpha0weight = max(0.0, 1.0 - scaledValue);
307 float alpha2weight = max(0.0, scaledValue - 1.0);
308 float alpha1weight = 1.0 - alpha0weight - alpha2weight;
309 c.a = _Alpha * c.a * (tex2D(_AlphaMask, TRANSFORM_TEX(uv, _AlphaMask)).r * alpha1weight + tex2D(_AlphaMask2, TRANSFORM_TEX(uv, _AlphaMask2)).r * alpha2weight + alpha0weight) * ComputeMask(MASK_INPUTS, _BaseMaskType, _BaseMaskParameters, _BaseMaskAxis);
310#else
311 c.a = _Alpha * c.a * tex2D(_AlphaMask, TRANSFORM_TEX(uv, _AlphaMask)).r * IN.vertColor.a * ComputeMask(MASK_INPUTS, _BaseMaskType, _BaseMaskParameters, _BaseMaskAxis);
312#endif
313 c.rgb = lerp(c.rgb, c.rgb * _DarkMultiplier, IN.vertColor.r);
314
315 return c;
316}
317
318#endif