예제 #1
0
        /// <summary>
        /// 非同期でByte配列からVRMモデルを読み込む
        /// </summary>
        /// <param name="vrmByteArray"></param>
        /// <returns></returns>
        public async Task <GameObject> LoadVrmModelFromByteArrayAsync(byte[] vrmByteArray)
        {
#if UNIVRM_LEGACY_IMPORTER
            await InitializeVrmContextFromByteArrayAsync(vrmByteArray);

            // 非同期処理(Task)で読み込みます
            await currentContext.LoadAsyncTask();

            // 読込が完了するとcontext.RootにモデルのGameObjectが入っています
            var root = currentContext.Root;

            return(root);
#elif UNIVRM_0_68_IMPORTER
            var parser = new GltfParser();
            await Task.Run(() =>
            {
                parser.ParseGlb(vrmByteArray);
            });

            currentContext = new VRMImporterContext(parser);
            await currentContext.LoadAsync();

            return(currentContext.Root);
#else
            return(null);
#endif
        }
예제 #2
0
    /// <summary>
    /// VRMファイルを読み込む
    /// </summary>
    /// <param name="bytes"></param>
    /// <returns></returns>
    async Task <VRMImporterContext> Load(string path)
    {
        var context = new VRMImporterContext();

        try
        {
            context.ParseGlb(File.ReadAllBytes(path));
            await context.LoadAsyncTask();
        }
        catch
        {
            context.Dispose();
            throw;
        }

        if (await AskLoadingVRM(path, context.ReadMeta(true)))
        {
            return(context);
        }
        else
        {
            context.Dispose();
            return(null);
        }
    }
        /// <summary>
        /// Taskで非同期にロードする例
        /// </summary>
        async void LoadVRMClicked()
        {
#if UNITY_STANDALONE_WIN
            var path = FileDialogForWindows.FileDialog("open VRM", ".vrm");
#else
            var path = Application.dataPath + "/default.vrm";
#endif
            if (string.IsNullOrEmpty(path))
            {
                return;
            }


            var context = new VRMImporterContext();

            var bytes = await ReadBytesAsync(path);

            // GLB形式でJSONを取得しParseします
            context.ParseGlb(bytes);

            // metaを取得(todo: thumbnailテクスチャのロード)
            var meta = context.ReadMeta();
            Debug.LogFormat("meta: title:{0}", meta.Title);

            // ParseしたJSONをシーンオブジェクトに変換していく
            var now = Time.time;
            await context.LoadAsyncTask();

            var delta = Time.time - now;
            Debug.LogFormat("LoadVrmAsync {0:0.0} seconds", delta);
            OnLoaded(context);
        }
예제 #4
0
        /// <summary>
        /// load VRM from byte array
        /// </summary>
        /// <param name="file"></param>
        /// <returns>generated model GameObject</returns>
        public async Task <GameObject> LoadVRM(byte[] file)
        {
            // Parse Json to GLB
            var context = new VRMImporterContext();

            context.ParseGlb(file);

            // Get VRM Meta data
            var meta = context.ReadMeta(true);

            Debug.Log("meta: title: " + meta.Title);

            //load
            await context.LoadAsyncTask();

            //put model
            var root = context.Root;

            root.transform.rotation = new Quaternion(0, 180, 0, 0);
            root.tag = "Player";
            await Task.Delay(500);

            root.AddComponent <ObjectMover>();
            context.ShowMeshes();
            return(root);
        }
예제 #5
0
    public async void Play()
    {
        record.RecordIncr();

        var path    = Application.streamingAssetsPath + "/" + "host.vrm";
        var bytes   = await new FileRead().ReadAllBytesAsync(path);
        var context = new VRMImporterContext();

        context.ParseGlb(bytes);
        var meta = context.ReadMeta(false);

        Debug.LogFormat("meta: title:{0}", meta.Title);
        await context.LoadAsyncTask();

        var go = context.Root;

        go.transform.SetParent(transform, false);
        go.transform.position = new Vector3(0, 0, 0);
        context.ShowMeshes();
        var pos = SetupVRIK(go);

        int progress = 0;
        int index    = record.RecordIndex;

        Observable.Interval(TimeSpan.FromSeconds(1 / 60)).Subscribe(_ => { if (progress >= 0)
                                                                           {
                                                                               progress = run(pos, progress, index);
                                                                           }
                                                                    });
    }
