Skip to content

Commit

Permalink
Merge pull request #1346 from anatawa12/more-shader-support-for-optim…
Browse files Browse the repository at this point in the history
…ize-texture

More shader support for optimize texture
  • Loading branch information
anatawa12 authored Nov 15, 2024
2 parents 500ef3c + 905b7c3 commit 172f364
Show file tree
Hide file tree
Showing 8 changed files with 156 additions and 1 deletion.
8 changes: 8 additions & 0 deletions API-Editor/ShaderInformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -233,6 +233,14 @@ internal MaterialInformationCallback()
[PublicAPI]
public abstract Vector4? GetVector(string propertyName, bool considerAnimation = true);

/// <summary>
/// Returns if the local Shader Keyword is enabled or not.
/// </summary>
/// <param name="keywordName">The name of local shader keyword</param>
/// <returns>true if the local shader keyword is enabled, false if disabled, null if unknown or mixed.</returns>
[PublicAPI]
public abstract bool? IsShaderKeywordEnabled(string keywordName);

/// <summary>
/// Registers UV Usage that are not considered by Avatar Optimizer.
///
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG-PRERELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ The format is based on [Keep a Changelog].

## [Unreleased]
### Added
- Optimize Texture support for Unity Standard, VRChat SDK Standard Lite, VRChat SDK Toon Lit Shaders `#1346`
- If you want more shader support, please comment to [`#1183`](https://github.com/anatawa12/AvatarOptimizer/issues/1183) with shader name and link!

### Changed
- Make error for MergeBone with MergePB rotation mode fix `#1345`
Expand Down
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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` `#1313` `#1328` `#1338` `#1334`
- Optimize Texture in Trace nad Optimize `#1181` `#1184` `#1193` `#1215` `#1225` `#1235` `#1268` `#1278` `#1313` `#1328` `#1338` `#1334` `#1346`
- 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`
Expand Down
61 changes: 61 additions & 0 deletions Editor/APIInternal/ShaderInformation.Standard.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
using Anatawa12.AvatarOptimizer.API;
using UnityEditor;
using UnityEngine;

namespace Anatawa12.AvatarOptimizer.APIInternal;

// Unity builtin "Standard" shader
// https://github.com/TwoTailsGames/Unity-Built-in-Shaders/blob/master/DefaultResourcesExtra/Standard.shader
[InitializeOnLoad]
class StandardShaderInformation : ShaderInformation
{
static StandardShaderInformation()
{
Register();
}

private static void Register()
{
var information = new StandardShaderInformation();
if (!GlobalObjectId.TryParse("GlobalObjectId_V1-4-0000000000000000f000000000000000-46-0", out var id)) return;
var shader = GlobalObjectId.GlobalObjectIdentifierToObjectSlow(id) as Shader;
if (shader == null) return;
ShaderInformationRegistry.RegisterShaderInformation(shader, information);
}

public override ShaderInformationKind SupportedInformationKind =>
ShaderInformationKind.VertexIndexUsage | ShaderInformationKind.TextureAndUVUsage;
public override void GetMaterialInformation(MaterialInformationCallback matInfo)
{
var mainTexST = matInfo.GetVector("_MainTex_ST");
Matrix2x3? mainTexSTMat = mainTexST is { } st ? Matrix2x3.NewScaleOffset(st) : null;
var mainTexSTMatParallex = mainTexSTMat;
if (matInfo.IsShaderKeywordEnabled("_PARALLAXMAP") != false)
mainTexSTMat = null;

matInfo.RegisterTextureUVUsage("_ParallaxMap", "_ParallaxMap", UsingUVChannels.UV0, mainTexSTMatParallex);

matInfo.RegisterTextureUVUsage("_MainTex", "_MainTex", UsingUVChannels.UV0, mainTexSTMat);
matInfo.RegisterTextureUVUsage("_MetallicGlossMap", "_MetallicGlossMap", UsingUVChannels.UV0, mainTexSTMat);
matInfo.RegisterTextureUVUsage("_BumpMap", "_BumpMap", UsingUVChannels.UV0, mainTexSTMat);
matInfo.RegisterTextureUVUsage("_OcclusionMap", "_OcclusionMap", UsingUVChannels.UV0, mainTexSTMat);
matInfo.RegisterTextureUVUsage("_EmissionMap", "_EmissionMap", UsingUVChannels.UV0, mainTexSTMat);
matInfo.RegisterTextureUVUsage("_DetailMask", "_DetailMask", UsingUVChannels.UV0, mainTexSTMat);

var detailMapST = matInfo.GetVector("_DetailAlbedoMap_ST");
Matrix2x3? detailMapSTMat = detailMapST is { } st2 ? Matrix2x3.NewScaleOffset(st2) : null;

if (matInfo.IsShaderKeywordEnabled("_PARALLAXMAP") != false)
detailMapSTMat = null;

var detailMapUV = matInfo.GetFloat("_UVSec") switch
{
null => UsingUVChannels.UV0 | UsingUVChannels.UV1,
0 => UsingUVChannels.UV0,
_ => UsingUVChannels.UV1,
};

matInfo.RegisterTextureUVUsage("_DetailAlbedoMap", "_DetailAlbedoMap", detailMapUV, detailMapSTMat);
matInfo.RegisterTextureUVUsage("_DetailNormalMap", "_DetailNormalMap", detailMapUV, detailMapSTMat);
}
}
3 changes: 3 additions & 0 deletions Editor/APIInternal/ShaderInformation.Standard.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

75 changes: 75 additions & 0 deletions Editor/APIInternal/ShaderInformation.VRCSDK.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
using Anatawa12.AvatarOptimizer.API;
using UnityEditor;
using UnityEngine;

namespace Anatawa12.AvatarOptimizer.APIInternal;

// VRChat SDK Mobile Shaders

[InitializeOnLoad]
class VRCSDKStandardLiteShaderInformation : ShaderInformation
{
static VRCSDKStandardLiteShaderInformation()
{
Register();
}

private static void Register()
{
var information = new VRCSDKStandardLiteShaderInformation();
ShaderInformationRegistry.RegisterShaderInformationWithGUID("0b7113dea2069fc4e8943843eff19f70", information);
}

public override ShaderInformationKind SupportedInformationKind =>
ShaderInformationKind.VertexIndexUsage | ShaderInformationKind.TextureAndUVUsage;
public override void GetMaterialInformation(MaterialInformationCallback matInfo)
{
var mainTexST = matInfo.GetVector("_MainTex_ST");
Matrix2x3? mainTexSTMat = mainTexST is { } st ? Matrix2x3.NewScaleOffset(st) : null;

matInfo.RegisterTextureUVUsage("_MetallicGlossMap", "_MetallicGlossMap", UsingUVChannels.UV0, mainTexSTMat);
matInfo.RegisterTextureUVUsage("_MainTex", "_MainTex", UsingUVChannels.UV0, mainTexSTMat);
matInfo.RegisterTextureUVUsage("_BumpMap", "_BumpMap", UsingUVChannels.UV0, mainTexSTMat);
matInfo.RegisterTextureUVUsage("_OcclusionMap", "_OcclusionMap", UsingUVChannels.UV0, mainTexSTMat);
matInfo.RegisterTextureUVUsage("_EmissionMap", "_EmissionMap", UsingUVChannels.UV0, mainTexSTMat);
matInfo.RegisterTextureUVUsage("_DetailMask", "_DetailMask", UsingUVChannels.UV0, mainTexSTMat);

var detailMapST = matInfo.GetVector("_DetailAlbedoMap_ST");
Matrix2x3? detailMapSTMat = detailMapST is { } st2 ? Matrix2x3.NewScaleOffset(st2) : null;
matInfo.RegisterTextureUVUsage("_DetailAlbedoMap", "_DetailAlbedoMap", UsingUVChannels.UV0, mainTexSTMat);

var detailMapUV = matInfo.GetFloat("_UVSec") switch
{
null => UsingUVChannels.UV0 | UsingUVChannels.UV1,
0 => UsingUVChannels.UV0,
_ => UsingUVChannels.UV1,
};

matInfo.RegisterTextureUVUsage("_DetailAlbedoMap", "_DetailAlbedoMap", detailMapUV, detailMapSTMat);
matInfo.RegisterTextureUVUsage("_DetailNormalMap", "_DetailNormalMap", detailMapUV, detailMapSTMat);
}
}

[InitializeOnLoad]
class VRCSDKToonLitShaderInformation : ShaderInformation
{
static VRCSDKToonLitShaderInformation()
{
Register();
}

private static void Register()
{
var information = new VRCSDKToonLitShaderInformation();
ShaderInformationRegistry.RegisterShaderInformationWithGUID("affc81f3d164d734d8f13053effb1c5c", information);
}

public override ShaderInformationKind SupportedInformationKind =>
ShaderInformationKind.VertexIndexUsage | ShaderInformationKind.TextureAndUVUsage;
public override void GetMaterialInformation(MaterialInformationCallback matInfo)
{
var mainTexST = matInfo.GetVector("_MainTex_ST");
Matrix2x3? mainTexSTMat = mainTexST is { } st ? Matrix2x3.NewScaleOffset(st) : null;
matInfo.RegisterTextureUVUsage("_MainTex", "_MainTex", UsingUVChannels.UV0, mainTexSTMat);
}
}
3 changes: 3 additions & 0 deletions Editor/APIInternal/ShaderInformation.VRCSDK.cs.meta

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Editor/Processors/ShaderMaterialInformation.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Anatawa12.AvatarOptimizer.ndmf;
using nadena.dev.ndmf;
using UnityEngine;
using UnityEngine.Rendering;

namespace Anatawa12.AvatarOptimizer.Processors;

Expand Down Expand Up @@ -144,6 +145,8 @@ public MaterialInformationCallbackImpl(Material material, ShaderInformationKind
public override Vector4? GetVector(string propertyName, bool considerAnimation = true) =>
GetValue(propertyName, _material.SafeGetVector, considerAnimation, VectorSubProperties);

public override bool? IsShaderKeywordEnabled(string keywordName) => _material.IsKeywordEnabled(keywordName);

public override void RegisterOtherUVUsage(UsingUVChannels uvChannel)
{
if ((_supportedKind & ShaderInformationKind.TextureAndUVUsage) == 0)
Expand Down

0 comments on commit 172f364

Please sign in to comment.