From 308074a0958d7eedb7df80c86277c0321fc3cf80 Mon Sep 17 00:00:00 2001 From: anatawa12 Date: Tue, 29 Oct 2024 09:55:43 +0900 Subject: [PATCH 1/4] fix: Integer and Int confusion --- API-Editor/ShaderInformation.cs | 9 +++ .../APIInternal/ShaderInformation.Liltoon.cs | 66 +++++++++---------- .../Processors/ShaderMaterialInformation.cs | 3 + Internal/Utils/Utils.cs | 3 + 4 files changed, 48 insertions(+), 33 deletions(-) diff --git a/API-Editor/ShaderInformation.cs b/API-Editor/ShaderInformation.cs index 9f889f81..c460be81 100644 --- a/API-Editor/ShaderInformation.cs +++ b/API-Editor/ShaderInformation.cs @@ -206,6 +206,15 @@ internal MaterialInformationCallback() [PublicAPI] public abstract int? GetInteger(string propertyName, bool considerAnimation = true); + /// + /// Returns the int value for the property in the material, or null if the property is not set or not found. + /// + /// The name of the property in the material. + /// Whether to consider the animation of the property. If this is true, this function will never + /// The int value for the property in the material, which is same as , or null if the property is animated. + [PublicAPI] + public abstract int? GetInt(string propertyName, bool considerAnimation = true); + /// /// Returns the float value for the property in the material, or null if the property is not set or not found. /// diff --git a/Editor/APIInternal/ShaderInformation.Liltoon.cs b/Editor/APIInternal/ShaderInformation.Liltoon.cs index fc4a83f0..b6d4f811 100644 --- a/Editor/APIInternal/ShaderInformation.Liltoon.cs +++ b/Editor/APIInternal/ShaderInformation.Liltoon.cs @@ -131,13 +131,13 @@ public override void GetMaterialInformation(MaterialInformationCallback matInfo) UsingUVChannels.NonMesh, null); // GradationMap UV is based on color LIL_SAMPLE_2D_WithMat("_MainColorAdjustMask", "_MainTex", uvMain, uvMainMatrix); // simple LIL_SAMPLE_2D - if (matInfo.GetInteger("_UseMain2ndTex") != 0) + if (matInfo.GetInt("_UseMain2ndTex") != 0) { // caller of lilGetMain2nd will pass sampler for _MainTex as samp SamplerStateInformation samp = "_MainTex"; UsingUVChannels uv2nd; - switch (matInfo.GetInteger("_Main2ndTex_UVMode")) + switch (matInfo.GetInt("_Main2ndTex_UVMode")) { case 0: uv2nd = UsingUVChannels.UV0; @@ -172,13 +172,13 @@ public override void GetMaterialInformation(MaterialInformationCallback matInfo) ); } - if (matInfo.GetInteger("_UseMain3rdTex") != 0) + if (matInfo.GetInt("_UseMain3rdTex") != 0) { // caller of lilGetMain3rd will pass sampler for _MainTex as samp var samp = "_MainTex"; UsingUVChannels uv3rd; - switch (matInfo.GetInteger("_Main2ndTex_UVMode")) + switch (matInfo.GetInt("_Main2ndTex_UVMode")) { case 0: uv3rd = UsingUVChannels.UV0; @@ -214,16 +214,16 @@ public override void GetMaterialInformation(MaterialInformationCallback matInfo) } LIL_SAMPLE_2D_ST_WithMat("_AlphaMask", "_MainTex", uvMain, uvMainMatrix); - if (matInfo.GetInteger("_UseBumpMap") != 0) + if (matInfo.GetInt("_UseBumpMap") != 0) { LIL_SAMPLE_2D_ST_WithMat("_BumpMap", "_MainTex", uvMain, uvMainMatrix); } - if (matInfo.GetInteger("_UseBump2ndMap") != 0) + if (matInfo.GetInt("_UseBump2ndMap") != 0) { var uvBump2nd = UsingUVChannels.UV0; - switch (matInfo.GetInteger("_Bump2ndMap_UVMode")) + switch (matInfo.GetInt("_Bump2ndMap_UVMode")) { case 0: uvBump2nd = UsingUVChannels.UV0; @@ -248,7 +248,7 @@ public override void GetMaterialInformation(MaterialInformationCallback matInfo) // Note: _Bump2ndScaleMask is defined as NoScaleOffset but sampled with LIL_SAMPLE_2D_ST? } - if (matInfo.GetInteger("_UseAnisotropy") != 0) + if (matInfo.GetInt("_UseAnisotropy") != 0) { LIL_SAMPLE_2D_ST_WithMat("_AnisotropyTangentMap", "_MainTex", uvMain, uvMainMatrix); LIL_SAMPLE_2D_ST_WithMat("_AnisotropyScaleMask", "_MainTex", uvMain, uvMainMatrix); @@ -257,13 +257,13 @@ public override void GetMaterialInformation(MaterialInformationCallback matInfo) LIL_SAMPLE_2D_ST_WithMat("_AnisotropyShiftNoiseMask", "_MainTex", uvMain, uvMainMatrix); } - if (matInfo.GetInteger("_UseBacklight") != 0) + if (matInfo.GetInt("_UseBacklight") != 0) { var samp = "_MainTex"; LIL_SAMPLE_2D_ST_WithMat("_BacklightColorTex", samp, uvMain, uvMainMatrix); } - if (matInfo.GetInteger("_UseShadow") != 0) + if (matInfo.GetInt("_UseShadow") != 0) { SamplerStateInformation samp = "_MainTex"; LIL_SAMPLE_2D_GRAD_WithMat("_ShadowStrengthMask", SamplerStateInformation.LinearRepeatSampler, uvMain, @@ -273,7 +273,7 @@ public override void GetMaterialInformation(MaterialInformationCallback matInfo) LIL_SAMPLE_2D_GRAD_WithMat("_ShadowBlurMask", SamplerStateInformation.LinearRepeatSampler, uvMain, uvMainMatrix); // lilSampleLUT - switch (matInfo.GetInteger("_ShadowColorType")) + switch (matInfo.GetInt("_ShadowColorType")) { case 1: LIL_SAMPLE_2D_WithMat("_ShadowColorTex", SamplerStateInformation.LinearClampSampler, @@ -297,14 +297,14 @@ public override void GetMaterialInformation(MaterialInformationCallback matInfo) } } - if (matInfo.GetInteger("_UseRimShade") != 0) + if (matInfo.GetInt("_UseRimShade") != 0) { var samp = "_MainTex"; LIL_SAMPLE_2D_WithMat("_RimShadeMask", samp, uvMain, uvMainMatrix); } - if (matInfo.GetInteger("_UseReflection") != 0) + if (matInfo.GetInt("_UseReflection") != 0) { // TODO: research var samp = "_MainTex"; // or SamplerStateInformation.LinearRepeatSampler in lil_pass_foreward_reblur.hlsl @@ -315,45 +315,45 @@ public override void GetMaterialInformation(MaterialInformationCallback matInfo) } // Matcap - if (matInfo.GetInteger("_UseMatCap") != 0) + if (matInfo.GetInt("_UseMatCap") != 0) { var samp = "_MainTex"; // caller of lilGetMatCap LIL_SAMPLE_2D("_MatCapTex", SamplerStateInformation.LinearRepeatSampler, UsingUVChannels.NonMesh); LIL_SAMPLE_2D_ST_WithMat("_MatCapBlendMask", samp, uvMain, uvMainMatrix); - if (matInfo.GetInteger("_MatCapCustomNormal") != 0) + if (matInfo.GetInt("_MatCapCustomNormal") != 0) { LIL_SAMPLE_2D_ST_WithMat("_MatCapBumpMap", samp, uvMain, uvMainMatrix); } } - if (matInfo.GetInteger("_UseMatCap2nd") != 0) + if (matInfo.GetInt("_UseMatCap2nd") != 0) { var samp = "_MainTex"; // caller of lilGetMatCap LIL_SAMPLE_2D("_MatCap2ndTex", SamplerStateInformation.LinearRepeatSampler, UsingUVChannels.NonMesh); LIL_SAMPLE_2D_ST_WithMat("_MatCap2ndBlendMask", samp, uvMain, uvMainMatrix); - if (matInfo.GetInteger("_MatCap2ndCustomNormal") != 0) + if (matInfo.GetInt("_MatCap2ndCustomNormal") != 0) { LIL_SAMPLE_2D_ST_WithMat("_MatCap2ndBumpMap", samp, uvMain, uvMainMatrix); } } // rim light - if (matInfo.GetInteger("_UseRim") != 0) + if (matInfo.GetInt("_UseRim") != 0) { var samp = "_MainTex"; // caller of lilGetRim LIL_SAMPLE_2D_ST_WithMat("_RimColorTex", samp, uvMain, uvMainMatrix); } - if (matInfo.GetInteger("_UseGlitter") != 0) + if (matInfo.GetInt("_UseGlitter") != 0) { var samp = "_MainTex"; // caller of lilGetGlitter LIL_SAMPLE_2D_ST_WithMat("_GlitterColorTex", samp, uvMain, uvMainMatrix); - if (matInfo.GetInteger("_GlitterApplyShape") != 0) + if (matInfo.GetInt("_GlitterApplyShape") != 0) { // complex uv LIL_SAMPLE_2D_GRAD("_GlitterShapeTex", SamplerStateInformation.LinearClampSampler, @@ -361,11 +361,11 @@ public override void GetMaterialInformation(MaterialInformationCallback matInfo) } } - if (matInfo.GetInteger("_UseEmission") != 0) + if (matInfo.GetInt("_UseEmission") != 0) { UsingUVChannels emissionUV = UsingUVChannels.UV0; - switch (matInfo.GetInteger("_EmissionMap_UVMode")) + switch (matInfo.GetInt("_EmissionMap_UVMode")) { case 1: emissionUV = UsingUVChannels.UV1; @@ -403,17 +403,17 @@ public override void GetMaterialInformation(MaterialInformationCallback matInfo) LIL_GET_EMIMASK_WithMat("_EmissionBlendMask", uvMain, uvMainMatrix); } - if (matInfo.GetInteger("_EmissionUseGrad") != 0) + if (matInfo.GetInt("_EmissionUseGrad") != 0) { LIL_SAMPLE_1D("_EmissionGradTex", SamplerStateInformation.LinearRepeatSampler, UsingUVChannels.NonMesh); } } - if (matInfo.GetInteger("_UseEmission2nd") != 0) + if (matInfo.GetInt("_UseEmission2nd") != 0) { UsingUVChannels emission2ndUV = UsingUVChannels.UV0; - switch (matInfo.GetInteger("_Emission2ndMap_UVMode")) + switch (matInfo.GetInt("_Emission2ndMap_UVMode")) { case 1: emission2ndUV = UsingUVChannels.UV1; @@ -453,28 +453,28 @@ public override void GetMaterialInformation(MaterialInformationCallback matInfo) LIL_GET_EMIMASK_WithMat("_Emission2ndBlendMask", uvMain, uvMainMatrix); } - if (matInfo.GetInteger("_Emission2ndUseGrad") != 0) + if (matInfo.GetInt("_Emission2ndUseGrad") != 0) { LIL_SAMPLE_1D("_Emission2ndGradTex", SamplerStateInformation.LinearRepeatSampler, UsingUVChannels.NonMesh); } } - if (matInfo.GetInteger("_UseParallax") != 0) + if (matInfo.GetInt("_UseParallax") != 0) { matInfo.RegisterTextureUVUsage("_ParallaxMap", SamplerStateInformation.LinearRepeatSampler, UsingUVChannels.UV0, null); } - if (matInfo.GetInteger("_UseAudioLink") != 0 && matInfo.GetInteger("_AudioLink2Vertex") != 0) + if (matInfo.GetInt("_UseAudioLink") != 0 && matInfo.GetInt("_AudioLink2Vertex") != 0) { - var _AudioLinkUVMode = matInfo.GetInteger("_AudioLinkUVMode"); + var _AudioLinkUVMode = matInfo.GetInt("_AudioLinkUVMode"); if (_AudioLinkUVMode is 3 or 4 or null) { // TODO: _AudioLinkMask_ScrollRotate var sampler = "_AudioLinkMask" | SamplerStateInformation.LinearRepeatSampler; - switch (matInfo.GetInteger("_AudioLinkMask_UVMode")) + switch (matInfo.GetInt("_AudioLinkMask_UVMode")) { case 0: default: @@ -518,7 +518,7 @@ public override void GetMaterialInformation(MaterialInformationCallback matInfo) ); } - if (matInfo.GetInteger("_UseOutline") != 0) + if (matInfo.GetInt("_UseOutline") != 0) { // not on material side, on editor side toggle LIL_SAMPLE_2D_WithMat("_OutlineTex", "_OutlineTex", uvMain, uvMainMatrix); @@ -527,7 +527,7 @@ public override void GetMaterialInformation(MaterialInformationCallback matInfo) // _OutlineVectorTex SamplerStateInformation.LinearRepeatSampler // UVs _OutlineVectorUVMode main,1,2,3 - switch (matInfo.GetInteger("_AudioLinkMask_UVMode")) + switch (matInfo.GetInt("_AudioLinkMask_UVMode")) { case 0: LIL_SAMPLE_2D_WithMat("_OutlineVectorTex", SamplerStateInformation.LinearRepeatSampler, uvMain, @@ -566,7 +566,7 @@ public override void GetMaterialInformation(MaterialInformationCallback matInfo) "_IDMaskPrior1", "_IDMaskPrior2", "_IDMaskPrior3", "_IDMaskPrior4", "_IDMaskPrior5", "_IDMaskPrior6", "_IDMaskPrior7", "_IDMaskPrior8", "_IDMaskIsBitmap", "_IDMaskCompile" }; - if (idMaskProperties.Any(prop => matInfo.GetInteger(prop) != 0)) + if (idMaskProperties.Any(prop => matInfo.GetInt(prop) != 0)) { // with _IDMaskFrom = 0..7, uv is used for ID Mask, but it will only use integer part // (cast to int with normal rounding in hlsl) so it's not necessary to register UV usage. diff --git a/Editor/Processors/ShaderMaterialInformation.cs b/Editor/Processors/ShaderMaterialInformation.cs index 1ac58edc..6eb81c66 100644 --- a/Editor/Processors/ShaderMaterialInformation.cs +++ b/Editor/Processors/ShaderMaterialInformation.cs @@ -126,6 +126,9 @@ public MaterialInformationCallbackImpl(Material material, ShaderInformationKind public override int? GetInteger(string propertyName, bool considerAnimation = true) => GetValue(propertyName, _material.SafeGetInteger, considerAnimation); + public override int? GetInt(string propertyName, bool considerAnimation = true) => + GetValue(propertyName, _material.SafeGetInt, considerAnimation); + public override float? GetFloat(string propertyName, bool considerAnimation = true) => GetValue(propertyName, _material.SafeGetFloat, considerAnimation); diff --git a/Internal/Utils/Utils.cs b/Internal/Utils/Utils.cs index bd54760e..0f4ecf82 100644 --- a/Internal/Utils/Utils.cs +++ b/Internal/Utils/Utils.cs @@ -380,6 +380,9 @@ public static float SafeGetFloat(this Material material, string propertyName) => public static int SafeGetInteger(this Material material, string propertyName) => material.HasProperty(propertyName) ? material.GetInteger(propertyName) : 0; + public static int SafeGetInt(this Material material, string propertyName) => + material.HasProperty(propertyName) ? material.GetInt(propertyName) : 0; + public static Vector4 SafeGetVector(this Material material, string propertyName) => material.HasProperty(propertyName) ? material.GetVector(propertyName) : Vector4.zero; From d3071a33a7ce9f84f2d7fcedcf3d6dd40480b3ba Mon Sep 17 00:00:00 2001 From: anatawa12 Date: Tue, 29 Oct 2024 10:28:15 +0900 Subject: [PATCH 2/4] chore: improve SafeGet check --- Internal/Utils/Utils.cs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Internal/Utils/Utils.cs b/Internal/Utils/Utils.cs index 0f4ecf82..50076f0c 100644 --- a/Internal/Utils/Utils.cs +++ b/Internal/Utils/Utils.cs @@ -375,19 +375,19 @@ public static void Assert(bool condition, string message) } public static float SafeGetFloat(this Material material, string propertyName) => - material.HasProperty(propertyName) ? material.GetFloat(propertyName) : 0; + material.HasFloat(propertyName) ? material.GetFloat(propertyName) : 0; public static int SafeGetInteger(this Material material, string propertyName) => - material.HasProperty(propertyName) ? material.GetInteger(propertyName) : 0; + material.HasInteger(propertyName) ? material.GetInteger(propertyName) : 0; public static int SafeGetInt(this Material material, string propertyName) => - material.HasProperty(propertyName) ? material.GetInt(propertyName) : 0; + material.HasInt(propertyName) ? material.GetInt(propertyName) : 0; public static Vector4 SafeGetVector(this Material material, string propertyName) => - material.HasProperty(propertyName) ? material.GetVector(propertyName) : Vector4.zero; + material.HasVector(propertyName) ? material.GetVector(propertyName) : Vector4.zero; public static Color SafeGetColor(this Material material, string propertyName) => - material.HasProperty(propertyName) ? material.GetColor(propertyName) : Color.clear; + material.HasColor(propertyName) ? material.GetColor(propertyName) : Color.clear; /// /// Fast compare of array. From f7e0d78baec86cc6304017a9fe20a9f0df40848f Mon Sep 17 00:00:00 2001 From: anatawa12 Date: Tue, 29 Oct 2024 10:31:08 +0900 Subject: [PATCH 3/4] chore: improve animation consideration in shader material information --- Editor/Processors/ShaderMaterialInformation.cs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/Editor/Processors/ShaderMaterialInformation.cs b/Editor/Processors/ShaderMaterialInformation.cs index 6eb81c66..fdac7efc 100644 --- a/Editor/Processors/ShaderMaterialInformation.cs +++ b/Editor/Processors/ShaderMaterialInformation.cs @@ -110,7 +110,8 @@ public MaterialInformationCallbackImpl(Material material, ShaderInformationKind public Shader Shader => _material.shader; - private T? GetValue(string propertyName, Func computer, bool considerAnimation) where T : struct + private T? GetValue(string propertyName, Func computer, bool considerAnimation, + string[]? subProperties = null) where T : struct { // animated; return null if (considerAnimation) @@ -118,6 +119,12 @@ public MaterialInformationCallbackImpl(Material material, ShaderInformationKind var animationProperty = $"material.{propertyName}"; if (_infos.Any(x => x.GetFloatNode(animationProperty).ComponentNodes.Any())) return null; + foreach (var subProperty in subProperties ?? Array.Empty()) + { + var subAnimationProperty = $"material.{propertyName}.{subProperty}"; + if (_infos.Any(x => x.GetFloatNode(subAnimationProperty).ComponentNodes.Any())) + return null; + } } return computer(propertyName); @@ -129,11 +136,13 @@ public MaterialInformationCallbackImpl(Material material, ShaderInformationKind public override int? GetInt(string propertyName, bool considerAnimation = true) => GetValue(propertyName, _material.SafeGetInt, considerAnimation); + private static readonly string[] VectorSubProperties = new[] { "r", "g", "b", "a", "x", "y", "z", "w" }; + public override float? GetFloat(string propertyName, bool considerAnimation = true) => - GetValue(propertyName, _material.SafeGetFloat, considerAnimation); + GetValue(propertyName, _material.SafeGetFloat, considerAnimation, VectorSubProperties); public override Vector4? GetVector(string propertyName, bool considerAnimation = true) => - GetValue(propertyName, _material.SafeGetVector, considerAnimation); + GetValue(propertyName, _material.SafeGetVector, considerAnimation, VectorSubProperties); public override void RegisterOtherUVUsage(UsingUVChannels uvChannel) { From fe37910e88451b0b3c661fe9761a92b91d47faef Mon Sep 17 00:00:00 2001 From: anatawa12 Date: Tue, 29 Oct 2024 10:32:16 +0900 Subject: [PATCH 4/4] docs(changelog): Integer and Int confusion --- CHANGELOG-PRERELEASE.md | 1 + CHANGELOG.md | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG-PRERELEASE.md b/CHANGELOG-PRERELEASE.md index 803fb3a6..6ba56e8a 100644 --- a/CHANGELOG-PRERELEASE.md +++ b/CHANGELOG-PRERELEASE.md @@ -18,6 +18,7 @@ The format is based on [Keep a Changelog]. ### Fixed - Fix non-VRChat project support `#1310` - 'shader' doesn't have a float or range property 'prop' error `#1312` +- Integer and Int confusion `#1313` ### Security diff --git a/CHANGELOG.md b/CHANGELOG.md index 28e49f0f..0690bd47 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -14,7 +14,7 @@ The format is based on [Keep a Changelog]. - We may relax some restriction in the future. - Because we have to check for each condition if we use AnyState but we can check for only one (in best case) with entry/exit, this generally reduces cost for checking an parameter in a state. - Combined with Entry / Exit to 1D BlendTree optimization, which is implemented in previous release, your AnyState layer may be optimized to 1D BlendTree. -- Optimize Texture in Trace nad Optimize `#1181` `#1184` `#1193` `#1215` `#1225` `#1235` `#1268` `#1278` +- Optimize Texture in Trace nad Optimize `#1181` `#1184` `#1193` `#1215` `#1225` `#1235` `#1268` `#1278` `#1313` - Avatar Optimizer will pack texture and tries to reduce the VRAM usage. - Currently liltoon is only supported. - `Copy Enablement Animation` to Merge Skinned Mesh `#1173`