Skip to content

Commit

Permalink
Merge pull request #1181 from anatawa12/optimize-texture-as-possible
Browse files Browse the repository at this point in the history
feat: Optimize Texture (UV Packing)
  • Loading branch information
anatawa12 authored Sep 17, 2024
2 parents 092edb0 + 97beb8d commit 21ae4f7
Show file tree
Hide file tree
Showing 25 changed files with 2,856 additions and 7 deletions.
367 changes: 367 additions & 0 deletions API-Editor/ShaderInformation.cs

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions API-Editor/ShaderInformation.cs.meta

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

10 changes: 8 additions & 2 deletions Assets/merge-texture-helper.shader
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ Shader "Hidden/merge_texture_helper"
_MainTex ("Texture", 2D) = "white" {}
// x, y, w, h
_Rect ("Rectangle", Vector) = (0, 0, 1, 1)
_SrcRect ("SourceRectangle", Vector) = (0, 0, 1, 1)
_NoClip ("NoClip", Int) = 0
}
SubShader
{
Expand Down Expand Up @@ -35,6 +37,8 @@ Shader "Hidden/merge_texture_helper"
sampler2D _MainTex;
float4 _MainTex_ST;
float4 _Rect;
float4 _SrcRect;
int _NoClip;

v2f vert (appdata v)
{
Expand All @@ -47,8 +51,10 @@ Shader "Hidden/merge_texture_helper"

fixed4 frag (v2f i) : SV_Target
{
fixed4 c = tex2D(_MainTex, TRANSFORM_TEX(i.uv, _MainTex));
clip(c.a - 0.0001);
float2 uv = i.uv * _SrcRect.zw + _SrcRect.xy;
fixed4 c = tex2D(_MainTex, uv);
if (_NoClip == 0)
clip(c.a - 0.0001);
return c;
}
ENDCG
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG-PRERELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ 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`
- Avatar Optimizer will pack texture and tries to reduce the VRAM usage.

### Changed
- Skip Enablement Mismatched Renderers is now disabled by default `#1169`
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ 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`
- Avatar Optimizer will pack texture and tries to reduce the VRAM usage.

### Changed
- Skip Enablement Mismatched Renderers is now disabled by default `#1169`
Expand Down
789 changes: 789 additions & 0 deletions Editor/APIInternal/ShaderInformation.Liltoon.cs

Large diffs are not rendered by default.

3 changes: 3 additions & 0 deletions Editor/APIInternal/ShaderInformation.Liltoon.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/Inspector/TraceAndOptimizeEditor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ internal class TraceAndOptimizeEditor : AvatarGlobalComponentEditorBase
private SerializedProperty _optimizeAnimator = null!; // Initialized in OnEnable
private SerializedProperty _mergeSkinnedMesh = null!; // Initialized in OnEnable
private SerializedProperty _allowShuffleMaterialSlots = null!; // Initialized in OnEnable
private SerializedProperty _optimizeTexture = null!; // Initialized in OnEnable
private SerializedProperty _mmdWorldCompatibility = null!; // Initialized in OnEnable
private SerializedProperty _advancedSettings = null!; // Initialized in OnEnable
private GUIContent _advancedSettingsLabel = new GUIContent();
Expand All @@ -29,6 +30,7 @@ private void OnEnable()
_optimizeAnimator = serializedObject.FindProperty(nameof(TraceAndOptimize.optimizeAnimator));
_mergeSkinnedMesh = serializedObject.FindProperty(nameof(TraceAndOptimize.mergeSkinnedMesh));
_allowShuffleMaterialSlots = serializedObject.FindProperty(nameof(TraceAndOptimize.allowShuffleMaterialSlots));
_optimizeTexture = serializedObject.FindProperty(nameof(TraceAndOptimize.optimizeTexture));
_mmdWorldCompatibility = serializedObject.FindProperty(nameof(TraceAndOptimize.mmdWorldCompatibility));
_advancedSettings = serializedObject.FindProperty(nameof(TraceAndOptimize.advancedSettings));
}
Expand Down Expand Up @@ -58,6 +60,7 @@ protected override void OnInspectorGUIInner()
EditorGUILayout.PropertyField(_allowShuffleMaterialSlots);
EditorGUI.indentLevel--;
}
EditorGUILayout.PropertyField(_optimizeTexture);

_advancedSettingsLabel.text = AAOL10N.Tr("TraceAndOptimize:prop:advancedOptimization");
AdvancedOpened = EditorGUILayout.Foldout(AdvancedOpened, _advancedSettingsLabel);
Expand Down
2 changes: 2 additions & 0 deletions Editor/OptimizerPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ protected override void Configure()
})
.Then.Run("Validation", (ctx) => ComponentValidation.ValidateAll(ctx.AvatarRootObject))
.Then.Run(Processors.TraceAndOptimizes.LoadTraceAndOptimizeConfiguration.Instance)
.Then.Run(Processors.DupliacteAssets.Instance)
.Then.Run(Processors.ParseAnimator.Instance)
.Then.Run(Processors.TraceAndOptimizes.AddRemoveEmptySubMesh.Instance)
.Then.Run(Processors.TraceAndOptimizes.AutoFreezeBlendShape.Instance)
Expand All @@ -73,6 +74,7 @@ protected override void Configure()
.Then.Run(Processors.TraceAndOptimizes.ConfigureRemoveZeroSizedPolygon.Instance)
.Then.Run(Processors.MergeBoneProcessor.Instance)
.Then.Run(Processors.RemoveZeroSizedPolygonProcessor.Instance)
.Then.Run(Processors.TraceAndOptimizes.OptimizeTexture.Instance)
.Then.Run(Processors.AnimatorOptimizer.RemoveInvalidProperties.Instance)
;
});
Expand Down
98 changes: 98 additions & 0 deletions Editor/Processors/DupliacteAssets.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
using System;
using Anatawa12.AvatarOptimizer.ndmf;
using nadena.dev.ndmf;
using UnityEditor;
using UnityEditor.Animations;
using UnityEngine;
using Object = UnityEngine.Object;

namespace Anatawa12.AvatarOptimizer.Processors;

/// <summary>
/// The class to clone something for future in-place modification
///
/// Currently this class is intended to clone
/// </summary>
internal class DupliacteAssets : Pass<DupliacteAssets>
{
protected override void Execute(BuildContext context)
{
if (!context.GetState<AAOEnabled>().Enabled) return;

var cloner = new Cloner();

foreach (var component in context.GetComponents<Component>())
{
switch (component)
{
case SkinnedMeshRenderer renderer:
{
var meshInfo2 = context.GetMeshInfoFor(renderer);
foreach (var subMesh in meshInfo2.SubMeshes)
foreach (ref var material in subMesh.SharedMaterials.AsSpan())
material = cloner.MapObject(material);
}
break;
default:
{
using var serializedObject = new SerializedObject(component);

foreach (var objectReferenceProperty in serializedObject.ObjectReferenceProperties())
{
objectReferenceProperty.objectReferenceValue = cloner.MapObject(objectReferenceProperty.objectReferenceValue);
}

serializedObject.ApplyModifiedPropertiesWithoutUndo();

break;
}
}
}
}

class Cloner : DeepCloneHelper
{
protected override Object? CustomClone(Object o) => null;

protected override ComponentSupport GetComponentSupport(Object o)
{
switch (o)
{
// Target Objects
case Material:
return ComponentSupport.Clone;

// intermediate objects
case Motion:
case AnimatorController:
case AnimatorOverrideController:
case AnimatorState:
case AnimatorStateMachine:
case AnimatorTransitionBase:
case StateMachineBehaviour:

#if AAO_VRM0
case VRM.BlendShapeAvatar:
case VRM.BlendShapeClip:
#endif
#if AAO_VRM1
case UniVRM10.VRM10Object:
case UniVRM10.VRM10Expression:
#endif
return ComponentSupport.Clone;

case Texture:
case MonoScript:
case Component:
case GameObject:
return ComponentSupport.NoClone;

case ScriptableObject:
return ComponentSupport.NoClone;

default:
return ComponentSupport.NoClone;
}
}
}
}
3 changes: 3 additions & 0 deletions Editor/Processors/DupliacteAssets.cs.meta

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

Loading

0 comments on commit 21ae4f7

Please sign in to comment.