From 048ce04ebdca3a1314ffa8b60e467dedc9d3ce0d Mon Sep 17 00:00:00 2001 From: ousttrue Date: Tue, 15 Jun 2021 13:43:16 +0900 Subject: [PATCH] =?UTF-8?q?runtime=20=E5=A4=89=E6=95=B0=E7=BD=AE=E3=81=8D?= =?UTF-8?q?=E5=A0=B4=E3=82=92=E9=9A=94=E9=9B=A2=E3=81=97=E3=81=A6=E3=80=81?= =?UTF-8?q?=E5=88=9D=E6=9C=9F=E5=8C=96=E3=82=92=E9=81=85=E5=BB=B6=E3=80=82?= =?UTF-8?q?Editor=20=E7=94=A8=E3=81=AE=E5=A4=89=E6=95=B0=E3=81=AE=E5=88=9D?= =?UTF-8?q?=E6=9C=9F=E5=8C=96=E3=82=92=E5=88=86=E9=9B=A2=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Editor/Components/LookAt/LookAtEditor.cs | 25 +++--- .../Components/VRM10ControllerEditor.cs | 6 -- Assets/VRM10/Editor/Components/VRM10Window.cs | 8 +- .../Runtime/Components/VRM10Controller.cs | 78 ++----------------- .../Components/VRM10ControllerRuntime.cs | 68 ++++++++++++++++ .../Components/VRM10ControllerRuntime.cs.meta | 11 +++ 6 files changed, 104 insertions(+), 92 deletions(-) create mode 100644 Assets/VRM10/Runtime/Components/VRM10ControllerRuntime.cs create mode 100644 Assets/VRM10/Runtime/Components/VRM10ControllerRuntime.cs.meta diff --git a/Assets/VRM10/Editor/Components/LookAt/LookAtEditor.cs b/Assets/VRM10/Editor/Components/LookAt/LookAtEditor.cs index 588a99d74..c23794e1a 100644 --- a/Assets/VRM10/Editor/Components/LookAt/LookAtEditor.cs +++ b/Assets/VRM10/Editor/Components/LookAt/LookAtEditor.cs @@ -9,39 +9,38 @@ namespace UniVRM10 { } - - public static void Draw3D(VRM10Controller target) + + public static void Draw3D(VRM10Controller target, Transform head) { - if(target==null) + if (target == null) { return; } - OnSceneGUIOffset(target); + if (head == null) + { + return; + } + + OnSceneGUIOffset(target, head); if (!Application.isPlaying) { // offset var p = target.LookAt.OffsetFromHead; - Handles.Label(target.Head.position, $"fromHead: [{p.x:0.00}, {p.y:0.00}, {p.z:0.00}]"); + Handles.Label(head.position, $"fromHead: [{p.x:0.00}, {p.y:0.00}, {p.z:0.00}]"); } else { - target.LookAt.OnSceneGUILookAt(target.Head); + target.LookAt.OnSceneGUILookAt(head); } } - static void OnSceneGUIOffset(VRM10Controller m_target) + static void OnSceneGUIOffset(VRM10Controller m_target, Transform head) { if (!m_target.LookAt.DrawGizmo) { return; } - var head = m_target.Head; - if (head == null) - { - return; - } - EditorGUI.BeginChangeCheck(); var worldOffset = head.localToWorldMatrix.MultiplyPoint(m_target.LookAt.OffsetFromHead); diff --git a/Assets/VRM10/Editor/Components/VRM10ControllerEditor.cs b/Assets/VRM10/Editor/Components/VRM10ControllerEditor.cs index e696cf205..5d1870d1e 100644 --- a/Assets/VRM10/Editor/Components/VRM10ControllerEditor.cs +++ b/Assets/VRM10/Editor/Components/VRM10ControllerEditor.cs @@ -75,12 +75,6 @@ namespace UniVRM10 EditorGUILayout.Separator(); } - // Setup runtime function. - if (UnityEngine.Application.isPlaying) - { - m_target.Setup(); - } - serializedObject.Update(); switch (_tab) { diff --git a/Assets/VRM10/Editor/Components/VRM10Window.cs b/Assets/VRM10/Editor/Components/VRM10Window.cs index 8bdae96b7..c82e848ff 100644 --- a/Assets/VRM10/Editor/Components/VRM10Window.cs +++ b/Assets/VRM10/Editor/Components/VRM10Window.cs @@ -62,11 +62,15 @@ namespace UniVRM10 } m_root = id; m_so = value != null ? new SerializedObject(value) : null; - m_constraints = null; + + var animator = Root.GetComponent(); + m_head = animator.GetBoneTransform(HumanBodyBones.Head); } } + Transform m_head; + public VRM10Constraint[] m_constraints; ScrollView m_scrollView = new ScrollView(); @@ -105,7 +109,7 @@ namespace UniVRM10 case VRMSceneUI.LookAt: Tools.hidden = true; - LookAtEditor.Draw3D(Root); + LookAtEditor.Draw3D(Root, m_head); break; case VRMSceneUI.SpringBone: diff --git a/Assets/VRM10/Runtime/Components/VRM10Controller.cs b/Assets/VRM10/Runtime/Components/VRM10Controller.cs index bd78a6cf7..9d3c3064d 100644 --- a/Assets/VRM10/Runtime/Components/VRM10Controller.cs +++ b/Assets/VRM10/Runtime/Components/VRM10Controller.cs @@ -58,27 +58,6 @@ namespace UniVRM10 } } - VRM10Constraint[] m_constraints; - - Transform m_head; - public Transform Head - { - get - { - if (m_head == null) - { - m_head = GetComponent().GetBoneTransform(HumanBodyBones.Head); - } - return m_head; - } - } - - void Reset() - { - var animator = GetComponent(); - m_head = animator.GetBoneTransform(HumanBodyBones.Head); - } - private void OnValidate() { if (LookAt != null) @@ -90,65 +69,22 @@ namespace UniVRM10 } } - public void Setup() - { - var animator = GetComponent(); - if (animator == null) return; + VRM10ControllerRuntime m_runtime; - m_head = animator.GetBoneTransform(HumanBodyBones.Head); - LookAt.Setup(animator, m_head); - Expression.Setup(transform, LookAt, LookAt.EyeDirectionApplicable); - } - - /// - /// 毎フレーム関連コンポーネントを解決する - /// - /// * Contraint - /// * Spring - /// * LookAt - /// * Expression - /// - /// - public void Process() + VRM10ControllerRuntime GetOrCreate() { - // - // constraint - // - if (m_constraints == null) + if (m_runtime == null) { - m_constraints = GetComponentsInChildren(); + m_runtime = new VRM10ControllerRuntime(this); } - foreach (var constraint in m_constraints) - { - constraint.Process(); - } - - // - // spring - // - SpringBone.Process(Controller.SpringBoneCenter); - - // - // gaze control - // - LookAt.Process(); - - // - // expression - // - Expression.Process(); - } - - private void Start() - { - Setup(); + return m_runtime; } private void Update() { if (Controller.UpdateType == VRM10ControllerImpl.UpdateTypes.Update) { - Process(); + GetOrCreate().Process(); } } @@ -156,7 +92,7 @@ namespace UniVRM10 { if (Controller.UpdateType == VRM10ControllerImpl.UpdateTypes.LateUpdate) { - Process(); + GetOrCreate().Process(); } } } diff --git a/Assets/VRM10/Runtime/Components/VRM10ControllerRuntime.cs b/Assets/VRM10/Runtime/Components/VRM10ControllerRuntime.cs new file mode 100644 index 000000000..e9362504c --- /dev/null +++ b/Assets/VRM10/Runtime/Components/VRM10ControllerRuntime.cs @@ -0,0 +1,68 @@ +using System; +using UnityEngine; + +namespace UniVRM10 +{ + /// + /// Play時 と Editorからの参照情報置き場 + /// + class VRM10ControllerRuntime + { + VRM10Controller m_target; + VRM10Constraint[] m_constraints; + Transform m_head; + + public VRM10ControllerRuntime(VRM10Controller target) + { + m_target = target; + var animator = target.GetComponent(); + if (animator == null) + { + throw new Exception(); + } + m_head = animator.GetBoneTransform(HumanBodyBones.Head); + target.LookAt.Setup(animator, m_head); + target.Expression.Setup(target.transform, target.LookAt, target.LookAt.EyeDirectionApplicable); + + if (m_constraints == null) + { + m_constraints = target.GetComponentsInChildren(); + } + } + + /// + /// 毎フレーム関連コンポーネントを解決する + /// + /// * Contraint + /// * Spring + /// * LookAt + /// * Expression + /// + /// + public void Process() + { + // + // constraint + // + foreach (var constraint in m_constraints) + { + constraint.Process(); + } + + // + // spring + // + m_target.SpringBone.Process(m_target.Controller.SpringBoneCenter); + + // + // gaze control + // + m_target.LookAt.Process(); + + // + // expression + // + m_target.Expression.Process(); + } + } +} diff --git a/Assets/VRM10/Runtime/Components/VRM10ControllerRuntime.cs.meta b/Assets/VRM10/Runtime/Components/VRM10ControllerRuntime.cs.meta new file mode 100644 index 000000000..434fc26fb --- /dev/null +++ b/Assets/VRM10/Runtime/Components/VRM10ControllerRuntime.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: ebbe3112f524c99438364defdc03e140 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: