公式ページ
問題
OcclusionLitに MToon のモデルを差し替えるとダサい
結論
目標
カスタムシェーダーを作る
MToonOcclusion.shader
Shader "VRM/MToonOcclusion"
{
Properties
{
// --- MToon Properties (Original) ---
_Cutoff ("Alpha Cutoff", Range(0, 1)) = 0.5
_Color ("Lit Color + Alpha", Color) = (1,1,1,1)
_ShadeColor ("Shade Color", Color) = (0.97, 0.81, 0.86, 1)
[NoScaleOffset] _MainTex ("Lit Texture + Alpha", 2D) = "white" {}
[NoScaleOffset] _ShadeTexture ("Shade Texture", 2D) = "white" {}
_BumpScale ("Normal Scale", Float) = 1.0
[Normal] _BumpMap ("Normal Texture", 2D) = "bump" {}
_ReceiveShadowRate ("Receive Shadow", Range(0, 1)) = 1
[NoScaleOffset] _ReceiveShadowTexture ("Receive Shadow Texture", 2D) = "white" {}
_ShadingGradeRate ("Shading Grade", Range(0, 1)) = 1
[NoScaleOffset] _ShadingGradeTexture ("Shading Grade Texture", 2D) = "white" {}
_ShadeShift ("Shade Shift", Range(-1, 1)) = 0
_ShadeToony ("Shade Toony", Range(0, 1)) = 0.9
_LightColorAttenuation ("Light Color Attenuation", Range(0, 1)) = 0
_IndirectLightIntensity ("Indirect Light Intensity", Range(0, 1)) = 0.1
[HDR] _RimColor ("Rim Color", Color) = (0,0,0)
[NoScaleOffset] _RimTexture ("Rim Texture", 2D) = "white" {}
_RimLightingMix ("Rim Lighting Mix", Range(0, 1)) = 0
[PowerSlider(4.0)] _RimFresnelPower ("Rim Fresnel Power", Range(0, 100)) = 1
_RimLift ("Rim Lift", Range(0, 1)) = 0
[NoScaleOffset] _SphereAdd ("Sphere Texture(Add)", 2D) = "black" {}
[HDR] _EmissionColor ("Color", Color) = (0,0,0)
[NoScaleOffset] _EmissionMap ("Emission", 2D) = "white" {}
[NoScaleOffset] _OutlineWidthTexture ("Outline Width Tex", 2D) = "white" {}
_OutlineWidth ("Outline Width", Range(0.01, 1)) = 0.5
_OutlineScaledMaxDistance ("Outline Scaled Max Distance", Range(1, 10)) = 1
_OutlineColor ("Outline Color", Color) = (0,0,0,1)
_OutlineLightingMix ("Outline Lighting Mix", Range(0, 1)) = 1
[NoScaleOffset] _UvAnimMaskTexture ("UV Animation Mask", 2D) = "white" {}
_UvAnimScrollX ("UV Animation Scroll X", Float) = 0
_UvAnimScrollY ("UV Animation Scroll Y", Float) = 0
_UvAnimRotation ("UV Animation Rotation", Float) = 0
// --- Occlusion Property (Added) ---
_EnvironmentDepthBias ("Environment Depth Bias", Float) = 0.0
// --- MToon Hidden Properties (Original) ---
[HideInInspector] _MToonVersion ("_MToonVersion", Float) = 39
[HideInInspector] _DebugMode ("_DebugMode", Float) = 0.0
[HideInInspector] _BlendMode ("_BlendMode", Float) = 0.0
[HideInInspector] _OutlineWidthMode ("_OutlineWidthMode", Float) = 0.0
[HideInInspector] _OutlineColorMode ("_OutlineColorMode", Float) = 0.0
[HideInInspector] _CullMode ("_CullMode", Float) = 2.0
[HideInInspector] _OutlineCullMode ("_OutlineCullMode", Float) = 1.0
[HideInInspector] _SrcBlend ("_SrcBlend", Float) = 1.0
[HideInInspector] _DstBlend ("_DstBlend", Float) = 0.0
[HideInInspector] _ZWrite ("_ZWrite", Float) = 1.0
[HideInInspector] _AlphaToMask ("_AlphaToMask", Float) = 0.0
}
// for SM 3.0
SubShader
{
// --- Tags (Modified for Transparency) ---
Tags { "RenderType" = "Transparent" "Queue" = "Transparent" }
// Forward Base
Pass
{
Name "FORWARD_BASE"
Tags { "LightMode" = "ForwardBase" }
Cull [_CullMode]
// --- Critical: Use premultiplied alpha blending for occlusion to work correctly ---
// The fragment shader premultiplies the color by alpha, so we use One, OneMinusSrcAlpha
Blend One OneMinusSrcAlpha
ZWrite [_ZWrite]
ZTest LEqual
BlendOp Add, Max
AlphaToMask [_AlphaToMask]
CGPROGRAM
#pragma target 3.0
#pragma shader_feature _ MTOON_DEBUG_NORMAL MTOON_DEBUG_LITSHADERATE
#pragma multi_compile _ _NORMALMAP
#pragma multi_compile _ _ALPHATEST_ON _ALPHABLEND_ON
// --- Occlusion Compile Option (Added) ---
#pragma multi_compile _ HARD_OCCLUSION SOFT_OCCLUSION
// --- Include file path (Modified) ---
#include "./MToonOcclusionCore.cginc"
#pragma vertex vert_forward_base
#pragma fragment frag_forward
#pragma multi_compile_fwdbase
#pragma multi_compile_fog
// #pragma multi_compile_instancing
ENDCG
}
// Forward Base Outline Pass
Pass
{
Name "FORWARD_BASE_ONLY_OUTLINE"
Tags { "LightMode" = "ForwardBase" }
Cull [_OutlineCullMode]
// --- Use same blend mode for outlines ---
Blend One OneMinusSrcAlpha
ZWrite [_ZWrite]
ZTest LEqual
Offset 1, 1
BlendOp Add, Max
AlphaToMask [_AlphaToMask]
CGPROGRAM
#pragma target 3.0
#pragma shader_feature _ MTOON_DEBUG_NORMAL MTOON_DEBUG_LITSHADERATE
#pragma multi_compile _ MTOON_OUTLINE_WIDTH_WORLD MTOON_OUTLINE_WIDTH_SCREEN
#pragma multi_compile _ MTOON_OUTLINE_COLOR_FIXED MTOON_OUTLINE_COLOR_MIXED
#pragma multi_compile _ _NORMALMAP
#pragma multi_compile _ _ALPHATEST_ON _ALPHABLEND_ON
#define MTOON_CLIP_IF_OUTLINE_IS_NONE
// --- Occlusion Compile Option (Added) ---
#pragma multi_compile _ HARD_OCCLUSION SOFT_OCCLUSION
// --- Include file path (Modified) ---
#include "./MToonOcclusionCore.cginc"
#pragma vertex vert_forward_base_outline
#pragma fragment frag_forward
#pragma multi_compile_fwdbase
#pragma multi_compile_fog
// #pragma multi_compile_instancing
ENDCG
}
// Forward Add
Pass
{
Name "FORWARD_ADD"
Tags { "LightMode" = "ForwardAdd" }
Cull [_CullMode]
// --- For additive lights, use additive blending ---
Blend One One
ZWrite Off
ZTest LEqual
BlendOp Add, Max
AlphaToMask [_AlphaToMask]
CGPROGRAM
#pragma target 3.0
#pragma shader_feature _ MTOON_DEBUG_NORMAL MTOON_DEBUG_LITSHADERATE
#pragma multi_compile _ _NORMALMAP
#pragma multi_compile _ _ALPHATEST_ON _ALPHABLEND_ON
#define MTOON_FORWARD_ADD
// --- Include file path (Modified) ---
#include "./MToonOcclusionCore.cginc"
#pragma vertex vert_forward_add
#pragma fragment frag_forward
#pragma multi_compile_fwdadd_fullshadows
#pragma multi_compile_fog
ENDCG
}
// Shadow rendering pass
Pass
{
Name "ShadowCaster"
Tags { "LightMode" = "ShadowCaster" }
Cull [_CullMode]
ZWrite On
ZTest LEqual
CGPROGRAM
#pragma target 3.0
#pragma multi_compile _ _ALPHATEST_ON _ALPHABLEND_ON
#pragma multi_compile_shadowcaster
#pragma vertex vertShadowCaster
#pragma fragment fragShadowCaster
#include "UnityStandardShadow.cginc"
ENDCG
}
}
Fallback "Unlit/Texture"
CustomEditor "MToon.MToonInspector"
}
MToonOcclusionCore.cginc
#ifndef MTOON_OCCLUSION_CORE_INCLUDED
#define MTOON_OCCLUSION_CORE_INCLUDED
#include "Lighting.cginc"
#include "AutoLight.cginc"
// --- Occlusion Include (Added) ---
#include "Packages/com.meta.xr.sdk.core/Shaders/EnvironmentDepth/BiRP/EnvironmentOcclusionBiRP.cginc"
// --- MToon Properties (Original) ---
half _Cutoff;
fixed4 _Color;
fixed4 _ShadeColor;
sampler2D _MainTex; float4 _MainTex_ST;
sampler2D _ShadeTexture;
half _BumpScale;
sampler2D _BumpMap;
sampler2D _ReceiveShadowTexture;
half _ReceiveShadowRate;
sampler2D _ShadingGradeTexture;
half _ShadingGradeRate;
half _ShadeShift;
half _ShadeToony;
half _LightColorAttenuation;
half _IndirectLightIntensity;
sampler2D _RimTexture;
half4 _RimColor;
half _RimLightingMix;
half _RimFresnelPower;
half _RimLift;
sampler2D _SphereAdd;
half4 _EmissionColor;
sampler2D _EmissionMap;
sampler2D _OutlineWidthTexture;
half _OutlineWidth;
half _OutlineScaledMaxDistance;
fixed4 _OutlineColor;
half _OutlineLightingMix;
sampler2D_float _UvAnimMaskTexture;
float _UvAnimScrollX;
float _UvAnimScrollY;
float _UvAnimRotation;
// --- Occlusion Property (Added) ---
float _EnvironmentDepthBias;
struct v2f
{
float4 pos : SV_POSITION;
float4 posWorld : TEXCOORD0;
half3 tspace0 : TEXCOORD1;
half3 tspace1 : TEXCOORD2;
half3 tspace2 : TEXCOORD3;
float2 uv0 : TEXCOORD4;
float isOutline : TEXCOORD5;
fixed4 color : TEXCOORD6;
UNITY_FOG_COORDS(7)
UNITY_SHADOW_COORDS(8)
UNITY_VERTEX_OUTPUT_STEREO
};
inline v2f InitializeV2F(appdata_full v, float4 projectedVertex, float isOutline)
{
v2f o;
UNITY_INITIALIZE_OUTPUT(v2f, o);
UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(o);
o.pos = projectedVertex;
o.posWorld = mul(unity_ObjectToWorld, v.vertex);
o.uv0 = v.texcoord;
half3 worldNormal = UnityObjectToWorldNormal(v.normal);
half3 worldTangent = UnityObjectToWorldDir(v.tangent);
half tangentSign = v.tangent.w * unity_WorldTransformParams.w;
half3 worldBitangent = cross(worldNormal, worldTangent) * tangentSign;
o.tspace0 = half3(worldTangent.x, worldBitangent.x, worldNormal.x);
o.tspace1 = half3(worldTangent.y, worldBitangent.y, worldNormal.y);
o.tspace2 = half3(worldTangent.z, worldBitangent.z, worldNormal.z);
o.isOutline = isOutline;
o.color = v.color;
UNITY_TRANSFER_SHADOW(o, o._ShadowCoord);
UNITY_TRANSFER_FOG(o, o.pos);
return o;
}
inline float4 CalculateOutlineVertexClipPosition(appdata_full v)
{
float outlineTex = tex2Dlod(_OutlineWidthTexture, float4(TRANSFORM_TEX(v.texcoord, _MainTex), 0, 0)).r;
#if defined(MTOON_OUTLINE_WIDTH_WORLD)
float3 worldNormalLength = length(mul((float3x3)transpose(unity_WorldToObject), v.normal));
float3 outlineOffset = 0.01 * _OutlineWidth * outlineTex * worldNormalLength * v.normal;
float4 vertex = UnityObjectToClipPos(v.vertex + outlineOffset);
#elif defined(MTOON_OUTLINE_WIDTH_SCREEN)
float4 nearUpperRight = mul(unity_CameraInvProjection, float4(1, 1, UNITY_NEAR_CLIP_VALUE, _ProjectionParams.y));
float aspect = abs(nearUpperRight.y / nearUpperRight.x);
float4 vertex = UnityObjectToClipPos(v.vertex);
float3 viewNormal = mul((float3x3)UNITY_MATRIX_IT_MV, v.normal.xyz);
float3 clipNormal = TransformViewToProjection(viewNormal.xyz);
float2 projectedNormal = normalize(clipNormal.xy);
projectedNormal *= min(vertex.w, _OutlineScaledMaxDistance);
projectedNormal.x *= aspect;
vertex.xy += 0.01 * _OutlineWidth * outlineTex * projectedNormal.xy * saturate(1 - abs(normalize(viewNormal).z));
#else
float4 vertex = UnityObjectToClipPos(v.vertex);
#endif
return vertex;
}
v2f vert_forward_base(appdata_full v)
{
UNITY_SETUP_INSTANCE_ID(v);
v.normal = normalize(v.normal);
return InitializeV2F(v, UnityObjectToClipPos(v.vertex), 0);
}
v2f vert_forward_base_outline(appdata_full v)
{
UNITY_SETUP_INSTANCE_ID(v);
v.normal = normalize(v.normal);
return InitializeV2F(v, CalculateOutlineVertexClipPosition(v), 1);
}
v2f vert_forward_add(appdata_full v)
{
UNITY_SETUP_INSTANCE_ID(v);
v.normal = normalize(v.normal);
return InitializeV2F(v, UnityObjectToClipPos(v.vertex), 0);
}
float4 frag_forward(v2f i) : SV_TARGET
{
#ifdef MTOON_CLIP_IF_OUTLINE_IS_NONE
#ifdef MTOON_OUTLINE_WIDTH_WORLD
#elif MTOON_OUTLINE_WIDTH_SCREEN
#else
clip(-1);
#endif
#endif
UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i);
const float PI_2 = 6.28318530718;
const float EPS_COL = 0.00001;
float2 mainUv = TRANSFORM_TEX(i.uv0, _MainTex);
float uvAnim = tex2D(_UvAnimMaskTexture, mainUv).r * _Time.y;
mainUv += float2(_UvAnimScrollX, _UvAnimScrollY) * uvAnim;
float rotateRad = _UvAnimRotation * PI_2 * uvAnim;
const float2 rotatePivot = float2(0.5, 0.5);
mainUv = mul(float2x2(cos(rotateRad), -sin(rotateRad), sin(rotateRad), cos(rotateRad)), mainUv - rotatePivot) + rotatePivot;
half4 mainTex = tex2D(_MainTex, mainUv);
half alpha = 1;
#ifdef _ALPHATEST_ON
alpha = _Color.a * mainTex.a;
alpha = (alpha - _Cutoff) / max(fwidth(alpha), EPS_COL) + 0.5;
clip(alpha - _Cutoff);
alpha = 1.0;
#endif
#ifdef _ALPHABLEND_ON
alpha = _Color.a * mainTex.a;
#if !_ALPHATEST_ON && SHADER_API_D3D11
clip(alpha - 0.0001);
#endif
#endif
#ifdef _NORMALMAP
half3 tangentNormal = UnpackScaleNormal(tex2D(_BumpMap, mainUv), _BumpScale);
half3 worldNormal;
worldNormal.x = dot(i.tspace0, tangentNormal);
worldNormal.y = dot(i.tspace1, tangentNormal);
worldNormal.z = dot(i.tspace2, tangentNormal);
#else
half3 worldNormal = half3(i.tspace0.z, i.tspace1.z, i.tspace2.z);
#endif
float3 worldView = normalize(lerp(_WorldSpaceCameraPos.xyz - i.posWorld.xyz, UNITY_MATRIX_V[2].xyz, unity_OrthoParams.w));
worldNormal *= step(0, dot(worldView, worldNormal)) * 2 - 1;
worldNormal *= lerp(+1.0, -1.0, i.isOutline);
worldNormal = normalize(worldNormal);
UNITY_LIGHT_ATTENUATION(shadowAttenuation, i, i.posWorld.xyz);
half3 lightDir = lerp(_WorldSpaceLightPos0.xyz, normalize(_WorldSpaceLightPos0.xyz - i.posWorld.xyz), _WorldSpaceLightPos0.w);
half3 lightColor = _LightColor0.rgb * step(0.5, length(lightDir));
half dotNL = dot(lightDir, worldNormal);
#ifdef MTOON_FORWARD_ADD
half lightAttenuation = 1;
#else
half lightAttenuation = shadowAttenuation * lerp(1, shadowAttenuation, _ReceiveShadowRate * tex2D(_ReceiveShadowTexture, mainUv).r);
#endif
half shadingGrade = 1.0 - _ShadingGradeRate * (1.0 - tex2D(_ShadingGradeTexture, mainUv).r);
half lightIntensity = dotNL;
lightIntensity = lightIntensity * 0.5 + 0.5;
lightIntensity = lightIntensity * lightAttenuation;
lightIntensity = lightIntensity * shadingGrade;
lightIntensity = lightIntensity * 2.0 - 1.0;
half maxIntensityThreshold = lerp(1, _ShadeShift, _ShadeToony);
half minIntensityThreshold = _ShadeShift;
lightIntensity = saturate((lightIntensity - minIntensityThreshold) / max(EPS_COL, (maxIntensityThreshold - minIntensityThreshold)));
half4 shade = _ShadeColor * tex2D(_ShadeTexture, mainUv);
half4 lit = _Color * mainTex;
half3 col = lerp(shade.rgb, lit.rgb, lightIntensity);
half3 lighting = lightColor;
lighting = lerp(lighting, max(EPS_COL, max(lighting.x, max(lighting.y, lighting.z))), _LightColorAttenuation);
#ifdef MTOON_FORWARD_ADD
#ifdef _ALPHABLEND_ON
lighting *= step(0, dotNL);
#endif
lighting *= 0.5;
lighting *= min(0, dotNL) + 1;
lighting *= shadowAttenuation;
#else
#endif
col *= lighting;
#ifdef MTOON_FORWARD_ADD
#else
half3 toonedGI = 0.5 * (ShadeSH9(half4(0, 1, 0, 1)) + ShadeSH9(half4(0, -1, 0, 1)));
half3 indirectLighting = lerp(toonedGI, ShadeSH9(half4(worldNormal, 1)), _IndirectLightIntensity);
indirectLighting = lerp(indirectLighting, max(EPS_COL, max(indirectLighting.x, max(indirectLighting.y, indirectLighting.z))), _LightColorAttenuation);
col += indirectLighting * lit;
col = min(col, lit);
#endif
#ifdef MTOON_FORWARD_ADD
half3 staticRimLighting = 0;
half3 mixedRimLighting = lighting;
#else
half3 staticRimLighting = 1;
half3 mixedRimLighting = lighting + indirectLighting;
#endif
half3 rimLighting = lerp(staticRimLighting, mixedRimLighting, _RimLightingMix);
half3 rim = pow(saturate(1.0 - dot(worldNormal, worldView) + _RimLift), max(_RimFresnelPower, EPS_COL)) * _RimColor.rgb * tex2D(_RimTexture, mainUv).rgb;
col += lerp(rim * rimLighting, half3(0, 0, 0), i.isOutline);
#ifdef MTOON_FORWARD_ADD
#else
half3 worldCameraUp = normalize(UNITY_MATRIX_V[1].xyz);
half3 worldViewUp = normalize(worldCameraUp - worldView * dot(worldView, worldCameraUp));
half3 worldViewRight = normalize(cross(worldView, worldViewUp));
half2 matcapUv = half2(dot(worldViewRight, worldNormal), dot(worldViewUp, worldNormal)) * 0.5 + 0.5;
half3 matcapLighting = tex2D(_SphereAdd, matcapUv);
col += lerp(matcapLighting, half3(0, 0, 0), i.isOutline);
#endif
#ifdef MTOON_FORWARD_ADD
#else
half3 emission = tex2D(_EmissionMap, mainUv).rgb * _EmissionColor.rgb;
col += lerp(emission, half3(0, 0, 0), i.isOutline);
#endif
#ifdef MTOON_OUTLINE_COLOR_FIXED
col = lerp(col, _OutlineColor, i.isOutline);
#elif MTOON_OUTLINE_COLOR_MIXED
col = lerp(col, _OutlineColor * lerp(half3(1, 1, 1), col, _OutlineLightingMix), i.isOutline);
#else
#endif
#ifdef MTOON_DEBUG_NORMAL
#ifdef MTOON_FORWARD_ADD
return float4(0, 0, 0, 0);
#else
return float4(worldNormal * 0.5 + 0.5, alpha);
#endif
#elif MTOON_DEBUG_LITSHADERATE
#ifdef MTOON_FORWARD_ADD
return float4(0, 0, 0, 0);
#else
return float4(lightIntensity * lighting, alpha);
#endif
#endif
half4 result = half4(col, alpha);
UNITY_APPLY_FOG(i.fogCoord, result);
// --- Apply Environment Occlusion with Premultiplied Alpha (Fixed) ---
// Only apply occlusion in the main forward base pass, not for additional lights
#if !defined(MTOON_FORWARD_ADD)
// For transparent rendering with occlusion, the color must be pre-multiplied with alpha
// before the occlusion macro. This is critical for correct blending with the environment.
#ifdef _ALPHABLEND_ON
result.rgb *= result.a;
#endif
// Apply environment depth occlusion
// The macro expects premultiplied alpha and will further modify the result
META_DEPTH_OCCLUDE_OUTPUT_PREMULTIPLY_WORLDPOS(i.posWorld.xyz, result, _EnvironmentDepthBias);
#endif
return result;
}
#endif // MTOON_OCCLUSION_CORE_INCLUDED

