diff options
| author | yum <yum.food.vr@gmail.com> | 2024-09-24 13:57:26 -0700 |
|---|---|---|
| committer | yum <yum.food.vr@gmail.com> | 2024-09-24 13:57:26 -0700 |
| commit | 2b9e36fde45c5f488539f390288e0a2731d11fcf (patch) | |
| tree | ed12017282d6145830b512ec2cf1a6d2834e15c7 | |
| parent | 88a524d71bf46bca1c65c3f7ed5fcd3f6edd1d5e (diff) | |
World lighting fixes
* Add channel selection for roughness and metallic textures
* This lets u combine roughness & metallic into one texture
* Add Bakery-compatible emission slot
* Add compile-time toggle for brightness clamping
* Add toggle for world interpolators
* Add emission type dropdown (baked/realtime/none)
* Switch to bilinear filtering for base PBR maps to match standard
shader behavior
* Add bugfixes for Mochie BRDF in world lighting mode
* Rename _NormalTex to _BumpMap for compatibility with standard shader
* World lighting mode uses Unity baked GI logic instead of normal world
lighting logic
* Use uv2 instead of lmuv for lightmap UVs
| -rw-r--r-- | Editor/tooner.cs | 87 | ||||
| -rw-r--r-- | MochieStandardBRDF.cginc | 30 | ||||
| -rw-r--r-- | feature_macros.cginc | 3 | ||||
| -rw-r--r-- | globals.cginc | 24 | ||||
| -rw-r--r-- | interpolators.cginc | 27 | ||||
| -rw-r--r-- | pbr.cginc | 70 | ||||
| -rw-r--r-- | tooner.shader | 19 | ||||
| -rw-r--r-- | tooner_lighting.cginc | 50 | ||||
| -rw-r--r-- | tooner_outline_pass.cginc | 8 |
9 files changed, 252 insertions, 66 deletions
diff --git a/Editor/tooner.cs b/Editor/tooner.cs index 29ff40b..55e2dba 100644 --- a/Editor/tooner.cs +++ b/Editor/tooner.cs @@ -64,11 +64,11 @@ public class ToonerGUI : ShaderGUI { } void DoNormalLogic() { - MaterialProperty bct = FindProperty("_NormalTex"); + MaterialProperty bct = FindProperty("_BumpMap"); SetKeyword("_NORMAL_MAP", bct.textureValue); } void DoNormalUI() { - MaterialProperty bct = FindProperty("_NormalTex"); + MaterialProperty bct = FindProperty("_BumpMap"); editor.TexturePropertySingleLine( MakeLabel(bct, "Normal"), bct, @@ -91,6 +91,9 @@ public class ToonerGUI : ShaderGUI { bc); if (bct.textureValue) { editor.TextureScaleOffsetProperty(bct); + + bc = FindProperty("_MetallicTexChannel"); + editor.RangeProperty(bc, "Channel"); } } @@ -108,6 +111,9 @@ public class ToonerGUI : ShaderGUI { if (bct.textureValue) { editor.TextureScaleOffsetProperty(bct); + bc = FindProperty("_RoughnessTexChannel"); + editor.RangeProperty(bc, "Channel"); + bc = FindProperty("_Roughness_Invert"); bool enabled = bc.floatValue > 1E-6; EditorGUI.BeginChangeCheck(); @@ -479,19 +485,31 @@ public class ToonerGUI : ShaderGUI { MaterialProperty bc; MaterialProperty bct; + { + EditorGUILayout.LabelField($"Base slot", EditorStyles.boldLabel); + EditorGUI.indentLevel += 1; + + bc = FindProperty($"_EmissionColor"); + bct = FindProperty($"_EmissionMap"); + editor.TexturePropertyWithHDRColor( + MakeLabel(bct, "Emission (RGB)"), + bct, bc, false); + SetKeyword($"_EMISSION", bct.textureValue); + + EditorGUI.indentLevel -= 1; + } for (int i = 0; i < 2; i++) { - EditorGUILayout.LabelField($"Slot {i}", EditorStyles.boldLabel); + EditorGUILayout.LabelField($"Extra slot {i}", EditorStyles.boldLabel); EditorGUI.indentLevel += 1; { - bc = FindProperty($"_Emission{i}Tex"); - bct = FindProperty($"_Emission{i}Strength"); - editor.TexturePropertySingleLine( - MakeLabel(bct, "Map"), - bc, - bct); - SetKeyword($"_EMISSION{i}", bc.textureValue); + bc = FindProperty($"_Emission{i}Color"); + bct = FindProperty($"_Emission{i}Tex"); + editor.TexturePropertyWithHDRColor( + MakeLabel(bct, "Emission (RGB)"), + bct, bc, false); + SetKeyword($"_EMISSION{i}", bct.textureValue); - if (bc.textureValue) { + if (bct.textureValue) { bc = FindProperty($"_Emission{i}_UV_Select"); editor.RangeProperty( bc, @@ -1936,15 +1954,37 @@ public class ToonerGUI : ShaderGUI { EditorGUI.indentLevel += 1; MaterialProperty bc; - bc = FindProperty("_Min_Brightness"); - editor.RangeProperty( - bc, - "Min brightness"); - bc = FindProperty("_Max_Brightness"); - editor.RangeProperty( - bc, - "Max brightness"); + bc = FindProperty("_Enable_World_Interpolators"); + bool world_interp = bc.floatValue > 1E-6; + EditorGUI.BeginChangeCheck(); + world_interp = EditorGUILayout.Toggle("World interpolators", + world_interp); + EditorGUI.EndChangeCheck(); + bc.floatValue = world_interp ? 1.0f : 0.0f; + SetKeyword("_WORLD_INTERPOLATORS", world_interp); + + bc = FindProperty("_Enable_Brightness_Clamp"); + bool brightness_clamp_enabled = bc.floatValue > 1E-6; + EditorGUI.BeginChangeCheck(); + brightness_clamp_enabled = EditorGUILayout.Toggle("Clamp brightness", + brightness_clamp_enabled); + EditorGUI.EndChangeCheck(); + bc.floatValue = brightness_clamp_enabled ? 1.0f : 0.0f; + SetKeyword("_BRIGHTNESS_CLAMP", brightness_clamp_enabled); + if (brightness_clamp_enabled) { + EditorGUI.indentLevel += 1; + bc = FindProperty("_Min_Brightness"); + editor.RangeProperty( + bc, + "Min brightness"); + + bc = FindProperty("_Max_Brightness"); + editor.RangeProperty( + bc, + "Max brightness"); + EditorGUI.indentLevel -= 1; + } bc = FindProperty("_Ambient_Occlusion"); editor.TexturePropertySingleLine( @@ -2063,6 +2103,15 @@ public class ToonerGUI : ShaderGUI { EditorGUI.indentLevel -= 1; } + EditorGUI.BeginChangeCheck(); + editor.LightmapEmissionProperty(); + if (EditorGUI.EndChangeCheck()) { + foreach (Material m in editor.targets) { + m.globalIlluminationFlags &= + ~MaterialGlobalIlluminationFlags.EmissiveIsBlack; + } + } + EditorGUI.indentLevel -= 1; } diff --git a/MochieStandardBRDF.cginc b/MochieStandardBRDF.cginc index 795bbee..2dd5c94 100644 --- a/MochieStandardBRDF.cginc +++ b/MochieStandardBRDF.cginc @@ -52,6 +52,36 @@ float GSAARoughness(float3 normal, float roughness){ return max(roughness, pow(base, 0.333)*_GSAAStrength); } +float3 Desaturate(float3 col){ + return dot(col, float3(0.3, 0.59, 0.11)); +} + +float3 GetContrast(float3 col, float contrast){ + return lerp(float3(0.5,0.5,0.5), col, contrast); +} + +float oetf_sRGB_scalar(float L) { + float V = 1.055 * (pow(L, 1.0 / 2.4)) - 0.055; + if (L <= 0.0031308) + V = L * 12.92; + return V; +} + +float3 oetf_sRGB(float3 L) { + return float3(oetf_sRGB_scalar(L.r), oetf_sRGB_scalar(L.g), oetf_sRGB_scalar(L.b)); +} + +float eotf_sRGB_scalar(float V) { + float L = pow((V + 0.055) / 1.055, 2.4); + if (V <= oetf_sRGB_scalar(0.0031308)) + L = V / 12.92; + return L; +} + +float3 GetHDR(float3 rgb) { + return float3(eotf_sRGB_scalar(rgb.r), eotf_sRGB_scalar(rgb.g), eotf_sRGB_scalar(rgb.b)); +} + half4 BRDF1_Mochie_PBS ( half3 diffColor, half3 specColor, half oneMinusReflectivity, half smoothness, half3 normal, half3 viewDir, half3 worldPos, half2 screenUVs, half4 screenPos, diff --git a/feature_macros.cginc b/feature_macros.cginc index f4857a6..4aed35b 100644 --- a/feature_macros.cginc +++ b/feature_macros.cginc @@ -8,6 +8,7 @@ #pragma shader_feature_local _ _PBR_SAMPLER_REPEAT #pragma shader_feature_local _ _PBR_SAMPLER_CLAMP #pragma shader_feature_local _ _CUBEMAP +#pragma shader_feature_local _ _EMISSION #pragma shader_feature_local _ _EMISSION0 #pragma shader_feature_local _ _EMISSION1 #pragma shader_feature_local _ _RENDERING_CUTOUT @@ -152,6 +153,8 @@ #pragma shader_feature_local _ _CLEARCOAT_MASK2 #pragma shader_feature_local _ _PROXIMITY_DIMMING #pragma shader_feature_local _ _DISCARD +#pragma shader_feature_local _ _BRIGHTNESS_CLAMP +#pragma shader_feature_local _ _WORLD_INTERPOLATORS #endif // __FEATURE_MACROS_INC diff --git a/globals.cginc b/globals.cginc index 64d3410..feaec79 100644 --- a/globals.cginc +++ b/globals.cginc @@ -5,6 +5,8 @@ SamplerState linear_repeat_s; SamplerState linear_clamp_s; +SamplerState bilinear_repeat_s; +SamplerState bilinear_clamp_s; float4 _Color; float _Metallic; @@ -43,8 +45,12 @@ float _FresnelStrength; float _UseFresnel; float _ReflectionStrength; float3 shadowedReflections; -float3 _ReflShadows; -float3 _ReflShadowStrength; +int _ReflShadows; +float _ReflShadowStrength; +float _BrightnessReflShad; +float _ContrastReflShad; +float _HDRReflShad; +float3 _TintReflShad; float _VRChatMirrorMode; float3 _VRChatMirrorCameraPos; @@ -89,11 +95,13 @@ float _SSRHeight; texture2D _MainTex; float4 _MainTex_ST; #endif -texture2D _NormalTex; -float4 _NormalTex_ST; +texture2D _BumpMap; +float4 _BumpMap_ST; texture2D _MetallicTex; +float _MetallicTexChannel; float4 _MetallicTex_ST; texture2D _RoughnessTex; +float _RoughnessTexChannel; float4 _RoughnessTex_ST; #if defined(_PBR_OVERLAY0) @@ -257,15 +265,19 @@ float _Decal3_Angle; float _Decal3_UV_Select; #endif +#if defined(_EMISSION) +texture2D _EmissionMap; +float3 _EmissionColor; +#endif #if defined(_EMISSION0) texture2D _Emission0Tex; -float _Emission0Strength; +float3 _Emission0Color; float _Emission0Multiplier; float _Emission0_UV_Select; #endif #if defined(_EMISSION1) texture2D _Emission1Tex; -float _Emission1Strength; +float3 _Emission1Color; float _Emission1Multiplier; float _Emission1_UV_Select; #endif diff --git a/interpolators.cginc b/interpolators.cginc index 3b9bdcd..bbe4742 100644 --- a/interpolators.cginc +++ b/interpolators.cginc @@ -12,11 +12,15 @@ struct appdata float2 uv0 : TEXCOORD0; float2 uv1 : TEXCOORD1; float2 uv2 : TEXCOORD2; +#if defined(_WORLD_INTERPOLATORS) + UNITY_LIGHTING_COORDS(3, 4) +#else float2 uv3 : TEXCOORD3; float2 uv4 : TEXCOORD4; float2 uv5 : TEXCOORD5; float2 uv6 : TEXCOORD6; float2 uv7 : TEXCOORD7; +#endif UNITY_VERTEX_INPUT_INSTANCE_ID }; @@ -27,14 +31,16 @@ struct v2f float2 uv0 : TEXCOORD0; float2 uv1 : TEXCOORD1; float2 uv2 : TEXCOORD2; +#if defined(_WORLD_INTERPOLATORS) + UNITY_LIGHTING_COORDS(2, 3) +#else float2 uv3 : TEXCOORD3; float2 uv4 : TEXCOORD4; float2 uv5 : TEXCOORD5; float2 uv6 : TEXCOORD6; float2 uv7 : TEXCOORD7; - #if defined(LIGHTMAP_ON) - float2 lmuv : TEXCOORD8; - #endif + SHADOW_COORDS(8) +#endif float3 worldPos : TEXCOORD9; float3 normal : TEXCOORD10; float3 objPos : TEXCOORD11; @@ -52,11 +58,15 @@ struct appdata float2 uv0 : TEXCOORD0; float2 uv1 : TEXCOORD1; float2 uv2 : TEXCOORD2; +#if defined(_WORLD_INTERPOLATORS) + UNITY_LIGHTING_COORDS(2, 3) +#else float2 uv3 : TEXCOORD3; float2 uv4 : TEXCOORD4; float2 uv5 : TEXCOORD5; float2 uv6 : TEXCOORD6; float2 uv7 : TEXCOORD7; +#endif float3 normal : NORMAL; float4 tangent : TANGENT; @@ -69,23 +79,24 @@ struct v2f float2 uv0 : TEXCOORD0; float2 uv1 : TEXCOORD1; float2 uv2 : TEXCOORD2; +#if defined(_WORLD_INTERPOLATORS) + UNITY_LIGHTING_COORDS(2, 3) +#else float2 uv3 : TEXCOORD3; float2 uv4 : TEXCOORD4; float2 uv5 : TEXCOORD5; float2 uv6 : TEXCOORD6; float2 uv7 : TEXCOORD7; - #if defined(LIGHTMAP_ON) - float2 lmuv : TEXCOORD8; - #endif + SHADOW_COORDS(8) +#endif float3 normal : TEXCOORD9; float4 tangent : TEXCOORD10; float3 worldPos : TEXCOORD11; float3 objPos : TEXCOORD12; float3 centerCamPos : TEXCOORD13; - SHADOW_COORDS(14) #if defined(VERTEXLIGHT_ON) - float3 vertexLightColor : TEXCOORD15; + float3 vertexLightColor : TEXCOORD14; #endif UNITY_VERTEX_INPUT_INSTANCE_ID @@ -82,6 +82,43 @@ float3 getDirectLightColor() return _LightColor0.rgb; } +#if defined(_WORLD_INTERPOLATORS) +UnityGI getBakedGI(v2f i, float3 view_dir, + UnityLight direct_light, float atten, float ao, float3 normal, + float smoothness, float3 specular_tint) { + UnityGIInput d; + d.light = direct_light; + d.worldPos = i.worldPos; + d.worldViewDir = view_dir; + d.atten = atten; + + d.ambient = 0; + d.lightmapUV.xy = i.uv2; + d.lightmapUV.zw = 0; + + d.probeHDR[0] = unity_SpecCube0_HDR; + d.probeHDR[1] = unity_SpecCube1_HDR; +#if defined(UNITY_SPECCUBE_BLENDING) || defined(UNITY_SPECCUBE_BOX_PROJECTION) + d.boxMin[0] = unity_SpecCube0_BoxMin; // .w holds lerp value for blending +#endif +#ifdef UNITY_SPECCUBE_BOX_PROJECTION + d.boxMax[0] = unity_SpecCube0_BoxMax; + d.probePosition[0] = unity_SpecCube0_ProbePosition; + d.boxMax[1] = unity_SpecCube1_BoxMax; + d.boxMin[1] = unity_SpecCube1_BoxMin; + d.probePosition[1] = unity_SpecCube1_ProbePosition; +#endif + + Unity_GlossyEnvironmentData g = UnityGlossyEnvironmentSetup(smoothness, -view_dir, normal, specular_tint); + // Replace the reflUVW if it has been compute in Vertex shader. Note: the compiler will optimize the calcul in UnityG lossyEnvironmentSetup itself +#if UNITY_STANDARD_SIMPLE + g.reflUVW = reflUVW; +#endif + + return UnityGlobalIllumination (d, ao, normal, g); +} +#endif // _WORLD_INTERPOLATORS + float GetRoughness(float smoothness) { float r = 1 - smoothness; r *= 1.7 - 0.7 * r; @@ -217,11 +254,7 @@ float4 getLitColor( UnityIndirect indirect_light; vertexLightColor *= _Vertex_Lighting_Factor; - indirect_light.diffuse = getIndirectDiffuse(vertexLightColor, normal); - indirect_light.specular = getIndirectSpecular(view_dir, normal, smoothness, - metallic, worldPos, uv); - float attenuation; UnityLight direct_light; direct_light.dir = getDirectLightDirection(i); direct_light.ndotl = DotClamped(normal, direct_light.dir); @@ -232,6 +265,25 @@ float4 getLitColor( direct_light.color = getDirectLightColor(); #endif +#if defined(_WORLD_INTERPOLATORS) + UNITY_LIGHT_ATTENUATION(shadow_attenuation, i, i.worldPos); +#else + float shadow_attenuation = getShadowAttenuation(i); +#endif + +#if defined(_WORLD_INTERPOLATORS) + { + UnityGI gi = getBakedGI(i, view_dir, direct_light, shadow_attenuation, ao, + normal, smoothness, specular_tint); + direct_light = gi.light; + indirect_light = gi.indirect; + } +#else + indirect_light.diffuse = getIndirectDiffuse(vertexLightColor, normal); + indirect_light.specular = getIndirectSpecular(view_dir, normal, smoothness, + metallic, worldPos, uv); +#endif + if (normals_mode == 0) { float e = 0.8; indirect_light.diffuse += direct_light.color * e; @@ -268,10 +320,14 @@ float4 getLitColor( indirect_light.diffuse[2]); // Do this to avoid division by 0. If both light sources are black, // sum_brightness could be 0; +#if defined(_BRIGHTNESS_CLAMP) brightnesses = max(brightnesses, min(_Min_Brightness, .001)); +#endif float sum_brightness = brightnesses[0] + brightnesses[1]; float2 brightness_proportions = brightnesses / sum_brightness; +#if defined(_BRIGHTNESS_CLAMP) sum_brightness = clamp(sum_brightness, _Min_Brightness, _Max_Brightness); +#endif direct_light.color[2] = sum_brightness * brightness_proportions[0]; indirect_light.diffuse[2] = sum_brightness * brightness_proportions[1]; @@ -299,7 +355,9 @@ float4 getLitColor( indirect_light.diffuse[2] *= _Lighting_Factor * _Indirect_Diffuse_Lighting_Factor; // Specular has to be clamped separately to avoid artifacting. +#if defined(_BRIGHTNESS_CLAMP) indirect_light.specular[2] = clamp(indirect_light.specular[2], _Min_Brightness, _Max_Brightness); +#endif direct_light.color = HSVtoRGB(direct_light.color); indirect_light.specular = HSVtoRGB(indirect_light.specular); @@ -310,8 +368,6 @@ float4 getLitColor( float3 direct_color = direct_light.color; direct_light.color *= ao; - // Apply shadows - float shadow_attenuation = getShadowAttenuation(i); direct_light.color *= shadow_attenuation; float2 screenUVs = 0; @@ -331,7 +387,7 @@ float4 getLitColor( /*thickness=*/1, /*ssColor=*/0, shadow_attenuation, - /*lightmapUV=*/0, + i.uv2, vertexLightColor, direct_light, indirect_light); diff --git a/tooner.shader b/tooner.shader index 84a6539..345e3a9 100644 --- a/tooner.shader +++ b/tooner.shader @@ -19,10 +19,12 @@ Shader "yum_food/tooner" _Clearcoat_Mask2("Clearcoat mask 2", 2D) = "white" {} _Clearcoat_Mask2_Invert("Clearcoat mask 2 invert", Float) = 0 - [NoScaleOffset] _MainTex("Base color", 2D) = "white" {} - [NoScaleOffset] _NormalTex("Normal", 2D) = "bump" {} - [NoScaleOffset] _MetallicTex("Metallic", 2D) = "white" {} - [NoScaleOffset] _RoughnessTex("Roughness", 2D) = "black" {} + _MainTex("Base color", 2D) = "white" {} + [Normal] _BumpMap("Normal", 2D) = "bump" {} + _MetallicTex("Metallic", 2D) = "white" {} + _MetallicTexChannel("Metallic", Range(0, 3)) = 2 + _RoughnessTex("Roughness", 2D) = "black" {} + _RoughnessTexChannel("Roughness", Range(0, 3)) = 1 _PBR_Sampler_Mode("Sampler mode", Range(0,1)) = 0 _PBR_Overlay0_Enable("Enable PBR overlay", Float) = 0.0 @@ -153,12 +155,15 @@ Shader "yum_food/tooner" _Decal3_Angle("Emission strength", Range(0,1)) = 0 _Decal3_UV_Select("UV channel", Range(0,7)) = 0 + [NoScaleOffset] _EmissionMap("Emission map", 2D) = "black" {} + _EmissionColor("Emission color", Color) = (0, 0, 0) + [NoScaleOffset] _Emission0Tex("Emission map", 2D) = "black" {} - _Emission0Strength("Emission strength", Range(0, 10)) = 0 + _Emission0Color("Emission color", Color) = (0, 0, 0) _Emission0Multiplier("Emission multiplier", Range(0, 2)) = 1 _Emission0_UV_Select("UV channel", Range(0,7)) = 0 [NoScaleOffset] _Emission1Tex("Emission map", 2D) = "black" {} - _Emission1Strength("Emission strength", Range(0, 10)) = 0 + _Emission1Color("Emission color", Color) = (0, 0, 0) _Emission1Multiplier("Emission multiplier", Range(0, 2)) = 1 _Emission1_UV_Select("UV channel", Range(0,7)) = 0 _Global_Emission_Factor("Global emission factor", Float) = 1 @@ -174,6 +179,8 @@ Shader "yum_food/tooner" _Indirect_Specular_Lighting_Factor2("Indirect specular lighting factor", Range(0, 5)) = 1 _Indirect_Diffuse_Lighting_Factor("Indirect diffuse lighting factor", Range(0, 5)) = 1 _Reflection_Probe_Saturation("Reflection probe saturation", Range(0, 1)) = 1 + _Enable_World_Interpolators("Enable world interpolators", Float) = 0 + _Enable_Brightness_Clamp("Enable brightness clamp", Float) = 1 _Min_Brightness("Min brightness", Range(0, 1)) = 0 _Max_Brightness("Max brightness", Range(0, 1.5)) = 1 _Mesh_Normal_Strength("Mesh normal strength", Range(0, 10)) = 1 diff --git a/tooner_lighting.cginc b/tooner_lighting.cginc index 24d240b..8867630 100644 --- a/tooner_lighting.cginc +++ b/tooner_lighting.cginc @@ -183,12 +183,17 @@ v2f vert(appdata v) o.tangent = float4(UnityObjectToWorldDir(v.tangent.xyz), v.tangent.w); o.uv0 = v.uv0; o.uv1 = v.uv1; +#if defined(_WORLD_INTERPOLATORS) + o.uv2 = v.uv2 * unity_LightmapST.xy + unity_LightmapST.zw; + UNITY_TRANSFER_LIGHTING(o, v.uv2); +#else o.uv2 = v.uv2; o.uv3 = v.uv3; o.uv4 = v.uv4; o.uv5 = v.uv5; o.uv6 = v.uv6; o.uv7 = v.uv7; +#endif #if defined(_MIRROR_UV_FLIP) if (_Mirror_UV_Flip_Enable_Dynamic) { bool in_mirror = isInMirror(); @@ -202,9 +207,6 @@ v2f vert(appdata v) o.uv7.x = lerp(o.uv7.x, 1 - o.uv7.x, in_mirror); } #endif -#if defined(LIGHTMAP_ON) - o.lmuv = v.uv1 * unity_LightmapST.xy + unity_LightmapST.zw; -#endif #if defined(SHADOWS_SCREEN) TRANSFER_SHADOW(o); #endif @@ -550,6 +552,7 @@ float2 get_uv_by_channel(v2f i, uint which_channel) { case 1: return i.uv1; break; +#if !defined(_WORLD_INTERPOLATORS) case 2: return i.uv2; break; @@ -568,6 +571,7 @@ float2 get_uv_by_channel(v2f i, uint which_channel) { case 7: return i.uv7; break; +#endif default: return 0; break; @@ -577,11 +581,11 @@ float2 get_uv_by_channel(v2f i, uint which_channel) { #define UV_SCOFF(i, tex_st, which_channel) get_uv_by_channel(i, round(which_channel)) * (tex_st).xy + (tex_st).zw #if defined(_PBR_SAMPLER_REPEAT) -#define GET_SAMPLER_PBR linear_repeat_s +#define GET_SAMPLER_PBR bilinear_repeat_s #elif defined(_PBR_SAMPLER_CLAMP) -#define GET_SAMPLER_PBR linear_clamp_s +#define GET_SAMPLER_PBR bilinear_clamp_s #else -#define GET_SAMPLER_PBR linear_clamp_s +#define GET_SAMPLER_PBR bilinear_clamp_s #endif #if defined(_PBR_OVERLAY0_SAMPLER_REPEAT) #define GET_SAMPLER_OV0 linear_repeat_s @@ -1270,7 +1274,8 @@ float4 effect(inout v2f i) // Not necessarily normalized after interpolation. i.normal = normalize(i.normal); - i.tangent.xyz = normalize(i.tangent.xyz); + i.tangent.xyz = normalize(i.tangent.xyz - i.normal * dot(i.tangent.xyz, i.normal)); + //i.tangent.xyz = normalize(i.tangent.xyz); #if defined(_TROCHOID) { @@ -1350,8 +1355,8 @@ float4 effect(inout v2f i) // Use UVs to smoothly blend between fully detailed normals when close up and // flat normals when far away. If we don't do this, then we see moire effects // on e.g. striped normal maps. - float3 raw_normal = UnpackScaleNormal(_NormalTex.SampleBias(GET_SAMPLER_PBR, - UV_SCOFF(i, _NormalTex_ST, 0), _Global_Sample_Bias), + float3 raw_normal = UnpackScaleNormal(_BumpMap.SampleBias(GET_SAMPLER_PBR, + UV_SCOFF(i, _BumpMap_ST, 0), _Global_Sample_Bias), _Tex_NormalStr); #else float3 raw_normal = UnpackNormal(float4(0.5, 0.5, 1, 1)); @@ -1362,8 +1367,8 @@ float4 effect(inout v2f i) float3 binormal = CreateBinormal(i.normal, i.tangent.xyz, i.tangent.w); // normalize is not necessary; result is already normalized float3 normal = float3( - raw_normal.x * i.tangent + - raw_normal.y * binormal + + raw_normal.x * normalize(i.tangent) + + raw_normal.y * normalize(binormal) + raw_normal.z * i.normal ); @@ -1387,12 +1392,13 @@ float4 effect(inout v2f i) #if defined(_METALLIC_MAP) float metallic = _MetallicTex.SampleBias(GET_SAMPLER_PBR, - UV_SCOFF(i, _MetallicTex_ST, 0), _Global_Sample_Bias); + UV_SCOFF(i, _MetallicTex_ST, 0), _Global_Sample_Bias)[round(_MetallicTexChannel)]; #else float metallic = _Metallic; #endif #if defined(_ROUGHNESS_MAP) - float roughness = _RoughnessTex.SampleBias(GET_SAMPLER_PBR, UV_SCOFF(i, _RoughnessTex_ST, 0), _Global_Sample_Bias); + float roughness = _RoughnessTex.SampleBias(GET_SAMPLER_PBR, + UV_SCOFF(i, _RoughnessTex_ST, 0), _Global_Sample_Bias)[round(_RoughnessTexChannel)]; if (_Roughness_Invert) { roughness = 1 - roughness; } @@ -2087,17 +2093,25 @@ float4 effect(inout v2f i) #if defined(_GLITTER) result.rgb += glitter_color_unlit * _Glitter_Brightness; #endif + // This version exists for compatibility with the Bakery lightmapper. We + // specifically need to expose _EmissionMap and _EmissionColor. +#if defined(_EMISSION) + { + float3 emission = _EmissionMap.SampleBias(linear_repeat_s, i.uv0, _Global_Sample_Bias); + result.rgb += emission * _EmissionColor * _Global_Emission_Factor; + } +#endif #if defined(_EMISSION0) { - float emission = _Emission0Tex.SampleBias(linear_repeat_s, get_uv_by_channel(i, round(_Emission0_UV_Select)), _Global_Sample_Bias); - result.rgb += albedo.rgb * emission * _Emission0Strength * + float3 emission = _Emission0Tex.SampleBias(linear_repeat_s, get_uv_by_channel(i, round(_Emission0_UV_Select)), _Global_Sample_Bias); + result.rgb += emission * _Emission0Color * _Global_Emission_Factor * _Emission0Multiplier; } #endif #if defined(_EMISSION1) { - float emission = _Emission1Tex.SampleBias(linear_repeat_s, get_uv_by_channel(i, round(_Emission1_UV_Select)), _Global_Sample_Bias); - result.rgb += albedo.rgb * emission * _Emission1Strength * + float3 emission = _Emission1Tex.SampleBias(linear_repeat_s, get_uv_by_channel(i, round(_Emission1_UV_Select)), _Global_Sample_Bias); + result.rgb += emission * _Emission1Color * _Global_Emission_Factor * _Emission1Multiplier; } #endif @@ -2123,6 +2137,8 @@ float4 effect(inout v2f i) fixed4 frag(v2f i) : SV_Target { + UNITY_APPLY_DITHER_CROSSFADE(i.pos.xy); + UNITY_SETUP_INSTANCE_ID(i); UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i); return effect(i); } diff --git a/tooner_outline_pass.cginc b/tooner_outline_pass.cginc index 52931d8..d8be2a7 100644 --- a/tooner_outline_pass.cginc +++ b/tooner_outline_pass.cginc @@ -79,16 +79,18 @@ v2f vert(appdata v) o.objPos = objPos; o.pos = UnityObjectToClipPos(objPos); o.normal = UnityObjectToWorldNormal(v.normal); - o.uv0 = v.uv0.xy; + o.uv0 = v.uv0; o.uv1 = v.uv1; +#if defined(_WORLD_INTERPOLATORS) + o.uv2 = v.uv2 * unity_LightmapST.xy + unity_LightmapST.zw; + UNITY_TRANSFER_LIGHTING(o, v.uv1); +#else o.uv2 = v.uv2; o.uv3 = v.uv3; o.uv4 = v.uv4; o.uv5 = v.uv5; o.uv6 = v.uv6; o.uv7 = v.uv7; -#if defined(LIGHTMAP_ON) - o.lmuv = v.uv1 * unity_LightmapST.xy + unity_LightmapST.zw; #endif o.centerCamPos = getCenterCamPos(); |
