private void Server_Received(object sender, DataReceivedEventArgs e) { synchronizationContext.Post(async(arg) => { //-----------システム系---------------- if (e.CommandType == typeof(PipeCommands.Hello)) { //Helloが来たらHelloを返す。すると初期値が送られてくる。 await server.SendCommandAsync(new PipeCommands.Hello { startTime = this.startTime }); Debug.Log(">Hello"); } else if (e.CommandType == typeof(PipeCommands.Bye)) { //Unity側終了処理 /* #if UNITY_EDITOR * UnityEditor.EditorApplication.isPlaying = false; #else * Application.Quit(); #endif */ Debug.Log(">Bye"); } //-----------基本設定---------------- //===========探索=========== else if (e.CommandType == typeof(PipeCommands.DiscoverRequest)) { var d = (PipeCommands.DiscoverRequest)e.Data; //探索開始 var req = GetComponent <EasyDeviceDiscoveryProtocolClient.Requester>(); req.servicePort = port; req.StartDiscover(() => { //次のデバイスに更新されないうちに取得しておく string ip = req.responseIpAddress; int port = req.responseServicePort; string name = req.deivceName; int found = req.foundDevices; //探索完了 synchronizationContext.Post(async(a) => { Debug.Log("DiscoverResponse"); await server.SendCommandAsync(new PipeCommands.DiscoverResponse { ip = ip, port = port, name = name, found = found, }); }, null); }); } //===========VRMライセンス応答=========== else if (e.CommandType == typeof(PipeCommands.VRMLicenceAnser)) { var d = (PipeCommands.VRMLicenceAnser)e.Data; if (d.Agree) { loader.Agree(); } else { loader.DisAgree(); } } //===========VRM読み込み=========== else if (e.CommandType == typeof(PipeCommands.LoadVRM)) { var d = (PipeCommands.LoadVRM)e.Data; Debug.Log("LoadVRM: " + d.filepath); //許諾画面を出す if (File.Exists(d.filepath)) { loader.LoadRequest(d.filepath, (path, bytes) => { synchronizationContext.Post((args) => { Debug.Log("Load start"); externalReceiver.LoadVRMFromData(bytes); hidePanel.SetActive(d.hide); }, null); }); //許諾応答要求を出す await server.SendCommandAsync(new PipeCommands.VRMLicenceCheck { skip = d.skip }); } } //===========DVRConnect Login=========== else if (e.CommandType == typeof(PipeCommands.LoginDVRConnect)) { var d = (PipeCommands.LoginDVRConnect)e.Data; Debug.Log("LoginDVRConnect"); synchronizationContext.Post(async(args) => { var config = new DVRAuthConfiguration(TokenManager.DVRSDK_ClientId, new UnitySettingStore(), new UniWebRequest(), new NewtonsoftJsonSerializer()); Authentication.Instance.Init(config); Authentication.Instance.Authorize( openBrowser: async(OpenBrowserResponse url) => { Application.OpenURL(url.VerificationUri); //KEYを応答する await server.SendCommandAsync(new PipeCommands.LoginDVRConnectResult { key = url.UserCode }); }, onAuthSuccess: async isSuccess => { if (isSuccess) { Debug.Log("AUTHENTICATION_OK"); await server.SendCommandAsync(new PipeCommands.LoginDVRConnectResult { key = "LOGIN OK" }); await GetAvatarsList(); } else { Debug.Log("AUTHENTICATION_FAILED"); await server.SendCommandAsync(new PipeCommands.LoginDVRConnectResult { key = "LOGIN ERROR" }); } }, onAuthError: async exception => { Debug.Log("AUTHENTICATION_FAILED"); Debug.LogError(exception); await server.SendCommandAsync(new PipeCommands.LoginDVRConnectResult { key = "LOGIN ERROR" }); }); }, null); } //===========DVRConnect Logout=========== else if (e.CommandType == typeof(PipeCommands.LogoutDVRConnect)) { var d = (PipeCommands.LogoutDVRConnect)e.Data; Debug.Log("LogoutDVRConnect"); synchronizationContext.Post(async(args) => { var config = new DVRAuthConfiguration(TokenManager.DVRSDK_ClientId, new UnitySettingStore(), new UniWebRequest(), new NewtonsoftJsonSerializer()); Authentication.Instance.Init(config); await Authentication.Instance.DoLogout(); await server.SendCommandAsync(new PipeCommands.LoginDVRConnectResult { key = "LOGOUT" }); }, null); } //===========DVRConnect Load=========== else if (e.CommandType == typeof(PipeCommands.LoadDVRConnect)) { var d = (PipeCommands.LoadDVRConnect)e.Data; Debug.Log("LoadDVRConnect : " + d.index.ToString()); synchronizationContext.Post(async(args) => { var avatars = await Authentication.Instance.Okami.GetAvatarsAsync(); var currentAvatar = avatars[d.index]; await LoadAvatarFromDVRSDK(currentAvatar); }, null); } //===========DVRConnect GetAvatar=========== else if (e.CommandType == typeof(PipeCommands.GetAvatarDVRConnect)) { Debug.Log("GetAvatarDVRConnect"); synchronizationContext.Post(async(args) => { await GetAvatarsList(); }, null); } //===========背景読み込み=========== else if (e.CommandType == typeof(PipeCommands.LoadBackground)) { var d = (PipeCommands.LoadBackground)e.Data; Debug.Log("LoadBackground: " + d.filepath); //TODO: 背景読み込み処理 if (d.filepath != null && d.filepath != "") { uWC.gameObject.SetActive(false); //VRMの場合 if (d.filepath.ToLower().EndsWith(".vrm")) { Destroy(backgroundObject); bgtexture = null; backgroundObject = null; //ファイルからモデルを読み込む //バイナリの読み込み if (File.Exists(d.filepath)) { //許諾画面を出す loader.LoadRequest(d.filepath, (path, bytes) => { //読み込み VRMImporterContext vrmImporter = new VRMImporterContext(); vrmImporter.ParseGlb(bytes); vrmImporter.LoadAsync(() => { GameObject Model = vrmImporter.Root; //backgroundObjectの下にぶら下げる backgroundObject = new GameObject(); backgroundObject.transform.SetParent(rootLockerTransform, false); backgroundObject.name = "backgroundObject"; //最後に設定されていた位置に設定 if (lastBackgroundPos != null) { if (lastBackgroundPos.cameraTaget) { //カメラターゲット時は、カメラアームに backgroundObject.transform.SetParent(cameraArm, false); } else { //そうでないときは独立した位置に backgroundObject.transform.SetParent(rootLockerTransform, false); } backgroundObject.transform.localPosition = new Vector3(lastBackgroundPos.Px, lastBackgroundPos.Py, lastBackgroundPos.Pz); backgroundObject.transform.localRotation = Quaternion.Euler(lastBackgroundPos.Rx, lastBackgroundPos.Ry, lastBackgroundPos.Rz); backgroundObject.transform.localScale = new Vector3(lastBackgroundPos.scale, lastBackgroundPos.scale, lastBackgroundPos.scale); } //その下にモデルをぶら下げる Model.transform.SetParent(backgroundObject.transform, false); vrmImporter.EnableUpdateWhenOffscreen(); vrmImporter.ShowMeshes(); }); }); //許諾応答要求を出す await server.SendCommandAsync(new PipeCommands.VRMLicenceCheck { }); } else { Debug.LogError("VRM load failed."); } } //画像の場合 else if (d.filepath.ToLower().EndsWith(".png")) { Destroy(backgroundObject); bgtexture = null; backgroundObject = null; if (File.Exists(d.filepath)) { backgroundObject = GameObject.CreatePrimitive(PrimitiveType.Quad); backgroundObject.transform.SetParent(rootLockerTransform, false); //最後に設定されていた位置に設定 if (lastBackgroundPos != null) { if (lastBackgroundPos.cameraTaget) { //カメラターゲット時は、カメラアームに backgroundObject.transform.SetParent(cameraArm, false); } else { //そうでないときは独立した位置に backgroundObject.transform.SetParent(rootLockerTransform, false); } backgroundObject.transform.localPosition = new Vector3(lastBackgroundPos.Px, lastBackgroundPos.Py, lastBackgroundPos.Pz); backgroundObject.transform.localRotation = Quaternion.Euler(lastBackgroundPos.Rx, lastBackgroundPos.Ry, lastBackgroundPos.Rz); } backgroundObjectUrl = "file://" + d.filepath; StartCoroutine("LoadTexture"); } } else { await server.SendCommandAsync(new PipeCommands.SendMessage { Message = "その背景は対応していません" }); } } } else if (e.CommandType == typeof(PipeCommands.RemoveBackground)) { Destroy(backgroundObject); backgroundObject = null; } //===========カメラ位置=========== else if (e.CommandType == typeof(PipeCommands.CameraControl)) { var d = (PipeCommands.CameraControl)e.Data; lookAtModel.zaxis = d.Rz; lookAtModel.height = d.Height; cameraArm.localPosition = new Vector3(0, d.Height, 0); cameraArm.localRotation = Quaternion.Euler(d.Rx, d.Ry, 0); camera_comp.transform.localPosition = new Vector3(0, 0, d.Zoom); camera_comp.fieldOfView = d.Fov; //ライト連動 //カメラのを持ってくる lightArm.localPosition = new Vector3(0, lookAtModel.height, 0); lightLookAtModel.height = lookAtModel.height; light_comp.transform.localPosition = new Vector3(0, 0, light_comp.transform.localPosition.z); } //===========ライト位置=========== else if (e.CommandType == typeof(PipeCommands.LightControl)) { var d = (PipeCommands.LightControl)e.Data; lightLookAtModel.height = lookAtModel.height; //カメラのを持ってくる lightLookAtModel.zaxis = d.Rz; lightArm.localPosition = new Vector3(0, lookAtModel.height, 0); light_comp.transform.localPosition = new Vector3(0, 0, d.Distance); light_comp.range = d.Range; light_comp.spotAngle = d.SpotAngle; switch (d.Type) { case PipeCommands.LightType.Directional: lightArm.localRotation = Quaternion.Euler(0, 0, 0); light_comp.transform.localRotation = Quaternion.Euler(d.Rx, d.Ry, d.Rz); lightLookAtModel.enabled = false; light_comp.type = LightType.Directional; break; case PipeCommands.LightType.Point: lightArm.localRotation = Quaternion.Euler(d.Rx, d.Ry, 0); lightLookAtModel.enabled = true; light_comp.type = LightType.Point; break; case PipeCommands.LightType.Spot: lightArm.localRotation = Quaternion.Euler(d.Rx, d.Ry, 0); lightLookAtModel.enabled = true; light_comp.type = LightType.Spot; break; default: lightArm.localRotation = Quaternion.Euler(0, 0, 0); light_comp.transform.localRotation = Quaternion.Euler(d.Rx, d.Ry, d.Rz); lightLookAtModel.enabled = false; light_comp.type = LightType.Directional; break; } } //===========背景オブジェクト位置=========== else if (e.CommandType == typeof(PipeCommands.BackgroundObjectControl)) { var d = (PipeCommands.BackgroundObjectControl)e.Data; lastBackgroundPos = d; uWC.partialWindowTitle = d.windowTtitle; if (d.windowCapture) { if (!uWC.gameObject.activeSelf) { uWC.gameObject.SetActive(true); if (backgroundObject != null) { Destroy(backgroundObject); backgroundObject = null; } } if (d.cameraTaget) { //カメラターゲット時は、カメラアームに uWC.transform.SetParent(cameraArm, false); } else { //そうでないときは独立した位置に uWC.transform.SetParent(rootLockerTransform, false); } uWC.transform.localPosition = new Vector3(d.Px, d.Py, d.Pz); uWC.transform.localRotation = Quaternion.Euler(d.Rx, d.Ry, d.Rz); uWC.scalePer1000Pixel = d.scale; } else { uWC.gameObject.SetActive(false); if (backgroundObject != null) { if (d.cameraTaget) { //カメラターゲット時は、カメラアームに backgroundObject.transform.SetParent(cameraArm, false); } else { //そうでないときは独立した位置に backgroundObject.transform.SetParent(rootLockerTransform, false); } backgroundObject.transform.localPosition = new Vector3(d.Px, d.Py, d.Pz); backgroundObject.transform.localRotation = Quaternion.Euler(d.Rx, d.Ry, d.Rz); if (bgtexture != null) { backgroundObject.transform.localScale = new Vector3(d.scale, d.scale * (float)bgtexture.height / (float)bgtexture.width, d.scale); } else { backgroundObject.transform.localScale = new Vector3(d.scale, d.scale, d.scale); } } } } //-----------詳細設定---------------- //===========EVMC4U=========== else if (e.CommandType == typeof(PipeCommands.EVMC4UControl)) { var d = (PipeCommands.EVMC4UControl)e.Data; var uosc = externalReceiver.gameObject.GetComponent <uOSC.uOscServer>(); //uOSCのポートを書き換え FieldInfo fieldInfo = typeof(uOSC.uOscServer).GetField("port", BindingFlags.NonPublic | BindingFlags.Instance); fieldInfo.SetValue(uosc, d.Port); port = d.Port; uosc.enabled = d.Enable; externalReceiver.Freeze = d.Freeze; externalReceiver.BoneFilter = d.BoneFilterValue; externalReceiver.BonePositionFilterEnable = d.BoneFilterEnable; externalReceiver.BoneRotationFilterEnable = d.BoneFilterEnable; externalReceiver.BlendShapeFilter = d.BlendShapeFilterValue; externalReceiver.BlendShapeFilterEnable = d.BlendShapeFilterEnable; } else if (e.CommandType == typeof(PipeCommands.EVMC4UTakePhotoCommand)) { var d = (PipeCommands.EVMC4UTakePhotoCommand)e.Data; GetComponent <HiResolutionPhotoCamera>().shot = true; } //===========Window=========== else if (e.CommandType == typeof(PipeCommands.WindowControl)) { var d = (PipeCommands.WindowControl)e.Data; windowManagerEx.SetThroughMouseClick(d.Transparent); windowManagerEx.SetWindowBackgroundTransparent(d.Transparent, backgroundColor); windowManagerEx.SetWindowBorder(d.NoBorder); windowManagerEx.SetWindowAlwaysTopMost(d.ForceForeground); } //===========Root位置=========== else if (e.CommandType == typeof(PipeCommands.RootPositionControl)) { var d = (PipeCommands.RootPositionControl)e.Data; cameraRootLocker.Lock = d.CameraLock; lookAtModel.enabled = d.CameraLock; lightRootLocker.Lock = d.LightLock; backgroundRootLocker.Lock = d.BackgroundLock; } //===========外部連携=========== else if (e.CommandType == typeof(PipeCommands.ExternalControl)) { var d = (PipeCommands.ExternalControl)e.Data; //TODO: OBS連動など } //===========仮想Webカメラ=========== else if (e.CommandType == typeof(PipeCommands.VirtualWebCamera)) { var d = (PipeCommands.VirtualWebCamera)e.Data; unityCapture.enabled = d.Enable; } //===========SEDSSサーバー=========== /* * else if (e.CommandType == typeof(PipeCommands.SEDSSServerControl)) * { * var d = (PipeCommands.SEDSSServerControl)e.Data; * * sedss_server.SetPassword(d.Password); * * //サーバー起動終了 * if (d.Enable) * { * ServerExchangePath = d.ExchangeFilePath; * sedss_server.StartServer(); * Debug.Log("[SEDSS Server] サーバー起動"); * } * else { * ServerExchangePath = null; * sedss_server.StopServer(); * Debug.Log("[SEDSS Server] サーバー停止"); * } * * sedss_server.OnDataUploaded = (bytes, id) => { * Debug.Log("[SEDSS Server] アップロードを受けました"); * externalReceiver.LoadVRMFromData(bytes); * return; * }; * sedss_server.OnDownloadRequest = (id) => * { * if (File.Exists(d.ExchangeFilePath)) * { * Debug.Log("[SEDSS Server] ダウンロード要求を受けました"); * byte[] data = File.ReadAllBytes(ServerExchangePath); * return data; * } * else { * Debug.LogError("[SEDSS Server] ダウンロード要求を受けましたがファイルが存在しません"); * return new byte[0]; * } * }; * } */ //===========SEDSSクライアント=========== else if (e.CommandType == typeof(PipeCommands.SEDSSClientRequestCommand)) { var d = (PipeCommands.SEDSSClientRequestCommand)e.Data; sedss_client.SetPassword(d.Password); sedss_client.SetAddress(d.Address); int port = 0; if (int.TryParse(d.Port, out port)) { sedss_client.port = port; if (d.RequestType == PipeCommands.SEDSS_RequestType.Downdload) { sedss_client.Download(d.ID, (bytes, id) => { Debug.Log("[SEDSS Client] ダウンロード完了"); externalReceiver.LoadVRMFromData(bytes); synchronizationContext.Post(async(a) => { await server.SendCommandAsync(new PipeCommands.SEDSSResult { ok = true }); hidePanel.SetActive(false); }, null); }, (err, id) => { Debug.LogError("[SEDSS Client] ダウンロード失敗(waidayoを再起動してもう一度お試しください)"); synchronizationContext.Post(async(a) => { await server.SendCommandAsync(new PipeCommands.SEDSSResult { ok = false }); hidePanel.SetActive(false); }, null); }); } else if (d.RequestType == PipeCommands.SEDSS_RequestType.Upload) { if (File.Exists(d.UploadFilePath)) { byte[] data = File.ReadAllBytes(d.UploadFilePath); sedss_client.Upload(data, d.ID, (id) => { Debug.Log("[SEDSS Client] アップロード完了"); synchronizationContext.Post(async(a) => { await server.SendCommandAsync(new PipeCommands.SEDSSResult { ok = true }); hidePanel.SetActive(false); }, null); }, (err, id) => { Debug.LogError("[SEDSS Client] アップロード失敗(waidayoを再起動してもう一度お試しください)"); synchronizationContext.Post(async(a) => { await server.SendCommandAsync(new PipeCommands.SEDSSResult { ok = false }); hidePanel.SetActive(false); }, null); }); } else { Debug.LogError("アップロードファイルが存在しません"); synchronizationContext.Post(async(a) => { await server.SendCommandAsync(new PipeCommands.SEDSSResult { ok = false }); }, null); } } else { Debug.LogError("不正なリクエスト"); synchronizationContext.Post(async(a) => { await server.SendCommandAsync(new PipeCommands.SEDSSResult { ok = false }); }, null); } } else { Debug.LogError("不正なポート番号"); synchronizationContext.Post(async(a) => { await server.SendCommandAsync(new PipeCommands.SEDSSResult { ok = false }); }, null); } } //-----------色設定---------------- //===========背景色=========== else if (e.CommandType == typeof(PipeCommands.BackgrounColor)) { var d = (PipeCommands.BackgrounColor)e.Data; Color c = new Color(d.r / 255f, d.g / 255f, d.b / 255f); backgroundColor = c; windowManagerEx.SetWindowBackgroundTransparent(false, c); } //===========ライト色=========== else if (e.CommandType == typeof(PipeCommands.LightColor)) { var d = (PipeCommands.LightColor)e.Data; light_comp.color = new Color(d.r / 255f, d.g / 255f, d.b / 255f); } //===========環境光=========== else if (e.CommandType == typeof(PipeCommands.EnvironmentColor)) { var d = (PipeCommands.EnvironmentColor)e.Data; RenderSettings.ambientLight = new Color(d.r / 255f, d.g / 255f, d.b / 255f); } //===========PostProcessing=========== else if (e.CommandType == typeof(PipeCommands.PostProcessingControl)) { var d = (PipeCommands.PostProcessingControl)e.Data; //アンチエイリアス if (d.AntiAliasingEnable) { PPSLayer.antialiasingMode = PostProcessLayer.Antialiasing.SubpixelMorphologicalAntialiasing; } else { PPSLayer.antialiasingMode = PostProcessLayer.Antialiasing.None; } var p = PPSVolume.sharedProfile; //ブルーム var bloom = p.GetSetting <Bloom>(); bloom.active = true; bloom.enabled.value = d.BloomEnable; bloom.intensity.value = d.BloomIntensity; bloom.threshold.value = d.BloomThreshold; //DoF var dof = p.GetSetting <DepthOfField>(); dof.active = true; dof.enabled.value = d.DoFEnable; dof.focusDistance.value = d.DoFFocusDistance; dof.aperture.value = d.DoFAperture; dof.focalLength.value = d.DoFFocusLength; switch (d.DoFMaxBlurSize) { case 1: dof.kernelSize.value = KernelSize.Small; break; case 2: dof.kernelSize.value = KernelSize.Medium; break; case 3: dof.kernelSize.value = KernelSize.Large; break; case 4: dof.kernelSize.value = KernelSize.VeryLarge; break; default: dof.kernelSize.value = KernelSize.Medium; break; } //CG var cg = p.GetSetting <ColorGrading>(); cg.active = true; cg.enabled.value = d.CGEnable; cg.temperature.value = d.CGTemperature; cg.saturation.value = d.CGSaturation; cg.contrast.value = d.CGContrast; var v = p.GetSetting <Vignette>(); v.active = true; v.enabled.value = d.VEnable; v.intensity.value = d.VIntensity; v.smoothness.value = d.VSmoothness; v.roundness.value = d.VRounded; var ca = p.GetSetting <ChromaticAberration>(); ca.active = true; ca.enabled.value = d.CAEnable; ca.intensity.value = d.CAIntensity; PPSVolume.sharedProfile = p; } else { //未対応のなにか } }, null); }