예제 #6
0
    public async Task ImportVrmAsync(string fullPath, Transform parent)
    {
        var bytes   = System.IO.File.ReadAllBytes(fullPath);
        var context = new VRMImporterContext();

        context.ParseGlb(bytes);
        await context.LoadAsyncTask();

        var root = context.Root;

        root.transform.SetParent(parent, false);
        root.transform.localPosition = new Vector3(0, 1f, 0);

        // 先にRigidbodyつける
        var rb = root.AddComponent <Rigidbody>();

        rb.freezeRotation = true;

        // コライダつける
        var capsuleCollider = root.AddComponent <CapsuleCollider>();

        capsuleCollider.center = new Vector3(0, 0.7f, 0);
        capsuleCollider.radius = 0.2f;
        capsuleCollider.height = 1.5f;

        //  アニメーションできるように、Animatorつける
        var rootAnimator = root.GetComponent <Animator>();

        rootAnimator.runtimeAnimatorController = loadedAnimatorController;

        // Playerとして扱うならタグつける
        root.tag = TagName.Player;

        context.ShowMeshes();
    }
예제 #7
0
        private async void ImportVRMAsync(string FilePath)
        {
            //VRMファイルのパスを指定します
            var path = FilePath;

            //ファイルをByte配列に読み込みます
            var bytes = File.ReadAllBytes(path);

            //VRMImporterContextがVRMを読み込む機能を提供します
            var context = new VRMImporterContext();

            // GLB形式でJSONを取得しParseします
            context.ParseGlb(bytes);

            // VRMのメタデータを取得
            var meta = context.ReadMeta(false); //引数をTrueに変えるとサムネイルも読み込みます

            //読み込めたかどうかログにモデル名を出力してみる
            Debug.LogFormat("meta: title:{0}", meta.Title);

            //非同期処理で読み込みます
            //context.LoadAsync(_ => OnLoaded(context));
            await context.LoadAsyncTask();

            OnLoaded(context);
        }
예제 #8
0
    private async System.Threading.Tasks.Task onLoadVRMAsync(AvatarSetting setting)
    {
        videoCapture.PlayStop();

        var path = "";

        if (setting.AvatarType == 0)
        {
            path = setting.VRMFilePath;
        }
        else
        {
            path = setting.FBXFilePath;
        }

        if (path != "")
        {
            //ファイルをByte配列に読み込みます
            var bytes = File.ReadAllBytes(path);

            //VRMImporterContextがVRMを読み込む機能を提供します
            var context = new VRMImporterContext();

            // GLB形式でJSONを取得しParseします
            context.ParseGlb(bytes);

            // VRMのメタデータを取得
            var meta = context.ReadMeta(false); //引数をTrueに変えるとサムネイルも読み込みます

            //読み込めたかどうかログにモデル名を出力してみる
            //Debug.LogFormat("meta: title:{0}", meta.Title);

            //非同期処理(Task)で読み込みます
            await context.LoadAsyncTask();

            ///
            //読込が完了するとcontext.RootにモデルのGameObjectが入っています
            var avatarObject = context.Root;
            avatarObject.name = setting.AvatarName;

            //モデルをワールド上に配置します
            avatarObject.transform.SetParent(transform.parent, false);

            SetVRMBounds(avatarObject.transform);

            //メッシュを表示します
            context.ShowMeshes();

            setting.Avatar             = avatarObject.AddComponent <VNectModel>();
            setting.Avatar.ModelObject = avatarObject;
            setting.Avatar.SetNose(setting.FaceOriX, setting.FaceOriY, setting.FaceOriZ);
            setting.Avatar.SkeletonMaterial = skeletonMaterial;
            DiactivateAvatars();
            avatars.options.Add(new Dropdown.OptionData(setting.AvatarName));
            barracudaRunner.InitVNectModel(setting.Avatar, configurationSetting);
        }
    }
        async static Task <VRMImporterContext> LoadAsync(Byte[] bytes)
        {
            var context = new VRMImporterContext();

            // GLB形式でJSONを取得しParseします
            context.ParseGlb(bytes);

            // ParseしたJSONをシーンオブジェクトに変換していく
            await context.LoadAsyncTask();

            return(context);
        }
예제 #10
0
    public async Task <Transform> LoadVRM()
    {
        var bytes   = File.ReadAllBytes(vrmPath);
        var context = new VRMImporterContext();

        context.ParseGlb(bytes);

        var metadata = context.ReadMeta(false);

        Debug.LogFormat("meta: title: {0}", metadata.Title);

        await context.LoadAsyncTask();

        context.ShowMeshes();

        return(context.Root.transform);
    }
