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: AutomaticConfiguration #265

Merged
merged 18 commits into from
Jul 17, 2023
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
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
anatawa12 marked this conversation as resolved.
Show resolved Hide resolved
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions .docs/content/docs/reference/automatic-configuration/index.ja.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
title: Automatic Configuration
weight: 1
---

# Automatic Configuration

このコンポーネントは、AvatarOptimizerを自動的に設定します。
Sayamame-beans marked this conversation as resolved.
Show resolved Hide resolved

現在、変更されていないBlendShapeを固定・除去するようにFreezeBlendShapeを設定します。
Sayamame-beans marked this conversation as resolved.
Show resolved Hide resolved

チェックボックスで自動設定する範囲を変更できます。
Sayamame-beans marked this conversation as resolved.
Show resolved Hide resolved

![component.png](component.png)
14 changes: 14 additions & 0 deletions .docs/content/docs/reference/automatic-configuration/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
---
title: Automatic Configuration
weight: 1
---

# Automatic Configuration

This component will configure AvatarOptimizer automatically.

Currently this will configure FreezeBlendShape for unchanged blendShapes.

You can enable/disable some automatic configuration features with checkboxes.

![component.png](component.png)
2 changes: 2 additions & 0 deletions CHANGELOG-PRERELEASE.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ The format is based on [Keep a Changelog].
## [Unreleased]
### Added
- Automatic bounds computation in MergeSkinnedMesh `#264`
- Automatic Configuration System `#265`
- Currently FreezeBlendShape can be automatically configured.

### Changed
- `#263`
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ The format is based on [Keep a Changelog].
## [Unreleased]
### Added
- Automatic bounds computation in MergeSkinnedMesh `#264`
- Automatic Configuration System `#265`
- Currently FreezeBlendShape can be automatically configured.

### Changed
- Support newly activated avatars in play mode for apply on play `#263`
Expand Down
33 changes: 33 additions & 0 deletions Editor/AutomaticConfiguration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
using UnityEditor;

namespace Anatawa12.AvatarOptimizer
{
[CustomEditor(typeof(AutomaticConfiguration))]
internal class AutomaticConfigurationEditor : AvatarGlobalComponentEditorBase
{
private SerializedProperty _freezeBlendShape;
private SerializedProperty _dontFreezeMmdShapes;

private void OnEnable()
{
_freezeBlendShape = serializedObject.FindProperty(nameof(AutomaticConfiguration.freezeBlendShape));
_dontFreezeMmdShapes = serializedObject.FindProperty(nameof(AutomaticConfiguration.dontFreezeMmdShapes));
}

protected override void OnInspectorGUIInner()
{
base.OnInspectorGUIInner();
serializedObject.UpdateIfRequiredOrScript();

EditorGUILayout.PropertyField(_freezeBlendShape);
if (_freezeBlendShape.boolValue)
{
EditorGUI.indentLevel++;
EditorGUILayout.PropertyField(_dontFreezeMmdShapes);
EditorGUI.indentLevel--;
}

serializedObject.ApplyModifiedProperties();
}
}
}
3 changes: 3 additions & 0 deletions Editor/AutomaticConfiguration.cs.meta

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

1 change: 1 addition & 0 deletions Editor/OptimizerProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ public static void ProcessObject(OptimizerSession session)

private static void DoProcessObject(OptimizerSession session)
{
new Processors.AutomaticConfigurationProcessor().Process(session);
new Processors.ClearEndpointPositionProcessor().Process(session);
new Processors.MergePhysBoneProcessor().Process(session);
new Processors.EditSkinnedMeshComponentProcessor().Process(session);
Expand Down
3 changes: 3 additions & 0 deletions Editor/Processors/AutomaticConfiguration.meta

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
using System.Linq;
using UnityEditor;
using UnityEngine;

namespace Anatawa12.AvatarOptimizer.Processors
{
partial class AutomaticConfigurationProcessor
{
private void AutoFreezeBlendShape()
{
foreach (var skinnedMeshRenderer in _session.GetComponents<SkinnedMeshRenderer>())
{
var mesh = skinnedMeshRenderer.sharedMesh;

// skip SMR without mesh
if (!mesh) continue;
// skip configured mesh
if (skinnedMeshRenderer.GetComponent<FreezeBlendShape>()) continue;

var modifies = GetModifiedProperties(skinnedMeshRenderer);
var blendShapeValues = Enumerable.Range(0, mesh.blendShapeCount)
.Select(i => skinnedMeshRenderer.GetBlendShapeWeight(i)).ToArray();
var notChanged = Enumerable.Range(0, mesh.blendShapeCount)
.Select(i => mesh.GetBlendShapeName(i))
.Where((name, i) =>
{
if (!modifies.TryGetValue($"blendShape.{name}", out var prop)) return true;

if (!prop.IsConst) return false;

if (prop.IsAlwaysApplied)
{
blendShapeValues[i] = prop.ConstValue;
return true;
}

return prop.ConstValue.CompareTo(blendShapeValues[i]) == 0;
})
.ToArray();

if (notChanged.Length == 0) continue;

for (var i = 0; i < blendShapeValues.Length; i++)
skinnedMeshRenderer.SetBlendShapeWeight(i, blendShapeValues[i]);
EditorUtility.SetDirty(skinnedMeshRenderer);

var freeze = skinnedMeshRenderer.gameObject.AddComponent<FreezeBlendShape>();
var serialized = new SerializedObject(freeze);
var editorUtil = PrefabSafeSet.EditorUtil<string>.Create(
serialized.FindProperty(nameof(FreezeBlendShape.shapeKeysSet)),
0, p => p.stringValue, (p, v) => p.stringValue = v);
foreach (var shape in notChanged)
editorUtil.GetElementOf(shape).EnsureAdded();
serialized.ApplyModifiedPropertiesWithoutUndo();
}
}
}
}

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

Loading