summaryrefslogtreecommitdiffstats
path: root/pbr.cginc
blob: d458acafd7eb11ea5f31e0101ab900adb9e10b1f (plain)
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#ifndef __PBR_INC
#define __PBR_INC

#include "UnityPBSLighting.cginc"
#include "AutoLight.cginc"

#include "globals.cginc"
#include "filament_math.cginc"
#include "globals.cginc"
#include "interpolators.cginc"
#include "MochieStandardBRDF.cginc"
#include "poi.cginc"

#if defined(_LTCGI)
#include "Third_Party/at.pimaker.ltcgi/Shaders/LTCGI_structs.cginc"

struct ltcgi_acc {
  float3 diffuse;
  float3 specular;
};

void ltcgi_cb_diffuse(inout ltcgi_acc acc, in ltcgi_output output);
void ltcgi_cb_specular(inout ltcgi_acc acc, in ltcgi_output output);

#define LTCGI_V2_CUSTOM_INPUT ltcgi_acc
#define LTCGI_V2_DIFFUSE_CALLBACK ltcgi_cb_diffuse
#define LTCGI_V2_SPECULAR_CALLBACK ltcgi_cb_specular

#include "Third_Party/at.pimaker.ltcgi/Shaders/LTCGI.cginc"
void ltcgi_cb_diffuse(inout ltcgi_acc acc, in ltcgi_output output) {
	acc.diffuse += output.intensity * output.color * _LTCGI_DiffuseColor;
}
void ltcgi_cb_specular(inout ltcgi_acc acc, in ltcgi_output output) {
	acc.specular += output.intensity * output.color * _LTCGI_SpecularColor;
}
#endif  // __LTCGI

UNITY_DECLARE_TEXCUBE(_Cubemap);

UnityLight CreateDirectLight(float3 normal, v2f i, out float attenuation)
{
  // This whole block is yoinked from AutoLight.cginc. I needed a way to
  // control shadow strength so I had to duplicate the code.
#if defined(DIRECTIONAL_COOKIE)
  DECLARE_LIGHT_COORD(i, i.worldPos);
  float shadow = UNITY_SHADOW_ATTENUATION(i, i.worldPos);
  attenuation = tex2D(_LightTexture0, lightCoord).w;
#elif defined(POINT_COOKIE)
  DECLARE_LIGHT_COORD(i, i.worldPos);
  float shadow = UNITY_SHADOW_ATTENUATION(i, i.worldPos);
  attenuation = tex2D(_LightTextureB0, dot(lightCoord, lightCoord).rr).r *
    texCUBE(_LightTexture0, lightCoord).w;
#elif defined(DIRECTIONAL)
  float shadow = UNITY_SHADOW_ATTENUATION(i, i.worldPos);
  attenuation = 1;
#elif defined(SPOT)
  DECLARE_LIGHT_COORD(i, i.worldPos);
  float shadow = UNITY_SHADOW_ATTENUATION(i, i.worldPos);
  attenuation = (lightCoord.z > 0) * UnitySpotCookie(lightCoord) * UnitySpotAttenuate(lightCoord.xyz);
#elif defined(POINT)
  unityShadowCoord3 lightCoord = mul(unity_WorldToLight, unityShadowCoord4(i.worldPos, 1)).xyz;
  float shadow = UNITY_SHADOW_ATTENUATION(i, i.worldPos);
  attenuation = tex2D(_LightTexture0, dot(lightCoord, lightCoord).rr).r;
#else
  float shadow = 1;
  attenuation = 1;
#endif
  attenuation *= lerp(1, shadow, _Shadow_Strength);

  UnityLight light;
  light.color = _LightColor0.rgb * attenuation;
#if defined(POINT) || defined(POINT_COOKIE) || defined(SPOT)
  light.dir = normalize((_WorldSpaceLightPos0 - i.worldPos).xyz);
#else
  light.dir = _WorldSpaceLightPos0;
#endif

  if (round(_Confabulate_Normals)) {
    light.dir = normal;
  }
  light.ndotl = DotClamped(normal, light.dir);

  return light;
}

float GetRoughness(float smoothness) {
  float r = 1 - smoothness;
  r *= 1.7 - 0.7 * r;
  return r;
}

float3 BoxProjection (
	float3 direction, float3 position,
	float4 cubemapPosition, float3 boxMin, float3 boxMax
) {
	#if UNITY_SPECCUBE_BOX_PROJECTION
		if (cubemapPosition.w > 0) {
			float3 factors =
				((direction > 0 ? boxMax : boxMin) - position) / direction;
			float scalar = min(min(factors.x, factors.y), factors.z);
			direction = direction * scalar + (position - cubemapPosition.xyz);
		}
	#endif
	return direction;
}