예제 #11
0
        public static async UniTask <GameObject> LoadVRMAsync(byte[] bytes)
        {
            var context = new VRMImporterContext();

            try
            {
                context.ParseGlb(bytes);
                var meta = context.ReadMeta(false);
                await context.LoadAsyncTask();

                context.ShowMeshes();
                return(context.Root);
            }
            catch (Exception e)
            {
                Debug.LogException(e);
                throw;
            }
        }
예제 #12
0
        public async UniTask <GameObject> LoadVRMAvater()
#endif
        {
            var path = VRMLoadUniRx.GetVRMPath();

            if (path == null)
            {
                path = Application.streamingAssetsPath + "/Avater/model.vrm";
            }
            byte[]     VRMByteData;
            GameObject go;

#if UNITY_WEBGL
            VRMByteData = VRMLoadUniRx.GetVRMData();
#else
            using (var uwr = UnityWebRequest.Get(path))
            {
                await uwr.SendWebRequest();

                VRMByteData = uwr.downloadHandler.data;
            }
#endif
            context = new VRMImporterContext();
            context.ParseGlb(VRMByteData);
#if UNITY_WEBGL
            context.Load();
#else
            await context.LoadAsyncTask();
#endif
            go = context.Root;
            context.ShowMeshes();

            go.AddComponent <Blinker>();
            go.AddComponent <FaceUpdate>();
            var animator = go.GetComponent <Animator>();
            animator.applyRootMotion           = true;
            animator.runtimeAnimatorController = (RuntimeAnimatorController)Instantiate(Resources.Load("MocapC86"));
            go.SetLayerRecursively(8);

            return(go);
        }
예제 #13
0
    // Start is called before the first frame update
    async void Start()
    {
        var path = GameObject.Find("SceneController").GetComponent <SceneController>().path;

        Debug.Log("start" + path);
        // VRMを読み込む機能の提供
        var context = new VRMImporterContext();
        // ファイルをByte配列に読み込みファイルを閉じる
        var bytes = File.ReadAllBytes(path);

        // GLB形式でJSONを取得しParseする
        context.ParseGlb(bytes);
        // metaデータの取得
        // 引数にサムネイルの取得設定を行える
        var meta = context.ReadMeta(false);

        Debug.LogFormat("meta_title: {0}", meta.Title);

        // LoadAsync後に引数のメソッドが呼び出されるように登録
        // context.LoadAsync(_ => OnLoaded(context));
        // 今UnityのVersionだとObsoleteになっている

        try
        {
            // awaitを使いたいのでメソッドにasyncを付ける
            await context.LoadAsyncTask();

            //
            context.EnableUpdateWhenOffscreen();

            OnLoaded(context);
        }
        catch (Exception e)
        {
            Debug.LogError(e);
            // 関連するリソースを破棄する
            context.Dispose();
            throw;
        }
    }
예제 #14
0
    public async Task LoadModel(string path)
    {
        if (!File.Exists(path))
        {
            Debug.LogError("file not found path = " + path);
            return;
        }
        //VRMファイルの読み込み
        var bytes   = File.ReadAllBytes(path);
        var context = new VRMImporterContext();

        context.ParseGlb(bytes);
        var meta = context.ReadMeta(false);
        await context.LoadAsyncTask();

        //VRMファイルの表示
        context.ShowMeshes();
        context.EnableUpdateWhenOffscreen();
        context.ShowMeshes();
        model = GameObject.Find("VRM");

        //VRMオブジェクトへの動的アタッチ
        model.AddComponent <AvatarController>();
        this.GetComponent <KinectManager>().ResetAvatarControllers(); //thisをKinectManagerをアタッチしたGameObjectに変更
        //BoneSendへのVRMオブジェクト代入
        this.GetComponent <SampleBonesSend>().Model = model;          //thisをSampleBonesSendをアタッチしたGameObjectに変更
        //BoneReceiveへのVRMオブジェクト代入
        this.GetComponent <SampleBonesReceive>().Model = model;       //thisをSampleBonesSendをアタッチしたGameObjectに変更
        //Blinker追加
        var lookAt = model.GetComponent <VRMLookAtHead>();

        if (lookAt != null)
        {
            model.AddComponent <Blinker>();
            blinker = model.GetComponent <Blinker>();
        }

        //AniLipSync-VRMへのVRMオブジェクト代入
        LipSync.blendShapeProxy = model.GetComponent <VRMBlendShapeProxy>(); //thisをSampleBonesSendをアタッチしたGameObjectに変更
    }
