generated from vrchat-community/template-package
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: MeshRenderers have incorrect scale (#311)
Closes: #309
- Loading branch information
1 parent
1251a75
commit 88d1f7f
Showing
5 changed files
with
184 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,162 @@ | ||
using System.Collections.Generic; | ||
using UnityEditor; | ||
using UnityEngine; | ||
using UnityEngine.SceneManagement; | ||
|
||
namespace nadena.dev.ndmf.preview | ||
{ | ||
// This is basically the same as ScaleAdjustedBones in ModularAvatar with slight changes, but I'm not ready to | ||
// create a public API just yet, so copied here... | ||
internal class ShadowBoneManager | ||
{ | ||
internal static ShadowBoneManager Instance { get; } = new(); | ||
|
||
private static int editorFrameCount = 0; | ||
private static int lastUpdateFrame = 0; | ||
private static int lastMutatingUpdate = 0; | ||
private static int mutatingUpdateCount = 0; | ||
|
||
[InitializeOnLoadMethod] | ||
static void Init() | ||
{ | ||
EditorApplication.update += () => editorFrameCount++; | ||
} | ||
|
||
internal class BoneState | ||
{ | ||
public Transform original; | ||
public Transform proxy; | ||
public int lastUsedFrame; | ||
public BoneState parentHint; | ||
} | ||
|
||
private Dictionary<Component, BoneState> _bones = new(new ObjectIdentityComparer<Component>()); | ||
//private List<BoneState> _states = new List<BoneState>(); | ||
|
||
public void Clear() | ||
{ | ||
foreach (var state in _bones.Values) | ||
{ | ||
if (state.proxy != null) Object.DestroyImmediate(state.proxy.gameObject); | ||
} | ||
|
||
_bones.Clear(); | ||
} | ||
|
||
public BoneState GetBone(Transform src, bool force = true) | ||
{ | ||
if (src == null) return null; | ||
|
||
if (_bones.TryGetValue(src, out var state)) | ||
{ | ||
state.lastUsedFrame = mutatingUpdateCount; | ||
return state; | ||
} | ||
|
||
if (!force) return null; | ||
|
||
var proxyObj = new GameObject(src.name); | ||
proxyObj.hideFlags = HideFlags.HideAndDontSave; | ||
proxyObj.AddComponent<SelfDestructComponent>().KeepAlive = this; | ||
|
||
#if MODULAR_AVATAR_DEBUG_HIDDEN | ||
proxyObj.hideFlags = HideFlags.DontSave; | ||
#endif | ||
|
||
var boneState = new BoneState(); | ||
boneState.original = src; | ||
boneState.proxy = proxyObj.transform; | ||
boneState.parentHint = null; | ||
boneState.lastUsedFrame = Time.frameCount; | ||
|
||
_bones[src] = boneState; | ||
|
||
CheckParent(CopyState(boneState), boneState); | ||
|
||
return boneState; | ||
} | ||
|
||
private List<Component> toRemove = new List<Component>(); | ||
private List<BoneState> stateList = new List<BoneState>(); | ||
|
||
public void Update() | ||
{ | ||
if (lastUpdateFrame == editorFrameCount) | ||
{ | ||
return; | ||
} | ||
|
||
lastUpdateFrame = editorFrameCount; | ||
|
||
if (lastMutatingUpdate != editorFrameCount) | ||
{ | ||
mutatingUpdateCount++; | ||
lastMutatingUpdate = editorFrameCount; | ||
} | ||
|
||
toRemove.Clear(); | ||
|
||
stateList.Clear(); | ||
stateList.AddRange(_bones.Values); | ||
|
||
foreach (var entry in stateList) | ||
{ | ||
if (entry.original == null || entry.proxy == null) | ||
{ | ||
if (entry.proxy != null) | ||
{ | ||
Object.DestroyImmediate(entry.proxy.gameObject); | ||
} | ||
|
||
toRemove.Add(entry.original); | ||
continue; | ||
} | ||
|
||
if (mutatingUpdateCount - entry.lastUsedFrame > 5 && entry.proxy.childCount == 0) | ||
{ | ||
Object.DestroyImmediate(entry.proxy.gameObject); | ||
toRemove.Add(entry.original); | ||
continue; | ||
} | ||
|
||
if (entry.original.gameObject.scene != entry.proxy.gameObject.scene && | ||
entry.proxy.transform.parent == null) | ||
{ | ||
SceneManager.MoveGameObjectToScene(entry.proxy.gameObject, entry.original.gameObject.scene); | ||
} | ||
|
||
Transform parent = CopyState(entry); | ||
|
||
CheckParent(parent, entry); | ||
} | ||
|
||
foreach (var remove in toRemove) | ||
{ | ||
_bones.Remove(remove); | ||
} | ||
} | ||
|
||
private void CheckParent(Transform parent, BoneState entry) | ||
{ | ||
if (parent != entry.parentHint?.original) | ||
{ | ||
entry.parentHint = GetBone(parent); | ||
entry.proxy.SetParent(entry.parentHint?.proxy, false); | ||
} | ||
} | ||
|
||
private static Transform CopyState(BoneState entry) | ||
{ | ||
Transform parent; | ||
var t = entry.original; | ||
|
||
parent = t.parent; | ||
|
||
entry.proxy.localPosition = t.localPosition; | ||
entry.proxy.localRotation = t.localRotation; | ||
entry.proxy.localScale = t.localScale; | ||
|
||
return parent; | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.