Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Optimize Texture (UV Packing) #1181

Merged
merged 30 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
499ed5f
feat(trace-and-optimize): initial commit for optimize texture
anatawa12 Sep 6, 2024
b3ed466
feat(shader-knowledge): add ShaderKnowledge for optimize texture
anatawa12 Sep 6, 2024
84bb23e
feat(optimize-texture): filter out materials
anatawa12 Sep 7, 2024
62d323b
chore(texture-optimizer): ban optimizing textures used by multiple se…
anatawa12 Sep 7, 2024
398d976
feat(optimize-texture): per-texture checking
anatawa12 Sep 8, 2024
3f4466b
feat(optimize-texture): implement basic algorithm for atlas
anatawa12 Sep 8, 2024
2747310
chore: clone material before processing / parsing material
anatawa12 Sep 9, 2024
7a7691d
feat: implement uv packing / atlas
anatawa12 Sep 9, 2024
9782a24
feat: duplicate vertex if used by multiple vertices
anatawa12 Sep 9, 2024
066030e
feat(optimize-texture): completely lossless per-block copying texture
anatawa12 Sep 10, 2024
eacf3eb
feat(optimize-texture): fix clipping
anatawa12 Sep 10, 2024
820c054
feat(optimize-texture): gave up implementing mipmap with block copying
anatawa12 Sep 10, 2024
6d69a42
chore: make DupliacteAssets internal
anatawa12 Sep 10, 2024
e74b5b4
feat(optimize-texture): use 1px texture if it's monotone
anatawa12 Sep 11, 2024
1aced2f
feat(optimize-texture): remove hard-coded texture formats for better …
anatawa12 Sep 11, 2024
77a0978
feat(internal): temporaryRenderTexture scope
anatawa12 Sep 11, 2024
cb07a8d
feat(optimize-texture): islands that is completely inside other islands
anatawa12 Sep 11, 2024
d846348
chore(optimize-texture): remove or implement small TODOs
anatawa12 Sep 11, 2024
b2786ea
refactor(optimize-texture): split to multiple functions
anatawa12 Sep 11, 2024
d66e30b
chore: change API for MaterialPropertyAnimationProvider
anatawa12 Sep 12, 2024
0e53101
feat(optimize-texture): initial full liltoon description
anatawa12 Sep 13, 2024
6264d17
feat(optimize-texture): (WIP) new API for shader information
anatawa12 Sep 14, 2024
25a47ea
feat(shader-knowledge): reimplement with new API
anatawa12 Sep 14, 2024
686aa62
fix: bugfixes in optimize texture
anatawa12 Sep 14, 2024
a794e8a
chore(shader-knowledge): remove traditional API
anatawa12 Sep 14, 2024
8739538
refactor: split function for future testing
anatawa12 Sep 16, 2024
2a423bf
chore: add minimal test for optimize texture
anatawa12 Sep 16, 2024
2f9fbc5
chore: create public api version of shader information
anatawa12 Sep 16, 2024
afc736a
refactor(shader-knowladge): rewrite with public api version of shader…
anatawa12 Sep 16, 2024
97beb8d
docs(changelog): Optimize Texture in Trace nad Optimize
anatawa12 Sep 16, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading