From aac55f605acf306204af1a3f2e11bc203b979eed Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Thu, 10 Jun 2021 21:34:01 +0900 Subject: [PATCH] fix view vector is zero --- .../VRM10/vrmc_materials_mtoon_attribute.hlsl | 2 +- ...vrmc_materials_mtoon_forward_fragment.hlsl | 4 ++-- .../vrmc_materials_mtoon_forward_vertex.hlsl | 11 +++++----- .../vrmc_materials_mtoon_lighting_mtoon.hlsl | 22 +++++++++++-------- 4 files changed, 22 insertions(+), 17 deletions(-) diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_attribute.hlsl b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_attribute.hlsl index a9068f852..7ef2a492d 100644 --- a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_attribute.hlsl +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_attribute.hlsl @@ -25,7 +25,7 @@ struct Varyings half4 tangentWS : TEXCOORD3; #endif float3 viewDirWS : TEXCOORD4; - half outlineFactor : TEXCOORD5; + // half outlineFactor : TEXCOORD5; UNITY_FOG_COORDS(6) UNITY_LIGHTING_COORDS(7,8) float4 pos : SV_POSITION; // UnityCG macro specified name. Accurately "positionCS" diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_forward_fragment.hlsl b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_forward_fragment.hlsl index 32973855f..0275269cd 100644 --- a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_forward_fragment.hlsl +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_forward_fragment.hlsl @@ -47,10 +47,10 @@ half4 MToonFragment(const Varyings input) : SV_Target MToonInput mtoonInput; mtoonInput.uv = uv; mtoonInput.normalWS = normalWS; - mtoonInput.viewDirWS = input.viewDirWS; + mtoonInput.viewDirWS = normalize(input.viewDirWS); mtoonInput.litColor = litColor.rgb; mtoonInput.alpha = alpha; - mtoonInput.outlineFactor = input.outlineFactor; + // mtoonInput.outlineFactor = input.outlineFactor; const half4 col = GetMToonLighting(unityLighting, mtoonInput); UNITY_APPLY_FOG(i.fogCoord, col); diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_forward_vertex.hlsl b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_forward_vertex.hlsl index 232495023..efb43d4a2 100644 --- a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_forward_vertex.hlsl +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_forward_vertex.hlsl @@ -17,16 +17,15 @@ Varyings MToonVertex(const Attributes v) // v is UnityCG macro specified name. UNITY_TRANSFER_INSTANCE_ID(v, output); UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output); - output.uv = TRANSFORM_TEX(v.texcoord0, _MainTex); - output.viewDirWS = MToon_GetWorldSpaceNormalizedViewDir(output.positionWS); - if (MToon_IsOutlinePass()) { output.normalWS = UnityObjectToWorldNormal(-v.normalOS); const VertexPositionInfo position = MToon_GetOutlineVertex(v.vertex.xyz, normalize(v.normalOS), output.uv); output.pos = position.positionCS; output.positionWS = position.positionWS; - output.outlineFactor = 1; + output.viewDirWS = MToon_GetWorldSpaceNormalizedViewDir(output.positionWS); + output.uv = TRANSFORM_TEX(v.texcoord0, _MainTex); + // output.outlineFactor = 1; } else { @@ -34,7 +33,9 @@ Varyings MToonVertex(const Attributes v) // v is UnityCG macro specified name. const VertexPositionInfo position = MToon_GetVertex(v.vertex.xyz); output.pos = position.positionCS; output.positionWS = position.positionWS; - output.outlineFactor = 0; + output.viewDirWS = MToon_GetWorldSpaceNormalizedViewDir(output.positionWS); + output.uv = TRANSFORM_TEX(v.texcoord0, _MainTex); + // output.outlineFactor = 0; } #if defined(_NORMALMAP) diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_lighting_mtoon.hlsl b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_lighting_mtoon.hlsl index 9727c262a..7ae0b2fd6 100644 --- a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_lighting_mtoon.hlsl +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_lighting_mtoon.hlsl @@ -14,7 +14,7 @@ struct MToonInput half3 viewDirWS; half3 litColor; half alpha; - half outlineFactor; + // half outlineFactor; }; inline half GetMToonLighting_Reflectance_ShadingShift(const MToonInput input) @@ -62,7 +62,6 @@ inline half3 GetMToonLighting_GlobalIllumination(const UnityLighting unityLight, { return 0; } - } inline half3 GetMToonLighting_Emissive(const MToonInput input) @@ -94,12 +93,12 @@ inline half3 GetMToonLighting_Rim_Matcap(const MToonInput input) } } -inline half3 GetMToonLighting_Rim(const MToonInput input, const half3 lighting) +inline half3 GetMToonLighting_Rim(const MToonInput input, const half3 reflectance) { if (MToon_IsForwardBasePass()) { const half3 parametricRimFactor = pow(saturate(1.0 - dot(input.normalWS, input.viewDirWS) + _RimLift), _RimFresnelPower) * _RimColor.rgb; - const half3 rimLightingFactor = lerp(half3(1, 1, 1), lighting, _RimLightingMix); + const half3 rimLightingFactor = lerp(half3(1, 1, 1), reflectance, _RimLightingMix); const half3 matcapFactor = GetMToonLighting_Rim_Matcap(input); if (MToon_IsRimMapOn()) @@ -125,14 +124,19 @@ half4 GetMToonLighting(const UnityLighting unityLight, const MToonInput input) const half3 indirect = GetMToonLighting_GlobalIllumination(unityLight, input); const half3 lighting = direct + indirect; const half3 emissive = GetMToonLighting_Emissive(input); - const half3 rim = GetMToonLighting_Rim(input, lighting); + const half3 rim = GetMToonLighting_Rim(input, reflectance); const half3 baseCol = lighting + emissive + rim; - const half3 outlineCol = _OutlineColor.rgb * lerp(half3(1, 1, 1), baseCol, _OutlineLightingMix); - const half3 col = lerp(baseCol, outlineCol, input.outlineFactor); - - return half4(col, input.alpha); + if (MToon_IsOutlinePass()) + { + const half3 outlineCol = _OutlineColor.rgb * lerp(half3(1, 1, 1), baseCol, _OutlineLightingMix); + return half4(outlineCol, input.alpha); + } + else + { + return half4(baseCol, input.alpha); + } } #endif