예제 #15
0
    private async Task ImportVRMAsync_Net4(string path)
    {
        //ファイルをByte配列に読み込みます
        var bytes = File.ReadAllBytes(path);

        //VRMImporterContextがVRMを読み込む機能を提供します
        context = new VRMImporterContext();

        // GLB形式でJSONを取得しParseします
        context.ParseGlb(bytes);

        //非同期処理(Task)で読み込みます
        await context.LoadAsyncTask();

        //読込が完了するとcontext.RootにモデルのGameObjectが入っています
        var root = context.Root;

        //モデルをワールド上に配置します
        root.transform.SetParent(transform, false);

        var animator = this.GetComponent <ToAnimator> ();
        var camIni   = this.GetComponent <CameraInitialize> ();
        var IKAni    = this.GetComponent <IKInitialize> ();

        //var VRMFaceIni = this.GetComponent<VRMFaceIni> ();
        //var VRMFaceTracker = this.GetComponent<VRMFaceTracker> ();

        animator.SetAnimator(root);
        camIni.CameraPositionInitialize(root);
        IKAni.IKInitilize(root);
        //VRMFaceIni.VRMFaceInitialize (root);
        //VRMFaceTracker.InitializeSession ();

        //メッシュを表示します
        context.ShowMeshes();
        Loading.SetActive(false);
    }
예제 #16
0
    public async void Load(string path)
    {
        Debug.Log("load" + path);
        var bytes = await new FileRead().ReadAllBytesAsync(path);

        Destroy(go);
        var context = new VRMImporterContext();

        context.ParseGlb(bytes);
        var meta = context.ReadMeta(false);

        Debug.LogFormat("meta: title:{0}", meta.Title);
        await context.LoadAsyncTask();

        go = context.Root;
        go.transform.SetParent(transform, false);
        go.transform.position = new Vector3(0, 0, 0);
        context.ShowMeshes();
        SetupVRIK(go);
        SetupLipSync(go);
        left.tag  = "Player";
        right.tag = "Player";
        ForceWarp.Warp();
    }
