From 09b2d4d3267f46d9fbc8f9b83195654fde4c92b0 Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Fri, 28 May 2021 18:10:59 +0900 Subject: [PATCH 01/17] duplicate MToon10 from MToon --- Assets/VRMShaders/VRM10/MToon10.meta | 8 + .../VRMShaders/VRM10/MToon10/Resources.meta | 8 + .../VRM10/MToon10/Resources/VRM10.meta | 8 + .../VRM10/vrmc_materials_mtoon.shader | 166 +++++++++++ .../VRM10/vrmc_materials_mtoon.shader.meta | 9 + .../VRM10/vrmc_materials_mtoon_core.hlsl | 282 ++++++++++++++++++ .../VRM10/vrmc_materials_mtoon_core.hlsl.meta | 9 + .../VRM10/vrmc_materials_mtoon_sm3.hlsl | 22 ++ .../VRM10/vrmc_materials_mtoon_sm3.hlsl.meta | 9 + 9 files changed, 521 insertions(+) create mode 100644 Assets/VRMShaders/VRM10/MToon10.meta create mode 100644 Assets/VRMShaders/VRM10/MToon10/Resources.meta create mode 100644 Assets/VRMShaders/VRM10/MToon10/Resources/VRM10.meta create mode 100644 Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon.shader create mode 100644 Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon.shader.meta create mode 100644 Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_core.hlsl create mode 100644 Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_core.hlsl.meta create mode 100644 Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_sm3.hlsl create mode 100644 Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_sm3.hlsl.meta diff --git a/Assets/VRMShaders/VRM10/MToon10.meta b/Assets/VRMShaders/VRM10/MToon10.meta new file mode 100644 index 000000000..7f57813b1 --- /dev/null +++ b/Assets/VRMShaders/VRM10/MToon10.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 3fbece93c77c84340a3c4f6e7e8c54cd +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources.meta b/Assets/VRMShaders/VRM10/MToon10/Resources.meta new file mode 100644 index 000000000..6b69de785 --- /dev/null +++ b/Assets/VRMShaders/VRM10/MToon10/Resources.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: d5b5138c63fcb784595ae54e08a0e9e7 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10.meta b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10.meta new file mode 100644 index 000000000..b88df9d64 --- /dev/null +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 09a1a32a6d2428740bd539c448ee1945 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon.shader b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon.shader new file mode 100644 index 000000000..9d63e8f01 --- /dev/null +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon.shader @@ -0,0 +1,166 @@ +Shader "VRM10/vrmc_materials_mtoon" +{ + Properties + { + _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 + + [HideInInspector] _MToonVersion ("_MToonVersion", Float) = 35 + [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 { "RenderType" = "Opaque" "Queue" = "Geometry" } + + // Forward Base + Pass + { + Name "FORWARD_BASE" + Tags { "LightMode" = "ForwardBase" } + + Cull [_CullMode] + Blend [_SrcBlend] [_DstBlend] + 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 + #include "./vrmc_materials_mtoon_sm3.hlsl" + #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] + Blend [_SrcBlend] [_DstBlend] + 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 + #include "./vrmc_materials_mtoon_sm3.hlsl" + #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] + Blend [_SrcBlend] 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 "./vrmc_materials_mtoon_sm3.hlsl" + #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" +} diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon.shader.meta b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon.shader.meta new file mode 100644 index 000000000..fab61232d --- /dev/null +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon.shader.meta @@ -0,0 +1,9 @@ +fileFormatVersion: 2 +guid: e0edbf68d81d1f340ae8b110086b7063 +timeCreated: 1514111466 +licenseType: Free +ShaderImporter: + defaultTextures: [] + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_core.hlsl b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_core.hlsl new file mode 100644 index 000000000..430e0831b --- /dev/null +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_core.hlsl @@ -0,0 +1,282 @@ +#ifndef MTOON_CORE_INCLUDED +#define MTOON_CORE_INCLUDED + +#include "Lighting.cginc" +#include "AutoLight.cginc" + +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 _UvAnimMaskTexture; +float _UvAnimScrollX; +float _UvAnimScrollY; +float _UvAnimRotation; + +//UNITY_INSTANCING_BUFFER_START(Props) +//UNITY_INSTANCING_BUFFER_END(Props) + +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_INPUT_INSTANCE_ID // necessary only if any instanced properties are going to be accessed in the fragment Shader. + 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); + //UNITY_TRANSFER_INSTANCE_ID(v, 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)); // ignore offset when normal toward camera + #else + float4 vertex = UnityObjectToClipPos(v.vertex); + #endif + return vertex; +} + +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_TRANSFER_INSTANCE_ID(v, o); + UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i); + + // const + const float PI_2 = 6.28318530718; + const float EPS_COL = 0.00001; + + // uv + float2 mainUv = TRANSFORM_TEX(i.uv0, _MainTex); + + // uv anim + float uvAnim = tex2D(_UvAnimMaskTexture, mainUv).r * _Time.y; + // translate uv in bottom-left origin coordinates. + mainUv += float2(_UvAnimScrollX, _UvAnimScrollY) * uvAnim; + // rotate uv counter-clockwise around (0.5, 0.5) in bottom-left origin coordinates. + 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; + + // main tex + half4 mainTex = tex2D(_MainTex, mainUv); + + // alpha + half alpha = 1; +#ifdef _ALPHATEST_ON + alpha = _Color.a * mainTex.a; + alpha = (alpha - _Cutoff) / max(fwidth(alpha), EPS_COL) + 0.5; // Alpha to Coverage + clip(alpha - _Cutoff); + alpha = 1.0; // Discarded, otherwise it should be assumed to have full opacity +#endif +#ifdef _ALPHABLEND_ON + alpha = _Color.a * mainTex.a; +#if !_ALPHATEST_ON && SHADER_API_D3D11 // Only enable this on D3D11, where I tested it + clip(alpha - 0.0001); // Slightly improves rendering with layered transparency +#endif +#endif + + // normal +#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; // flip if projection matrix is flipped + worldNormal *= lerp(+1.0, -1.0, i.isOutline); + worldNormal = normalize(worldNormal); + + // Unity lighting + 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)); // length(lightDir) is zero if directional light is disabled. + 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 + + // Decide albedo color rate from Direct Light + half shadingGrade = 1.0 - _ShadingGradeRate * (1.0 - tex2D(_ShadingGradeTexture, mainUv).r); + half lightIntensity = dotNL; // [-1, +1] + lightIntensity = lightIntensity * 0.5 + 0.5; // from [-1, +1] to [0, 1] + lightIntensity = lightIntensity * lightAttenuation; // receive shadow + lightIntensity = lightIntensity * shadingGrade; // darker + lightIntensity = lightIntensity * 2.0 - 1.0; // from [0, 1] to [-1, +1] + // tooned. mapping from [minIntensityThreshold, maxIntensityThreshold] to [0, 1] + half maxIntensityThreshold = lerp(1, _ShadeShift, _ShadeToony); + half minIntensityThreshold = _ShadeShift; + lightIntensity = saturate((lightIntensity - minIntensityThreshold) / max(EPS_COL, (maxIntensityThreshold - minIntensityThreshold))); + + // Albedo color + half4 shade = _ShadeColor * tex2D(_ShadeTexture, mainUv); + half4 lit = _Color * mainTex; + half3 col = lerp(shade.rgb, lit.rgb, lightIntensity); + + // Direct Light + half3 lighting = lightColor; + lighting = lerp(lighting, max(EPS_COL, max(lighting.x, max(lighting.y, lighting.z))), _LightColorAttenuation); // color atten +#ifdef MTOON_FORWARD_ADD +#ifdef _ALPHABLEND_ON + lighting *= step(0, dotNL); // darken if transparent. Because Unity's transparent material can't receive shadowAttenuation. +#endif + lighting *= 0.5; // darken if additional light. + lighting *= min(0, dotNL) + 1; // darken dotNL < 0 area by using half lambert + lighting *= shadowAttenuation; // darken if receiving shadow +#else + // base light does not darken. +#endif + col *= lighting; + + // Indirect Light +#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); // color atten + col += indirectLighting * lit; + + col = min(col, lit); // comment out if you want to PBR absolutely. +#endif + + // parametric rim lighting +#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), _RimFresnelPower) * _RimColor.rgb * tex2D(_RimTexture, mainUv).rgb; + col += lerp(rim * rimLighting, half3(0, 0, 0), i.isOutline); + + // additive matcap +#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 + + // Emission +#ifdef MTOON_FORWARD_ADD +#else + half3 emission = tex2D(_EmissionMap, mainUv).rgb * _EmissionColor.rgb; + col += lerp(emission, half3(0, 0, 0), i.isOutline); +#endif + + // outline +#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 + + // debug +#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); + return result; +} + +#endif // MTOON_CORE_INCLUDED diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_core.hlsl.meta b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_core.hlsl.meta new file mode 100644 index 000000000..6f4d4a593 --- /dev/null +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_core.hlsl.meta @@ -0,0 +1,9 @@ +fileFormatVersion: 2 +guid: 22be54a117660824da79e2b52a710d59 +timeCreated: 1514120022 +licenseType: Free +ShaderImporter: + defaultTextures: [] + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_sm3.hlsl b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_sm3.hlsl new file mode 100644 index 000000000..d50fd0d5e --- /dev/null +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_sm3.hlsl @@ -0,0 +1,22 @@ +#include "./vrmc_materials_mtoon_core.hlsl" + +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); +} diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_sm3.hlsl.meta b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_sm3.hlsl.meta new file mode 100644 index 000000000..910f77863 --- /dev/null +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_sm3.hlsl.meta @@ -0,0 +1,9 @@ +fileFormatVersion: 2 +guid: 5aae6ba3f344c0e43914d19a64704aba +timeCreated: 1514120022 +licenseType: Free +ShaderImporter: + defaultTextures: [] + userData: + assetBundleName: + assetBundleVariant: From 8ec0bfca40152a3bade93652ad1b46ad7e4ab246 Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Fri, 28 May 2021 19:24:42 +0900 Subject: [PATCH 02/17] define MToon10 properties according to schema --- .../VRM10/vrmc_materials_mtoon.shader | 133 ++++++++++-------- 1 file changed, 73 insertions(+), 60 deletions(-) diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon.shader b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon.shader index 9d63e8f01..e0d4071ce 100644 --- a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon.shader +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon.shader @@ -2,69 +2,82 @@ Shader "VRM10/vrmc_materials_mtoon" { Properties { - _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 + // Rendering + _AlphaMode ("alphaMode", Int) = 0 + _TransparentWithZWrite ("mtoon.transparentWithZWrite", Int) = 0 + _Cutoff ("alphaCutoff", Range(0, 1)) = 0.5 // Unity specified name + _RenderQueueOffset ("mtoon.renderQueueOffsetNumber", Int) = 0 + _DoubleSided ("doubleSided", Int) = 0 - [HideInInspector] _MToonVersion ("_MToonVersion", Float) = 35 - [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 + // Lighting + _Color ("pbrMetallicRoughness.baseColorFactor", Color) = (1,1,1,1) // Unity specified name + _MainTex ("pbrMetallicRoughness.baseColorTexture", 2D) = "white" {} // Unity specified name + _ShadeColor ("mtoon.shadeColorFactor", Color) = (1, 1, 1, 1) + _ShadeTex ("mtoon.shadeMultiplyTexture", 2D) = "white" {} + [Normal] _BumpMap ("normalTexture", 2D) = "bump" {} // Unity specified name + _BumpScale ("normalTexture.scale", Float) = 1.0 // Unity specified name + _ShadingShiftColor ("mtoon.shadingShiftFactor", Range(0, 1)) = 0 + _ShadingShiftTex ("mtoon.shadingShiftTexture", 2D) = "black" {} // channel R + _ShadingShiftTexScale ("mtoon.shadingShiftTexture.scale", Float) = 1 + _ShadingToonyColor ("mtoon.shadingToonyFactor", Range(0, 1)) = 0.9 +// _ShadingToonyTex ("mtoon.shadingToonyTexture", 2D) = "black" {} // parameter texture // need? +// _ShadingToonyTexScale ("mtoon.shadingToonyTexture.scale", Float) = 1 // need? + + // GI + _GiEqualization ("mtoon.giEqualizationFactor", Range(0, 1)) = 0.9 + + // Emission + _EmissionColor ("emissiveFactor", Color) = (0, 0, 0) // Unity specified name + _EmissionMap ("emissiveTexture", 2D) = "white" {} // Unity specified name + + // Rim Lighting + _RimMatcapTex ("mtoon.matcapTexture", 2D) = "black" {} + _RimColor ("mtoon.parametricRimColorFactor", Color) = (0, 0, 0) + _RimFresnelPower ("mtoon.parametricRimFresnelPowerFactor", Float) = 5.0 + _RimLift ("mtoon.parametricRimLiftFactor", Float) = 0 + _RimTex ("mtoon.rimMultiplyTexture", 2D) = "white" {} + _RimLightingMix ("mtoon.rimLightingMixFactor", Float) = 1 + + // Outline + _OutlineWidthMode ("mtoon.outlineWidthMode", Int) = 0 + _OutlineWidth ("mtoon.outlineWidthFactor", Float) = 0 + _OutlineWidthTex ("mtoon.outlineWidthMultiplyTexture", 2D) = "white" {} // channel G + _OutlineColor ("mtoon.outlineColorFactor", Color) = (0, 0, 0) + _OutlineLightingMix ("mtoon.outlineLightingMixFactor", Float) = 1 // default 0 + + // UV Animation + _UvAnimMaskTex ("mtoon.uvAnimationMaskTexture", 2D) = "white" {} // channel B + _UvAnimScrollXSpeed ("mtoon.uvAnimationScrollXSpeedFactor", Float) = 0 + _UvAnimScrollYSpeed ("mtoon.uvAnimationScrollYSpeedFactor", Float) = 0 + _UvAnimRotationSpeed ("mtoon.uvAnimationRotationSpeedFactor", Float) = 0 + + // Unity ShaderPass Mode + [HideInInspector] _M_CullMode ("_CullMode", Float) = 2.0 + [HideInInspector] _M_SrcBlend ("_SrcBlend", Float) = 1.0 + [HideInInspector] _M_DstBlend ("_DstBlend", Float) = 0.0 + [HideInInspector] _M_ZWrite ("_ZWrite", Float) = 1.0 + [HideInInspector] _M_AlphaToMask ("_AlphaToMask", Float) = 0.0 + + // etc + [HideInInspector] _M_DebugMode ("_DebugMode", Float) = 0.0 } - // for SM 3.0 SubShader { Tags { "RenderType" = "Opaque" "Queue" = "Geometry" } - // Forward Base + // Built-in Forward Base Pass Pass { Name "FORWARD_BASE" Tags { "LightMode" = "ForwardBase" } - Cull [_CullMode] - Blend [_SrcBlend] [_DstBlend] - ZWrite [_ZWrite] + Cull [_M_CullMode] + Blend [_M_SrcBlend] [_M_DstBlend] + ZWrite [_M_ZWrite] ZTest LEqual BlendOp Add, Max - AlphaToMask [_AlphaToMask] + AlphaToMask [_M_AlphaToMask] CGPROGRAM #pragma target 3.0 @@ -81,19 +94,19 @@ Shader "VRM10/vrmc_materials_mtoon" } - // Forward Base Outline Pass + // Built-in Forward Base Outline Pass Pass { Name "FORWARD_BASE_ONLY_OUTLINE" Tags { "LightMode" = "ForwardBase" } - Cull [_OutlineCullMode] - Blend [_SrcBlend] [_DstBlend] - ZWrite [_ZWrite] + Cull Front + Blend [_M_SrcBlend] [_M_DstBlend] + ZWrite [_M_ZWrite] ZTest LEqual Offset 1, 1 BlendOp Add, Max - AlphaToMask [_AlphaToMask] + AlphaToMask [_M_AlphaToMask] CGPROGRAM #pragma target 3.0 @@ -113,18 +126,18 @@ Shader "VRM10/vrmc_materials_mtoon" } - // Forward Add + // Built-in Forward Add Pass Pass { Name "FORWARD_ADD" Tags { "LightMode" = "ForwardAdd" } - Cull [_CullMode] - Blend [_SrcBlend] One + Cull [_M_CullMode] + Blend [_M_SrcBlend] One ZWrite Off ZTest LEqual BlendOp Add, Max - AlphaToMask [_AlphaToMask] + AlphaToMask [_M_AlphaToMask] CGPROGRAM #pragma target 3.0 @@ -140,13 +153,13 @@ Shader "VRM10/vrmc_materials_mtoon" ENDCG } - // Shadow rendering pass + // Built-in Shadow Rendering Pass Pass { Name "ShadowCaster" Tags { "LightMode" = "ShadowCaster" } - Cull [_CullMode] + Cull [_M_CullMode] ZWrite On ZTest LEqual From 44067be1fce88b12001c67d830e3a14d4e6552a3 Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Mon, 31 May 2021 20:18:34 +0900 Subject: [PATCH 03/17] Implements basic components of mtoon10. --- Assets/VRMShaders/VRM10/MToon10/Editor.meta | 8 + .../VRM10/MToon10/Editor/MToonDefined.meta | 3 + .../MToon10/Editor/MToonDefined/AlphaMode.cs | 9 + .../Editor/MToonDefined/AlphaMode.cs.meta | 3 + .../MToonDefined/TransparentWithZWriteMode.cs | 8 + .../TransparentWithZWriteMode.cs.meta | 3 + .../VRM10/MToon10/Editor/MToonInspector.cs | 64 ++++ .../MToon10/Editor/MToonInspector.cs.meta | 11 + .../VRM10/MToon10/Editor/MToonValidator.cs | 80 +++++ .../MToon10/Editor/MToonValidator.cs.meta | 3 + .../MToon10/Editor/MaterialExtensions.cs | 29 ++ .../MToon10/Editor/MaterialExtensions.cs.meta | 3 + .../VRMShaders/VRM10/MToon10/Editor/Prop.cs | 22 ++ .../VRM10/MToon10/Editor/Prop.cs.meta | 3 + .../VRM10/MToon10/Editor/PropExtensions.cs | 29 ++ .../MToon10/Editor/PropExtensions.cs.meta | 3 + .../VRM10/MToon10/Editor/UnityDefined.meta | 3 + .../UnityDefined/UnityAlphaModeKeyword.cs | 9 + .../UnityAlphaModeKeyword.cs.meta | 3 + .../UnityDefined/UnityAlphaToMaskMode.cs | 8 + .../UnityDefined/UnityAlphaToMaskMode.cs.meta | 3 + .../Editor/UnityDefined/UnityRenderTag.cs | 10 + .../UnityDefined/UnityRenderTag.cs.meta | 3 + .../Editor/UnityDefined/UnityZWriteMode.cs | 8 + .../UnityDefined/UnityZWriteMode.cs.meta | 3 + .../VRMShaders.VRM10.MToon10.Editor.asmdef | 15 + ...RMShaders.VRM10.MToon10.Editor.asmdef.meta | 7 + .../VRM10/vrmc_materials_mtoon.shader | 123 ++++---- .../VRM10/vrmc_materials_mtoon_attribute.hlsl | 34 +++ .../vrmc_materials_mtoon_attribute.hlsl.meta | 3 + .../VRM10/vrmc_materials_mtoon_core.hlsl | 282 ------------------ .../VRM10/vrmc_materials_mtoon_define.hlsl | 8 + .../vrmc_materials_mtoon_define.hlsl.meta | 3 + ...vrmc_materials_mtoon_forward_fragment.hlsl | 57 ++++ ...aterials_mtoon_forward_fragment.hlsl.meta} | 6 +- .../vrmc_materials_mtoon_forward_vertex.hlsl | 35 +++ ...c_materials_mtoon_forward_vertex.hlsl.meta | 3 + .../VRM10/vrmc_materials_mtoon_input.hlsl | 50 ++++ ...a => vrmc_materials_mtoon_input.hlsl.meta} | 6 +- .../VRM10/vrmc_materials_mtoon_lighting.hlsl | 13 + .../vrmc_materials_mtoon_lighting.hlsl.meta | 3 + .../VRM10/vrmc_materials_mtoon_sm3.hlsl | 22 -- .../vrmc_materials_mtoon_unity_lighting.hlsl | 41 +++ ...c_materials_mtoon_unity_lighting.hlsl.meta | 3 + .../VRM10/vrmc_materials_mtoon_uv.hlsl | 26 ++ .../VRM10/vrmc_materials_mtoon_uv.hlsl.meta | 3 + 46 files changed, 696 insertions(+), 378 deletions(-) create mode 100644 Assets/VRMShaders/VRM10/MToon10/Editor.meta create mode 100644 Assets/VRMShaders/VRM10/MToon10/Editor/MToonDefined.meta create mode 100644 Assets/VRMShaders/VRM10/MToon10/Editor/MToonDefined/AlphaMode.cs create mode 100644 Assets/VRMShaders/VRM10/MToon10/Editor/MToonDefined/AlphaMode.cs.meta create mode 100644 Assets/VRMShaders/VRM10/MToon10/Editor/MToonDefined/TransparentWithZWriteMode.cs create mode 100644 Assets/VRMShaders/VRM10/MToon10/Editor/MToonDefined/TransparentWithZWriteMode.cs.meta create mode 100644 Assets/VRMShaders/VRM10/MToon10/Editor/MToonInspector.cs create mode 100644 Assets/VRMShaders/VRM10/MToon10/Editor/MToonInspector.cs.meta create mode 100644 Assets/VRMShaders/VRM10/MToon10/Editor/MToonValidator.cs create mode 100644 Assets/VRMShaders/VRM10/MToon10/Editor/MToonValidator.cs.meta create mode 100644 Assets/VRMShaders/VRM10/MToon10/Editor/MaterialExtensions.cs create mode 100644 Assets/VRMShaders/VRM10/MToon10/Editor/MaterialExtensions.cs.meta create mode 100644 Assets/VRMShaders/VRM10/MToon10/Editor/Prop.cs create mode 100644 Assets/VRMShaders/VRM10/MToon10/Editor/Prop.cs.meta create mode 100644 Assets/VRMShaders/VRM10/MToon10/Editor/PropExtensions.cs create mode 100644 Assets/VRMShaders/VRM10/MToon10/Editor/PropExtensions.cs.meta create mode 100644 Assets/VRMShaders/VRM10/MToon10/Editor/UnityDefined.meta create mode 100644 Assets/VRMShaders/VRM10/MToon10/Editor/UnityDefined/UnityAlphaModeKeyword.cs create mode 100644 Assets/VRMShaders/VRM10/MToon10/Editor/UnityDefined/UnityAlphaModeKeyword.cs.meta create mode 100644 Assets/VRMShaders/VRM10/MToon10/Editor/UnityDefined/UnityAlphaToMaskMode.cs create mode 100644 Assets/VRMShaders/VRM10/MToon10/Editor/UnityDefined/UnityAlphaToMaskMode.cs.meta create mode 100644 Assets/VRMShaders/VRM10/MToon10/Editor/UnityDefined/UnityRenderTag.cs create mode 100644 Assets/VRMShaders/VRM10/MToon10/Editor/UnityDefined/UnityRenderTag.cs.meta create mode 100644 Assets/VRMShaders/VRM10/MToon10/Editor/UnityDefined/UnityZWriteMode.cs create mode 100644 Assets/VRMShaders/VRM10/MToon10/Editor/UnityDefined/UnityZWriteMode.cs.meta create mode 100644 Assets/VRMShaders/VRM10/MToon10/Editor/VRMShaders.VRM10.MToon10.Editor.asmdef create mode 100644 Assets/VRMShaders/VRM10/MToon10/Editor/VRMShaders.VRM10.MToon10.Editor.asmdef.meta create mode 100644 Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_attribute.hlsl create mode 100644 Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_attribute.hlsl.meta delete mode 100644 Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_core.hlsl create mode 100644 Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_define.hlsl create mode 100644 Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_define.hlsl.meta create mode 100644 Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_forward_fragment.hlsl rename Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/{vrmc_materials_mtoon_core.hlsl.meta => vrmc_materials_mtoon_forward_fragment.hlsl.meta} (56%) create mode 100644 Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_forward_vertex.hlsl create mode 100644 Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_forward_vertex.hlsl.meta create mode 100644 Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_input.hlsl rename Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/{vrmc_materials_mtoon_sm3.hlsl.meta => vrmc_materials_mtoon_input.hlsl.meta} (56%) create mode 100644 Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_lighting.hlsl create mode 100644 Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_lighting.hlsl.meta delete mode 100644 Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_sm3.hlsl create mode 100644 Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_unity_lighting.hlsl create mode 100644 Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_unity_lighting.hlsl.meta create mode 100644 Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_uv.hlsl create mode 100644 Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_uv.hlsl.meta diff --git a/Assets/VRMShaders/VRM10/MToon10/Editor.meta b/Assets/VRMShaders/VRM10/MToon10/Editor.meta new file mode 100644 index 000000000..9e936b2a7 --- /dev/null +++ b/Assets/VRMShaders/VRM10/MToon10/Editor.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: d4b26a27a0aab424c9022e525037a718 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/VRMShaders/VRM10/MToon10/Editor/MToonDefined.meta b/Assets/VRMShaders/VRM10/MToon10/Editor/MToonDefined.meta new file mode 100644 index 000000000..291a05829 --- /dev/null +++ b/Assets/VRMShaders/VRM10/MToon10/Editor/MToonDefined.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 3f051e7d892a4bccbcc8fd1f4c2ca162 +timeCreated: 1622456532 \ No newline at end of file diff --git a/Assets/VRMShaders/VRM10/MToon10/Editor/MToonDefined/AlphaMode.cs b/Assets/VRMShaders/VRM10/MToon10/Editor/MToonDefined/AlphaMode.cs new file mode 100644 index 000000000..9cec1a274 --- /dev/null +++ b/Assets/VRMShaders/VRM10/MToon10/Editor/MToonDefined/AlphaMode.cs @@ -0,0 +1,9 @@ +namespace VRMShaders.VRM10.MToon10.Editor +{ + public enum AlphaMode + { + Opaque = 0, + Cutout = 1, + Transparent = 2, + } +} \ No newline at end of file diff --git a/Assets/VRMShaders/VRM10/MToon10/Editor/MToonDefined/AlphaMode.cs.meta b/Assets/VRMShaders/VRM10/MToon10/Editor/MToonDefined/AlphaMode.cs.meta new file mode 100644 index 000000000..b5134f04a --- /dev/null +++ b/Assets/VRMShaders/VRM10/MToon10/Editor/MToonDefined/AlphaMode.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 4915ab0041b841b6aa0cdfecae764008 +timeCreated: 1622453782 \ No newline at end of file diff --git a/Assets/VRMShaders/VRM10/MToon10/Editor/MToonDefined/TransparentWithZWriteMode.cs b/Assets/VRMShaders/VRM10/MToon10/Editor/MToonDefined/TransparentWithZWriteMode.cs new file mode 100644 index 000000000..946e52bd4 --- /dev/null +++ b/Assets/VRMShaders/VRM10/MToon10/Editor/MToonDefined/TransparentWithZWriteMode.cs @@ -0,0 +1,8 @@ +namespace VRMShaders.VRM10.MToon10.Editor +{ + public enum TransparentWithZWriteMode + { + Off = 0, + On = 1, + } +} \ No newline at end of file diff --git a/Assets/VRMShaders/VRM10/MToon10/Editor/MToonDefined/TransparentWithZWriteMode.cs.meta b/Assets/VRMShaders/VRM10/MToon10/Editor/MToonDefined/TransparentWithZWriteMode.cs.meta new file mode 100644 index 000000000..2e9251da5 --- /dev/null +++ b/Assets/VRMShaders/VRM10/MToon10/Editor/MToonDefined/TransparentWithZWriteMode.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: d5df515a36d3460891b348872459476f +timeCreated: 1622456521 \ No newline at end of file diff --git a/Assets/VRMShaders/VRM10/MToon10/Editor/MToonInspector.cs b/Assets/VRMShaders/VRM10/MToon10/Editor/MToonInspector.cs new file mode 100644 index 000000000..4a59eead2 --- /dev/null +++ b/Assets/VRMShaders/VRM10/MToon10/Editor/MToonInspector.cs @@ -0,0 +1,64 @@ +using System; +using System.Collections; +using System.Collections.Generic; +using System.Linq; +using UnityEditor; +using UnityEngine; +using UnityEngine.Rendering; + +namespace VRMShaders.VRM10.MToon10.Editor +{ + public class MToonInspector : ShaderGUI + { + public override void OnGUI(MaterialEditor materialEditor, MaterialProperty[] properties) + { + var props = PropExtensions.PropertyNames + .ToDictionary(x => x.Key, x => FindProperty(x.Value, properties)); + var materials = materialEditor.targets.Select(x => x as Material).ToArray(); + + EditorGUILayout.LabelField("Rendering", EditorStyles.boldLabel); + EditorGUILayout.BeginVertical(GUI.skin.box); + { + EditorGUILayout.LabelField("Mode", EditorStyles.boldLabel); + if (PopupEnum("Alpha Mode", props[Prop.AlphaMode], materialEditor)) + { + Validate(materials); + } + + if (PopupEnum("Transparent With ZWrite Mode", props[Prop.TransparentWithZWrite], materialEditor)) + { + Validate(materials); + } + } + EditorGUILayout.EndVertical(); + EditorGUILayout.Space(); + + base.OnGUI(materialEditor, properties); + } + + private static void Validate(Material[] materials) + { + foreach (var material in materials) + { + new MToonValidator(material).Validate(); + } + } + + private static bool PopupEnum(string name, MaterialProperty property, MaterialEditor editor) where T : struct + { + EditorGUI.showMixedValue = property.hasMixedValue; + EditorGUI.BeginChangeCheck(); + var ret = EditorGUILayout.Popup(name, (int) property.floatValue, Enum.GetNames(typeof(T))); + var changed = EditorGUI.EndChangeCheck(); + if (changed) + { + editor.RegisterPropertyChangeUndo("EnumPopUp"); + property.floatValue = ret; + } + + EditorGUI.showMixedValue = false; + return changed; + } + + } +} \ No newline at end of file diff --git a/Assets/VRMShaders/VRM10/MToon10/Editor/MToonInspector.cs.meta b/Assets/VRMShaders/VRM10/MToon10/Editor/MToonInspector.cs.meta new file mode 100644 index 000000000..23defdaef --- /dev/null +++ b/Assets/VRMShaders/VRM10/MToon10/Editor/MToonInspector.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: b8b6f1e6a8a133a4987361f178d9f548 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/VRMShaders/VRM10/MToon10/Editor/MToonValidator.cs b/Assets/VRMShaders/VRM10/MToon10/Editor/MToonValidator.cs new file mode 100644 index 000000000..6528ae130 --- /dev/null +++ b/Assets/VRMShaders/VRM10/MToon10/Editor/MToonValidator.cs @@ -0,0 +1,80 @@ +using System; +using UnityEngine; +using UnityEngine.Rendering; + +namespace VRMShaders.VRM10.MToon10.Editor +{ + public sealed class MToonValidator + { + private readonly Material _material; + + public MToonValidator(Material material) + { + _material = material; + } + + public void Validate() + { + var alphaMode = (AlphaMode) _material.GetInt(Prop.AlphaMode); + var zWriteMode = (TransparentWithZWriteMode) _material.GetInt(Prop.TransparentWithZWrite); + SetUnityRenderSettings(_material, alphaMode, zWriteMode); + } + + private static void SetUnityRenderSettings(Material material, AlphaMode alphaMode, TransparentWithZWriteMode zWriteMode) + { + material.SetInt(Prop.AlphaMode, (int) alphaMode); + material.SetInt(Prop.TransparentWithZWrite, (int) zWriteMode); + + switch (alphaMode) + { + case AlphaMode.Opaque: + material.SetOverrideTag(UnityRenderTag.Key, UnityRenderTag.OpaqueValue); + material.SetInt(Prop.UnitySrcBlend, (int) BlendMode.One); + material.SetInt(Prop.UnityDstBlend, (int) BlendMode.Zero); + material.SetInt(Prop.UnityZWrite, (int) UnityZWriteMode.On); + material.SetInt(Prop.UnityAlphaToMask, (int) UnityAlphaToMaskMode.Off); + material.SetKeyword(UnityAlphaModeKeyword.AlphaTest, false); + material.SetKeyword(UnityAlphaModeKeyword.AlphaBlend, false); + material.SetKeyword(UnityAlphaModeKeyword.AlphaPremultiply, false); + material.renderQueue = (int) RenderQueue.Geometry; + break; + case AlphaMode.Cutout: + material.SetOverrideTag(UnityRenderTag.Key, UnityRenderTag.TransparentCutoutValue); + material.SetInt(Prop.UnitySrcBlend, (int) BlendMode.One); + material.SetInt(Prop.UnityDstBlend, (int) BlendMode.Zero); + material.SetInt(Prop.UnityZWrite, (int) UnityZWriteMode.On); + material.SetInt(Prop.UnityAlphaToMask, (int) UnityAlphaToMaskMode.On); + material.SetKeyword(UnityAlphaModeKeyword.AlphaTest, true); + material.SetKeyword(UnityAlphaModeKeyword.AlphaBlend, false); + material.SetKeyword(UnityAlphaModeKeyword.AlphaPremultiply, false); + material.renderQueue = (int) RenderQueue.AlphaTest; + break; + case AlphaMode.Transparent when zWriteMode == TransparentWithZWriteMode.Off: + material.SetOverrideTag(UnityRenderTag.Key, UnityRenderTag.TransparentValue); + material.SetInt(Prop.UnitySrcBlend, (int) BlendMode.SrcAlpha); + material.SetInt(Prop.UnityDstBlend, (int) BlendMode.OneMinusSrcAlpha); + material.SetInt(Prop.UnityZWrite, (int) UnityZWriteMode.Off); + material.SetInt(Prop.UnityAlphaToMask, (int) UnityAlphaToMaskMode.Off); + material.SetKeyword(UnityAlphaModeKeyword.AlphaTest, false); + material.SetKeyword(UnityAlphaModeKeyword.AlphaBlend, true); + material.SetKeyword(UnityAlphaModeKeyword.AlphaPremultiply, false); + material.renderQueue = (int) RenderQueue.Transparent; + break; + case AlphaMode.Transparent when zWriteMode == TransparentWithZWriteMode.On: + material.SetOverrideTag(UnityRenderTag.Key, UnityRenderTag.TransparentValue); + material.SetInt(Prop.UnitySrcBlend, (int) BlendMode.SrcAlpha); + material.SetInt(Prop.UnityDstBlend, (int) BlendMode.OneMinusSrcAlpha); + material.SetInt(Prop.UnityZWrite, (int) UnityZWriteMode.On); + material.SetInt(Prop.UnityAlphaToMask, (int) UnityAlphaToMaskMode.Off); + material.SetKeyword(UnityAlphaModeKeyword.AlphaTest, false); + material.SetKeyword(UnityAlphaModeKeyword.AlphaBlend, true); + material.SetKeyword(UnityAlphaModeKeyword.AlphaPremultiply, false); + material.renderQueue = (int) RenderQueue.GeometryLast + 1; // Transparent First + break; + default: + SetUnityRenderSettings(material, AlphaMode.Opaque, TransparentWithZWriteMode.Off); + break; + } + } + } +} \ No newline at end of file diff --git a/Assets/VRMShaders/VRM10/MToon10/Editor/MToonValidator.cs.meta b/Assets/VRMShaders/VRM10/MToon10/Editor/MToonValidator.cs.meta new file mode 100644 index 000000000..ee337bf55 --- /dev/null +++ b/Assets/VRMShaders/VRM10/MToon10/Editor/MToonValidator.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 18b52251a22d40ccbfc16c4a89d09c51 +timeCreated: 1622453963 \ No newline at end of file diff --git a/Assets/VRMShaders/VRM10/MToon10/Editor/MaterialExtensions.cs b/Assets/VRMShaders/VRM10/MToon10/Editor/MaterialExtensions.cs new file mode 100644 index 000000000..a9a5c3ba0 --- /dev/null +++ b/Assets/VRMShaders/VRM10/MToon10/Editor/MaterialExtensions.cs @@ -0,0 +1,29 @@ +using UnityEngine; + +namespace VRMShaders.VRM10.MToon10.Editor +{ + public static class MaterialExtensions + { + public static void SetKeyword(this Material mat, string keyword, bool isEnabled) + { + if (isEnabled) + { + mat.EnableKeyword(keyword); + } + else + { + mat.DisableKeyword(keyword); + } + } + + public static int GetInt(this Material mat, Prop prop) + { + return mat.GetInt(prop.ToName()); + } + + public static void SetInt(this Material mat, Prop prop, int val) + { + mat.SetInt(prop.ToName(), val); + } + } +} \ No newline at end of file diff --git a/Assets/VRMShaders/VRM10/MToon10/Editor/MaterialExtensions.cs.meta b/Assets/VRMShaders/VRM10/MToon10/Editor/MaterialExtensions.cs.meta new file mode 100644 index 000000000..5bb45496f --- /dev/null +++ b/Assets/VRMShaders/VRM10/MToon10/Editor/MaterialExtensions.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 173b50f65d2a44109cc67a84f18bde53 +timeCreated: 1622456072 \ No newline at end of file diff --git a/Assets/VRMShaders/VRM10/MToon10/Editor/Prop.cs b/Assets/VRMShaders/VRM10/MToon10/Editor/Prop.cs new file mode 100644 index 000000000..31b369a55 --- /dev/null +++ b/Assets/VRMShaders/VRM10/MToon10/Editor/Prop.cs @@ -0,0 +1,22 @@ +namespace VRMShaders.VRM10.MToon10.Editor +{ + public enum Prop + { + // Rendering + AlphaMode, + TransparentWithZWrite, + AlphaCutoff, + RenderQueueOffsetNumber, + DoubleSided, + + // Lighting + BaseColorFactor, + + // Unity Required + UnityCullMode, + UnitySrcBlend, + UnityDstBlend, + UnityZWrite, + UnityAlphaToMask, + } +} \ No newline at end of file diff --git a/Assets/VRMShaders/VRM10/MToon10/Editor/Prop.cs.meta b/Assets/VRMShaders/VRM10/MToon10/Editor/Prop.cs.meta new file mode 100644 index 000000000..ee3595a36 --- /dev/null +++ b/Assets/VRMShaders/VRM10/MToon10/Editor/Prop.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: aea6e3bb9944499e94c1026d60104432 +timeCreated: 1622453772 \ No newline at end of file diff --git a/Assets/VRMShaders/VRM10/MToon10/Editor/PropExtensions.cs b/Assets/VRMShaders/VRM10/MToon10/Editor/PropExtensions.cs new file mode 100644 index 000000000..795ae0e8a --- /dev/null +++ b/Assets/VRMShaders/VRM10/MToon10/Editor/PropExtensions.cs @@ -0,0 +1,29 @@ +using System.Collections.Generic; + +namespace VRMShaders.VRM10.MToon10.Editor +{ + public static class PropExtensions + { + private static readonly Dictionary _propertyNames = new Dictionary + { + [Prop.AlphaMode] = "_AlphaMode", + [Prop.TransparentWithZWrite] = "_TransparentWithZWrite", + [Prop.AlphaCutoff] = "_Cutoff", + [Prop.RenderQueueOffsetNumber] = "_RenderQueueOffset", + [Prop.DoubleSided] = "_DoubleSided", + [Prop.BaseColorFactor] = "_Color", + [Prop.UnityCullMode] = "_M_CullMode", + [Prop.UnitySrcBlend] = "_M_SrcBlend", + [Prop.UnityDstBlend] = "_M_DstBlend", + [Prop.UnityZWrite] = "_M_ZWrite", + [Prop.UnityAlphaToMask] = "_M_AlphaToMask", + }; + + public static IReadOnlyDictionary PropertyNames => _propertyNames; + + public static string ToName(this Prop prop) + { + return PropertyNames[prop]; + } + } +} \ No newline at end of file diff --git a/Assets/VRMShaders/VRM10/MToon10/Editor/PropExtensions.cs.meta b/Assets/VRMShaders/VRM10/MToon10/Editor/PropExtensions.cs.meta new file mode 100644 index 000000000..a398f2658 --- /dev/null +++ b/Assets/VRMShaders/VRM10/MToon10/Editor/PropExtensions.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 3c8828cd778c400f81b8a1c93b54ee50 +timeCreated: 1622454119 \ No newline at end of file diff --git a/Assets/VRMShaders/VRM10/MToon10/Editor/UnityDefined.meta b/Assets/VRMShaders/VRM10/MToon10/Editor/UnityDefined.meta new file mode 100644 index 000000000..cace89a6f --- /dev/null +++ b/Assets/VRMShaders/VRM10/MToon10/Editor/UnityDefined.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 5b210153ed9c4949ad794ca34f18e1b1 +timeCreated: 1622456318 \ No newline at end of file diff --git a/Assets/VRMShaders/VRM10/MToon10/Editor/UnityDefined/UnityAlphaModeKeyword.cs b/Assets/VRMShaders/VRM10/MToon10/Editor/UnityDefined/UnityAlphaModeKeyword.cs new file mode 100644 index 000000000..2dd590eb7 --- /dev/null +++ b/Assets/VRMShaders/VRM10/MToon10/Editor/UnityDefined/UnityAlphaModeKeyword.cs @@ -0,0 +1,9 @@ +namespace VRMShaders.VRM10.MToon10.Editor +{ + public static class UnityAlphaModeKeyword + { + public const string AlphaTest = "_ALPHATEST_ON"; + public const string AlphaBlend = "_ALPHABLEND_ON"; + public const string AlphaPremultiply = "_ALPHAPREMULTIPLY_ON"; + } +} \ No newline at end of file diff --git a/Assets/VRMShaders/VRM10/MToon10/Editor/UnityDefined/UnityAlphaModeKeyword.cs.meta b/Assets/VRMShaders/VRM10/MToon10/Editor/UnityDefined/UnityAlphaModeKeyword.cs.meta new file mode 100644 index 000000000..d2a8bd20e --- /dev/null +++ b/Assets/VRMShaders/VRM10/MToon10/Editor/UnityDefined/UnityAlphaModeKeyword.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 1e9a4286845a4c998a402e50da51ecc9 +timeCreated: 1622456299 \ No newline at end of file diff --git a/Assets/VRMShaders/VRM10/MToon10/Editor/UnityDefined/UnityAlphaToMaskMode.cs b/Assets/VRMShaders/VRM10/MToon10/Editor/UnityDefined/UnityAlphaToMaskMode.cs new file mode 100644 index 000000000..ab0e1f736 --- /dev/null +++ b/Assets/VRMShaders/VRM10/MToon10/Editor/UnityDefined/UnityAlphaToMaskMode.cs @@ -0,0 +1,8 @@ +namespace VRMShaders.VRM10.MToon10.Editor +{ + public enum UnityAlphaToMaskMode + { + Off = 0, + On = 1, + } +} \ No newline at end of file diff --git a/Assets/VRMShaders/VRM10/MToon10/Editor/UnityDefined/UnityAlphaToMaskMode.cs.meta b/Assets/VRMShaders/VRM10/MToon10/Editor/UnityDefined/UnityAlphaToMaskMode.cs.meta new file mode 100644 index 000000000..9c04631a6 --- /dev/null +++ b/Assets/VRMShaders/VRM10/MToon10/Editor/UnityDefined/UnityAlphaToMaskMode.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: d254dee2e3b3407abcdc3f6f84b3cd59 +timeCreated: 1622456292 \ No newline at end of file diff --git a/Assets/VRMShaders/VRM10/MToon10/Editor/UnityDefined/UnityRenderTag.cs b/Assets/VRMShaders/VRM10/MToon10/Editor/UnityDefined/UnityRenderTag.cs new file mode 100644 index 000000000..2a7a50136 --- /dev/null +++ b/Assets/VRMShaders/VRM10/MToon10/Editor/UnityDefined/UnityRenderTag.cs @@ -0,0 +1,10 @@ +namespace VRMShaders.VRM10.MToon10.Editor +{ + public static class UnityRenderTag + { + public const string Key = "RenderType"; + public const string OpaqueValue = "Opaque"; + public const string TransparentCutoutValue = "TransparentCutout"; + public const string TransparentValue = "Transparent"; + } +} \ No newline at end of file diff --git a/Assets/VRMShaders/VRM10/MToon10/Editor/UnityDefined/UnityRenderTag.cs.meta b/Assets/VRMShaders/VRM10/MToon10/Editor/UnityDefined/UnityRenderTag.cs.meta new file mode 100644 index 000000000..fdde5a13e --- /dev/null +++ b/Assets/VRMShaders/VRM10/MToon10/Editor/UnityDefined/UnityRenderTag.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: df547717467444c29ff3dcec1e555e17 +timeCreated: 1622456295 \ No newline at end of file diff --git a/Assets/VRMShaders/VRM10/MToon10/Editor/UnityDefined/UnityZWriteMode.cs b/Assets/VRMShaders/VRM10/MToon10/Editor/UnityDefined/UnityZWriteMode.cs new file mode 100644 index 000000000..4d7a85441 --- /dev/null +++ b/Assets/VRMShaders/VRM10/MToon10/Editor/UnityDefined/UnityZWriteMode.cs @@ -0,0 +1,8 @@ +namespace VRMShaders.VRM10.MToon10.Editor +{ + public enum UnityZWriteMode + { + Off = 0, + On = 1, + } +} \ No newline at end of file diff --git a/Assets/VRMShaders/VRM10/MToon10/Editor/UnityDefined/UnityZWriteMode.cs.meta b/Assets/VRMShaders/VRM10/MToon10/Editor/UnityDefined/UnityZWriteMode.cs.meta new file mode 100644 index 000000000..3e73786ad --- /dev/null +++ b/Assets/VRMShaders/VRM10/MToon10/Editor/UnityDefined/UnityZWriteMode.cs.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 4c80d709af9c46f9b57290659de4c1c6 +timeCreated: 1622456288 \ No newline at end of file diff --git a/Assets/VRMShaders/VRM10/MToon10/Editor/VRMShaders.VRM10.MToon10.Editor.asmdef b/Assets/VRMShaders/VRM10/MToon10/Editor/VRMShaders.VRM10.MToon10.Editor.asmdef new file mode 100644 index 000000000..e85abaf72 --- /dev/null +++ b/Assets/VRMShaders/VRM10/MToon10/Editor/VRMShaders.VRM10.MToon10.Editor.asmdef @@ -0,0 +1,15 @@ +{ + "name": "VRMShaders.VRM10.MToon10.Editor", + "references": [], + "includePlatforms": [ + "Editor" + ], + "excludePlatforms": [], + "allowUnsafeCode": false, + "overrideReferences": false, + "precompiledReferences": [], + "autoReferenced": true, + "defineConstraints": [], + "versionDefines": [], + "noEngineReferences": false +} \ No newline at end of file diff --git a/Assets/VRMShaders/VRM10/MToon10/Editor/VRMShaders.VRM10.MToon10.Editor.asmdef.meta b/Assets/VRMShaders/VRM10/MToon10/Editor/VRMShaders.VRM10.MToon10.Editor.asmdef.meta new file mode 100644 index 000000000..b50a6f771 --- /dev/null +++ b/Assets/VRMShaders/VRM10/MToon10/Editor/VRMShaders.VRM10.MToon10.Editor.asmdef.meta @@ -0,0 +1,7 @@ +fileFormatVersion: 2 +guid: e3216dcc79b326b47b307c06b99c7331 +AssemblyDefinitionImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon.shader b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon.shader index e0d4071ce..80fb7ee05 100644 --- a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon.shader +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon.shader @@ -1,4 +1,4 @@ -Shader "VRM10/vrmc_materials_mtoon" +Shader "Hidden/VRM10/vrmc_materials_mtoon" { Properties { @@ -10,7 +10,7 @@ Shader "VRM10/vrmc_materials_mtoon" _DoubleSided ("doubleSided", Int) = 0 // Lighting - _Color ("pbrMetallicRoughness.baseColorFactor", Color) = (1,1,1,1) // Unity specified name + _Color ("pbrMetallicRoughness.baseColorFactor", Color) = (1, 1, 1, 1) // Unity specified name _MainTex ("pbrMetallicRoughness.baseColorTexture", 2D) = "white" {} // Unity specified name _ShadeColor ("mtoon.shadeColorFactor", Color) = (1, 1, 1, 1) _ShadeTex ("mtoon.shadeMultiplyTexture", 2D) = "white" {} @@ -27,12 +27,12 @@ Shader "VRM10/vrmc_materials_mtoon" _GiEqualization ("mtoon.giEqualizationFactor", Range(0, 1)) = 0.9 // Emission - _EmissionColor ("emissiveFactor", Color) = (0, 0, 0) // Unity specified name + _EmissionColor ("emissiveFactor", Color) = (0, 0, 0, 1) // Unity specified name _EmissionMap ("emissiveTexture", 2D) = "white" {} // Unity specified name // Rim Lighting _RimMatcapTex ("mtoon.matcapTexture", 2D) = "black" {} - _RimColor ("mtoon.parametricRimColorFactor", Color) = (0, 0, 0) + _RimColor ("mtoon.parametricRimColorFactor", Color) = (0, 0, 0, 1) _RimFresnelPower ("mtoon.parametricRimFresnelPowerFactor", Float) = 5.0 _RimLift ("mtoon.parametricRimLiftFactor", Float) = 0 _RimTex ("mtoon.rimMultiplyTexture", 2D) = "white" {} @@ -42,8 +42,8 @@ Shader "VRM10/vrmc_materials_mtoon" _OutlineWidthMode ("mtoon.outlineWidthMode", Int) = 0 _OutlineWidth ("mtoon.outlineWidthFactor", Float) = 0 _OutlineWidthTex ("mtoon.outlineWidthMultiplyTexture", 2D) = "white" {} // channel G - _OutlineColor ("mtoon.outlineColorFactor", Color) = (0, 0, 0) - _OutlineLightingMix ("mtoon.outlineLightingMixFactor", Float) = 1 // default 0 + _OutlineColor ("mtoon.outlineColorFactor", Color) = (0, 0, 0, 1) + _OutlineLightingMix ("mtoon.outlineLightingMixFactor", Float) = 1 // UV Animation _UvAnimMaskTex ("mtoon.uvAnimationMaskTexture", 2D) = "white" {} // channel B @@ -52,14 +52,14 @@ Shader "VRM10/vrmc_materials_mtoon" _UvAnimRotationSpeed ("mtoon.uvAnimationRotationSpeedFactor", Float) = 0 // Unity ShaderPass Mode - [HideInInspector] _M_CullMode ("_CullMode", Float) = 2.0 - [HideInInspector] _M_SrcBlend ("_SrcBlend", Float) = 1.0 - [HideInInspector] _M_DstBlend ("_DstBlend", Float) = 0.0 - [HideInInspector] _M_ZWrite ("_ZWrite", Float) = 1.0 - [HideInInspector] _M_AlphaToMask ("_AlphaToMask", Float) = 0.0 + _M_CullMode ("_CullMode", Float) = 2.0 + _M_SrcBlend ("_SrcBlend", Float) = 1.0 + _M_DstBlend ("_DstBlend", Float) = 0.0 + _M_ZWrite ("_ZWrite", Float) = 1.0 + _M_AlphaToMask ("_AlphaToMask", Float) = 0.0 // etc - [HideInInspector] _M_DebugMode ("_DebugMode", Float) = 0.0 + _M_DebugMode ("_DebugMode", Float) = 0.0 } SubShader @@ -77,55 +77,28 @@ Shader "VRM10/vrmc_materials_mtoon" ZWrite [_M_ZWrite] ZTest LEqual BlendOp Add, Max - AlphaToMask [_M_AlphaToMask] + AlphaToMask Off - CGPROGRAM + HLSLPROGRAM #pragma target 3.0 - #pragma shader_feature _ MTOON_DEBUG_NORMAL MTOON_DEBUG_LITSHADERATE - #pragma multi_compile _ _NORMALMAP - #pragma multi_compile _ _ALPHATEST_ON _ALPHABLEND_ON - #include "./vrmc_materials_mtoon_sm3.hlsl" - #pragma vertex vert_forward_base - #pragma fragment frag_forward + + // Unity defined keywords #pragma multi_compile_fwdbase #pragma multi_compile_fog -// #pragma multi_compile_instancing - ENDCG + #pragma multi_compile_instancing + + #pragma multi_compile_local __ _ALPHATEST_ON _ALPHABLEND_ON + #pragma multi_compile_local __ _NORMALMAP + #pragma multi_compile_local __ _UVANIMATION + + #pragma vertex MToonVertex + #pragma fragment MToonFragment + + #include "./vrmc_materials_mtoon_forward_vertex.hlsl" + #include "./vrmc_materials_mtoon_forward_fragment.hlsl" + ENDHLSL } - - // Built-in Forward Base Outline Pass - Pass - { - Name "FORWARD_BASE_ONLY_OUTLINE" - Tags { "LightMode" = "ForwardBase" } - - Cull Front - Blend [_M_SrcBlend] [_M_DstBlend] - ZWrite [_M_ZWrite] - ZTest LEqual - Offset 1, 1 - BlendOp Add, Max - AlphaToMask [_M_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 - #include "./vrmc_materials_mtoon_sm3.hlsl" - #pragma vertex vert_forward_base_outline - #pragma fragment frag_forward - #pragma multi_compile_fwdbase - #pragma multi_compile_fog -// #pragma multi_compile_instancing - ENDCG - } - - // Built-in Forward Add Pass Pass { @@ -139,21 +112,27 @@ Shader "VRM10/vrmc_materials_mtoon" BlendOp Add, Max AlphaToMask [_M_AlphaToMask] - CGPROGRAM + HLSLPROGRAM #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 "./vrmc_materials_mtoon_sm3.hlsl" - #pragma vertex vert_forward_add - #pragma fragment frag_forward + + // Unity defined keywords #pragma multi_compile_fwdadd_fullshadows #pragma multi_compile_fog - ENDCG + #pragma multi_compile_instancing + + #pragma multi_compile_local __ _ALPHATEST_ON _ALPHABLEND_ON + #pragma multi_compile_local __ _NORMALMAP + #pragma multi_compile_local __ _UVANIMATION + + #pragma vertex MToonVertex + #pragma fragment MToonFragment + + #include "./vrmc_materials_mtoon_forward_vertex.hlsl" + #include "./vrmc_materials_mtoon_forward_fragment.hlsl" + ENDHLSL } - // Built-in Shadow Rendering Pass + // Shadow rendering pass Pass { Name "ShadowCaster" @@ -165,15 +144,23 @@ Shader "VRM10/vrmc_materials_mtoon" CGPROGRAM #pragma target 3.0 - #pragma multi_compile _ _ALPHATEST_ON _ALPHABLEND_ON + + // Unity defined keywords #pragma multi_compile_shadowcaster + + #pragma multi_compile_local __ _ALPHATEST_ON _ALPHABLEND_ON + + // Use unity standard shadow implementation. + // internal usage: + // keywords: _ALPHATEST_ON _ALPHABLEND_ON + // variables: _MainTex.a _Color.a _Cutoff #pragma vertex vertShadowCaster #pragma fragment fragShadowCaster + #include "UnityStandardShadow.cginc" ENDCG } } - Fallback "Unlit/Texture" - CustomEditor "MToon.MToonInspector" + CustomEditor "VRMShaders.VRM10.MToon10.Editor.MToonInspector" } 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 new file mode 100644 index 000000000..903500a5e --- /dev/null +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_attribute.hlsl @@ -0,0 +1,34 @@ +#ifndef VRMC_MATERIALS_MTOON_ATTRIBUTE_INCLUDED +#define VRMC_MATERIALS_MTOON_ATTRIBUTE_INCLUDED + +#include +#include + +struct Attributes +{ + float4 vertex : POSITION; // UnityCG macro specified name. Accurately "positionOS" + float3 normalOS : NORMAL; +#if defined(_NORMALMAP) + float4 tangentOS : TANGENT; +#endif + float2 texcoord0 : TEXCOORD0; + float2 texcoord1 : TEXCOORD1; + UNITY_VERTEX_INPUT_INSTANCE_ID +}; + +struct Varyings +{ + float2 uv : TEXCOORD0; + float3 positionWS : TEXCOORD1; + half3 normalWS : TEXCOORD2; +#if defined(_NORMALMAP) + half4 tangentWS : TEXCOORD3; +#endif + UNITY_FOG_COORDS(4) + UNITY_LIGHTING_COORDS(5,6) + float4 pos : SV_POSITION; // UnityCG macro specified name. Accurately "positionCS" + UNITY_VERTEX_INPUT_INSTANCE_ID + UNITY_VERTEX_OUTPUT_STEREO +}; + +#endif diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_attribute.hlsl.meta b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_attribute.hlsl.meta new file mode 100644 index 000000000..cf1996460 --- /dev/null +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_attribute.hlsl.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 82d864eead0e48f79295fe490a6d614f +timeCreated: 1622450640 \ No newline at end of file diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_core.hlsl b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_core.hlsl deleted file mode 100644 index 430e0831b..000000000 --- a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_core.hlsl +++ /dev/null @@ -1,282 +0,0 @@ -#ifndef MTOON_CORE_INCLUDED -#define MTOON_CORE_INCLUDED - -#include "Lighting.cginc" -#include "AutoLight.cginc" - -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 _UvAnimMaskTexture; -float _UvAnimScrollX; -float _UvAnimScrollY; -float _UvAnimRotation; - -//UNITY_INSTANCING_BUFFER_START(Props) -//UNITY_INSTANCING_BUFFER_END(Props) - -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_INPUT_INSTANCE_ID // necessary only if any instanced properties are going to be accessed in the fragment Shader. - 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); - //UNITY_TRANSFER_INSTANCE_ID(v, 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)); // ignore offset when normal toward camera - #else - float4 vertex = UnityObjectToClipPos(v.vertex); - #endif - return vertex; -} - -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_TRANSFER_INSTANCE_ID(v, o); - UNITY_SETUP_STEREO_EYE_INDEX_POST_VERTEX(i); - - // const - const float PI_2 = 6.28318530718; - const float EPS_COL = 0.00001; - - // uv - float2 mainUv = TRANSFORM_TEX(i.uv0, _MainTex); - - // uv anim - float uvAnim = tex2D(_UvAnimMaskTexture, mainUv).r * _Time.y; - // translate uv in bottom-left origin coordinates. - mainUv += float2(_UvAnimScrollX, _UvAnimScrollY) * uvAnim; - // rotate uv counter-clockwise around (0.5, 0.5) in bottom-left origin coordinates. - 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; - - // main tex - half4 mainTex = tex2D(_MainTex, mainUv); - - // alpha - half alpha = 1; -#ifdef _ALPHATEST_ON - alpha = _Color.a * mainTex.a; - alpha = (alpha - _Cutoff) / max(fwidth(alpha), EPS_COL) + 0.5; // Alpha to Coverage - clip(alpha - _Cutoff); - alpha = 1.0; // Discarded, otherwise it should be assumed to have full opacity -#endif -#ifdef _ALPHABLEND_ON - alpha = _Color.a * mainTex.a; -#if !_ALPHATEST_ON && SHADER_API_D3D11 // Only enable this on D3D11, where I tested it - clip(alpha - 0.0001); // Slightly improves rendering with layered transparency -#endif -#endif - - // normal -#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; // flip if projection matrix is flipped - worldNormal *= lerp(+1.0, -1.0, i.isOutline); - worldNormal = normalize(worldNormal); - - // Unity lighting - 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)); // length(lightDir) is zero if directional light is disabled. - 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 - - // Decide albedo color rate from Direct Light - half shadingGrade = 1.0 - _ShadingGradeRate * (1.0 - tex2D(_ShadingGradeTexture, mainUv).r); - half lightIntensity = dotNL; // [-1, +1] - lightIntensity = lightIntensity * 0.5 + 0.5; // from [-1, +1] to [0, 1] - lightIntensity = lightIntensity * lightAttenuation; // receive shadow - lightIntensity = lightIntensity * shadingGrade; // darker - lightIntensity = lightIntensity * 2.0 - 1.0; // from [0, 1] to [-1, +1] - // tooned. mapping from [minIntensityThreshold, maxIntensityThreshold] to [0, 1] - half maxIntensityThreshold = lerp(1, _ShadeShift, _ShadeToony); - half minIntensityThreshold = _ShadeShift; - lightIntensity = saturate((lightIntensity - minIntensityThreshold) / max(EPS_COL, (maxIntensityThreshold - minIntensityThreshold))); - - // Albedo color - half4 shade = _ShadeColor * tex2D(_ShadeTexture, mainUv); - half4 lit = _Color * mainTex; - half3 col = lerp(shade.rgb, lit.rgb, lightIntensity); - - // Direct Light - half3 lighting = lightColor; - lighting = lerp(lighting, max(EPS_COL, max(lighting.x, max(lighting.y, lighting.z))), _LightColorAttenuation); // color atten -#ifdef MTOON_FORWARD_ADD -#ifdef _ALPHABLEND_ON - lighting *= step(0, dotNL); // darken if transparent. Because Unity's transparent material can't receive shadowAttenuation. -#endif - lighting *= 0.5; // darken if additional light. - lighting *= min(0, dotNL) + 1; // darken dotNL < 0 area by using half lambert - lighting *= shadowAttenuation; // darken if receiving shadow -#else - // base light does not darken. -#endif - col *= lighting; - - // Indirect Light -#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); // color atten - col += indirectLighting * lit; - - col = min(col, lit); // comment out if you want to PBR absolutely. -#endif - - // parametric rim lighting -#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), _RimFresnelPower) * _RimColor.rgb * tex2D(_RimTexture, mainUv).rgb; - col += lerp(rim * rimLighting, half3(0, 0, 0), i.isOutline); - - // additive matcap -#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 - - // Emission -#ifdef MTOON_FORWARD_ADD -#else - half3 emission = tex2D(_EmissionMap, mainUv).rgb * _EmissionColor.rgb; - col += lerp(emission, half3(0, 0, 0), i.isOutline); -#endif - - // outline -#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 - - // debug -#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); - return result; -} - -#endif // MTOON_CORE_INCLUDED diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_define.hlsl b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_define.hlsl new file mode 100644 index 000000000..46d0b2bf3 --- /dev/null +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_define.hlsl @@ -0,0 +1,8 @@ +#ifndef VRMC_MATERIALS_MTOON_DEFINE_INCLUDED +#define VRMC_MATERIALS_MTOON_DEFINE_INCLUDED + +// define +static const float PI_2 = 6.28318530718; +static const float EPS_COL = 0.00001; + +#endif diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_define.hlsl.meta b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_define.hlsl.meta new file mode 100644 index 000000000..95942fc3f --- /dev/null +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_define.hlsl.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: cfd10dec96ea4e3f9fad70e2e5dc148e +timeCreated: 1622451960 \ No newline at end of file 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 new file mode 100644 index 000000000..974047274 --- /dev/null +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_forward_fragment.hlsl @@ -0,0 +1,57 @@ +#ifndef VRMC_MATERIALS_MTOON_FORWARD_FRAGMENT_INCLUDED +#define VRMC_MATERIALS_MTOON_FORWARD_FRAGMENT_INCLUDED + +#include +#include + +#include "./vrmc_materials_mtoon_define.hlsl" +#include "./vrmc_materials_mtoon_input.hlsl" +#include "./vrmc_materials_mtoon_attribute.hlsl" +#include "./vrmc_materials_mtoon_lighting.hlsl" +#include "./vrmc_materials_mtoon_uv.hlsl" +#include "./vrmc_materials_mtoon_unity_lighting.hlsl" + +half4 MToonFragment(Varyings input) : SV_Target +{ + // Get MToon UV (with UVAnimation) + const float2 uv = GetMToonUv(input.uv); + + // Get LitColor with Alpha + const half4 litColor = UNITY_SAMPLE_TEX2D(_MainTex, uv) * _Color; + + // Alpha Test +#if defined(_ALPHATEST_ON) + const half rawAlpha = _Color.a; + const half tmpAlpha = (rawAlpha - _Cutoff) / max(fwidth(rawAlpha), 0.00001) + 0.5; // Alpha to Coverage + clip(tmpAlpha - _Cutoff); + const half alpha = 1.0; +#elif defined(_ALPHABLEND_ON) + const half alpha = litColor.a; + clip(alpha - EPS_COL); +#else + const half alpha = 1.0; +#endif + + // Get Normal in WorldSpace from Normalmap if available +#if defined(_NORMALMAP) + const half3 bitangent = input.tangentWS.w * cross(normalize(input.normalWS), normalize(input.tangentWS.xyz)); + const half3x3 tangentToWorld = half3x3(normalize(input.tangentWS.xyz), normalize(bitangent), normalize(input.normalWS)); + + const half3 normalTS = normalize(UnpackNormalWithScale(UNITY_SAMPLE_TEX2D(_BumpMap, uv), _BumpScale)); + const half3 normalWS = normalize(mul(normalTS, tangentToWorld)); +#else + const half3 normalWS = normalize(input.normalWS); +#endif + + // Get Unity Lighting + const UnityLighting unityLighting = GetUnityLighting(input, normalWS); + + // Get MToon Lighting + const half4 col = GetMToonLighting(unityLighting.directLight, unityLighting.indirectLight, uv, litColor, alpha); + + UNITY_APPLY_FOG(i.fogCoord, col); + + return col; +} + +#endif diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_core.hlsl.meta b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_forward_fragment.hlsl.meta similarity index 56% rename from Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_core.hlsl.meta rename to Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_forward_fragment.hlsl.meta index 6f4d4a593..4ca74a095 100644 --- a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_core.hlsl.meta +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_forward_fragment.hlsl.meta @@ -1,9 +1,9 @@ fileFormatVersion: 2 -guid: 22be54a117660824da79e2b52a710d59 -timeCreated: 1514120022 -licenseType: Free +guid: 3717321cda361ad48abfc6b4fcb7a320 ShaderImporter: + externalObjects: {} defaultTextures: [] + nonModifiableTextures: [] userData: assetBundleName: assetBundleVariant: 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 new file mode 100644 index 000000000..a28cb05ca --- /dev/null +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_forward_vertex.hlsl @@ -0,0 +1,35 @@ +#ifndef VRMC_MATERIALS_MTOON_FORWARD_VERTEX_INCLUDED +#define VRMC_MATERIALS_MTOON_FORWARD_VERTEX_INCLUDED + +#include +#include + +#include "./vrmc_materials_mtoon_define.hlsl" +#include "./vrmc_materials_mtoon_input.hlsl" +#include "./vrmc_materials_mtoon_attribute.hlsl" + +Varyings MToonVertex(Attributes v) // v is UnityCG macro specified name. +{ + Varyings output = (Varyings)0; + + UNITY_SETUP_INSTANCE_ID(v); + UNITY_TRANSFER_INSTANCE_ID(v, output); + UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output); + + output.pos = UnityObjectToClipPos(v.vertex); + output.positionWS = mul(unity_ObjectToWorld, v.vertex); + output.uv = TRANSFORM_TEX(v.texcoord0, _MainTex); + + output.normalWS = UnityObjectToWorldNormal(v.normalOS); +#if defined(_NORMALMAP) + const half tangentSign = v.tangentOS.w * unity_WorldTransformParams.w; + output.tangentWS = half4(UnityObjectToWorldDir(v.tangentOS), tangentSign); +#endif + + UNITY_TRANSFER_FOG(output, output.positionWS); + UNITY_TRANSFER_LIGHTING(output, v.texcoord1.xy); + + return output; +} + +#endif diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_forward_vertex.hlsl.meta b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_forward_vertex.hlsl.meta new file mode 100644 index 000000000..7fc3a33ce --- /dev/null +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_forward_vertex.hlsl.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 26b921255f4e4d62a8c0ec730521831f +timeCreated: 1622458970 \ No newline at end of file diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_input.hlsl b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_input.hlsl new file mode 100644 index 000000000..8519868c0 --- /dev/null +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_input.hlsl @@ -0,0 +1,50 @@ +#ifndef VRMC_MATERIALS_MTOON_INPUT_INCLUDED +#define VRMC_MATERIALS_MTOON_INPUT_INCLUDED + +#include + +// Textures +UNITY_DECLARE_TEX2D(_MainTex); +UNITY_DECLARE_TEX2D(_ShadeTex); +UNITY_DECLARE_TEX2D(_BumpMap); +UNITY_DECLARE_TEX2D(_ShadingShiftTex); +UNITY_DECLARE_TEX2D(_EmissionMap); +UNITY_DECLARE_TEX2D(_RimMatcapTex); +UNITY_DECLARE_TEX2D(_RimTex); +UNITY_DECLARE_TEX2D(_OutlineWidthTex); +UNITY_DECLARE_TEX2D(_UvAnimMaskTex); + +CBUFFER_START(UnityPerMaterial) +// Vector +float4 _MainTex_ST; +// Colors +half4 _Color; +half4 _ShadeColor; +half4 _ShadingShiftColor; +half4 _ShadingToonyColor; +half4 _EmissionColor; +half4 _RimColor; +half4 _OutlineColor; +// Floats +half _Cutoff; +half _BumpScale; +half _ShadingShiftTexScale; +half _GiEqualization; +half _RimFresnelPower; +half _RimLift; +half _RimLightingMix; +half _OutlineWidth; +half _OutlineLightingMix; +half _UvAnimScrollXSpeed; +half _UvAnimScrollYSpeed; +half _UvAnimRotationSpeed; +CBUFFER_END + +// No Using on shader +// half _AlphaMode; +// half _TransparentWithZWrite; +// half _RenderQueueOffset; +// half _DoubleSided; +// half _OutlineWidthMode; + +#endif diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_sm3.hlsl.meta b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_input.hlsl.meta similarity index 56% rename from Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_sm3.hlsl.meta rename to Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_input.hlsl.meta index 910f77863..7098c392a 100644 --- a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_sm3.hlsl.meta +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_input.hlsl.meta @@ -1,9 +1,9 @@ fileFormatVersion: 2 -guid: 5aae6ba3f344c0e43914d19a64704aba -timeCreated: 1514120022 -licenseType: Free +guid: c7f37d6d268db6f4da25955d0ca7cb01 ShaderImporter: + externalObjects: {} defaultTextures: [] + nonModifiableTextures: [] userData: assetBundleName: assetBundleVariant: diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_lighting.hlsl b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_lighting.hlsl new file mode 100644 index 000000000..6e54b3008 --- /dev/null +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_lighting.hlsl @@ -0,0 +1,13 @@ +#ifndef VRMC_MATERIALS_MTOON_LIGHTING_INCLUDED +#define VRMC_MATERIALS_MTOON_LIGHTING_INCLUDED + +#include "./vrmc_materials_mtoon_input.hlsl" + +half4 GetMToonLighting(half3 directLight, half3 indirectLight, float2 uv, half4 litColor, half alpha) +{ + const half4 albedo = litColor; + + return half4(albedo.rgb * (directLight + indirectLight), alpha); +} + +#endif diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_lighting.hlsl.meta b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_lighting.hlsl.meta new file mode 100644 index 000000000..24e340e7e --- /dev/null +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_lighting.hlsl.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 54d13d2ffd7249259772084874b76659 +timeCreated: 1622449184 \ No newline at end of file diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_sm3.hlsl b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_sm3.hlsl deleted file mode 100644 index d50fd0d5e..000000000 --- a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_sm3.hlsl +++ /dev/null @@ -1,22 +0,0 @@ -#include "./vrmc_materials_mtoon_core.hlsl" - -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); -} diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_unity_lighting.hlsl b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_unity_lighting.hlsl new file mode 100644 index 000000000..3cf6e9afa --- /dev/null +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_unity_lighting.hlsl @@ -0,0 +1,41 @@ +#ifndef VRMC_MATERIALS_MTOON_UNITY_LIGHTING_INCLUDED +#define VRMC_MATERIALS_MTOON_UNITY_LIGHTING_INCLUDED + +#include +#include +#include +#include "./vrmc_materials_mtoon_input.hlsl" +#include "./vrmc_materials_mtoon_attribute.hlsl" + +struct UnityLighting +{ + half3 directLight; + half3 indirectLight; +}; + +UnityLighting GetUnityLighting(Varyings input, half3 normalWS) +{ + UNITY_LIGHT_ATTENUATION(atten, input, input.positionWS); + + const half3 lightDir = normalize(UnityWorldSpaceLightDir(input.positionWS)); + const half3 lightColor = _LightColor0.rgb; + const half dotNL = dot(lightDir, normalWS); + +#if defined(UNITY_PASS_FORWARDBASE) + const half3 indirect = ShadeSH9(half4(normalWS, 1)); + const half3 direct = lightColor * max(0, dotNL) * atten; +#elif defined(UNITY_PASS_FORWARDADD) + const half3 indirect = 0; + const half3 direct = lightColor * max(0, dotNL) * atten; +#else + const half3 indirect = half3(1, 1, 0); // error + const half3 direct = 0; +#endif + + UnityLighting output = (UnityLighting) 0; + output.directLight = direct; + output.indirectLight = indirect; + return output; +} + +#endif diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_unity_lighting.hlsl.meta b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_unity_lighting.hlsl.meta new file mode 100644 index 000000000..7314301c4 --- /dev/null +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_unity_lighting.hlsl.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 08491980bb224017bbb8e432761db8c4 +timeCreated: 1622450586 \ No newline at end of file diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_uv.hlsl b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_uv.hlsl new file mode 100644 index 000000000..546ef7faf --- /dev/null +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_uv.hlsl @@ -0,0 +1,26 @@ +#ifndef VRMC_MATERIALS_MTOON_UV_INCLUDED +#define VRMC_MATERIALS_MTOON_UV_INCLUDED + +#include +#include "./vrmc_materials_mtoon_define.hlsl" +#include "./vrmc_materials_mtoon_input.hlsl" + +float2 GetMToonUv(float2 geometryUv) +{ + // get raw uv with _MainTex_ST + const float2 uvRaw = TRANSFORM_TEX(geometryUv, _MainTex); + +#if defined(_UVANIMATION) + const float uvAnimationCoef = UNITY_SAMPLE_TEX2D(_UvAnimMaskTex, uvRaw).b * _Time.y; + const float2 uvAnimationAdd = uvAnimationCoef * float2(_UvAnimScrollXSpeed, _UvAnimScrollYSpeed); + const float rotateRad = uvAnimationCoef * _UvAnimRotationSpeed * PI_2; + const float2 rotatePivot = float2(0.5, 0.5); + const float2 uv = mul(float2x2(cos(rotateRad), -sin(rotateRad), sin(rotateRad), cos(rotateRad)), uvRaw + uvAnimationAdd - rotatePivot) + rotatePivot; +#else + const float2 uv = uvRaw; +#endif + + return uv; +} + +#endif diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_uv.hlsl.meta b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_uv.hlsl.meta new file mode 100644 index 000000000..454192c22 --- /dev/null +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_uv.hlsl.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 1381f11f525a4d90a57348ada020ea01 +timeCreated: 1622451732 \ No newline at end of file From d25eb746df5fe3170ee67739836afdd4e2f7fc37 Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Mon, 31 May 2021 20:21:17 +0900 Subject: [PATCH 04/17] Fix alpha source --- .../Resources/VRM10/vrmc_materials_mtoon_forward_fragment.hlsl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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 974047274..af43b1340 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 @@ -21,7 +21,7 @@ half4 MToonFragment(Varyings input) : SV_Target // Alpha Test #if defined(_ALPHATEST_ON) - const half rawAlpha = _Color.a; + const half rawAlpha = litColor.a; const half tmpAlpha = (rawAlpha - _Cutoff) / max(fwidth(rawAlpha), 0.00001) + 0.5; // Alpha to Coverage clip(tmpAlpha - _Cutoff); const half alpha = 1.0; From 485c43efe6617fae666dfc754afdc4f89aa1ee09 Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Tue, 1 Jun 2021 19:21:28 +0900 Subject: [PATCH 05/17] implements lighting & emission. --- .../VRM10/vrmc_materials_mtoon.shader | 4 +- .../VRM10/vrmc_materials_mtoon_define.hlsl | 5 ++ ...vrmc_materials_mtoon_forward_fragment.hlsl | 7 ++- .../VRM10/vrmc_materials_mtoon_input.hlsl | 4 +- .../VRM10/vrmc_materials_mtoon_lighting.hlsl | 49 +++++++++++++++++-- .../vrmc_materials_mtoon_unity_lighting.hlsl | 34 +++++++------ 6 files changed, 80 insertions(+), 23 deletions(-) diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon.shader b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon.shader index 80fb7ee05..b4c78f4ec 100644 --- a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon.shader +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon.shader @@ -16,10 +16,10 @@ Shader "Hidden/VRM10/vrmc_materials_mtoon" _ShadeTex ("mtoon.shadeMultiplyTexture", 2D) = "white" {} [Normal] _BumpMap ("normalTexture", 2D) = "bump" {} // Unity specified name _BumpScale ("normalTexture.scale", Float) = 1.0 // Unity specified name - _ShadingShiftColor ("mtoon.shadingShiftFactor", Range(0, 1)) = 0 + _ShadingShiftFactor ("mtoon.shadingShiftFactor", Range(-1, 1)) = 0 _ShadingShiftTex ("mtoon.shadingShiftTexture", 2D) = "black" {} // channel R _ShadingShiftTexScale ("mtoon.shadingShiftTexture.scale", Float) = 1 - _ShadingToonyColor ("mtoon.shadingToonyFactor", Range(0, 1)) = 0.9 + _ShadingToonyFactor ("mtoon.shadingToonyFactor", Range(0, 1)) = 0.9 // _ShadingToonyTex ("mtoon.shadingToonyTexture", 2D) = "black" {} // parameter texture // need? // _ShadingToonyTexScale ("mtoon.shadingToonyTexture.scale", Float) = 1 // need? diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_define.hlsl b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_define.hlsl index 46d0b2bf3..5067bfea9 100644 --- a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_define.hlsl +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_define.hlsl @@ -5,4 +5,9 @@ static const float PI_2 = 6.28318530718; static const float EPS_COL = 0.00001; +inline half3 mtoon_linearstep(half3 start, half3 end, half t) +{ + return min(max((t - start) / (end - start), 0.0), 1.0); +} + #endif 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 af43b1340..48fd5fd07 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,7 +47,12 @@ half4 MToonFragment(Varyings input) : SV_Target const UnityLighting unityLighting = GetUnityLighting(input, normalWS); // Get MToon Lighting - const half4 col = GetMToonLighting(unityLighting.directLight, unityLighting.indirectLight, uv, litColor, alpha); + MToonLightingInput lightingInput; + lightingInput.normalWS = normalWS; + lightingInput.uv = uv; + lightingInput.litColor = litColor.rgb; + lightingInput.alpha = alpha; + const half4 col = GetMToonLighting(unityLighting, lightingInput); UNITY_APPLY_FOG(i.fogCoord, col); diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_input.hlsl b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_input.hlsl index 8519868c0..5870fd6f6 100644 --- a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_input.hlsl +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_input.hlsl @@ -20,15 +20,15 @@ float4 _MainTex_ST; // Colors half4 _Color; half4 _ShadeColor; -half4 _ShadingShiftColor; -half4 _ShadingToonyColor; half4 _EmissionColor; half4 _RimColor; half4 _OutlineColor; // Floats half _Cutoff; half _BumpScale; +half _ShadingShiftFactor; half _ShadingShiftTexScale; +half _ShadingToonyFactor; half _GiEqualization; half _RimFresnelPower; half _RimLift; diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_lighting.hlsl b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_lighting.hlsl index 6e54b3008..982d7018f 100644 --- a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_lighting.hlsl +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_lighting.hlsl @@ -1,13 +1,56 @@ #ifndef VRMC_MATERIALS_MTOON_LIGHTING_INCLUDED #define VRMC_MATERIALS_MTOON_LIGHTING_INCLUDED +#include "./vrmc_materials_mtoon_define.hlsl" #include "./vrmc_materials_mtoon_input.hlsl" +#include "./vrmc_materials_mtoon_unity_lighting.hlsl" -half4 GetMToonLighting(half3 directLight, half3 indirectLight, float2 uv, half4 litColor, half alpha) +struct MToonLightingInput { - const half4 albedo = litColor; + float2 uv; + half3 normalWS; + half3 litColor; + half alpha; +}; - return half4(albedo.rgb * (directLight + indirectLight), alpha); +half4 GetMToonLighting(UnityLighting lighting, MToonLightingInput input) +{ + const half dotNL = dot(input.normalWS, lighting.directLightDirection); + const half shadingInput = lerp(-1, 1, mtoon_linearstep(-1, 1, dotNL) * lighting.directLightAttenuation); + const half shadingShift = UNITY_SAMPLE_TEX2D(_ShadingShiftTex, input.uv).r * _ShadingShiftTexScale + _ShadingShiftFactor; + const half shadingToony = _ShadingToonyFactor; + const half shading = mtoon_linearstep(-1.0 + shadingToony, +1.0 - shadingToony, shadingInput + shadingShift); + + // lighting +#if defined(UNITY_PASS_FORWARDBASE) + const half3 shadeColor = UNITY_SAMPLE_TEX2D(_ShadeTex, input.uv).rgb * _ShadeColor.rgb; + + const half3 direct = lerp(shadeColor, input.litColor, shading) * lighting.directLightColor; + const half3 indirect = input.litColor * lerp(lighting.indirectLight, lighting.indirectLightEqualized, _GiEqualization); + +#elif defined(UNITY_PASS_FORWARDADD) + const half3 direct = input.litColor * shading * lighting.directLightColor * lerp(1, 0.5, shadingToony); + const half3 indirect = 0; + +#else + const half3 direct = 0; // unexpected + const half3 indirect = 0; + +#endif + + // emission +#if defined(UNITY_PASS_FORWARDBASE) + const half3 emission = UNITY_SAMPLE_TEX2D(_EmissionMap, input.uv).rgb * _EmissionColor.rgb; +#elif defined(UNITY_PASS_FORWARDADD) + const half3 emission = 0; +#else + const half3 emission = 0; +#endif + + + const half3 col = direct + indirect + emission; + + return half4(col, input.alpha); } #endif diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_unity_lighting.hlsl b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_unity_lighting.hlsl index 3cf6e9afa..363dbef2b 100644 --- a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_unity_lighting.hlsl +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_unity_lighting.hlsl @@ -9,8 +9,11 @@ struct UnityLighting { - half3 directLight; half3 indirectLight; + half3 indirectLightEqualized; + half3 directLightColor; + half3 directLightDirection; + half3 directLightAttenuation; }; UnityLighting GetUnityLighting(Varyings input, half3 normalWS) @@ -19,22 +22,23 @@ UnityLighting GetUnityLighting(Varyings input, half3 normalWS) const half3 lightDir = normalize(UnityWorldSpaceLightDir(input.positionWS)); const half3 lightColor = _LightColor0.rgb; - const half dotNL = dot(lightDir, normalWS); - -#if defined(UNITY_PASS_FORWARDBASE) - const half3 indirect = ShadeSH9(half4(normalWS, 1)); - const half3 direct = lightColor * max(0, dotNL) * atten; -#elif defined(UNITY_PASS_FORWARDADD) - const half3 indirect = 0; - const half3 direct = lightColor * max(0, dotNL) * atten; -#else - const half3 indirect = half3(1, 1, 0); // error - const half3 direct = 0; -#endif UnityLighting output = (UnityLighting) 0; - output.directLight = direct; - output.indirectLight = indirect; + +#if defined(UNITY_PASS_FORWARDBASE) + output.indirectLight = ShadeSH9(half4(normalWS, 1)); + output.indirectLightEqualized = (ShadeSH9(half4(0, 1, 0, 1)) + ShadeSH9(half4(0, -1, 0, 1))) * 0.5; + output.directLightColor = lightColor; + output.directLightDirection = lightDir; + output.directLightAttenuation = atten; +#elif defined(UNITY_PASS_FORWARDADD) + output.indirectLight = 0; + output.indirectLightEqualized = 0; + output.directLightColor = lightColor; + output.directLightDirection = lightDir; + output.directLightAttenuation = atten; +#endif + return output; } From 0182e974bf215ea4fe018a009030e3ad134cde26 Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Wed, 2 Jun 2021 15:58:05 +0900 Subject: [PATCH 06/17] follow mtoon0.x --- .../Resources/VRM10/vrmc_materials_mtoon_lighting.hlsl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_lighting.hlsl b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_lighting.hlsl index 982d7018f..69ed4bb1d 100644 --- a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_lighting.hlsl +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_lighting.hlsl @@ -29,7 +29,7 @@ half4 GetMToonLighting(UnityLighting lighting, MToonLightingInput input) const half3 indirect = input.litColor * lerp(lighting.indirectLight, lighting.indirectLightEqualized, _GiEqualization); #elif defined(UNITY_PASS_FORWARDADD) - const half3 direct = input.litColor * shading * lighting.directLightColor * lerp(1, 0.5, shadingToony); + const half3 direct = input.litColor * shading * lighting.directLightColor * 0.5; const half3 indirect = 0; #else @@ -44,7 +44,7 @@ half4 GetMToonLighting(UnityLighting lighting, MToonLightingInput input) #elif defined(UNITY_PASS_FORWARDADD) const half3 emission = 0; #else - const half3 emission = 0; + const half3 emission = 0; // unexpected #endif From f857935bbd755dcdef8ffabc358207eb5c3cc58e Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Wed, 2 Jun 2021 19:31:54 +0900 Subject: [PATCH 07/17] Separate functions --- .../VRM10/vrmc_materials_mtoon.shader | 2 +- .../VRM10/vrmc_materials_mtoon_attribute.hlsl | 5 +- .../VRM10/vrmc_materials_mtoon_define.hlsl | 39 +++++++++ ...vrmc_materials_mtoon_forward_fragment.hlsl | 10 +-- .../vrmc_materials_mtoon_forward_vertex.hlsl | 2 + .../VRM10/vrmc_materials_mtoon_input.hlsl | 2 +- .../VRM10/vrmc_materials_mtoon_lighting.hlsl | 81 +++++++++++++------ 7 files changed, 107 insertions(+), 34 deletions(-) diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon.shader b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon.shader index b4c78f4ec..4384fbe73 100644 --- a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon.shader +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon.shader @@ -31,7 +31,7 @@ Shader "Hidden/VRM10/vrmc_materials_mtoon" _EmissionMap ("emissiveTexture", 2D) = "white" {} // Unity specified name // Rim Lighting - _RimMatcapTex ("mtoon.matcapTexture", 2D) = "black" {} + _MatcapTex ("mtoon.matcapTexture", 2D) = "black" {} _RimColor ("mtoon.parametricRimColorFactor", Color) = (0, 0, 0, 1) _RimFresnelPower ("mtoon.parametricRimFresnelPowerFactor", Float) = 5.0 _RimLift ("mtoon.parametricRimLiftFactor", Float) = 0 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 903500a5e..f1aa1e87b 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 @@ -24,8 +24,9 @@ struct Varyings #if defined(_NORMALMAP) half4 tangentWS : TEXCOORD3; #endif - UNITY_FOG_COORDS(4) - UNITY_LIGHTING_COORDS(5,6) + float3 viewDirWS : TEXCOORD4; + UNITY_FOG_COORDS(5) + UNITY_LIGHTING_COORDS(6,7) float4 pos : SV_POSITION; // UnityCG macro specified name. Accurately "positionCS" UNITY_VERTEX_INPUT_INSTANCE_ID UNITY_VERTEX_OUTPUT_STEREO diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_define.hlsl b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_define.hlsl index 5067bfea9..04845a980 100644 --- a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_define.hlsl +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_define.hlsl @@ -10,4 +10,43 @@ inline half3 mtoon_linearstep(half3 start, half3 end, half t) return min(max((t - start) / (end - start), 0.0), 1.0); } +inline bool MToon_IsPerspective() +{ + return unity_OrthoParams.w != 1.0; +} + +inline float3 MToon_GetWorldSpaceNormalizedViewDir(float3 positionWS) +{ + if (MToon_IsPerspective()) + { + return normalize(_WorldSpaceCameraPos.xyz - positionWS); + } + else + { + return normalize(UNITY_MATRIX_V[2].xyz); + } +} + +inline half3x3 MToon_GetTangentToWorld(half3 normalWS, half4 tangentWS) +{ + const half3 normalizedNormalWS = normalize(normalWS); + const half3 normalizedTangentWS = normalize(tangentWS.xyz); + + const half3 normalizedBitangentWS = normalize(tangentWS.w * cross(normalizedNormalWS, normalizedTangentWS)); + + return half3x3(normalizedTangentWS, normalizedBitangentWS, normalizedNormalWS); +} + +inline bool MToon_IsForwardBasePass() +{ +#if defined(UNITY_PASS_FORWARDBASE) + return true; +#elif defined(UNITY_PASS_FORWARDADD) + return false; +#else + // ???? + return false; +#endif +} + #endif 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 48fd5fd07..8bedbb8ed 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 @@ -34,11 +34,8 @@ half4 MToonFragment(Varyings input) : SV_Target // Get Normal in WorldSpace from Normalmap if available #if defined(_NORMALMAP) - const half3 bitangent = input.tangentWS.w * cross(normalize(input.normalWS), normalize(input.tangentWS.xyz)); - const half3x3 tangentToWorld = half3x3(normalize(input.tangentWS.xyz), normalize(bitangent), normalize(input.normalWS)); - const half3 normalTS = normalize(UnpackNormalWithScale(UNITY_SAMPLE_TEX2D(_BumpMap, uv), _BumpScale)); - const half3 normalWS = normalize(mul(normalTS, tangentToWorld)); + const half3 normalWS = normalize(mul(normalTS, MToon_GetTangentToWorld(input.normalWS, input.tangentWS))); #else const half3 normalWS = normalize(input.normalWS); #endif @@ -47,9 +44,10 @@ half4 MToonFragment(Varyings input) : SV_Target const UnityLighting unityLighting = GetUnityLighting(input, normalWS); // Get MToon Lighting - MToonLightingInput lightingInput; - lightingInput.normalWS = normalWS; + MToonInput lightingInput; lightingInput.uv = uv; + lightingInput.normalWS = normalWS; + lightingInput.viewDirWS = input.viewDirWS; lightingInput.litColor = litColor.rgb; lightingInput.alpha = alpha; const half4 col = GetMToonLighting(unityLighting, lightingInput); 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 a28cb05ca..d4603c81b 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 @@ -26,6 +26,8 @@ Varyings MToonVertex(Attributes v) // v is UnityCG macro specified name. output.tangentWS = half4(UnityObjectToWorldDir(v.tangentOS), tangentSign); #endif + output.viewDirWS = MToon_GetWorldSpaceNormalizedViewDir(output.positionWS); + UNITY_TRANSFER_FOG(output, output.positionWS); UNITY_TRANSFER_LIGHTING(output, v.texcoord1.xy); diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_input.hlsl b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_input.hlsl index 5870fd6f6..fd738a6cc 100644 --- a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_input.hlsl +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_input.hlsl @@ -9,7 +9,7 @@ UNITY_DECLARE_TEX2D(_ShadeTex); UNITY_DECLARE_TEX2D(_BumpMap); UNITY_DECLARE_TEX2D(_ShadingShiftTex); UNITY_DECLARE_TEX2D(_EmissionMap); -UNITY_DECLARE_TEX2D(_RimMatcapTex); +UNITY_DECLARE_TEX2D(_MatcapTex); UNITY_DECLARE_TEX2D(_RimTex); UNITY_DECLARE_TEX2D(_OutlineWidthTex); UNITY_DECLARE_TEX2D(_UvAnimMaskTex); diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_lighting.hlsl b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_lighting.hlsl index 69ed4bb1d..904151711 100644 --- a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_lighting.hlsl +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_lighting.hlsl @@ -5,50 +5,83 @@ #include "./vrmc_materials_mtoon_input.hlsl" #include "./vrmc_materials_mtoon_unity_lighting.hlsl" -struct MToonLightingInput +struct MToonInput { float2 uv; half3 normalWS; + half3 viewDirWS; half3 litColor; half alpha; }; -half4 GetMToonLighting(UnityLighting lighting, MToonLightingInput input) +inline half GetMToonLighting_Reflectance(UnityLighting lighting, MToonInput input) { const half dotNL = dot(input.normalWS, lighting.directLightDirection); const half shadingInput = lerp(-1, 1, mtoon_linearstep(-1, 1, dotNL) * lighting.directLightAttenuation); const half shadingShift = UNITY_SAMPLE_TEX2D(_ShadingShiftTex, input.uv).r * _ShadingShiftTexScale + _ShadingShiftFactor; const half shadingToony = _ShadingToonyFactor; - const half shading = mtoon_linearstep(-1.0 + shadingToony, +1.0 - shadingToony, shadingInput + shadingShift); + return mtoon_linearstep(-1.0 + shadingToony, +1.0 - shadingToony, shadingInput + shadingShift); +} - // lighting -#if defined(UNITY_PASS_FORWARDBASE) - const half3 shadeColor = UNITY_SAMPLE_TEX2D(_ShadeTex, input.uv).rgb * _ShadeColor.rgb; +inline half3 GetMToonLighting_BasicLighting(UnityLighting unityLight, MToonInput input, half reflectance) +{ + if (MToon_IsForwardBasePass()) + { + const half3 shadeColor = UNITY_SAMPLE_TEX2D(_ShadeTex, input.uv).rgb * _ShadeColor.rgb; - const half3 direct = lerp(shadeColor, input.litColor, shading) * lighting.directLightColor; - const half3 indirect = input.litColor * lerp(lighting.indirectLight, lighting.indirectLightEqualized, _GiEqualization); + const half3 direct = lerp(shadeColor, input.litColor, reflectance) * unityLight.directLightColor; + const half3 indirect = input.litColor * lerp(unityLight.indirectLight, unityLight.indirectLightEqualized, _GiEqualization); -#elif defined(UNITY_PASS_FORWARDADD) - const half3 direct = input.litColor * shading * lighting.directLightColor * 0.5; - const half3 indirect = 0; + return direct + indirect; + } + else + { + const half3 direct = input.litColor * reflectance * unityLight.directLightColor * 0.5; + const half3 indirect = 0; -#else - const half3 direct = 0; // unexpected - const half3 indirect = 0; + return direct + indirect; + } +} -#endif +inline half3 GetMToonLighting_Emissive(MToonInput input) +{ + if (MToon_IsForwardBasePass()) + { + return UNITY_SAMPLE_TEX2D(_EmissionMap, input.uv).rgb * _EmissionColor.rgb; + } + else + { + return 0; + } +} - // emission -#if defined(UNITY_PASS_FORWARDBASE) - const half3 emission = UNITY_SAMPLE_TEX2D(_EmissionMap, input.uv).rgb * _EmissionColor.rgb; -#elif defined(UNITY_PASS_FORWARDADD) - const half3 emission = 0; -#else - const half3 emission = 0; // unexpected -#endif +inline half3 GetMToonLighting_Rim(MToonInput input, half3 lighting) +{ + if (MToon_IsForwardBasePass()) + { + const half3 worldUpWS = half3(0, 1, 0); + const half3 matcapRightWS = cross(input.viewDirWS, worldUpWS); + const half2 matcapUv = float2(dot(matcapRightWS, input.normalWS), dot(worldUpWS, input.normalWS)) * 0.5 + 0.5; + const half3 matcapFactor = UNITY_SAMPLE_TEX2D(_MatcapTex, matcapUv).rgb; + 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); + return (matcapFactor + parametricRimFactor) * UNITY_SAMPLE_TEX2D(_RimTex, input.uv).rgb * rimLightingFactor; + } + else + { + return 0; + } +} +half4 GetMToonLighting(UnityLighting unityLight, MToonInput input) +{ + const half reflectance = GetMToonLighting_Reflectance(unityLight, input); - const half3 col = direct + indirect + emission; + const half3 lighting = GetMToonLighting_BasicLighting(unityLight, input, reflectance); + const half3 emissive = GetMToonLighting_Emissive(input); + const half3 rim = GetMToonLighting_Rim(input, lighting); + + const half3 col = lighting + emissive + rim; return half4(col, input.alpha); } From e6d5b4d653deeb2889777d8a5ec13c07f41c9769 Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Wed, 2 Jun 2021 20:07:39 +0900 Subject: [PATCH 08/17] make function arguments constant. --- .../VRM10/vrmc_materials_mtoon_define.hlsl | 19 +++++++-- ...vrmc_materials_mtoon_forward_fragment.hlsl | 16 ++++---- .../vrmc_materials_mtoon_forward_vertex.hlsl | 2 +- .../VRM10/vrmc_materials_mtoon_lighting.hlsl | 10 ++--- .../vrmc_materials_mtoon_unity_lighting.hlsl | 39 ++++++++++--------- .../VRM10/vrmc_materials_mtoon_uv.hlsl | 25 ++++++------ 6 files changed, 63 insertions(+), 48 deletions(-) diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_define.hlsl b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_define.hlsl index 04845a980..56fa66bfd 100644 --- a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_define.hlsl +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_define.hlsl @@ -5,7 +5,7 @@ static const float PI_2 = 6.28318530718; static const float EPS_COL = 0.00001; -inline half3 mtoon_linearstep(half3 start, half3 end, half t) +inline half3 mtoon_linearstep(const half3 start, const half3 end, const half t) { return min(max((t - start) / (end - start), 0.0), 1.0); } @@ -15,7 +15,7 @@ inline bool MToon_IsPerspective() return unity_OrthoParams.w != 1.0; } -inline float3 MToon_GetWorldSpaceNormalizedViewDir(float3 positionWS) +inline float3 MToon_GetWorldSpaceNormalizedViewDir(const float3 positionWS) { if (MToon_IsPerspective()) { @@ -27,7 +27,7 @@ inline float3 MToon_GetWorldSpaceNormalizedViewDir(float3 positionWS) } } -inline half3x3 MToon_GetTangentToWorld(half3 normalWS, half4 tangentWS) +inline half3x3 MToon_GetTangentToWorld(const half3 normalWS, const half4 tangentWS) { const half3 normalizedNormalWS = normalize(normalWS); const half3 normalizedTangentWS = normalize(tangentWS.xyz); @@ -37,6 +37,7 @@ inline half3x3 MToon_GetTangentToWorld(half3 normalWS, half4 tangentWS) return half3x3(normalizedTangentWS, normalizedBitangentWS, normalizedNormalWS); } +// Compile-time constant inline bool MToon_IsForwardBasePass() { #if defined(UNITY_PASS_FORWARDBASE) @@ -44,7 +45,17 @@ inline bool MToon_IsForwardBasePass() #elif defined(UNITY_PASS_FORWARDADD) return false; #else - // ???? + // unexpected + return false; +#endif +} + +// Compile-time constant +inline bool MToon_IsUvAnimationOn() +{ +#if defined(_UVANIMATION) + return true; +#else return false; #endif } 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 8bedbb8ed..57b3a88bb 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 @@ -11,7 +11,7 @@ #include "./vrmc_materials_mtoon_uv.hlsl" #include "./vrmc_materials_mtoon_unity_lighting.hlsl" -half4 MToonFragment(Varyings input) : SV_Target +half4 MToonFragment(const Varyings input) : SV_Target { // Get MToon UV (with UVAnimation) const float2 uv = GetMToonUv(input.uv); @@ -44,13 +44,13 @@ half4 MToonFragment(Varyings input) : SV_Target const UnityLighting unityLighting = GetUnityLighting(input, normalWS); // Get MToon Lighting - MToonInput lightingInput; - lightingInput.uv = uv; - lightingInput.normalWS = normalWS; - lightingInput.viewDirWS = input.viewDirWS; - lightingInput.litColor = litColor.rgb; - lightingInput.alpha = alpha; - const half4 col = GetMToonLighting(unityLighting, lightingInput); + MToonInput mtoonInput; + mtoonInput.uv = uv; + mtoonInput.normalWS = normalWS; + mtoonInput.viewDirWS = input.viewDirWS; + mtoonInput.litColor = litColor.rgb; + mtoonInput.alpha = alpha; + 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 d4603c81b..2d5c650da 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 @@ -8,7 +8,7 @@ #include "./vrmc_materials_mtoon_input.hlsl" #include "./vrmc_materials_mtoon_attribute.hlsl" -Varyings MToonVertex(Attributes v) // v is UnityCG macro specified name. +Varyings MToonVertex(const Attributes v) // v is UnityCG macro specified name. { Varyings output = (Varyings)0; diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_lighting.hlsl b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_lighting.hlsl index 904151711..a637bf2bd 100644 --- a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_lighting.hlsl +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_lighting.hlsl @@ -14,7 +14,7 @@ struct MToonInput half alpha; }; -inline half GetMToonLighting_Reflectance(UnityLighting lighting, MToonInput input) +inline half GetMToonLighting_Reflectance(const UnityLighting lighting, const MToonInput input) { const half dotNL = dot(input.normalWS, lighting.directLightDirection); const half shadingInput = lerp(-1, 1, mtoon_linearstep(-1, 1, dotNL) * lighting.directLightAttenuation); @@ -23,7 +23,7 @@ inline half GetMToonLighting_Reflectance(UnityLighting lighting, MToonInput inpu return mtoon_linearstep(-1.0 + shadingToony, +1.0 - shadingToony, shadingInput + shadingShift); } -inline half3 GetMToonLighting_BasicLighting(UnityLighting unityLight, MToonInput input, half reflectance) +inline half3 GetMToonLighting_BasicLighting(const UnityLighting unityLight, const MToonInput input, const half reflectance) { if (MToon_IsForwardBasePass()) { @@ -43,7 +43,7 @@ inline half3 GetMToonLighting_BasicLighting(UnityLighting unityLight, MToonInput } } -inline half3 GetMToonLighting_Emissive(MToonInput input) +inline half3 GetMToonLighting_Emissive(const MToonInput input) { if (MToon_IsForwardBasePass()) { @@ -55,7 +55,7 @@ inline half3 GetMToonLighting_Emissive(MToonInput input) } } -inline half3 GetMToonLighting_Rim(MToonInput input, half3 lighting) +inline half3 GetMToonLighting_Rim(const MToonInput input, const half3 lighting) { if (MToon_IsForwardBasePass()) { @@ -73,7 +73,7 @@ inline half3 GetMToonLighting_Rim(MToonInput input, half3 lighting) } } -half4 GetMToonLighting(UnityLighting unityLight, MToonInput input) +half4 GetMToonLighting(const UnityLighting unityLight, const MToonInput input) { const half reflectance = GetMToonLighting_Reflectance(unityLight, input); diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_unity_lighting.hlsl b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_unity_lighting.hlsl index 363dbef2b..e1764d6af 100644 --- a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_unity_lighting.hlsl +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_unity_lighting.hlsl @@ -16,30 +16,33 @@ struct UnityLighting half3 directLightAttenuation; }; -UnityLighting GetUnityLighting(Varyings input, half3 normalWS) +UnityLighting GetUnityLighting(const Varyings input, const half3 normalWS) { UNITY_LIGHT_ATTENUATION(atten, input, input.positionWS); const half3 lightDir = normalize(UnityWorldSpaceLightDir(input.positionWS)); const half3 lightColor = _LightColor0.rgb; - UnityLighting output = (UnityLighting) 0; - -#if defined(UNITY_PASS_FORWARDBASE) - output.indirectLight = ShadeSH9(half4(normalWS, 1)); - output.indirectLightEqualized = (ShadeSH9(half4(0, 1, 0, 1)) + ShadeSH9(half4(0, -1, 0, 1))) * 0.5; - output.directLightColor = lightColor; - output.directLightDirection = lightDir; - output.directLightAttenuation = atten; -#elif defined(UNITY_PASS_FORWARDADD) - output.indirectLight = 0; - output.indirectLightEqualized = 0; - output.directLightColor = lightColor; - output.directLightDirection = lightDir; - output.directLightAttenuation = atten; -#endif - - return output; + if (MToon_IsForwardBasePass()) + { + UnityLighting output; + output.indirectLight = ShadeSH9(half4(normalWS, 1)); + output.indirectLightEqualized = (ShadeSH9(half4(0, 1, 0, 1)) + ShadeSH9(half4(0, -1, 0, 1))) * 0.5; + output.directLightColor = lightColor; + output.directLightDirection = lightDir; + output.directLightAttenuation = atten; + return output; + } + else + { + UnityLighting output; + output.indirectLight = 0; + output.indirectLightEqualized = 0; + output.directLightColor = lightColor; + output.directLightDirection = lightDir; + output.directLightAttenuation = atten; + return output; + } } #endif diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_uv.hlsl b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_uv.hlsl index 546ef7faf..6c92d4139 100644 --- a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_uv.hlsl +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_uv.hlsl @@ -5,22 +5,23 @@ #include "./vrmc_materials_mtoon_define.hlsl" #include "./vrmc_materials_mtoon_input.hlsl" -float2 GetMToonUv(float2 geometryUv) +float2 GetMToonUv(const float2 geometryUv) { // get raw uv with _MainTex_ST const float2 uvRaw = TRANSFORM_TEX(geometryUv, _MainTex); -#if defined(_UVANIMATION) - const float uvAnimationCoef = UNITY_SAMPLE_TEX2D(_UvAnimMaskTex, uvRaw).b * _Time.y; - const float2 uvAnimationAdd = uvAnimationCoef * float2(_UvAnimScrollXSpeed, _UvAnimScrollYSpeed); - const float rotateRad = uvAnimationCoef * _UvAnimRotationSpeed * PI_2; - const float2 rotatePivot = float2(0.5, 0.5); - const float2 uv = mul(float2x2(cos(rotateRad), -sin(rotateRad), sin(rotateRad), cos(rotateRad)), uvRaw + uvAnimationAdd - rotatePivot) + rotatePivot; -#else - const float2 uv = uvRaw; -#endif - - return uv; + if (MToon_IsUvAnimationOn()) + { + const float uvAnimationCoef = UNITY_SAMPLE_TEX2D(_UvAnimMaskTex, uvRaw).b * _Time.y; + const float2 uvAnimationAdd = uvAnimationCoef * float2(_UvAnimScrollXSpeed, _UvAnimScrollYSpeed); + const float rotateRad = uvAnimationCoef * _UvAnimRotationSpeed * PI_2; + const float2 rotatePivot = float2(0.5, 0.5); + return mul(float2x2(cos(rotateRad), -sin(rotateRad), sin(rotateRad), cos(rotateRad)), uvRaw + uvAnimationAdd - rotatePivot) + rotatePivot; + } + else + { + return uvRaw; + } } #endif From 2b8295d5d46e6798020a0af042f9817d426cd28e Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Wed, 2 Jun 2021 20:25:07 +0900 Subject: [PATCH 09/17] Separate compile-time define --- .../VRM10/vrmc_materials_mtoon_define.hlsl | 36 ---------------- ...vrmc_materials_mtoon_forward_fragment.hlsl | 3 +- .../vrmc_materials_mtoon_forward_vertex.hlsl | 3 +- .../VRM10/vrmc_materials_mtoon_lighting.hlsl | 2 + .../vrmc_materials_mtoon_unity_lighting.hlsl | 1 + .../VRM10/vrmc_materials_mtoon_utility.hlsl | 42 +++++++++++++++++++ .../vrmc_materials_mtoon_utility.hlsl.meta | 3 ++ 7 files changed, 50 insertions(+), 40 deletions(-) create mode 100644 Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_utility.hlsl create mode 100644 Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_utility.hlsl.meta diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_define.hlsl b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_define.hlsl index 56fa66bfd..e7efedfdd 100644 --- a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_define.hlsl +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_define.hlsl @@ -1,42 +1,6 @@ #ifndef VRMC_MATERIALS_MTOON_DEFINE_INCLUDED #define VRMC_MATERIALS_MTOON_DEFINE_INCLUDED -// define -static const float PI_2 = 6.28318530718; -static const float EPS_COL = 0.00001; - -inline half3 mtoon_linearstep(const half3 start, const half3 end, const half t) -{ - return min(max((t - start) / (end - start), 0.0), 1.0); -} - -inline bool MToon_IsPerspective() -{ - return unity_OrthoParams.w != 1.0; -} - -inline float3 MToon_GetWorldSpaceNormalizedViewDir(const float3 positionWS) -{ - if (MToon_IsPerspective()) - { - return normalize(_WorldSpaceCameraPos.xyz - positionWS); - } - else - { - return normalize(UNITY_MATRIX_V[2].xyz); - } -} - -inline half3x3 MToon_GetTangentToWorld(const half3 normalWS, const half4 tangentWS) -{ - const half3 normalizedNormalWS = normalize(normalWS); - const half3 normalizedTangentWS = normalize(tangentWS.xyz); - - const half3 normalizedBitangentWS = normalize(tangentWS.w * cross(normalizedNormalWS, normalizedTangentWS)); - - return half3x3(normalizedTangentWS, normalizedBitangentWS, normalizedNormalWS); -} - // Compile-time constant inline bool MToon_IsForwardBasePass() { 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 57b3a88bb..df0ab2460 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 @@ -2,9 +2,8 @@ #define VRMC_MATERIALS_MTOON_FORWARD_FRAGMENT_INCLUDED #include -#include - #include "./vrmc_materials_mtoon_define.hlsl" +#include "./vrmc_materials_mtoon_utility.hlsl" #include "./vrmc_materials_mtoon_input.hlsl" #include "./vrmc_materials_mtoon_attribute.hlsl" #include "./vrmc_materials_mtoon_lighting.hlsl" 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 2d5c650da..15b597141 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 @@ -3,8 +3,7 @@ #include #include - -#include "./vrmc_materials_mtoon_define.hlsl" +#include "./vrmc_materials_mtoon_utility.hlsl" #include "./vrmc_materials_mtoon_input.hlsl" #include "./vrmc_materials_mtoon_attribute.hlsl" diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_lighting.hlsl b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_lighting.hlsl index a637bf2bd..d2ce3212d 100644 --- a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_lighting.hlsl +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_lighting.hlsl @@ -1,7 +1,9 @@ #ifndef VRMC_MATERIALS_MTOON_LIGHTING_INCLUDED #define VRMC_MATERIALS_MTOON_LIGHTING_INCLUDED +#include #include "./vrmc_materials_mtoon_define.hlsl" +#include "./vrmc_materials_mtoon_utility.hlsl" #include "./vrmc_materials_mtoon_input.hlsl" #include "./vrmc_materials_mtoon_unity_lighting.hlsl" diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_unity_lighting.hlsl b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_unity_lighting.hlsl index e1764d6af..4e3efe509 100644 --- a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_unity_lighting.hlsl +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_unity_lighting.hlsl @@ -4,6 +4,7 @@ #include #include #include +#include "./vrmc_materials_mtoon_utility.hlsl" #include "./vrmc_materials_mtoon_input.hlsl" #include "./vrmc_materials_mtoon_attribute.hlsl" diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_utility.hlsl b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_utility.hlsl new file mode 100644 index 000000000..98b994edc --- /dev/null +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_utility.hlsl @@ -0,0 +1,42 @@ +#ifndef VRMC_MATERIALS_MTOON_UTILITY_INCLUDED +#define VRMC_MATERIALS_MTOON_UTILITY_INCLUDED + +#include + +// define +static const float PI_2 = 6.28318530718; +static const float EPS_COL = 0.00001; + +inline half3 mtoon_linearstep(const half3 start, const half3 end, const half t) +{ + return min(max((t - start) / (end - start), 0.0), 1.0); +} + +inline bool MToon_IsPerspective() +{ + return unity_OrthoParams.w != 1.0; +} + +inline float3 MToon_GetWorldSpaceNormalizedViewDir(const float3 positionWS) +{ + if (MToon_IsPerspective()) + { + return normalize(_WorldSpaceCameraPos.xyz - positionWS); + } + else + { + return normalize(UNITY_MATRIX_V[2].xyz); + } +} + +inline half3x3 MToon_GetTangentToWorld(const half3 normalWS, const half4 tangentWS) +{ + const half3 normalizedNormalWS = normalize(normalWS); + const half3 normalizedTangentWS = normalize(tangentWS.xyz); + + const half3 normalizedBitangentWS = normalize(tangentWS.w * cross(normalizedNormalWS, normalizedTangentWS)); + + return half3x3(normalizedTangentWS, normalizedBitangentWS, normalizedNormalWS); +} + +#endif diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_utility.hlsl.meta b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_utility.hlsl.meta new file mode 100644 index 000000000..c00034f64 --- /dev/null +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_utility.hlsl.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 79a73f8bac4e45498341e9b6c1ea841e +timeCreated: 1622632833 \ No newline at end of file From fd52ae2af42e00676bc510ed77479898a4d82ddc Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Wed, 2 Jun 2021 20:49:22 +0900 Subject: [PATCH 10/17] Refactoring --- .../VRM10/vrmc_materials_mtoon_define.hlsl | 10 ++++++++ ...vrmc_materials_mtoon_forward_fragment.hlsl | 18 ++++++------- .../vrmc_materials_mtoon_geometry_normal.hlsl | 25 +++++++++++++++++++ ..._materials_mtoon_geometry_normal.hlsl.meta | 3 +++ ... => vrmc_materials_mtoon_geometry_uv.hlsl} | 7 +++--- ...rmc_materials_mtoon_geometry_uv.hlsl.meta} | 0 ... vrmc_materials_mtoon_lighting_mtoon.hlsl} | 6 ++--- ..._materials_mtoon_lighting_mtoon.hlsl.meta} | 0 ... vrmc_materials_mtoon_lighting_unity.hlsl} | 6 ++--- ..._materials_mtoon_lighting_unity.hlsl.meta} | 0 10 files changed, 55 insertions(+), 20 deletions(-) create mode 100644 Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_geometry_normal.hlsl create mode 100644 Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_geometry_normal.hlsl.meta rename Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/{vrmc_materials_mtoon_uv.hlsl => vrmc_materials_mtoon_geometry_uv.hlsl} (77%) rename Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/{vrmc_materials_mtoon_uv.hlsl.meta => vrmc_materials_mtoon_geometry_uv.hlsl.meta} (100%) rename Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/{vrmc_materials_mtoon_lighting.hlsl => vrmc_materials_mtoon_lighting_mtoon.hlsl} (92%) rename Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/{vrmc_materials_mtoon_lighting.hlsl.meta => vrmc_materials_mtoon_lighting_mtoon.hlsl.meta} (100%) rename Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/{vrmc_materials_mtoon_unity_lighting.hlsl => vrmc_materials_mtoon_lighting_unity.hlsl} (86%) rename Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/{vrmc_materials_mtoon_unity_lighting.hlsl.meta => vrmc_materials_mtoon_lighting_unity.hlsl.meta} (100%) diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_define.hlsl b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_define.hlsl index e7efedfdd..52dc4e9ea 100644 --- a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_define.hlsl +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_define.hlsl @@ -24,4 +24,14 @@ inline bool MToon_IsUvAnimationOn() #endif } +// Compile-time constant +inline bool MToon_IsNormalMapOn() +{ +#if defined(_NORMALMAP) + return true; +#else + return false; +#endif +} + #endif 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 df0ab2460..bf01828ef 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 @@ -6,14 +6,15 @@ #include "./vrmc_materials_mtoon_utility.hlsl" #include "./vrmc_materials_mtoon_input.hlsl" #include "./vrmc_materials_mtoon_attribute.hlsl" -#include "./vrmc_materials_mtoon_lighting.hlsl" -#include "./vrmc_materials_mtoon_uv.hlsl" -#include "./vrmc_materials_mtoon_unity_lighting.hlsl" +#include "./vrmc_materials_mtoon_geometry_uv.hlsl" +#include "./vrmc_materials_mtoon_geometry_normal.hlsl" +#include "./vrmc_materials_mtoon_lighting_unity.hlsl" +#include "./vrmc_materials_mtoon_lighting_mtoon.hlsl" half4 MToonFragment(const Varyings input) : SV_Target { // Get MToon UV (with UVAnimation) - const float2 uv = GetMToonUv(input.uv); + const float2 uv = GetMToonGeometry_Uv(input.uv); // Get LitColor with Alpha const half4 litColor = UNITY_SAMPLE_TEX2D(_MainTex, uv) * _Color; @@ -31,13 +32,8 @@ half4 MToonFragment(const Varyings input) : SV_Target const half alpha = 1.0; #endif - // Get Normal in WorldSpace from Normalmap if available -#if defined(_NORMALMAP) - const half3 normalTS = normalize(UnpackNormalWithScale(UNITY_SAMPLE_TEX2D(_BumpMap, uv), _BumpScale)); - const half3 normalWS = normalize(mul(normalTS, MToon_GetTangentToWorld(input.normalWS, input.tangentWS))); -#else - const half3 normalWS = normalize(input.normalWS); -#endif + // Get Normal + const float3 normalWS = GetMToonGeometry_Normal(input, uv); // Get Unity Lighting const UnityLighting unityLighting = GetUnityLighting(input, normalWS); diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_geometry_normal.hlsl b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_geometry_normal.hlsl new file mode 100644 index 000000000..db0304252 --- /dev/null +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_geometry_normal.hlsl @@ -0,0 +1,25 @@ +#ifndef VRMC_MATERIALS_MTOON_GEOMETRY_NORMAL_INCLUDED +#define VRMC_MATERIALS_MTOON_GEOMETRY_NORMAL_INCLUDED + +#include +#include "./vrmc_materials_mtoon_define.hlsl" +#include "./vrmc_materials_mtoon_utility.hlsl" +#include "./vrmc_materials_mtoon_input.hlsl" + +inline float3 GetMToonGeometry_NormalWithoutNormalMap(const half3 normalWS) +{ + return normalize(normalWS); +} + +inline float3 GetMToonGeometry_Normal(const Varyings input, const float2 mtoonUv) +{ +#if defined(_NORMALMAP) + // Get Normal in WorldSpace from Normalmap if available + const half3 normalTS = normalize(UnpackNormalWithScale(UNITY_SAMPLE_TEX2D(_BumpMap, mtoonUv), _BumpScale)); + return normalize(mul(normalTS, MToon_GetTangentToWorld(input.normalWS, input.tangentWS))); +#else + return GetMToonGeometry_NormalWithoutNormalMap(input.normalWS); +#endif +} + +#endif diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_geometry_normal.hlsl.meta b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_geometry_normal.hlsl.meta new file mode 100644 index 000000000..9e69be004 --- /dev/null +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_geometry_normal.hlsl.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 5ddfcfc47ee84eed9365b902bf76ce75 +timeCreated: 1622633324 \ No newline at end of file diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_uv.hlsl b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_geometry_uv.hlsl similarity index 77% rename from Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_uv.hlsl rename to Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_geometry_uv.hlsl index 6c92d4139..2c64f5f6e 100644 --- a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_uv.hlsl +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_geometry_uv.hlsl @@ -1,11 +1,12 @@ -#ifndef VRMC_MATERIALS_MTOON_UV_INCLUDED -#define VRMC_MATERIALS_MTOON_UV_INCLUDED +#ifndef VRMC_MATERIALS_MTOON_GEOMETRY_UV_INCLUDED +#define VRMC_MATERIALS_MTOON_GEOMETRY_UV_INCLUDED #include #include "./vrmc_materials_mtoon_define.hlsl" +#include "./vrmc_materials_mtoon_utility.hlsl" #include "./vrmc_materials_mtoon_input.hlsl" -float2 GetMToonUv(const float2 geometryUv) +float2 GetMToonGeometry_Uv(const float2 geometryUv) { // get raw uv with _MainTex_ST const float2 uvRaw = TRANSFORM_TEX(geometryUv, _MainTex); diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_uv.hlsl.meta b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_geometry_uv.hlsl.meta similarity index 100% rename from Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_uv.hlsl.meta rename to Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_geometry_uv.hlsl.meta diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_lighting.hlsl b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_lighting_mtoon.hlsl similarity index 92% rename from Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_lighting.hlsl rename to Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_lighting_mtoon.hlsl index d2ce3212d..f3815e925 100644 --- a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_lighting.hlsl +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_lighting_mtoon.hlsl @@ -1,11 +1,11 @@ -#ifndef VRMC_MATERIALS_MTOON_LIGHTING_INCLUDED -#define VRMC_MATERIALS_MTOON_LIGHTING_INCLUDED +#ifndef VRMC_MATERIALS_MTOON_LIGHTING_MTOON_INCLUDED +#define VRMC_MATERIALS_MTOON_LIGHTING_MTOON_INCLUDED #include #include "./vrmc_materials_mtoon_define.hlsl" #include "./vrmc_materials_mtoon_utility.hlsl" #include "./vrmc_materials_mtoon_input.hlsl" -#include "./vrmc_materials_mtoon_unity_lighting.hlsl" +#include "./vrmc_materials_mtoon_lighting_unity.hlsl" struct MToonInput { diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_lighting.hlsl.meta b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_lighting_mtoon.hlsl.meta similarity index 100% rename from Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_lighting.hlsl.meta rename to Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_lighting_mtoon.hlsl.meta diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_unity_lighting.hlsl b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_lighting_unity.hlsl similarity index 86% rename from Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_unity_lighting.hlsl rename to Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_lighting_unity.hlsl index 4e3efe509..5940d9bba 100644 --- a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_unity_lighting.hlsl +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_lighting_unity.hlsl @@ -1,10 +1,10 @@ -#ifndef VRMC_MATERIALS_MTOON_UNITY_LIGHTING_INCLUDED -#define VRMC_MATERIALS_MTOON_UNITY_LIGHTING_INCLUDED +#ifndef VRMC_MATERIALS_MTOON_LIGHTING_UNITY_INCLUDED +#define VRMC_MATERIALS_MTOON_LIGHTING_UNITY_INCLUDED #include #include #include -#include "./vrmc_materials_mtoon_utility.hlsl" +#include "./vrmc_materials_mtoon_define.hlsl" #include "./vrmc_materials_mtoon_input.hlsl" #include "./vrmc_materials_mtoon_attribute.hlsl" diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_unity_lighting.hlsl.meta b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_lighting_unity.hlsl.meta similarity index 100% rename from Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_unity_lighting.hlsl.meta rename to Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_lighting_unity.hlsl.meta From 9c089b85ad76bf6052e931cbf5adbeb4b5a07663 Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Wed, 2 Jun 2021 20:54:09 +0900 Subject: [PATCH 11/17] fix matcap uv --- .../VRM10/vrmc_materials_mtoon_lighting_mtoon.hlsl | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) 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 f3815e925..d0dbbbf83 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 @@ -62,8 +62,10 @@ inline half3 GetMToonLighting_Rim(const MToonInput input, const half3 lighting) if (MToon_IsForwardBasePass()) { const half3 worldUpWS = half3(0, 1, 0); - const half3 matcapRightWS = cross(input.viewDirWS, worldUpWS); - const half2 matcapUv = float2(dot(matcapRightWS, input.normalWS), dot(worldUpWS, input.normalWS)) * 0.5 + 0.5; + // TODO: use view space axis if abs(dot(viewDir, worldUp)) == 1.0 + const half3 matcapRightAxisWS = normalize(cross(input.viewDirWS, worldUpWS)); + const half3 matcapUpAxisWS = normalize(cross(matcapRightAxisWS, input.viewDirWS)); + const half2 matcapUv = float2(dot(matcapRightAxisWS, input.normalWS), dot(matcapUpAxisWS, input.normalWS)) * 0.5 + 0.5; const half3 matcapFactor = UNITY_SAMPLE_TEX2D(_MatcapTex, matcapUv).rgb; 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); From 598e3d70132db65a1625531b8f248bdbbd9fc844 Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Wed, 2 Jun 2021 21:01:41 +0900 Subject: [PATCH 12/17] Separate GI func --- .../vrmc_materials_mtoon_lighting_mtoon.hlsl | 31 +++++++++++++------ 1 file changed, 21 insertions(+), 10 deletions(-) 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 d0dbbbf83..c487ce1bd 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 @@ -25,26 +25,33 @@ inline half GetMToonLighting_Reflectance(const UnityLighting lighting, const MTo return mtoon_linearstep(-1.0 + shadingToony, +1.0 - shadingToony, shadingInput + shadingShift); } -inline half3 GetMToonLighting_BasicLighting(const UnityLighting unityLight, const MToonInput input, const half reflectance) +inline half3 GetMToonLighting_DirectLighting(const UnityLighting unityLight, const MToonInput input, const half reflectance) { if (MToon_IsForwardBasePass()) { const half3 shadeColor = UNITY_SAMPLE_TEX2D(_ShadeTex, input.uv).rgb * _ShadeColor.rgb; - const half3 direct = lerp(shadeColor, input.litColor, reflectance) * unityLight.directLightColor; - const half3 indirect = input.litColor * lerp(unityLight.indirectLight, unityLight.indirectLightEqualized, _GiEqualization); - - return direct + indirect; + return lerp(shadeColor, input.litColor, reflectance) * unityLight.directLightColor; } else { - const half3 direct = input.litColor * reflectance * unityLight.directLightColor * 0.5; - const half3 indirect = 0; - - return direct + indirect; + return input.litColor * reflectance * unityLight.directLightColor * 0.5; } } +inline half3 GetMToonLighting_GlobalIllumination(const UnityLighting unityLight, const MToonInput input) +{ + if (MToon_IsForwardBasePass()) + { + return input.litColor * lerp(unityLight.indirectLight, unityLight.indirectLightEqualized, _GiEqualization); + } + else + { + return 0; + } + +} + inline half3 GetMToonLighting_Emissive(const MToonInput input) { if (MToon_IsForwardBasePass()) @@ -81,8 +88,12 @@ half4 GetMToonLighting(const UnityLighting unityLight, const MToonInput input) { const half reflectance = GetMToonLighting_Reflectance(unityLight, input); - const half3 lighting = GetMToonLighting_BasicLighting(unityLight, input, reflectance); + const half3 direct = GetMToonLighting_DirectLighting(unityLight, input, reflectance); + 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 col = lighting + emissive + rim; From c81e4c275a1202aadb6070852b54ec20162a4efe Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Fri, 4 Jun 2021 21:21:04 +0900 Subject: [PATCH 13/17] add outline pass --- .../VRM10/vrmc_materials_mtoon.shader | 39 ++++++++++- .../VRM10/vrmc_materials_mtoon_attribute.hlsl | 5 +- .../VRM10/vrmc_materials_mtoon_define.hlsl | 34 ++++++++++ ...vrmc_materials_mtoon_forward_fragment.hlsl | 1 + .../vrmc_materials_mtoon_forward_vertex.hlsl | 25 +++++-- .../vrmc_materials_mtoon_geometry_vertex.hlsl | 66 +++++++++++++++++++ ..._materials_mtoon_geometry_vertex.hlsl.meta | 3 + .../vrmc_materials_mtoon_lighting_mtoon.hlsl | 8 ++- .../VRM10/vrmc_materials_mtoon_utility.hlsl | 5 ++ 9 files changed, 175 insertions(+), 11 deletions(-) create mode 100644 Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_geometry_vertex.hlsl create mode 100644 Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_geometry_vertex.hlsl.meta diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon.shader b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon.shader index 4384fbe73..dabf697db 100644 --- a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon.shader +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon.shader @@ -62,6 +62,7 @@ Shader "Hidden/VRM10/vrmc_materials_mtoon" _M_DebugMode ("_DebugMode", Float) = 0.0 } + // Shader Model 3.0 SubShader { Tags { "RenderType" = "Opaque" "Queue" = "Geometry" } @@ -77,7 +78,7 @@ Shader "Hidden/VRM10/vrmc_materials_mtoon" ZWrite [_M_ZWrite] ZTest LEqual BlendOp Add, Max - AlphaToMask Off + AlphaToMask [_M_AlphaToMask] HLSLPROGRAM #pragma target 3.0 @@ -99,6 +100,42 @@ Shader "Hidden/VRM10/vrmc_materials_mtoon" ENDHLSL } + // Built-in Forward Base Pass: OUTLINE + Pass + { + Name "FORWARD_BASE_OUTLINE" + Tags { "LightMode" = "ForwardBase" } + + Cull Front + Blend [_M_SrcBlend] [_M_DstBlend] + ZWrite [_M_ZWrite] + ZTest LEqual + BlendOp Add, Max + AlphaToMask [_M_AlphaToMask] + + HLSLPROGRAM + #pragma target 3.0 + + // Unity defined keywords + #pragma multi_compile_fwdbase + #pragma multi_compile_fog + #pragma multi_compile_instancing + + #pragma multi_compile_local __ _ALPHATEST_ON _ALPHABLEND_ON + #pragma multi_compile_local __ _NORMALMAP + #pragma multi_compile_local __ _UVANIMATION + + #define MTOON_PASS_OUTLINE + #define MTOON_OUTLINE_WIDTH_WORLD + + #pragma vertex MToonVertex + #pragma fragment MToonFragment + + #include "./vrmc_materials_mtoon_forward_vertex.hlsl" + #include "./vrmc_materials_mtoon_forward_fragment.hlsl" + ENDHLSL + } + // Built-in Forward Add Pass Pass { 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 f1aa1e87b..a9068f852 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,8 +25,9 @@ struct Varyings half4 tangentWS : TEXCOORD3; #endif float3 viewDirWS : TEXCOORD4; - UNITY_FOG_COORDS(5) - UNITY_LIGHTING_COORDS(6,7) + half outlineFactor : TEXCOORD5; + UNITY_FOG_COORDS(6) + UNITY_LIGHTING_COORDS(7,8) float4 pos : SV_POSITION; // UnityCG macro specified name. Accurately "positionCS" UNITY_VERTEX_INPUT_INSTANCE_ID UNITY_VERTEX_OUTPUT_STEREO diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_define.hlsl b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_define.hlsl index 52dc4e9ea..4c1eac67a 100644 --- a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_define.hlsl +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_define.hlsl @@ -1,6 +1,10 @@ #ifndef VRMC_MATERIALS_MTOON_DEFINE_INCLUDED #define VRMC_MATERIALS_MTOON_DEFINE_INCLUDED +#ifndef UNITY_SAMPLE_TEX2D_LOD + #define UNITY_SAMPLE_TEX2D_LOD(tex,coord,lod) tex.SampleLevel (sampler##tex,coord,lod) +#endif + // Compile-time constant inline bool MToon_IsForwardBasePass() { @@ -14,6 +18,16 @@ inline bool MToon_IsForwardBasePass() #endif } +// Compile-time constant +inline bool MToon_IsOutlinePass() +{ +#if defined(MTOON_PASS_OUTLINE) + return true; +#else + return false; +#endif +} + // Compile-time constant inline bool MToon_IsUvAnimationOn() { @@ -34,4 +48,24 @@ inline bool MToon_IsNormalMapOn() #endif } +// Compile-time constant +inline bool MToon_IsOutlineModeWorldCoordinates() +{ + #if defined(MTOON_OUTLINE_WIDTH_WORLD) + return true; +#else + return false; +#endif +} + +// Compile-time constant +inline bool MToon_IsOutlineModeScreenCoordinates() +{ + #if defined(MTOON_OUTLINE_WIDTH_SCREEN) + return true; +#else + return false; +#endif +} + #endif 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 bf01828ef..5d9dc4a57 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 @@ -45,6 +45,7 @@ half4 MToonFragment(const Varyings input) : SV_Target mtoonInput.viewDirWS = input.viewDirWS; mtoonInput.litColor = litColor.rgb; mtoonInput.alpha = alpha; + 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 15b597141..232495023 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 @@ -3,9 +3,11 @@ #include #include +#include "./vrmc_materials_mtoon_define.hlsl" #include "./vrmc_materials_mtoon_utility.hlsl" #include "./vrmc_materials_mtoon_input.hlsl" #include "./vrmc_materials_mtoon_attribute.hlsl" +#include "./vrmc_materials_mtoon_geometry_vertex.hlsl" Varyings MToonVertex(const Attributes v) // v is UnityCG macro specified name. { @@ -15,18 +17,31 @@ Varyings MToonVertex(const Attributes v) // v is UnityCG macro specified name. UNITY_TRANSFER_INSTANCE_ID(v, output); UNITY_INITIALIZE_VERTEX_OUTPUT_STEREO(output); - output.pos = UnityObjectToClipPos(v.vertex); - output.positionWS = mul(unity_ObjectToWorld, v.vertex); 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; + } + else + { + output.normalWS = UnityObjectToWorldNormal(v.normalOS); + const VertexPositionInfo position = MToon_GetVertex(v.vertex.xyz); + output.pos = position.positionCS; + output.positionWS = position.positionWS; + output.outlineFactor = 0; + } - output.normalWS = UnityObjectToWorldNormal(v.normalOS); #if defined(_NORMALMAP) const half tangentSign = v.tangentOS.w * unity_WorldTransformParams.w; output.tangentWS = half4(UnityObjectToWorldDir(v.tangentOS), tangentSign); #endif - output.viewDirWS = MToon_GetWorldSpaceNormalizedViewDir(output.positionWS); - UNITY_TRANSFER_FOG(output, output.positionWS); UNITY_TRANSFER_LIGHTING(output, v.texcoord1.xy); diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_geometry_vertex.hlsl b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_geometry_vertex.hlsl new file mode 100644 index 000000000..dad41bfcc --- /dev/null +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_geometry_vertex.hlsl @@ -0,0 +1,66 @@ +#ifndef VRMC_MATERIALS_MTOON_GEOMETRY_VERTEX_INCLUDED +#define VRMC_MATERIALS_MTOON_GEOMETRY_VERTEX_INCLUDED + +#include +#include "./vrmc_materials_mtoon_define.hlsl" +#include "./vrmc_materials_mtoon_utility.hlsl" +#include "./vrmc_materials_mtoon_input.hlsl" + +struct VertexPositionInfo +{ + float4 positionWS; + float4 positionCS; +}; + +inline VertexPositionInfo MToon_GetOutlineVertex(const float3 positionOS, const half3 normalOS, const float2 uv) +{ + if (MToon_IsOutlineModeWorldCoordinates()) + { + const float3 positionWS = mul(unity_ObjectToWorld, float4(positionOS, 1)).xyz; + const half outlineWidth = _OutlineWidth * UNITY_SAMPLE_TEX2D_LOD(_OutlineWidthTex, uv, 0); + const half3 normalWS = UnityObjectToWorldNormal(normalOS); + + VertexPositionInfo output; + output.positionWS = float4(positionWS + normalWS * outlineWidth, 1); + output.positionCS = UnityWorldToClipPos(output.positionWS); + return output; + } + else if (MToon_IsOutlineModeScreenCoordinates()) + { + const float3 positionWS = mul(unity_ObjectToWorld, float4(positionOS, 1)).xyz; + const half outlineWidth = _OutlineWidth * UNITY_SAMPLE_TEX2D_LOD(_OutlineWidthTex, uv, 0); + + const float4 nearUpperRight = mul(unity_CameraInvProjection, float4(1, 1, UNITY_NEAR_CLIP_VALUE, _ProjectionParams.y)); + const half aspect = abs(nearUpperRight.y / nearUpperRight.x); + + float4 positionCS = UnityObjectToClipPos(positionOS); + const half3 normalVS = MToon_GetObjectToViewNormal(normalOS); + const half3 normalCS = TransformViewToProjection(normalVS.xyz); + half2 normalProjectedCS = normalize(normalCS.xy); + normalProjectedCS *= positionCS.w; + normalProjectedCS.x *= aspect; + positionCS.xy += outlineWidth * normalProjectedCS.xy * saturate(1 - abs(normalVS.z)); // ignore offset when normal toward camera + + VertexPositionInfo output; + output.positionWS = float4(positionWS, 1); + output.positionCS = positionCS; + return output; + } + else + { + VertexPositionInfo output; + output.positionWS = mul(unity_ObjectToWorld, float4(positionOS, 1)); + output.positionCS = UnityWorldToClipPos(output.positionWS); + return output; + } +} + +inline VertexPositionInfo MToon_GetVertex(const float3 positionOS) +{ + VertexPositionInfo output; + output.positionWS = mul(unity_ObjectToWorld, float4(positionOS, 1)); + output.positionCS = UnityWorldToClipPos(output.positionWS); + return output; +} + +#endif diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_geometry_vertex.hlsl.meta b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_geometry_vertex.hlsl.meta new file mode 100644 index 000000000..c11dec7d6 --- /dev/null +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_geometry_vertex.hlsl.meta @@ -0,0 +1,3 @@ +fileFormatVersion: 2 +guid: 92e89e208e604f28acdce8a142d1e241 +timeCreated: 1622805067 \ No newline at end of file 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 c487ce1bd..aa97eaa13 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,6 +14,7 @@ struct MToonInput half3 viewDirWS; half3 litColor; half alpha; + half outlineFactor; }; inline half GetMToonLighting_Reflectance(const UnityLighting lighting, const MToonInput input) @@ -91,12 +92,13 @@ half4 GetMToonLighting(const UnityLighting unityLight, const MToonInput input) const half3 direct = GetMToonLighting_DirectLighting(unityLight, input, reflectance); 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 col = lighting + emissive + rim; + 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); } diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_utility.hlsl b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_utility.hlsl index 98b994edc..080df8451 100644 --- a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_utility.hlsl +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_utility.hlsl @@ -39,4 +39,9 @@ inline half3x3 MToon_GetTangentToWorld(const half3 normalWS, const half4 tangent return half3x3(normalizedTangentWS, normalizedBitangentWS, normalizedNormalWS); } +inline half3 MToon_GetObjectToViewNormal(const half3 normalOS) +{ + return normalize(mul((half3x3)UNITY_MATRIX_IT_MV, normalOS)); +} + #endif From 977482df90d9b6644aec8d97535dc5cd80b04957 Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Mon, 7 Jun 2021 20:10:20 +0900 Subject: [PATCH 14/17] Disable unnecessary shader variants/ --- .../MToon10/Resources/VRM10/vrmc_materials_mtoon.shader | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon.shader b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon.shader index dabf697db..ef2b0a5f7 100644 --- a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon.shader +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon.shader @@ -84,7 +84,7 @@ Shader "Hidden/VRM10/vrmc_materials_mtoon" #pragma target 3.0 // Unity defined keywords - #pragma multi_compile_fwdbase + #pragma multi_compile_fwdbase nolightmap nodynlightmap nodirlightmap novertexlight #pragma multi_compile_fog #pragma multi_compile_instancing @@ -117,7 +117,7 @@ Shader "Hidden/VRM10/vrmc_materials_mtoon" #pragma target 3.0 // Unity defined keywords - #pragma multi_compile_fwdbase + #pragma multi_compile_fwdbase nolightmap nodynlightmap nodirlightmap novertexlight #pragma multi_compile_fog #pragma multi_compile_instancing @@ -153,7 +153,7 @@ Shader "Hidden/VRM10/vrmc_materials_mtoon" #pragma target 3.0 // Unity defined keywords - #pragma multi_compile_fwdadd_fullshadows + #pragma multi_compile_fwdadd_fullshadows nolightmap nodynlightmap nodirlightmap novertexlight #pragma multi_compile_fog #pragma multi_compile_instancing @@ -183,7 +183,7 @@ Shader "Hidden/VRM10/vrmc_materials_mtoon" #pragma target 3.0 // Unity defined keywords - #pragma multi_compile_shadowcaster + #pragma multi_compile_shadowcaster nolightmap nodynlightmap nodirlightmap novertexlight #pragma multi_compile_local __ _ALPHATEST_ON _ALPHABLEND_ON From ccbc53d0d8cdec2b4c52cba815ecb8e78eb03fb4 Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Mon, 7 Jun 2021 20:11:00 +0900 Subject: [PATCH 15/17] Suppress TextureFetch if not needed. --- .../VRM10/vrmc_materials_mtoon.shader | 23 +++++++--- .../VRM10/vrmc_materials_mtoon_define.hlsl | 44 ++++++++++++++++++- .../vrmc_materials_mtoon_geometry_uv.hlsl | 6 +-- .../vrmc_materials_mtoon_geometry_vertex.hlsl | 14 +++++- .../vrmc_materials_mtoon_lighting_mtoon.hlsl | 18 ++++++-- 5 files changed, 89 insertions(+), 16 deletions(-) diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon.shader b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon.shader index ef2b0a5f7..ffed3c5cb 100644 --- a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon.shader +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon.shader @@ -89,8 +89,11 @@ Shader "Hidden/VRM10/vrmc_materials_mtoon" #pragma multi_compile_instancing #pragma multi_compile_local __ _ALPHATEST_ON _ALPHABLEND_ON - #pragma multi_compile_local __ _NORMALMAP - #pragma multi_compile_local __ _UVANIMATION + #pragma multi_compile_local __ _MTOON_NORMALMAP + #pragma multi_compile_local __ _MTOON_SHADINGMAP + #pragma multi_compile_local __ _MTOON_EMISSIVE + #pragma multi_compile_local __ _MTOON_UVANIMATION + #pragma multi_compile_local __ _MTOON_RIM #pragma vertex MToonVertex #pragma fragment MToonFragment @@ -122,11 +125,14 @@ Shader "Hidden/VRM10/vrmc_materials_mtoon" #pragma multi_compile_instancing #pragma multi_compile_local __ _ALPHATEST_ON _ALPHABLEND_ON - #pragma multi_compile_local __ _NORMALMAP - #pragma multi_compile_local __ _UVANIMATION + #pragma multi_compile_local __ _MTOON_OUTLINEMAP + #pragma multi_compile_local __ _MTOON_NORMALMAP + #pragma multi_compile_local __ _MTOON_SHADINGMAP + #pragma multi_compile_local __ _MTOON_EMISSIVE + #pragma multi_compile_local __ _MTOON_UVANIMATION + #pragma multi_compile_local __ _MTOON_RIM #define MTOON_PASS_OUTLINE - #define MTOON_OUTLINE_WIDTH_WORLD #pragma vertex MToonVertex #pragma fragment MToonFragment @@ -158,8 +164,11 @@ Shader "Hidden/VRM10/vrmc_materials_mtoon" #pragma multi_compile_instancing #pragma multi_compile_local __ _ALPHATEST_ON _ALPHABLEND_ON - #pragma multi_compile_local __ _NORMALMAP - #pragma multi_compile_local __ _UVANIMATION + #pragma multi_compile_local __ _MTOON_NORMALMAP + #pragma multi_compile_local __ _MTOON_SHADINGMAP + #pragma multi_compile_local __ _MTOON_EMISSIVE + #pragma multi_compile_local __ _MTOON_UVANIMATION + #pragma multi_compile_local __ _MTOON_RIM #pragma vertex MToonVertex #pragma fragment MToonFragment diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_define.hlsl b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_define.hlsl index 4c1eac67a..2de74dd5d 100644 --- a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_define.hlsl +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_define.hlsl @@ -31,7 +31,27 @@ inline bool MToon_IsOutlinePass() // Compile-time constant inline bool MToon_IsUvAnimationOn() { -#if defined(_UVANIMATION) +#if defined(_MTOON_UVANIMATION) + return true; +#else + return false; +#endif +} + +// Compile-time constant +inline bool MToon_IsRimOn() +{ +#if defined(_MTOON_RIM) + return true; +#else + return false; +#endif +} + +// Compile-time constant +inline bool MToon_IsEmissiveOn() +{ +#if defined(_MTOON_EMISSIVE) return true; #else return false; @@ -41,7 +61,27 @@ inline bool MToon_IsUvAnimationOn() // Compile-time constant inline bool MToon_IsNormalMapOn() { -#if defined(_NORMALMAP) +#if defined(_MTOON_NORMALMAP) + return true; +#else + return false; +#endif +} + +// Compile-time constant +inline bool MToon_IsShadingMapOn() +{ +#if defined(_MTOON_SHADINGMAP) + return true; +#else + return false; +#endif +} + +// Compile-time constant +inline bool MToon_IsOutlineMapOn() +{ +#if defined(_MTOON_OUTLINEMAP) return true; #else return false; diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_geometry_uv.hlsl b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_geometry_uv.hlsl index 2c64f5f6e..c07980ae2 100644 --- a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_geometry_uv.hlsl +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_geometry_uv.hlsl @@ -6,7 +6,7 @@ #include "./vrmc_materials_mtoon_utility.hlsl" #include "./vrmc_materials_mtoon_input.hlsl" -float2 GetMToonGeometry_Uv(const float2 geometryUv) +inline float2 GetMToonGeometry_Uv(const float2 geometryUv) { // get raw uv with _MainTex_ST const float2 uvRaw = TRANSFORM_TEX(geometryUv, _MainTex); @@ -14,10 +14,10 @@ float2 GetMToonGeometry_Uv(const float2 geometryUv) if (MToon_IsUvAnimationOn()) { const float uvAnimationCoef = UNITY_SAMPLE_TEX2D(_UvAnimMaskTex, uvRaw).b * _Time.y; - const float2 uvAnimationAdd = uvAnimationCoef * float2(_UvAnimScrollXSpeed, _UvAnimScrollYSpeed); + const float2 translate = uvAnimationCoef * float2(_UvAnimScrollXSpeed, _UvAnimScrollYSpeed); const float rotateRad = uvAnimationCoef * _UvAnimRotationSpeed * PI_2; const float2 rotatePivot = float2(0.5, 0.5); - return mul(float2x2(cos(rotateRad), -sin(rotateRad), sin(rotateRad), cos(rotateRad)), uvRaw + uvAnimationAdd - rotatePivot) + rotatePivot; + return mul(float2x2(cos(rotateRad), -sin(rotateRad), sin(rotateRad), cos(rotateRad)), uvRaw + translate - rotatePivot) + rotatePivot; } else { diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_geometry_vertex.hlsl b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_geometry_vertex.hlsl index dad41bfcc..21af39c3e 100644 --- a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_geometry_vertex.hlsl +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_geometry_vertex.hlsl @@ -12,12 +12,24 @@ struct VertexPositionInfo float4 positionCS; }; +inline half MToon_GetOutlineVertex_OutlineWidth(const float2 uv) +{ + if (MToon_IsOutlineMapOn()) + { + return _OutlineWidth * UNITY_SAMPLE_TEX2D_LOD(_OutlineWidthTex, uv, 0); + } + else + { + return _OutlineWidth; + } +} + inline VertexPositionInfo MToon_GetOutlineVertex(const float3 positionOS, const half3 normalOS, const float2 uv) { if (MToon_IsOutlineModeWorldCoordinates()) { const float3 positionWS = mul(unity_ObjectToWorld, float4(positionOS, 1)).xyz; - const half outlineWidth = _OutlineWidth * UNITY_SAMPLE_TEX2D_LOD(_OutlineWidthTex, uv, 0); + const half outlineWidth = MToon_GetOutlineVertex_OutlineWidth(uv); const half3 normalWS = UnityObjectToWorldNormal(normalOS); VertexPositionInfo output; 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 aa97eaa13..d871bf3c5 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 @@ -17,11 +17,23 @@ struct MToonInput half outlineFactor; }; +inline half GetMToonLighting_Reflectance_ShadingShift(const MToonInput input) +{ + if (MToon_IsShadingMapOn()) + { + return UNITY_SAMPLE_TEX2D(_ShadingShiftTex, input.uv).r * _ShadingShiftTexScale + _ShadingShiftFactor; + } + else + { + return _ShadingShiftFactor; + } +} + inline half GetMToonLighting_Reflectance(const UnityLighting lighting, const MToonInput input) { const half dotNL = dot(input.normalWS, lighting.directLightDirection); const half shadingInput = lerp(-1, 1, mtoon_linearstep(-1, 1, dotNL) * lighting.directLightAttenuation); - const half shadingShift = UNITY_SAMPLE_TEX2D(_ShadingShiftTex, input.uv).r * _ShadingShiftTexScale + _ShadingShiftFactor; + const half shadingShift = GetMToonLighting_Reflectance_ShadingShift(input); const half shadingToony = _ShadingToonyFactor; return mtoon_linearstep(-1.0 + shadingToony, +1.0 - shadingToony, shadingInput + shadingShift); } @@ -55,7 +67,7 @@ inline half3 GetMToonLighting_GlobalIllumination(const UnityLighting unityLight, inline half3 GetMToonLighting_Emissive(const MToonInput input) { - if (MToon_IsForwardBasePass()) + if (MToon_IsForwardBasePass() && MToon_IsEmissiveOn()) { return UNITY_SAMPLE_TEX2D(_EmissionMap, input.uv).rgb * _EmissionColor.rgb; } @@ -67,7 +79,7 @@ inline half3 GetMToonLighting_Emissive(const MToonInput input) inline half3 GetMToonLighting_Rim(const MToonInput input, const half3 lighting) { - if (MToon_IsForwardBasePass()) + if (MToon_IsForwardBasePass() && MToon_IsRimOn()) { const half3 worldUpWS = half3(0, 1, 0); // TODO: use view space axis if abs(dot(viewDir, worldUp)) == 1.0 From d700b393ff5f4803cb2e34113eb6f0a247f27238 Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Mon, 7 Jun 2021 20:33:01 +0900 Subject: [PATCH 16/17] Suppress variants. --- .../VRM10/vrmc_materials_mtoon.shader | 25 +++++------- .../VRM10/vrmc_materials_mtoon_define.hlsl | 38 ++----------------- .../vrmc_materials_mtoon_geometry_uv.hlsl | 24 +++++++++--- .../vrmc_materials_mtoon_geometry_vertex.hlsl | 2 +- .../vrmc_materials_mtoon_lighting_mtoon.hlsl | 30 ++++++++++----- 5 files changed, 54 insertions(+), 65 deletions(-) diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon.shader b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon.shader index ffed3c5cb..7ff2cfa53 100644 --- a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon.shader +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon.shader @@ -89,11 +89,9 @@ Shader "Hidden/VRM10/vrmc_materials_mtoon" #pragma multi_compile_instancing #pragma multi_compile_local __ _ALPHATEST_ON _ALPHABLEND_ON - #pragma multi_compile_local __ _MTOON_NORMALMAP - #pragma multi_compile_local __ _MTOON_SHADINGMAP - #pragma multi_compile_local __ _MTOON_EMISSIVE - #pragma multi_compile_local __ _MTOON_UVANIMATION - #pragma multi_compile_local __ _MTOON_RIM + #pragma multi_compile_local __ _NORMALMAP + #pragma multi_compile_local __ _MTOON_EMISSIVEMAP + #pragma multi_compile_local __ _MTOON_PARAMETERMAP #pragma vertex MToonVertex #pragma fragment MToonFragment @@ -125,12 +123,9 @@ Shader "Hidden/VRM10/vrmc_materials_mtoon" #pragma multi_compile_instancing #pragma multi_compile_local __ _ALPHATEST_ON _ALPHABLEND_ON - #pragma multi_compile_local __ _MTOON_OUTLINEMAP - #pragma multi_compile_local __ _MTOON_NORMALMAP - #pragma multi_compile_local __ _MTOON_SHADINGMAP - #pragma multi_compile_local __ _MTOON_EMISSIVE - #pragma multi_compile_local __ _MTOON_UVANIMATION - #pragma multi_compile_local __ _MTOON_RIM + #pragma multi_compile_local __ _NORMALMAP + #pragma multi_compile_local __ _MTOON_EMISSIVEMAP + #pragma multi_compile_local __ _MTOON_PARAMETERMAP #define MTOON_PASS_OUTLINE @@ -164,11 +159,9 @@ Shader "Hidden/VRM10/vrmc_materials_mtoon" #pragma multi_compile_instancing #pragma multi_compile_local __ _ALPHATEST_ON _ALPHABLEND_ON - #pragma multi_compile_local __ _MTOON_NORMALMAP - #pragma multi_compile_local __ _MTOON_SHADINGMAP - #pragma multi_compile_local __ _MTOON_EMISSIVE - #pragma multi_compile_local __ _MTOON_UVANIMATION - #pragma multi_compile_local __ _MTOON_RIM + #pragma multi_compile_local __ _NORMALMAP + #pragma multi_compile_local __ _MTOON_EMISSIVEMAP + #pragma multi_compile_local __ _MTOON_PARAMETERMAP #pragma vertex MToonVertex #pragma fragment MToonFragment diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_define.hlsl b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_define.hlsl index 2de74dd5d..885bd51dd 100644 --- a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_define.hlsl +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_define.hlsl @@ -28,36 +28,6 @@ inline bool MToon_IsOutlinePass() #endif } -// Compile-time constant -inline bool MToon_IsUvAnimationOn() -{ -#if defined(_MTOON_UVANIMATION) - return true; -#else - return false; -#endif -} - -// Compile-time constant -inline bool MToon_IsRimOn() -{ -#if defined(_MTOON_RIM) - return true; -#else - return false; -#endif -} - -// Compile-time constant -inline bool MToon_IsEmissiveOn() -{ -#if defined(_MTOON_EMISSIVE) - return true; -#else - return false; -#endif -} - // Compile-time constant inline bool MToon_IsNormalMapOn() { @@ -69,9 +39,9 @@ inline bool MToon_IsNormalMapOn() } // Compile-time constant -inline bool MToon_IsShadingMapOn() +inline bool MToon_IsEmissiveMapOn() { -#if defined(_MTOON_SHADINGMAP) +#if defined(_MTOON_EMISSIVEMAP) return true; #else return false; @@ -79,9 +49,9 @@ inline bool MToon_IsShadingMapOn() } // Compile-time constant -inline bool MToon_IsOutlineMapOn() +inline bool MToon_IsParameterMapOn() { -#if defined(_MTOON_OUTLINEMAP) +#if defined(_MTOON_PARAMETERMAP) return true; #else return false; diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_geometry_uv.hlsl b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_geometry_uv.hlsl index c07980ae2..ed1f1556c 100644 --- a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_geometry_uv.hlsl +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_geometry_uv.hlsl @@ -6,18 +6,32 @@ #include "./vrmc_materials_mtoon_utility.hlsl" #include "./vrmc_materials_mtoon_input.hlsl" +inline float GetMToonGeometry_Uv_Time(const float2 uvRaw) +{ + if (MToon_IsParameterMapOn()) + { + return UNITY_SAMPLE_TEX2D(_UvAnimMaskTex, uvRaw).b * _Time.y; + } + else + { + return _Time.y; + } +} + inline float2 GetMToonGeometry_Uv(const float2 geometryUv) { // get raw uv with _MainTex_ST const float2 uvRaw = TRANSFORM_TEX(geometryUv, _MainTex); - if (MToon_IsUvAnimationOn()) + if (MToon_IsParameterMapOn()) { - const float uvAnimationCoef = UNITY_SAMPLE_TEX2D(_UvAnimMaskTex, uvRaw).b * _Time.y; - const float2 translate = uvAnimationCoef * float2(_UvAnimScrollXSpeed, _UvAnimScrollYSpeed); - const float rotateRad = uvAnimationCoef * _UvAnimRotationSpeed * PI_2; + const float uvAnimationTime = GetMToonGeometry_Uv_Time(uvRaw); + const float2 translate = uvAnimationTime * float2(_UvAnimScrollXSpeed, _UvAnimScrollYSpeed); + const float rotateRad = uvAnimationTime * _UvAnimRotationSpeed * PI_2; + const float cosRotate = cos(rotateRad); + const float sinRotate = sin(rotateRad); const float2 rotatePivot = float2(0.5, 0.5); - return mul(float2x2(cos(rotateRad), -sin(rotateRad), sin(rotateRad), cos(rotateRad)), uvRaw + translate - rotatePivot) + rotatePivot; + return mul(float2x2(cosRotate, -sinRotate, sinRotate, cosRotate), uvRaw + translate - rotatePivot) + rotatePivot; } else { diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_geometry_vertex.hlsl b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_geometry_vertex.hlsl index 21af39c3e..d9c024edc 100644 --- a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_geometry_vertex.hlsl +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_geometry_vertex.hlsl @@ -14,7 +14,7 @@ struct VertexPositionInfo inline half MToon_GetOutlineVertex_OutlineWidth(const float2 uv) { - if (MToon_IsOutlineMapOn()) + if (MToon_IsParameterMapOn()) { return _OutlineWidth * UNITY_SAMPLE_TEX2D_LOD(_OutlineWidthTex, uv, 0); } 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 d871bf3c5..d701769fa 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 @@ -19,7 +19,7 @@ struct MToonInput inline half GetMToonLighting_Reflectance_ShadingShift(const MToonInput input) { - if (MToon_IsShadingMapOn()) + if (MToon_IsParameterMapOn()) { return UNITY_SAMPLE_TEX2D(_ShadingShiftTex, input.uv).r * _ShadingShiftTexScale + _ShadingShiftFactor; } @@ -67,11 +67,28 @@ inline half3 GetMToonLighting_GlobalIllumination(const UnityLighting unityLight, inline half3 GetMToonLighting_Emissive(const MToonInput input) { - if (MToon_IsForwardBasePass() && MToon_IsEmissiveOn()) + if (MToon_IsForwardBasePass() && MToon_IsEmissiveMapOn()) { return UNITY_SAMPLE_TEX2D(_EmissionMap, input.uv).rgb * _EmissionColor.rgb; } else + { + return _EmissionColor.rgb; + } +} + +inline half3 GetMToonLighting_Rim_Matcap(const MToonInput input) +{ + if (MToon_IsParameterMapOn()) + { + const half3 worldUpWS = half3(0, 1, 0); + // TODO: use view space axis if abs(dot(viewDir, worldUp)) == 1.0 + const half3 matcapRightAxisWS = normalize(cross(input.viewDirWS, worldUpWS)); + const half3 matcapUpAxisWS = normalize(cross(matcapRightAxisWS, input.viewDirWS)); + const half2 matcapUv = float2(dot(matcapRightAxisWS, input.normalWS), dot(matcapUpAxisWS, input.normalWS)) * 0.5 + 0.5; + return UNITY_SAMPLE_TEX2D(_MatcapTex, matcapUv).rgb; + } + else { return 0; } @@ -79,16 +96,11 @@ inline half3 GetMToonLighting_Emissive(const MToonInput input) inline half3 GetMToonLighting_Rim(const MToonInput input, const half3 lighting) { - if (MToon_IsForwardBasePass() && MToon_IsRimOn()) + if (MToon_IsForwardBasePass()) { - const half3 worldUpWS = half3(0, 1, 0); - // TODO: use view space axis if abs(dot(viewDir, worldUp)) == 1.0 - const half3 matcapRightAxisWS = normalize(cross(input.viewDirWS, worldUpWS)); - const half3 matcapUpAxisWS = normalize(cross(matcapRightAxisWS, input.viewDirWS)); - const half2 matcapUv = float2(dot(matcapRightAxisWS, input.normalWS), dot(matcapUpAxisWS, input.normalWS)) * 0.5 + 0.5; - const half3 matcapFactor = UNITY_SAMPLE_TEX2D(_MatcapTex, matcapUv).rgb; 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 matcapFactor = GetMToonLighting_Rim_Matcap(input); return (matcapFactor + parametricRimFactor) * UNITY_SAMPLE_TEX2D(_RimTex, input.uv).rgb * rimLightingFactor; } else From 1f2cc762b7b427f030aee88c4e9f084f2f156583 Mon Sep 17 00:00:00 2001 From: Masataka SUMI Date: Mon, 7 Jun 2021 20:44:08 +0900 Subject: [PATCH 17/17] fix --- .../MToon10/Resources/VRM10/vrmc_materials_mtoon_define.hlsl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_define.hlsl b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_define.hlsl index 885bd51dd..59cad75b6 100644 --- a/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_define.hlsl +++ b/Assets/VRMShaders/VRM10/MToon10/Resources/VRM10/vrmc_materials_mtoon_define.hlsl @@ -31,7 +31,7 @@ inline bool MToon_IsOutlinePass() // Compile-time constant inline bool MToon_IsNormalMapOn() { -#if defined(_MTOON_NORMALMAP) +#if defined(_NORMALMAP) return true; #else return false;