From eb84bb298d2b95aec7b2ae12cbf25ac64f25379a Mon Sep 17 00:00:00 2001 From: tylermurphy534 Date: Sun, 6 Nov 2022 15:12:42 -0500 Subject: move to self host --- .../VRCSDK/Dependencies/Oculus/Spatializer.meta | 8 + .../Dependencies/Oculus/Spatializer/Editor.meta | 8 + .../Spatializer/Editor/ONSPAudioSourceEditor.cs | 101 +++++ .../Editor/ONSPAudioSourceEditor.cs.meta | 12 + .../Dependencies/Oculus/Spatializer/Scripts.meta | 8 + .../Oculus/Spatializer/Scripts/ONSPAudioSource.cs | 406 +++++++++++++++++++++ .../Spatializer/Scripts/ONSPAudioSource.cs.meta | 11 + .../Oculus/Spatializer/Scripts/ONSPSettings.cs | 55 +++ .../Spatializer/Scripts/ONSPSettings.cs.meta | 11 + 9 files changed, 620 insertions(+) create mode 100644 VRCSDK3Worlds/Assets/VRCSDK/Dependencies/Oculus/Spatializer.meta create mode 100644 VRCSDK3Worlds/Assets/VRCSDK/Dependencies/Oculus/Spatializer/Editor.meta create mode 100644 VRCSDK3Worlds/Assets/VRCSDK/Dependencies/Oculus/Spatializer/Editor/ONSPAudioSourceEditor.cs create mode 100644 VRCSDK3Worlds/Assets/VRCSDK/Dependencies/Oculus/Spatializer/Editor/ONSPAudioSourceEditor.cs.meta create mode 100644 VRCSDK3Worlds/Assets/VRCSDK/Dependencies/Oculus/Spatializer/Scripts.meta create mode 100644 VRCSDK3Worlds/Assets/VRCSDK/Dependencies/Oculus/Spatializer/Scripts/ONSPAudioSource.cs create mode 100644 VRCSDK3Worlds/Assets/VRCSDK/Dependencies/Oculus/Spatializer/Scripts/ONSPAudioSource.cs.meta create mode 100644 VRCSDK3Worlds/Assets/VRCSDK/Dependencies/Oculus/Spatializer/Scripts/ONSPSettings.cs create mode 100644 VRCSDK3Worlds/Assets/VRCSDK/Dependencies/Oculus/Spatializer/Scripts/ONSPSettings.cs.meta (limited to 'VRCSDK3Worlds/Assets/VRCSDK/Dependencies/Oculus') diff --git a/VRCSDK3Worlds/Assets/VRCSDK/Dependencies/Oculus/Spatializer.meta b/VRCSDK3Worlds/Assets/VRCSDK/Dependencies/Oculus/Spatializer.meta new file mode 100644 index 00000000..7f862483 --- /dev/null +++ b/VRCSDK3Worlds/Assets/VRCSDK/Dependencies/Oculus/Spatializer.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: 67712a1ed85339c4fb4df0252af25383 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/VRCSDK3Worlds/Assets/VRCSDK/Dependencies/Oculus/Spatializer/Editor.meta b/VRCSDK3Worlds/Assets/VRCSDK/Dependencies/Oculus/Spatializer/Editor.meta new file mode 100644 index 00000000..50e9796b --- /dev/null +++ b/VRCSDK3Worlds/Assets/VRCSDK/Dependencies/Oculus/Spatializer/Editor.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: bd8bc73f073055440815e9a0c0a91ac0 +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/VRCSDK3Worlds/Assets/VRCSDK/Dependencies/Oculus/Spatializer/Editor/ONSPAudioSourceEditor.cs b/VRCSDK3Worlds/Assets/VRCSDK/Dependencies/Oculus/Spatializer/Editor/ONSPAudioSourceEditor.cs new file mode 100644 index 00000000..62ad97c2 --- /dev/null +++ b/VRCSDK3Worlds/Assets/VRCSDK/Dependencies/Oculus/Spatializer/Editor/ONSPAudioSourceEditor.cs @@ -0,0 +1,101 @@ +/************************************************************************************ +Filename : ONSPAudioSourceEditor.cs +Content : This script adds editor functionality to OculusSpatializerUserParams script. +Copyright : Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved. + +Licensed under the Oculus SDK Version 3.5 (the "License"); +you may not use the Oculus SDK except in compliance with the License, +which is provided at the time of installation or download, or which +otherwise accompanies this software in either electronic or hard copy form. + +You may obtain a copy of the License at + +https://developer.oculus.com/licenses/sdk-3.5/ + +Unless required by applicable law or agreed to in writing, the Oculus SDK +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +************************************************************************************/ +#define CUSTOM_LAYOUT + +using UnityEditor; +using UnityEngine; +using System.Collections.Generic; + +[CustomEditor(typeof(ONSPAudioSource))] + +public class OculusSpatializerUserParamsEditor : Editor +{ + // target component + private ONSPAudioSource m_Component; + + // OnEnable + void OnEnable() + { + m_Component = (ONSPAudioSource)target; + } + + // OnInspectorGUI + public override void OnInspectorGUI() + { + GUI.color = Color.white; + Undo.RecordObject(m_Component, "OculusSpatializerUserParams"); + + { + EditorGUILayout.HelpBox("Please use a VRC_SpatialAudioSource in the future.", MessageType.Error); + #if CUSTOM_LAYOUT + m_Component.EnableSpatialization = EditorGUILayout.Toggle("Spatialization Enabled", m_Component.EnableSpatialization); + m_Component.EnableRfl = EditorGUILayout.Toggle("Reflections Enabled", m_Component.EnableRfl); + m_Component.Gain = EditorGUILayout.FloatField("Gain", m_Component.Gain); + + Separator(); + + Label ("OCULUS ATTENUATION"); + m_Component.UseInvSqr = EditorGUILayout.Toggle("Enabled", m_Component.UseInvSqr); + Label (""); + Label("RANGE (0.0 - 1000000.0 meters)"); + m_Component.Near = EditorGUILayout.FloatField("Minimum", m_Component.Near); + m_Component.Far = EditorGUILayout.FloatField("Maximum", m_Component.Far); + + Label(""); + Label("VOLUMETRIC RADIUS (0.0 - 1000.0 meters)"); + m_Component.VolumetricRadius = EditorGUILayout.FloatField("Radius", m_Component.VolumetricRadius); + + Separator(); + + Label("REVERB SEND LEVEL (-60.0 - 20.0 decibels)"); + m_Component.ReverbSend = EditorGUILayout.FloatField(" ", m_Component.ReverbSend); + + Separator(); + + #else + DrawDefaultInspector (); + #endif + } + + if (GUI.changed) + { + EditorUtility.SetDirty(m_Component); + } + } + + // Utilities, move out of here (or copy over to other editor script) + + // Separator + void Separator() + { + GUI.color = new Color(1, 1, 1, 0.25f); + GUILayout.Box("", "HorizontalSlider", GUILayout.Height(16)); + GUI.color = Color.white; + } + + // Label + void Label(string label) + { + EditorGUILayout.LabelField (label); + } + +} + diff --git a/VRCSDK3Worlds/Assets/VRCSDK/Dependencies/Oculus/Spatializer/Editor/ONSPAudioSourceEditor.cs.meta b/VRCSDK3Worlds/Assets/VRCSDK/Dependencies/Oculus/Spatializer/Editor/ONSPAudioSourceEditor.cs.meta new file mode 100644 index 00000000..cabe4386 --- /dev/null +++ b/VRCSDK3Worlds/Assets/VRCSDK/Dependencies/Oculus/Spatializer/Editor/ONSPAudioSourceEditor.cs.meta @@ -0,0 +1,12 @@ +fileFormatVersion: 2 +guid: cb850b86de9091d4db4595959c56f954 +timeCreated: 1442269815 +licenseType: Pro +MonoImporter: + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/VRCSDK3Worlds/Assets/VRCSDK/Dependencies/Oculus/Spatializer/Scripts.meta b/VRCSDK3Worlds/Assets/VRCSDK/Dependencies/Oculus/Spatializer/Scripts.meta new file mode 100644 index 00000000..1399d434 --- /dev/null +++ b/VRCSDK3Worlds/Assets/VRCSDK/Dependencies/Oculus/Spatializer/Scripts.meta @@ -0,0 +1,8 @@ +fileFormatVersion: 2 +guid: cfc714641b0deb345be4779c85e44bbe +folderAsset: yes +DefaultImporter: + externalObjects: {} + userData: + assetBundleName: + assetBundleVariant: diff --git a/VRCSDK3Worlds/Assets/VRCSDK/Dependencies/Oculus/Spatializer/Scripts/ONSPAudioSource.cs b/VRCSDK3Worlds/Assets/VRCSDK/Dependencies/Oculus/Spatializer/Scripts/ONSPAudioSource.cs new file mode 100644 index 00000000..677ffb90 --- /dev/null +++ b/VRCSDK3Worlds/Assets/VRCSDK/Dependencies/Oculus/Spatializer/Scripts/ONSPAudioSource.cs @@ -0,0 +1,406 @@ +/************************************************************************************ +Filename : ONSPAudioSource.cs +Content : Interface into the Oculus Native Spatializer Plugin +Copyright : Copyright (c) Facebook Technologies, LLC and its affiliates. All rights reserved. + +Licensed under the Oculus SDK Version 3.5 (the "License"); +you may not use the Oculus SDK except in compliance with the License, +which is provided at the time of installation or download, or which +otherwise accompanies this software in either electronic or hard copy form. + +You may obtain a copy of the License at + +https://developer.oculus.com/licenses/sdk-3.5/ + +Unless required by applicable law or agreed to in writing, the Oculus SDK +distributed under the License is distributed on an "AS IS" BASIS, +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +See the License for the specific language governing permissions and +limitations under the License. +************************************************************************************/ + +// Uncomment below to test access of read-only spatializer parameters +//#define TEST_READONLY_PARAMETERS + +using UnityEngine; +using System; +using System.Collections; +using System.Runtime.InteropServices; + +public class ONSPAudioSource : MonoBehaviour +{ +#if TEST_READONLY_PARAMETERS + // Spatializer read-only system parameters (global) + static int readOnly_GlobalRelectionOn = 8; + static int readOnly_NumberOfUsedSpatializedVoices = 9; +#endif + +#if VRC_CLIENT + // don't include in SDK, not compatible with Unity's version of spatializer + [RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)] + static void OnBeforeSceneLoadRuntimeMethod() + { + OSP_SetGlobalVoiceLimit(ONSPSettings.Instance.voiceLimit); + } +#endif + + // Import functions + public const string strONSPS = "AudioPluginOculusSpatializer"; + + [DllImport(strONSPS)] + private static extern void ONSP_GetGlobalRoomReflectionValues(ref bool reflOn, ref bool reverbOn, + ref float width, ref float height, ref float length); + + // Public + + [SerializeField] + private bool enableSpatialization = true; + public bool EnableSpatialization + { + get + { + return enableSpatialization; + } + set + { + enableSpatialization = value; + } + } + + [SerializeField] + private float gain = 0.0f; + public float Gain + { + get + { + return gain; + } + set + { + gain = Mathf.Clamp(value, 0.0f, 24.0f); + } + } + + [SerializeField] + private bool useInvSqr = false; + public bool UseInvSqr + { + get + { + return useInvSqr; + } + set + { + useInvSqr = value; + } + } + + [SerializeField] + private float near = 0.25f; + public float Near + { + get + { + return near; + } + set + { + near = Mathf.Clamp(value, 0.0f, 1000000.0f); + } + } + + [SerializeField] + private float far = 250.0f; + public float Far + { + get + { + return far; + } + set + { + far = Mathf.Clamp(value, 0.0f, 1000000.0f); + } + } + + [SerializeField] + private float volumetricRadius = 0.0f; + public float VolumetricRadius + { + get + { + return volumetricRadius; + } + set + { + volumetricRadius = Mathf.Clamp(value, 0.0f, 1000.0f); + } + } + + [SerializeField] + private float reverbSend = 0.0f; + public float ReverbSend + { + get + { + return reverbSend; + } + set + { + reverbSend = Mathf.Clamp(value, -60.0f, 20.0f); + } + } + + + [SerializeField] + private bool enableRfl = false; + public bool EnableRfl + { + get + { + return enableRfl; + } + set + { + enableRfl = value; + } + } + + + /// VRCHAT: We need to reset the params after avatar loading and viseme setup + public void Reset() + { + var source = GetComponent(); + + if(source == null) + { + enabled = false; + return; + } + + SetParameters(ref source); + } + + /// + /// Awake this instance. + /// + void Awake() + { + Reset(); + } + + /// + /// Start this instance. + /// + void Start() + { +#if VRC_CLIENT + var source = GetComponent(); + + if(source != null && source.outputAudioMixerGroup != VRCAudioManager.GetAvatarGroup()) + VRCAudioManager.ApplyGameAudioMixerSettings(source); +#endif + } + + /// + /// Update this instance. + /// + void LateUpdate() + { + // We might iterate through multiple sources / game object + var source = GetComponent(); + + if(source == null) + { + enabled = false; + return; + } + + // READ-ONLY PARAMETER TEST +#if TEST_READONLY_PARAMETERS + float rfl_enabled = 0.0f; + source.GetSpatializerFloat(readOnly_GlobalRelectionOn, out rfl_enabled); + float num_voices = 0.0f; + source.GetSpatializerFloat(readOnly_NumberOfUsedSpatializedVoices, out num_voices); + + String readOnly = System.String.Format + ("Read only values: refl enabled: {0:F0} num voices: {1:F0}", rfl_enabled, num_voices); + Debug.Log(readOnly); +#endif + + // Check to see if we should disable spatializion + if ((Application.isPlaying == false) || + (AudioListener.pause == true) || + (source.isPlaying == false) || + (source.isActiveAndEnabled == false) + ) + { + source.spatialize = false; + return; + } + else + { + SetParameters(ref source); + } + } + + enum Parameters : int + { + P_GAIN = 0, + P_USEINVSQR, + P_NEAR, + P_FAR, + P_RADIUS, + P_DISABLE_RFL, + P_VSPEAKERMODE, + P_AMBISTAT, + P_READONLY_GLOBAL_RFL_ENABLED, // READ-ONLY + P_READONLY_NUM_VOICES, // READ-ONLY + P_SENDLEVEL, + P_NUM + }; + + /// + /// Sets the parameters. + /// + /// Source. + public void SetParameters(ref AudioSource source) + { + // VRCHAT: indentation is weird intentionally, for easier diff + // VRC jnuccio: added try here to catch unknown exception reported in analytics + try + { + if (source == null) + return; + + // See if we should enable spatialization + source.spatialize = enableSpatialization; + + source.SetSpatializerFloat((int)Parameters.P_GAIN, gain); + // All inputs are floats; convert bool to 0.0 and 1.0 + if(useInvSqr == true) + source.SetSpatializerFloat((int)Parameters.P_USEINVSQR, 1.0f); + else + source.SetSpatializerFloat((int)Parameters.P_USEINVSQR, 0.0f); + + source.SetSpatializerFloat((int)Parameters.P_NEAR, near); + source.SetSpatializerFloat((int)Parameters.P_FAR, far); + + source.SetSpatializerFloat((int)Parameters.P_RADIUS, volumetricRadius); + + if(enableRfl == true) + source.SetSpatializerFloat((int)Parameters.P_DISABLE_RFL, 0.0f); + else + source.SetSpatializerFloat((int)Parameters.P_DISABLE_RFL, 1.0f); + + source.SetSpatializerFloat((int)Parameters.P_SENDLEVEL, reverbSend); + + // VRCHAT: indentation is weird intentionally, for easier diff + } + catch (Exception) + { + // not sure why this throws sometimes + } + } + + private static ONSPAudioSource RoomReflectionGizmoAS = null; + + /// + /// + /// + void OnDrawGizmos() + { + // Are we the first one created? make sure to set our static ONSPAudioSource + // for drawing out room parameters once + if(RoomReflectionGizmoAS == null) + { + RoomReflectionGizmoAS = this; + } + + Color c; + const float colorSolidAlpha = 0.1f; + + // Draw the near/far spheres + + // Near (orange) + c.r = 1.0f; + c.g = 0.5f; + c.b = 0.0f; + c.a = 1.0f; + Gizmos.color = c; + Gizmos.DrawWireSphere(transform.position, Near); + c.a = colorSolidAlpha; + Gizmos.color = c; + Gizmos.DrawSphere(transform.position, Near); + + // Far (red) + c.r = 1.0f; + c.g = 0.0f; + c.b = 0.0f; + c.a = 1.0f; + Gizmos.color = Color.red; + Gizmos.DrawWireSphere(transform.position, Far); + c.a = colorSolidAlpha; + Gizmos.color = c; + Gizmos.DrawSphere(transform.position, Far); + + // VolumetricRadius (purple) + c.r = 1.0f; + c.g = 0.0f; + c.b = 1.0f; + c.a = 1.0f; + Gizmos.color = c; + Gizmos.DrawWireSphere(transform.position, VolumetricRadius); + c.a = colorSolidAlpha; + Gizmos.color = c; + Gizmos.DrawSphere(transform.position, VolumetricRadius); + + // Draw room parameters ONCE only, provided reflection engine is on + if (RoomReflectionGizmoAS == this) + { + // Get global room parameters (write new C api to get reflection values) + bool reflOn = false; + bool reverbOn = false; + float width = 1.0f; + float height = 1.0f; + float length = 1.0f; + + ONSP_GetGlobalRoomReflectionValues(ref reflOn, ref reverbOn, ref width, ref height, ref length); + + // TO DO: Get the room reflection values and render those out as well (like we do in the VST) + + if((Camera.main != null) && (reflOn == true)) + { + // Set color of cube (cyan is early reflections only, white is with reverb on) + if(reverbOn == true) + c = Color.white; + else + c = Color.cyan; + + Gizmos.color = c; + Gizmos.DrawWireCube(Camera.main.transform.position, new Vector3(width, height, length)); + c.a = colorSolidAlpha; + Gizmos.color = c; + Gizmos.DrawCube(Camera.main.transform.position, new Vector3(width, height, length)); + } + } + } + + /// + /// + /// + void OnDestroy() + { + // We will null out single pointer instance + // of the room reflection gizmo since we are being destroyed. + // Any ONSPAS that is alive or born will re-set this pointer + // so that we only draw it once + if(RoomReflectionGizmoAS == this) + { + RoomReflectionGizmoAS = null; + } + } + + [System.Runtime.InteropServices.DllImport("AudioPluginOculusSpatializer")] + private static extern int OSP_SetGlobalVoiceLimit(int VoiceLimit); +} diff --git a/VRCSDK3Worlds/Assets/VRCSDK/Dependencies/Oculus/Spatializer/Scripts/ONSPAudioSource.cs.meta b/VRCSDK3Worlds/Assets/VRCSDK/Dependencies/Oculus/Spatializer/Scripts/ONSPAudioSource.cs.meta new file mode 100644 index 00000000..a5d0b64e --- /dev/null +++ b/VRCSDK3Worlds/Assets/VRCSDK/Dependencies/Oculus/Spatializer/Scripts/ONSPAudioSource.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: e503ea6418d27594caa33b93cac1b06a +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 1 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: diff --git a/VRCSDK3Worlds/Assets/VRCSDK/Dependencies/Oculus/Spatializer/Scripts/ONSPSettings.cs b/VRCSDK3Worlds/Assets/VRCSDK/Dependencies/Oculus/Spatializer/Scripts/ONSPSettings.cs new file mode 100644 index 00000000..de9c0c19 --- /dev/null +++ b/VRCSDK3Worlds/Assets/VRCSDK/Dependencies/Oculus/Spatializer/Scripts/ONSPSettings.cs @@ -0,0 +1,55 @@ +using UnityEngine; +using System.Collections; + +#if UNITY_EDITOR +[UnityEditor.InitializeOnLoad] +#endif +public sealed class ONSPSettings : ScriptableObject +{ + [SerializeField] + public int voiceLimit = 16; // very conservative fallback + + private static ONSPSettings instance; + public static ONSPSettings Instance + { + get + { + if (instance == null) + { +#if UNITY_ANDROID + instance = Resources.Load("ONSPSettings-Android"); +#else + instance = Resources.Load("ONSPSettings"); +#endif + + // This can happen if the developer never input their App Id into the Unity Editor + // and therefore never created the OculusPlatformSettings.asset file + // Use a dummy object with defaults for the getters so we don't have a null pointer exception + if (instance == null) + { + instance = ScriptableObject.CreateInstance(); + +#if UNITY_EDITOR + // Only in the editor should we save it to disk + string properPath = System.IO.Path.Combine(UnityEngine.Application.dataPath, "Resources"); + if (!System.IO.Directory.Exists(properPath)) + { + UnityEditor.AssetDatabase.CreateFolder("Assets", "Resources"); + } + + string fullPath = System.IO.Path.Combine( + System.IO.Path.Combine("Assets", "Resources"), +#if UNITY_ANDROID + "ONSPSettings-Android.asset"); +#else + "ONSPSettings.asset"); +#endif + UnityEditor.AssetDatabase.CreateAsset(instance, fullPath); +#endif + } + } + + return instance; + } + } +} diff --git a/VRCSDK3Worlds/Assets/VRCSDK/Dependencies/Oculus/Spatializer/Scripts/ONSPSettings.cs.meta b/VRCSDK3Worlds/Assets/VRCSDK/Dependencies/Oculus/Spatializer/Scripts/ONSPSettings.cs.meta new file mode 100644 index 00000000..95435008 --- /dev/null +++ b/VRCSDK3Worlds/Assets/VRCSDK/Dependencies/Oculus/Spatializer/Scripts/ONSPSettings.cs.meta @@ -0,0 +1,11 @@ +fileFormatVersion: 2 +guid: ad074644ff568a14187a3690cfbd7534 +MonoImporter: + externalObjects: {} + serializedVersion: 2 + defaultReferences: [] + executionOrder: 0 + icon: {instanceID: 0} + userData: + assetBundleName: + assetBundleVariant: -- cgit v1.2.3-freya