예제 #17
0
    // Called by ObjectSync when become ready
    async void OnSyncReady()
    {
        Debug.Log("OnSyncReady");

        SyncObject obj  = GetComponent <ObjectSync>().SyncObject;
        SyncNode   node = GetComponent <ObjectSync>().Node;

        obj.BeforeSync += OnBeforeSync;
        obj.AfterSync  += OnAfterSync;
        obj.RegisterFieldUpdateHandler("leftHand", OnLeftHandUpdated);
        obj.RegisterFieldUpdateHandler("rightHand", OnRightHandUpdated);
        obj.RegisterFieldUpdateHandler("mouseGrabber", OnMouseGrabberUpdated);

        OnLeftHandUpdated();
        OnRightHandUpdated();
        OnMouseGrabberUpdated();

        Blob vrmBlob;

        if (GetComponent <ObjectSync>().IsOriginal)
        {
            byte[] vrmData = File.ReadAllBytes(Settings.Instance.AvatarPath);
            // Use MIME type for GLTF binary https://www.iana.org/assignments/media-types/model/gltf-binary
            vrmBlob = new Blob(vrmData, "model/gltf-binary");
            BlobHandle vrmHandle = vrmBlob.GenerateHandle();
            node.WriteBlob(vrmHandle, vrmBlob);
            obj.SetField("vrm", vrmHandle);
        }
        else
        {
            while (!obj.HasField("vrm") || !(obj.GetField("vrm") is BlobHandle))
            {
                obj.WriteDebugLog("PlayerAvatar", "Field vrm not ready");
                await UniTask.WaitForFixedUpdate();
            }
            BlobHandle blobHandle = (BlobHandle)obj.GetField("vrm");
            vrmBlob = await node.ReadBlob(blobHandle);
        }

        // Disable collision detection (and character control) during VRM load
        // When the avatar collides with something during creation,
        // it goes to wrong position (e.g. floating).
        // Note: CharacterController inherits Collider.
        //  (See https://docs.unity3d.com/2019.3/Documentation/ScriptReference/CharacterController.html )
        GetComponent <CharacterController>().enabled = false;

        // Load VRM from byte array
        // https://github.com/vrm-c/UniVRM/wiki/Runtime-import
        // https://qiita.com/sh_akira/items/8155e4b69107c2a7ede6
        ctx      = new VRMImporterContext();
        ctx.Root = new GameObject();    // VRM is loaded as a separate object
        ctx.ParseGlb(vrmBlob.Data);

        var meta = ctx.ReadMeta();

        obj.WriteLog("PlayerAvatar", $"Loading VRM {meta.Title} created by {meta.Author} ({meta.ContactInformation})");
        obj.WriteLog("PlayerAvatar", $"AllowedUser={meta.AllowedUser}, ViolentUsage={meta.ViolentUssage}");
        obj.WriteLog("PlayerAvatar", $"SexualUsage={meta.SexualUssage}, CommercialUsage={meta.CommercialUssage}");
        obj.WriteLog("PlayerAvatar", $"OtherPermissionUrl={meta.OtherPermissionUrl}");
        obj.WriteLog("PlayerAvatar", $"LicenseType={meta.LicenseType}");
        obj.WriteLog("PlayerAvatar", $"OtherLicenseUrl={meta.OtherLicenseUrl}");

        await ctx.LoadAsyncTask();

        ctx.EnableUpdateWhenOffscreen();
        ctx.ShowMeshes();

        // Enable collision (and character controller) again (see the disabling line above)
        GetComponent <CharacterController>().enabled = true;

        // Move VRM avatar inside this gameObject
        ctx.Root.transform.SetParent(transform);
        ctx.Root.transform.localPosition = Vector3.zero;
        ctx.Root.transform.localRotation = Quaternion.identity;

        GetComponent <Animator>().avatar = ctx.Root.GetComponent <Animator>().avatar;

        obj.WriteLog("PlayerAvatar", $"VRM loaded");

        if (GetComponent <ObjectSync>().IsOriginal)
        {
            // Set up first person view (do not display avatar of the player)
            //  https://vrm.dev/en/univrm/components/univrm_firstperson/
            //  https://vrm.dev/en/dev/univrm-0.xx/programming/univrm_use_firstperson/
            var fp = GetComponentInChildren <VRMFirstPerson>();
            fp.Setup();
            if (XRRig != null)
            {
                XRRig.transform.position = fp.FirstPersonBone.position + fp.FirstPersonBone.rotation * fp.FirstPersonOffset;
                XRRig.transform.rotation = transform.rotation;  // face forward
                // Do not render layer "VRMThirdPersonOnly" on first person camera
                xrCamera.cullingMask &= ~LayerMask.GetMask("VRMThirdPersonOnly");
                SetHeadShadow();
            }
            if (ThirdPersonCamera != null)
            {
                // Do not render layer "VRMFirstPersonOnly" on third person camera
                ThirdPersonCamera.cullingMask &= ~LayerMask.GetMask("VRMFirstPersonOnly");
            }

            // Left hand
            if (leftController.HasValue)
            {
                // If left hand device is present
                var leftId = await node.CreateObject();

                leftHandObj = node.Objects[leftId];
                leftHandObj.SetField("parent", obj.GetObjectRef());
                leftHandObj.SetField("tags", new Sequence(new IValue[] {
                    new Primitive <string>("constantVelocity"),
                    new Primitive <string>("collider")
                }));
                obj.SetField("leftHand", leftHandObj.GetObjectRef());
            }
            // Right hand
            if (rightController.HasValue)
            {
                // If right hand device is present
                var rightId = await node.CreateObject();

                rightHandObj = node.Objects[rightId];
                rightHandObj.SetField("parent", obj.GetObjectRef());
                rightHandObj.SetField("tags", new Sequence(new IValue[] {
                    new Primitive <string>("constantVelocity"),
                    new Primitive <string>("collider")
                }));
                obj.SetField("rightHand", rightHandObj.GetObjectRef());
            }
            // Mouse
            var mouseGrabberId = await node.CreateObject();

            mouseGrabberObj = node.Objects[mouseGrabberId];
            mouseGrabberObj.SetField("parent", obj.GetObjectRef());
            mouseGrabberObj.SetField("tags", new Sequence(new IValue[] {
                new Primitive <string>("constantVelocity"),
                new Primitive <string>("collider")
            }));
            obj.SetField("mouseGrabber", mouseGrabberObj.GetObjectRef());
        }
    }