Skip to content

Commit

Permalink
chore: reset
Browse files Browse the repository at this point in the history
  • Loading branch information
saturday06 committed Oct 22, 2023
1 parent 00b39b4 commit a6467b3
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 42 deletions.
103 changes: 61 additions & 42 deletions Assets/VrmDowngrader/VrmDowngrader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ public class VrmDowngrader : MonoBehaviour
private Button SaveButton =>
_saveButton ??= GetComponent<UIDocument>().rootVisualElement.Q<Button>("SaveButton");

private Button? _resetButton;
private Button ResetButton =>
_resetButton ??= GetComponent<UIDocument>().rootVisualElement.Q<Button>("ResetButton");

private Label? _errorMessageLabel;
private Label ErrorMessageLabel =>
_errorMessageLabel ??= GetComponent<UIDocument>().rootVisualElement.Q<Label>(
Expand All @@ -33,21 +37,29 @@ public class VrmDowngrader : MonoBehaviour

private byte[]? _vrm0Bytes;

private bool _opening = false;

private async Task OnOpenButtonClicked(byte[] vrm1Bytes)
{
ErrorMessageLabel.text = "";
var logoLabel = GetComponent<UIDocument>().rootVisualElement.Q<Label>();
logoLabel.text = "";
OpenButton.text = "loading...";
await WebGL.WaitForNextFrame();

Debug.Log("開始");
var cancellationTokenSource = new CancellationTokenSource();
var cancellationToken = cancellationTokenSource.Token;
if (_opening)
{
return;
}
_opening = true;

Vrm10Instance vrm10Instance;
try
{
ErrorMessageLabel.text = "";
var logoLabel = GetComponent<UIDocument>().rootVisualElement.Q<Label>();
logoLabel.text = "";
OpenButton.text = "loading...";
await WebGL.WaitForNextFrame();

Debug.Log("開始");
var cancellationTokenSource = new CancellationTokenSource();
var cancellationToken = cancellationTokenSource.Token;

Vrm10Instance vrm10Instance;
vrm10Instance = await Vrm10.LoadBytesAsync(
vrm1Bytes,
canLoadVrm0X: false,
Expand All @@ -64,6 +76,36 @@ private async Task OnOpenButtonClicked(byte[] vrm1Bytes)
}
OpenButton.style.display = DisplayStyle.None;
Debug.Log("インポートはうまくいきました");

Debug.Log("VRM1のコンポーネントをVRM0で置換していきます");
// https://github.com/vrm-c/UniVRM/blob/7e052b19b3c0b4cd02e63159fc37db820729554e/Assets/VRM10/Runtime/Migration/MigrationVrmMeta.cs
var vrm0Meta = ScriptableObject.CreateInstance<VRMMetaObject>();
var vrm1Meta = vrm10Instance.Vrm.Meta;
Debug.Log("VRM1のコンポーネントをVRM0で置換していきます 1");
// var vrm0Meta = VRMMeta.
Debug.Log("VRM1のコンポーネントをVRM0で置換していきます 2");
vrm0Meta.Title = vrm1Meta.Name;
Debug.Log("VRM1のコンポーネントをVRM0で置換していきます 3");
vrm0Meta.Author = string.Join("/ ", vrm1Meta.Authors);
Debug.Log("VRM1のコンポーネントをVRM0で置換していきます 4");
vrm0Meta.Version = vrm1Meta.Version;
Debug.Log("VRM1のコンポーネントをVRM0で置換していきます 5");
var vrm0MetaComponent = vrm10Instance.gameObject.AddComponent<VRMMeta>();
vrm0MetaComponent.Meta = vrm0Meta;

Debug.Log("エクスポートします");
var configuration = new UniGLTF.GltfExportSettings();
var textureSerializer = new RuntimeTextureSerializer();
var exportingGltfData = VRMExporter.Export(
configuration,
vrm10Instance.gameObject,
textureSerializer
);
_vrm0Bytes = exportingGltfData.ToGlbBytes();

Debug.LogFormat("エクスポートしました {0} bytes", _vrm0Bytes.Length);
SaveButton.style.display = DisplayStyle.Flex;
ResetButton.style.display = DisplayStyle.Flex;
}
catch (Exception e)
{
Expand All @@ -74,35 +116,10 @@ private async Task OnOpenButtonClicked(byte[] vrm1Bytes)
ErrorMessageLabel.text = e.ToString();
return;
}

Debug.Log("VRM1のコンポーネントをVRM0で置換していきます");
// https://github.com/vrm-c/UniVRM/blob/7e052b19b3c0b4cd02e63159fc37db820729554e/Assets/VRM10/Runtime/Migration/MigrationVrmMeta.cs
var vrm0Meta = ScriptableObject.CreateInstance<VRMMetaObject>();
var vrm1Meta = vrm10Instance.Vrm.Meta;
Debug.Log("VRM1のコンポーネントをVRM0で置換していきます 1");
// var vrm0Meta = VRMMeta.
Debug.Log("VRM1のコンポーネントをVRM0で置換していきます 2");
vrm0Meta.Title = vrm1Meta.Name;
Debug.Log("VRM1のコンポーネントをVRM0で置換していきます 3");
vrm0Meta.Author = string.Join("/ ", vrm1Meta.Authors);
Debug.Log("VRM1のコンポーネントをVRM0で置換していきます 4");
vrm0Meta.Version = vrm1Meta.Version;
Debug.Log("VRM1のコンポーネントをVRM0で置換していきます 5");
var vrm0MetaComponent = vrm10Instance.gameObject.AddComponent<VRMMeta>();
vrm0MetaComponent.Meta = vrm0Meta;

Debug.Log("エクスポートします");
var configuration = new UniGLTF.GltfExportSettings();
var textureSerializer = new RuntimeTextureSerializer();
var exportingGltfData = VRMExporter.Export(
configuration,
vrm10Instance.gameObject,
textureSerializer
);
_vrm0Bytes = exportingGltfData.ToGlbBytes();

Debug.LogFormat("エクスポートしました {0} bytes", _vrm0Bytes.Length);
SaveButton.style.display = DisplayStyle.Flex;
finally
{
_opening = false;
}
}

private void OnSaveButtonClicked()
Expand Down Expand Up @@ -166,7 +183,7 @@ public void WebBrowserVrm0Opened(string url)
}
vrm1Bytes = unityWebRequest.downloadHandler.data;
}
_ = OnOpenButtonClicked(vrm1Bytes);
await OnOpenButtonClicked(vrm1Bytes);
},
CancellationToken.None,
TaskCreationOptions.None,
Expand All @@ -191,7 +208,6 @@ private void Start()
{
try
{
OpenButton.SetEnabled(false); // 二重クリック防止のため確実にここに書く
#if UNITY_EDITOR
var path = UnityEditor.EditorUtility.OpenFilePanel("Open VRM1", "", "vrm");
if (string.IsNullOrEmpty(path))
Expand All @@ -217,14 +233,17 @@ private void Start()
{
try
{
SaveButton.SetEnabled(false); // 二重クリック防止のため確実にここに書く
OnSaveButtonClicked();
}
catch (Exception e)
{
Debug.LogException(e);
}
};
ResetButton.clicked += () =>
{
ResetScene();
};
}
}
}
1 change: 1 addition & 0 deletions Assets/VrmDowngrader/VrmDowngrader.uxml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<ui:Label tabindex="-1" text="VRM Downgrader" parse-escape-sequences="true" display-tooltip-when-elided="true" style="color: rgb(233, 233, 233); font-size: 32px; -unity-text-align: upper-center; -unity-font-style: bold;" />
<ui:Button text="Open VRM1" parse-escape-sequences="true" display-tooltip-when-elided="true" name="OpenButton" style="font-size: 32px; -unity-font-style: normal; padding-right: 12px; margin-right: 32px; margin-left: 32px;" />
<ui:Button text="Save VRM0" parse-escape-sequences="true" display-tooltip-when-elided="true" name="SaveButton" style="font-size: 32px; -unity-font-style: normal; display: none; margin-right: 32px; margin-left: 32px;" />
<ui:Button text="Reset" parse-escape-sequences="true" display-tooltip-when-elided="true" name="ResetButton" style="font-size: 32px; -unity-font-style: normal; display: none; margin-right: 32px; margin-left: 32px;" />
<ui:Label tabindex="-1" parse-escape-sequences="true" display-tooltip-when-elided="true" name="ErrorMessageLabel" style="font-size: 12px; color: rgb(255, 142, 0);" />
</ui:VisualElement>
</ui:UXML>

0 comments on commit a6467b3

Please sign in to comment.