yum-archive/Tooner

A toon shader for Unity's BIRP.

git clone https://git.yummers.dev/yum-archive/Tooner

yumAdd two rim lighting slots2814e9b

v0.0.0
4.8 KiB178 linesraw
1#ifndef __PBR_INC
2#define __PBR_INC
3
4#include "AutoLight.cginc"
5#include "UnityPBSLighting.cginc"
6
7#include "globals.cginc"
8#include "interpolators.cginc"
9#include "poi.cginc"
10
11UNITY_DECLARE_TEXCUBE(_Cubemap);
12
13UnityLight CreateDirectLight(float3 normal, v2f i)
14{
15  UNITY_LIGHT_ATTENUATION(attenuation, i, i.worldPos);
16  UnityLight light;
17  light.color = _LightColor0.rgb * attenuation;
18#if defined(POINT) || defined(POINT_COOKIE) || defined(SPOT)
19  light.dir = normalize((_WorldSpaceLightPos0 - i.worldPos).xyz);
20#else
21  light.dir = _WorldSpaceLightPos0;
22#endif
23  if (round(_Confabulate_Normals)) {
24    light.dir = normal;
25  }
26  light.ndotl = DotClamped(normal, light.dir);
27
28  return light;
29}
30
31float GetRoughness(float smoothness) {
32  float r = 1 - smoothness;
33  r *= 1.7 - 0.7 * r;
34  return r;
35}
36
37float3 BoxProjection (
38	float3 direction, float3 position,
39	float4 cubemapPosition, float3 boxMin, float3 boxMax
40) {
41	#if UNITY_SPECCUBE_BOX_PROJECTION
42		UNITY_BRANCH
43		if (cubemapPosition.w > 0) {
44			float3 factors =
45				((direction > 0 ? boxMax : boxMin) - position) / direction;
46			float scalar = min(min(factors.x, factors.y), factors.z);
47			direction = direction * scalar + (position - cubemapPosition);
48		}
49	#endif
50	return direction;
51}
52
53UnityIndirect CreateIndirectLight(float4 vertexLightColor, float3 view_dir, float3 normal,
54    float smoothness, float3 worldPos, float2 uv) {
55  UnityIndirect indirect;
56  indirect.diffuse = vertexLightColor;
57  indirect.specular = 0;
58
59#if defined(FORWARD_BASE_PASS)
60
61#if defined(LIGHTMAP_ON)
62  // Avatars are not static, don't use lightmap.
63  indirect.diffuse = 0;
64#else
65  indirect.diffuse += max(0, ShadeSH9(float4(normal, 1)));
66#endif
67  float3 reflect_dir = reflect(-view_dir, normal);
68  Unity_GlossyEnvironmentData env_data;
69  env_data.roughness = GetRoughness(smoothness);
70  env_data.reflUVW = BoxProjection(
71    reflect_dir, worldPos,
72    unity_SpecCube0_ProbePosition,
73    unity_SpecCube0_BoxMin, unity_SpecCube0_BoxMax
74  );
75  float3 probe0 = Unity_GlossyEnvironment(
76      UNITY_PASS_TEXCUBE(unity_SpecCube0), unity_SpecCube0_HDR, env_data
77      );
78  env_data.reflUVW = BoxProjection(
79      reflect_dir, worldPos,
80      unity_SpecCube1_ProbePosition,
81      unity_SpecCube1_BoxMin, unity_SpecCube1_BoxMax
82      );
83#if UNITY_SPECCUBE_BLENDING
84  float interpolator = unity_SpecCube0_BoxMin.w;
85  UNITY_BRANCH
86    if (interpolator < 0.99999) {
87      float3 probe1 = Unity_GlossyEnvironment(
88          UNITY_PASS_TEXCUBE_SAMPLER(unity_SpecCube1, unity_SpecCube0),
89          unity_SpecCube0_HDR, env_data
90          );
91      indirect.specular = lerp(probe1, probe0, interpolator);
92    }
93    else {
94      indirect.specular = probe0;
95    }
96#else
97  indirect.specular = probe0;
98#endif  // UNITY_SPECCUBE_BLENDING
99
100#if defined(_CUBEMAP)
101  float roughness = GetRoughness(smoothness);
102  probe0 =
103    UNITY_SAMPLE_TEXCUBE_LOD(
104        _Cubemap,
105        reflect_dir,
106        roughness * UNITY_SPECCUBE_LOD_STEPS);
107#endif  // _CUBEMAP
108
109  indirect.specular = probe0;
110#endif  // FORWARD_BASE_PASS
111
112  return indirect;
113}
114
115float4 getLitColor(
116    float4 vertexLightColor,
117    float4 albedo,
118    float3 worldPos,
119    float3 normal,
120    float metallic, float smoothness, float2 uv, v2f i)
121{
122  float3 specular_tint;
123  float one_minus_reflectivity;
124  albedo.rgb = DiffuseAndSpecularFromMetallic(
125    albedo, metallic, specular_tint, one_minus_reflectivity);
126
127  float3 view_dir = normalize(_WorldSpaceCameraPos - worldPos);
128
129  uint normals_mode = round(_Mesh_Normals_Mode);
130  bool flat = (normals_mode == 0);
131  float3 flat_normal = normalize(
132    (1.0 / _Flatten_Mesh_Normals_Str) * normal +
133    _Flatten_Mesh_Normals_Str * view_dir);
134  float3 spherical_normal = normalize(UnityObjectToWorldNormal(normalize(i.objPos)));
135  normal = lerp(spherical_normal, flat_normal, flat);
136
137	UnityIndirect indirect_light = CreateIndirectLight(vertexLightColor,
138			view_dir, normal, smoothness, worldPos, uv);
139
140  UnityLight direct_light = CreateDirectLight(normal, i);
141  if (flat) {
142    float e = 0.8;
143    indirect_light.diffuse += direct_light.color * e;
144    direct_light.color *= (1 - e);
145  }
146
147  direct_light.color = clamp(direct_light.color, _Min_Brightness, _Max_Brightness*.5);
148  indirect_light.diffuse = clamp(indirect_light.diffuse, _Min_Brightness, _Max_Brightness);
149  indirect_light.specular = clamp(indirect_light.specular, _Min_Brightness, _Max_Brightness);
150
151  float3 pbr;
152  if (round(_Confabulate_Normals)) {
153    pbr = UNITY_BRDF_PBS(
154        albedo,
155        specular_tint,
156        one_minus_reflectivity,
157        smoothness,
158        view_dir,
159        normal,
160        direct_light,
161        indirect_light).xyz;
162  } else {
163    pbr = UNITY_BRDF_PBS(
164        albedo,
165        specular_tint,
166        one_minus_reflectivity,
167        smoothness,
168        normal,
169        view_dir,
170        direct_light,
171        indirect_light).xyz;
172  }
173
174  return float4(pbr, albedo.a);
175}
176
177#endif  // __PBR_INC
178