UnityIndirect CreateIndirectLight(float4 vertexLightColor, float3 view_dir, float3 normal,
    float smoothness, float3 worldPos, float2 uv) {
  UnityIndirect indirect;
  indirect.diffuse = vertexLightColor;
  indirect.specular = 0;

#if defined(FORWARD_BASE_PASS)

#if defined(LIGHTMAP_ON)
  // Avatars are not static, don't use lightmap.
  indirect.diffuse = 0;
#else
  if (_Mesh_Normals_Mode == 3) {  // Toon
    indirect.diffuse += max(0, BetterSH9(float4(0, 0, 0, 1)));
  } else {
    indirect.diffuse += max(0, BetterSH9(float4(normal, 1)));
  }
#endif
  float3 reflect_dir = reflect(-view_dir, normal);
  Unity_GlossyEnvironmentData env_data;
  env_data.roughness = GetRoughness(smoothness);
  env_data.reflUVW = BoxProjection(
    reflect_dir, worldPos,
    unity_SpecCube0_ProbePosition,
    unity_SpecCube0_BoxMin, unity_SpecCube0_BoxMax
  );
  float3 probe0 = Unity_GlossyEnvironment(
      UNITY_PASS_TEXCUBE(unity_SpecCube0), unity_SpecCube0_HDR, env_data
      );
  indirect.specular = probe0;
#if UNITY_SPECCUBE_BLENDING
  if (unity_SpecCube0_BoxMin.w < 0.99999) {
    env_data.reflUVW = BoxProjection(
        reflect_dir, worldPos,
        unity_SpecCube1_ProbePosition,
        unity_SpecCube1_BoxMin.xyz, unity_SpecCube1_BoxMax.xyz);
    float3 probe1 = Unity_GlossyEnvironment(
        UNITY_PASS_TEXCUBE_SAMPLER(unity_SpecCube1, unity_SpecCube0),
        unity_SpecCube1_HDR, env_data
        );
    indirect.specular = lerp(probe1, probe0, unity_SpecCube0_BoxMin.w);
  }
#else
  indirect.specular = probe0;
#endif  // UNITY_SPECCUBE_BLENDING

  // Lifted from poi toon shader (MIT).
  float horizon = min(1 + dot(reflect_dir, normal), 1);
  indirect.specular *= horizon * horizon;

#if defined(_CUBEMAP)
  float roughness = GetRoughness(smoothness);
  indirect.specular =
    UNITY_SAMPLE_TEXCUBE_LOD(
        _Cubemap,
        reflect_dir,
        roughness * UNITY_SPECCUBE_LOD_STEPS);
#endif  // _CUBEMAP

#endif  // FORWARD_BASE_PASS

  return indirect;
}

float4 getLitColor(
    float4 vertexLightColor,
    float4 albedo,
    float3 worldPos,
    float3 normal,
    float metallic,
    float smoothness,
    float2 uv,
    float ao,
    // hack while i figure out view-dependent flickering in outlines
    bool enable_direct,
    v2f i)
{
  float3 specular_tint;
  float one_minus_reflectivity;
  albedo.rgb = DiffuseAndSpecularFromMetallic(
    albedo, metallic, specular_tint, one_minus_reflectivity);

  float3 view_dir = normalize(_WorldSpaceCameraPos - worldPos);

  uint normals_mode = round(_Mesh_Normals_Mode);
  float3 flat_normal = normalize(
    (1.0 / _Flatten_Mesh_Normals_Str) * normal +
    _Flatten_Mesh_Normals_Str * view_dir);
  float3 spherical_normal = normalize(UnityObjectToWorldNormal(normalize(i.objPos)));
  normal = lerp(normal, flat_normal, normals_mode == 0);
  normal = lerp(normal, spherical_normal, normals_mode == 1);

	UnityIndirect indirect_light = CreateIndirectLight(vertexLightColor,
			view_dir, normal, smoothness, worldPos, uv);

  float attenuation;
  UnityLight direct_light = CreateDirectLight(normal, i, attenuation);
  if (normals_mode == 0) {
    float e = 0.8;
    indirect_light.diffuse += direct_light.color * e;
    direct_light.color *= (1 - e);
  }

#if defined(_LTCGI)
  ltcgi_acc acc = (ltcgi_acc) 0;
  if ((bool) round(_LTCGI_Enabled)) {
    LTCGI_Contribution(
        acc,
        i.worldPos,
        normal,
        view_dir,
        GetRoughness(smoothness),
        0);
    indirect_light.diffuse += acc.diffuse;
    indirect_light.specular += acc.specular;
  }
#endif

  direct_light.color *= _Lighting_Factor * _Direct_Lighting_Factor * enable_direct;
  indirect_light.specular *= _Lighting_Factor * _Indirect_Specular_Lighting_Factor;
  indirect_light.diffuse *= _Lighting_Factor * _Indirect_Diffuse_Lighting_Factor;

  if (_Reflection_Probe_Saturation < 1.0) {
    indirect_light.specular = RGBtoHSV(indirect_light.specular);
    indirect_light.specular[1] *= _Reflection_Probe_Saturation;
    indirect_light.specular = HSVtoRGB(indirect_light.specular);
    indirect_light.diffuse = RGBtoHSV(indirect_light.diffuse);
    indirect_light.diffuse[1] *= _Reflection_Probe_Saturation;
    indirect_light.diffuse = HSVtoRGB(indirect_light.diffuse);
  }

  direct_light.color = clamp(direct_light.color, _Min_Brightness, _Max_Brightness);
  indirect_light.diffuse = clamp(indirect_light.diffuse, _Min_Brightness, _Max_Brightness);
  indirect_light.specular = clamp(indirect_light.specular, _Min_Brightness, _Max_Brightness);

  // Apply AO
  indirect_light.diffuse *= ao;
  float3 direct_color = direct_light.color;
  direct_light.color *= ao;

  float2 screenUVs = 0;
  float4 screenPos = 0;
#if 1
  float4 pbr = BRDF1_Mochie_PBS(
      albedo,
      specular_tint,
      one_minus_reflectivity,
      smoothness,
      normal,
      view_dir,
      i.worldPos,
      screenUVs,
      screenPos,
      metallic,
      /*thickness=*/1,
      /*ssColor=*/0,
      attenuation,
      /*lightmapUV=*/0,
      vertexLightColor,
      direct_light,
      indirect_light);
#else
    float3 pbr = UNITY_BRDF_PBS(
        albedo,
        specular_tint,
        one_minus_reflectivity,
        smoothness,
        normal,
        view_dir,
        direct_light,
        indirect_light).xyz;
#endif

#if defined(_CLEARCOAT)
    // Direct lighting
    float cc_mask = _Clearcoat_Mask.SampleGrad(linear_repeat_s, i.uv, ddx(i.uv.x), ddy(i.uv.y));
    {
      float3 cc_L = direct_light.dir;
      half3 cc_H = Unity_SafeNormalize(cc_L + view_dir);
      half cc_LoH = saturate(dot(direct_light.dir, cc_H));
      float3 cc_N = normalize(i.normal);
      half cc_NoH = saturate(dot(i.normal, cc_H));
      float clearcoat = FilamentClearcoat(
          _Clearcoat_Roughness,
          _Clearcoat_Strength,
          cc_NoH,
          cc_LoH,
          cc_H);
      pbr.rgb += clearcoat * saturate(dot(i.normal, cc_L)) *
        cc_mask * direct_color * 10;
    }
    // Indirect specular lighting
#if 1
    {
      float3 in_L = normalize(reflect(-view_dir, i.normal));
      half3 in_H = i.normal;
      half in_LoH = saturate(dot(in_L, in_H));
      half in_NoH = 1;
      float clearcoat = FilamentClearcoat(
          _Clearcoat_Roughness,
          _Clearcoat_Strength,
          in_NoH,
          in_LoH,
          in_H);
      pbr.rgb += clearcoat * saturate(dot(i.normal, in_L)) *
        cc_mask * indirect_light.specular * 1;
    }
#endif
#if defined(VERTEXLIGHT_ON)
    // Vertex lights
    for (uint ii = 0; ii < 4; ii++) {
      float3 vpos = float3(unity_4LightPosX0[ii], unity_4LightPosY0[ii],
          unity_4LightPosZ0[ii]);
      float3 vl = normalize(vpos - i.worldPos);
      float3 c = unity_LightColor[0].rgb;

      half3 vhalf = Unity_SafeNormalize(half3(vl) + view_dir);
      half vlh = saturate(dot(vl, vhalf));
      half cc_vnh = saturate(dot(i.normal, vhalf));

      float clearcoat = FilamentClearcoat(
          _Clearcoat_Roughness,
          _Clearcoat_Strength,
          cc_vnh,
          vlh,
          vhalf);
      pbr.rgb += clearcoat * saturate(dot(i.normal, vl)) *
        cc_mask * c * 10;
    }
#endif
#endif

  return float4(pbr.rgb, albedo.a);
}

#endif  // __PBR_INC