From e1db3122039ca3b794144bc13c563fa0dc81df72 Mon Sep 17 00:00:00 2001 From: ousttrue Date: Sat, 25 Jan 2025 02:14:18 +0900 Subject: [PATCH] menu "Build 10 WebGL for CI". Update sample --- Assets/VRM/Editor/BuildClass.cs | 20 +++++ Assets/VRM/Editor/VrmTopMenu.cs | 9 ++- Assets/VRM/Samples~/SimpleViewer/Plugins.meta | 8 -- .../SimpleViewer/Plugins/OpenFile.jslib | 21 ----- .../SimpleViewer/Plugins/OpenFile.jslib.meta | 32 -------- Assets/VRM/Samples~/SimpleViewer/ViewerUI.cs | 4 +- .../VRM10Viewer/FaceCameraTarget.asset | 1 + .../Samples~/VRM10Viewer/VRM10ViewerUI.cs | 78 ++++++++++++++++--- 8 files changed, 94 insertions(+), 79 deletions(-) delete mode 100644 Assets/VRM/Samples~/SimpleViewer/Plugins.meta delete mode 100644 Assets/VRM/Samples~/SimpleViewer/Plugins/OpenFile.jslib delete mode 100644 Assets/VRM/Samples~/SimpleViewer/Plugins/OpenFile.jslib.meta diff --git a/Assets/VRM/Editor/BuildClass.cs b/Assets/VRM/Editor/BuildClass.cs index 43f99e2f4..410de15a4 100644 --- a/Assets/VRM/Editor/BuildClass.cs +++ b/Assets/VRM/Editor/BuildClass.cs @@ -46,5 +46,25 @@ namespace VRM throw new System.Exception(report.summary.ToString()); } } + + public static void BuildWebGL_VRM10Viewer() + { + var scenes = new string[]{ + "./Assets/VRM10_Samples/VRM10Viewer/VRM10Viewer.unity", + }; + + var report = BuildPipeline.BuildPlayer(new BuildPlayerOptions + { + scenes = scenes, + locationPathName = "Build/VRM10Viewer", + target = BuildTarget.WebGL, + } + ); + + if (report.summary.result != UnityEditor.Build.Reporting.BuildResult.Succeeded) + { + throw new System.Exception(report.summary.ToString()); + } + } } } diff --git a/Assets/VRM/Editor/VrmTopMenu.cs b/Assets/VRM/Editor/VrmTopMenu.cs index 895cf1cdf..7c8caa901 100644 --- a/Assets/VRM/Editor/VrmTopMenu.cs +++ b/Assets/VRM/Editor/VrmTopMenu.cs @@ -55,10 +55,13 @@ namespace VRM [MenuItem(DevelopmentMenuPrefix + "/Build dummy for CI", false, 93)] private static void BuildDummyForCi() => BuildClass.Build(); - [MenuItem(DevelopmentMenuPrefix + "/Build WebGL for CI", false, 94)] - private static void BuildWebGLForCi() => BuildClass.BuildWebGL_SimpleViewer(); + [MenuItem(DevelopmentMenuPrefix + "/Build 0x WebGL for CI", false, 94)] + private static void BuildWebGLForCi0x() => BuildClass.BuildWebGL_SimpleViewer(); - [MenuItem(DevelopmentMenuPrefix + "/Create UnityPackage", false, 95)] + [MenuItem(DevelopmentMenuPrefix + "/Build 10 WebGL for CI", false, 95)] + private static void BuildWebGLForCi10() => BuildClass.BuildWebGL_VRM10Viewer(); + + [MenuItem(DevelopmentMenuPrefix + "/Create UnityPackage", false, 99)] private static void CreateUnityPackage() => VRMExportUnityPackage.CreateUnityPackageWithoutBuild(); #endif } diff --git a/Assets/VRM/Samples~/SimpleViewer/Plugins.meta b/Assets/VRM/Samples~/SimpleViewer/Plugins.meta deleted file mode 100644 index fdecce026..000000000 --- a/Assets/VRM/Samples~/SimpleViewer/Plugins.meta +++ /dev/null @@ -1,8 +0,0 @@ -fileFormatVersion: 2 -guid: ccb1849bc34bcd740ba2e17462125200 -folderAsset: yes -DefaultImporter: - externalObjects: {} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/VRM/Samples~/SimpleViewer/Plugins/OpenFile.jslib b/Assets/VRM/Samples~/SimpleViewer/Plugins/OpenFile.jslib deleted file mode 100644 index 48ba0ce57..000000000 --- a/Assets/VRM/Samples~/SimpleViewer/Plugins/OpenFile.jslib +++ /dev/null @@ -1,21 +0,0 @@ -mergeInto(LibraryManager.library, { - WebGLFileDialog: function () { - const file_input_id = "file-input"; - var file_input = document.getElementById(file_input_id); - if (!file_input) { - file_input = document.createElement('input'); - file_input.setAttribute('type', 'file'); - file_input.setAttribute('id', file_input_id); - // file_input.setAttribute('accept', '.vrm') - file_input.style.visibility = 'hidden'; - file_input.onclick = function (event) { - event.target.value = null; - }; - file_input.onchange = function (event) { - SendMessage('Canvas', 'FileSelected', URL.createObjectURL(event.target.files[0])); - } - document.body.appendChild(file_input); - } - file_input.click(); - }, -}); \ No newline at end of file diff --git a/Assets/VRM/Samples~/SimpleViewer/Plugins/OpenFile.jslib.meta b/Assets/VRM/Samples~/SimpleViewer/Plugins/OpenFile.jslib.meta deleted file mode 100644 index ee0cd7c4f..000000000 --- a/Assets/VRM/Samples~/SimpleViewer/Plugins/OpenFile.jslib.meta +++ /dev/null @@ -1,32 +0,0 @@ -fileFormatVersion: 2 -guid: 2e8941ad33d65584f8ef2f8b829a05e7 -PluginImporter: - externalObjects: {} - serializedVersion: 2 - iconMap: {} - executionOrder: {} - defineConstraints: [] - isPreloaded: 0 - isOverridable: 0 - isExplicitlyReferenced: 0 - validateReferences: 1 - platformData: - - first: - Any: - second: - enabled: 0 - settings: {} - - first: - Editor: Editor - second: - enabled: 0 - settings: - DefaultValueInitialized: true - - first: - WebGL: WebGL - second: - enabled: 1 - settings: {} - userData: - assetBundleName: - assetBundleVariant: diff --git a/Assets/VRM/Samples~/SimpleViewer/ViewerUI.cs b/Assets/VRM/Samples~/SimpleViewer/ViewerUI.cs index 36ca09951..575f44556 100644 --- a/Assets/VRM/Samples~/SimpleViewer/ViewerUI.cs +++ b/Assets/VRM/Samples~/SimpleViewer/ViewerUI.cs @@ -4,7 +4,6 @@ using System.IO; using System.Linq; using System.Threading.Tasks; using UniGLTF; -using UniGLTF.SpringBoneJobs; using UniHumanoid; using Unity.Collections; using UnityEngine; @@ -294,8 +293,7 @@ namespace VRM.SimpleViewer private void Start() { - m_version.text = string.Format("VRMViewer {0}.{1}", - PackageVersion.MAJOR, PackageVersion.MINOR); + m_version.text = string.Format("SimpleViewer {0}", PackageVersion.VERSION); m_open.onClick.AddListener(OnOpenClicked); m_reset.onClick.AddListener(() => m_loaded?.ResetSpringbone()); diff --git a/Assets/VRM10/Samples~/VRM10Viewer/FaceCameraTarget.asset b/Assets/VRM10/Samples~/VRM10Viewer/FaceCameraTarget.asset index c1311d8e0..dc5b55f8e 100644 --- a/Assets/VRM10/Samples~/VRM10Viewer/FaceCameraTarget.asset +++ b/Assets/VRM10/Samples~/VRM10Viewer/FaceCameraTarget.asset @@ -26,6 +26,7 @@ CustomRenderTexture: m_UseDynamicScale: 0 m_BindMS: 0 m_EnableCompatibleFormat: 1 + m_EnableRandomWrite: 0 m_TextureSettings: serializedVersion: 2 m_FilterMode: 1 diff --git a/Assets/VRM10/Samples~/VRM10Viewer/VRM10ViewerUI.cs b/Assets/VRM10/Samples~/VRM10Viewer/VRM10ViewerUI.cs index 5dbc28055..cba67b0ab 100644 --- a/Assets/VRM10/Samples~/VRM10Viewer/VRM10ViewerUI.cs +++ b/Assets/VRM10/Samples~/VRM10Viewer/VRM10ViewerUI.cs @@ -1,8 +1,11 @@ using System; +using System.Collections; using System.Collections.Generic; using System.IO; using System.Linq; +using System.Runtime.InteropServices; using System.Threading; +using System.Threading.Tasks; using UniGLTF; using UniGLTF.SpringBoneJobs.Blittables; using UnityEngine; @@ -474,8 +477,7 @@ namespace UniVRM10.VRM10Viewer m_autoBlink = gameObject.AddComponent(); m_autoLipsync = gameObject.AddComponent(); - m_version.text = string.Format("VRMViewer {0}.{1}", - VRM10SpecVersion.MAJOR, VRM10SpecVersion.MINOR); + m_version.text = string.Format("VRM10ViewerUI {0}", PackageVersion.VERSION); m_openModel.onClick.AddListener(OnOpenModelClicked); m_openMotion.onClick.AddListener(OnOpenMotionClicked); @@ -491,7 +493,7 @@ namespace UniVRM10.VRM10Viewer if (ArgumentChecker.TryGetFirstLoadable(out var cmd)) { - LoadModel(cmd); + var _ = LoadModel(cmd); } m_texts.Start(); @@ -502,7 +504,6 @@ namespace UniVRM10.VRM10Viewer _cancellationTokenSource?.Dispose(); } - private void Update() { if (Input.GetKeyDown(KeyCode.Tab)) @@ -639,15 +640,29 @@ namespace UniVRM10.VRM10Viewer } } - void OnOpenModelClicked() + [DllImport("__Internal")] + public static extern void WebGLFileDialog(string target, string message); + + string FileDialog() { #if UNITY_STANDALONE_WIN - var path = VRM10FileDialogForWindows.FileDialog("open VRM", "vrm"); + return VRM10FileDialogForWindows.FileDialog("open VRM", "vrm"); +#elif UNITY_WEBGL + // Open WebGLFileDialog + // see: Assets/UniGLTF/Runtime/Utils/Plugins/OpenFile.jslib + WebGLFileDialog("Canvas", "FileSelected"); + // Control flow does not return here. return empty string with dummy + return null; #elif UNITY_EDITOR - var path = UnityEditor.EditorUtility.OpenFilePanel("Open VRM", "", "vrm"); + return UnityEditor.EditorUtility.OpenFilePanel("Open VRM", "", "vrm"); #else - var path = Application.dataPath + "/default.vrm"; + return Application.dataPath + "/default.vrm"; #endif + } + + void OnOpenModelClicked() + { + var path = FileDialog(); if (string.IsNullOrEmpty(path)) { return; @@ -660,7 +675,24 @@ namespace UniVRM10.VRM10Viewer return; } - LoadModel(path); + _ = LoadModel(path); + } + + /// + /// for WebGL + /// call from OpenFile.jslib + /// + public void FileSelected(string url) + { + Debug.Log($"FileSelected: {url}"); + StartCoroutine(LoadCoroutine(url)); + } + + IEnumerator LoadCoroutine(string url) + { + var www = new WWW(url); + yield return www; + var _ = LoadModel("WebGL.vrm", www.bytes); } async void OnOpenMotionClicked() @@ -760,7 +792,29 @@ namespace UniVRM10.VRM10Viewer } } - async void LoadModel(string path) + IAwaitCaller GetIAwaitCaller() + { + if (m_useAsync) + { +#if UNITY_WEBGL + return new RuntimeOnlyNoThreadAwaitCaller(); +#else + return new RuntimeOnlyAwaitCaller(); +#endif + } + else + { + return new ImmediateCaller(); + } + } + + async Task LoadModel(string path) + { + var bytes = await File.ReadAllBytesAsync(path); + await LoadModel(path, bytes); + } + + async Task LoadModel(string path, byte[] bytes) { // cleanup m_loaded?.Dispose(); @@ -772,10 +826,10 @@ namespace UniVRM10.VRM10Viewer try { Debug.LogFormat("{0}", path); - var vrm10Instance = await Vrm10.LoadPathAsync(path, + var vrm10Instance = await Vrm10.LoadBytesAsync(bytes, canLoadVrm0X: true, showMeshes: false, - awaitCaller: m_useAsync.enabled ? new RuntimeOnlyAwaitCaller() : new ImmediateCaller(), + awaitCaller: GetIAwaitCaller(), vrmMetaInformationCallback: m_texts.UpdateMeta, ct: cancellationToken, springboneRuntime: m_useSpringboneSingelton.isOn ? new Vrm10FastSpringboneRuntime() : new Vrm10FastSpringboneRuntimeStandalone());