public void ExportScene(MaterialValue defaultMaterial, bool exportOBJ)
 {
     try
     {
         scene.Export(computeDevice, simulationSettings, defaultMaterial, context, exportOBJ);
     }
     catch (Exception e)
     {
         Debug.LogError("Phonon Geometry not attached. " + e.Message);
     }
 }
Exemple #2
0
 public void ExportScene(MaterialValue defaultMaterial, bool exportOBJ)
 {
     try
     {
         scene.Export(computeDevice, defaultMaterial, context, exportOBJ);
     }
     catch (Exception e)
     {
         Debug.LogError(e.Message);
     }
 }
Exemple #3
0
        //
        // Copies data from another object.
        //
        public void CopyFrom(MaterialValue other)
        {
            LowFreqAbsorption  = other.LowFreqAbsorption;
            MidFreqAbsorption  = other.MidFreqAbsorption;
            HighFreqAbsorption = other.HighFreqAbsorption;

            Scattering = other.Scattering;

            LowFreqTransmission  = other.LowFreqTransmission;
            MidFreqTransmission  = other.MidFreqTransmission;
            HighFreqTransmission = other.HighFreqTransmission;
        }
Exemple #4
0
        static Material CopyMaterial(MaterialValue materialValue)
        {
            var material = new Material();

            material.absorptionLow    = materialValue.LowFreqAbsorption;
            material.absorptionMid    = materialValue.MidFreqAbsorption;
            material.absorptionHigh   = materialValue.HighFreqAbsorption;
            material.scattering       = materialValue.Scattering;
            material.transmissionLow  = materialValue.LowFreqTransmission;
            material.transmissionMid  = materialValue.MidFreqTransmission;
            material.transmissionHigh = materialValue.HighFreqTransmission;
            return(material);
        }
        static Material MaterialFromSteamAudioMaterial(MaterialValue steamAudioMaterial)
        {
            var material = new Material();

            material.absorptionLow    = steamAudioMaterial.LowFreqAbsorption;
            material.absorptionMid    = steamAudioMaterial.MidFreqAbsorption;
            material.absorptionHigh   = steamAudioMaterial.HighFreqAbsorption;
            material.scattering       = steamAudioMaterial.Scattering;
            material.transmissionLow  = steamAudioMaterial.LowFreqTransmission;
            material.transmissionMid  = steamAudioMaterial.MidFreqTransmission;
            material.transmissionHigh = steamAudioMaterial.HighFreqTransmission;
            return(material);
        }
        //
        // Draws the inspector.
        //
        public override void OnInspectorGUI()
        {
            serializedObject.Update();

            EditorGUILayout.Space();
            EditorGUILayout.LabelField("Material Preset", EditorStyles.boldLabel);
            EditorGUILayout.PropertyField(serializedObject.FindProperty("Preset"));

            if (serializedObject.FindProperty("Preset").enumValueIndex < 11)
            {
                MaterialValue actualValue = ((SteamAudioMaterial)target).Value;
                actualValue.CopyFrom(MaterialPresetList.PresetValue(serializedObject.FindProperty("Preset").enumValueIndex));
            }
            else
            {
                EditorGUILayout.LabelField("Custom Material", EditorStyles.boldLabel);
                EditorGUILayout.PropertyField(serializedObject.FindProperty("Value"));
            }

            EditorGUILayout.Space();

            // Save changes.
            serializedObject.ApplyModifiedProperties();
        }
Exemple #7
0
        public Error Export(ComputeDevice computeDevice, MaterialValue defaultMaterial,
                            IntPtr globalContext, bool exportOBJ = false)
        {
            var error = Error.None;

            SceneType sceneType = SceneType.Phonon;    // Scene type should always be Phonon when exporting.

            var objects = SceneExporter.GetStaticGameObjectsForExport(SceneManager.GetActiveScene());

            Vector3[]  vertices        = null;
            Triangle[] triangles       = null;
            int[]      materialIndices = null;
            Material[] materials       = null;
            SceneExporter.GetGeometryAndMaterialBuffers(objects, ref vertices, ref triangles, ref materialIndices,
                                                        ref materials, false, exportOBJ);

            if (vertices.Length == 0 || triangles.Length == 0 || materialIndices.Length == 0 || materials.Length == 0)
            {
                throw new Exception(
                          "No Steam Audio Geometry tagged. Attach Steam Audio Geometry to one or more GameObjects that " +
                          "contain Mesh or Terrain geometry.");
            }

            error = PhononCore.iplCreateScene(globalContext, computeDevice.GetDevice(), sceneType,
                                              materials.Length, materials, null, null, null, null, IntPtr.Zero, ref scene);
            if (error != Error.None)
            {
                throw new Exception("Unable to create scene for export (" + materials.Length.ToString() +
                                    " materials): [" + error.ToString() + "]");
            }

            var staticMesh = IntPtr.Zero;

            error = PhononCore.iplCreateStaticMesh(scene, vertices.Length, triangles.Length, vertices, triangles,
                                                   materialIndices, ref staticMesh);
            if (error != Error.None)
            {
                throw new Exception("Unable to create static mesh for export (" + vertices.Length.ToString() +
                                    " vertices, " + triangles.Length.ToString() + " triangles): [" + error.ToString() + "]");
            }

#if UNITY_EDITOR
            if (!Directory.Exists(Application.streamingAssetsPath))
            {
                UnityEditor.AssetDatabase.CreateFolder("Assets", "StreamingAssets");
            }
#endif

            if (exportOBJ)
            {
                PhononCore.iplSaveSceneAsObj(scene, Common.ConvertString(ObjFileName()));
                Debug.Log("Scene exported to " + ObjFileName() + ".");
            }
            else
            {
                var dataSize = PhononCore.iplSaveScene(scene, null);
                var data     = new byte[dataSize];
                PhononCore.iplSaveScene(scene, data);

                var fileName = SceneFileName();
                File.WriteAllBytes(fileName, data);

                Debug.Log("Scene exported to " + fileName + ".");
            }

            PhononCore.iplDestroyStaticMesh(ref staticMesh);
            PhononCore.iplDestroyScene(ref scene);
            return(error);
        }
        //
        // Draws the inspector.
        //
        public override void OnInspectorGUI()
        {
            serializedObject.Update();

            // Audio Engine
            GUI.enabled = !EditorApplication.isPlayingOrWillChangePlaymode;
            EditorGUILayout.Space();
            EditorGUILayout.LabelField("Audio Engine Integration", EditorStyles.boldLabel);
            string[] engines             = { "Unity", "FMOD Studio" };
            var      audioEngineProperty = serializedObject.FindProperty("audioEngine");

            audioEngineProperty.enumValueIndex = EditorGUILayout.Popup("Audio Engine",
                                                                       audioEngineProperty.enumValueIndex, engines);

            // Scene Settings
            SteamAudioManager steamAudioManager = ((SteamAudioManager)target);

            EditorGUILayout.Space();
            EditorGUILayout.LabelField("Global Material Settings", EditorStyles.boldLabel);
            EditorGUILayout.PropertyField(serializedObject.FindProperty("materialPreset"));
            if (serializedObject.FindProperty("materialPreset").enumValueIndex < 11)
            {
                MaterialValue actualValue = steamAudioManager.materialValue;
                actualValue.CopyFrom(MaterialPresetList.PresetValue(
                                         serializedObject.FindProperty("materialPreset").enumValueIndex));
            }
            else
            {
                EditorGUILayout.PropertyField(serializedObject.FindProperty("materialValue"));
            }

            EditorGUILayout.Space();
            EditorGUILayout.LabelField("Scene Export", EditorStyles.boldLabel);
            EditorGUILayout.BeginHorizontal();
            EditorGUILayout.PrefixLabel(" ");
            if (GUILayout.Button("Export to OBJ"))
            {
                steamAudioManager.ExportScene(true);
            }
            if (GUILayout.Button("Pre-Export Scene"))
            {
                steamAudioManager.ExportScene(false);
            }
            EditorGUILayout.EndHorizontal();

            // Simulation Settings
            EditorGUILayout.Space();
            EditorGUILayout.LabelField("Simulation Settings", EditorStyles.boldLabel);
            EditorGUILayout.PropertyField(serializedObject.FindProperty("simulationPreset"));
            if (serializedObject.FindProperty("simulationPreset").enumValueIndex < 3)
            {
                SimulationSettingsValue actualValue = steamAudioManager.simulationValue;
                actualValue.CopyFrom(SimulationSettingsPresetList.PresetValue(
                                         serializedObject.FindProperty("simulationPreset").enumValueIndex));
            }
            else
            {
                EditorGUILayout.PropertyField(serializedObject.FindProperty("simulationValue"));

                SimulationSettingsValue actualValue = steamAudioManager.simulationValue;

                if (actualValue.AmbisonicsOrder > 2)
                {
                    var numChannels = (actualValue.AmbisonicsOrder + 1) * (actualValue.AmbisonicsOrder + 1);
                    EditorGUILayout.HelpBox("Ambisonics order " + actualValue.AmbisonicsOrder.ToString() +
                                            " uses " + numChannels.ToString() + " channels per source for processing indirect sound. " +
                                            "This may significantly increase CPU usage. Consider reducing this value unless necessary.",
                                            MessageType.Warning);
                }

                if (Application.isEditor && EditorApplication.isPlayingOrWillChangePlaymode)
                {
                    IntPtr environment = steamAudioManager.GameEngineState().Environment().GetEnvironment();
                    if (environment != IntPtr.Zero)
                    {
                        PhononCore.iplSetNumBounces(environment, actualValue.RealtimeBounces);
                    }
                }
            }

            // Fold Out for Advanced Settings
            EditorGUILayout.Space();
            EditorGUILayout.LabelField("Advanced Options", EditorStyles.boldLabel);
            serializedObject.FindProperty("showLoadTimeOptions").boolValue =
                EditorGUILayout.Foldout(serializedObject.FindProperty("showLoadTimeOptions").boolValue,
                                        "Per Frame Query Optimization");

            if (steamAudioManager.showLoadTimeOptions)
            {
                EditorGUILayout.PropertyField(serializedObject.FindProperty("updateComponents"));

                var updateComponents = serializedObject.FindProperty("updateComponents").boolValue;
                if (!updateComponents)
                {
                    EditorGUILayout.PropertyField(serializedObject.FindProperty("currentAudioListener"));
                }
            }

            serializedObject.FindProperty("showMassBakingOptions").boolValue =
                EditorGUILayout.Foldout(serializedObject.FindProperty("showMassBakingOptions").boolValue,
                                        "Consolidated Baking Options");
            if (steamAudioManager.showMassBakingOptions)
            {
                bool noSettingMessage = false;
                noSettingMessage = ProbeGenerationGUI() || noSettingMessage;
                noSettingMessage = BakeAllGUI() || noSettingMessage;
                noSettingMessage = BakedSourcesGUI(steamAudioManager) || noSettingMessage;
                noSettingMessage = BakedStaticListenerNodeGUI(steamAudioManager) || noSettingMessage;
                noSettingMessage = BakedReverbGUI(steamAudioManager) || noSettingMessage;

                if (!noSettingMessage)
                {
                    EditorGUILayout.LabelField("Scene does not contain any baking related components.");
                }
            }

            GUI.enabled = true;
            EditorGUILayout.HelpBox("Do not manually add Steam Audio Manager component. " +
                                    "Click Window > Steam Audio.", MessageType.Info);

            EditorGUILayout.Space();
            serializedObject.ApplyModifiedProperties();
        }
Exemple #9
0
        public Error Export(ComputeDevice computeDevice, SimulationSettings simulationSettings,
                            MaterialValue defaultMaterial, IntPtr globalContext, bool exportOBJ = false)
        {
            var error = Error.None;

            var objects           = GameObject.FindObjectsOfType <SteamAudioGeometry>();
            var totalNumVertices  = 0;
            var totalNumTriangles = 0;
            var totalNumMaterials = 1;  // Global material.

            for (var i = 0; i < objects.Length; ++i)
            {
                totalNumVertices  += objects[i].GetNumVertices();
                totalNumTriangles += objects[i].GetNumTriangles();
                totalNumMaterials += objects[i].GetNumMaterials();
            }

            simulationSettings.sceneType = SceneType.Phonon;    // Scene type should always be Phonon when exporting.

            var vertices        = new Vector3[totalNumVertices];
            var triangles       = new Triangle[totalNumTriangles];
            var materialIndices = new int[totalNumTriangles];
            var materials       = new Material[totalNumMaterials + 1]; // Offset added to avoid creating Material
                                                                       // for each object and then copying it.

            var vertexOffset   = 0;
            var triangleOffset = 0;

            var materialOffset = 1;

            materials[0].absorptionHigh   = defaultMaterial.HighFreqAbsorption;
            materials[0].absorptionMid    = defaultMaterial.MidFreqAbsorption;
            materials[0].absorptionLow    = defaultMaterial.LowFreqAbsorption;
            materials[0].scattering       = defaultMaterial.Scattering;
            materials[0].transmissionHigh = defaultMaterial.HighFreqTransmission;
            materials[0].transmissionMid  = defaultMaterial.MidFreqTransmission;
            materials[0].transmissionLow  = defaultMaterial.LowFreqTransmission;

            for (var i = 0; i < objects.Length; ++i)
            {
                objects[i].GetGeometry(vertices, ref vertexOffset, triangles, ref triangleOffset, materials,
                                       materialIndices, ref materialOffset);
            }

            error = PhononCore.iplCreateScene(globalContext, computeDevice.GetDevice(), simulationSettings,
                                              materials.Length, materials, null, null, null, null, IntPtr.Zero, ref scene);
            if (error != Error.None)
            {
                throw new Exception("Unable to create scene for export (" + objects.Length.ToString() +
                                    " materials): [" + error.ToString() + "]");
            }

            var staticMesh = IntPtr.Zero;

            error = PhononCore.iplCreateStaticMesh(scene, totalNumVertices, totalNumTriangles, vertices, triangles,
                                                   materialIndices, ref staticMesh);
            if (error != Error.None)
            {
                throw new Exception("Unable to create static mesh for export (" + totalNumVertices.ToString() +
                                    " vertices, " + totalNumTriangles.ToString() + " triangles): [" + error.ToString() + "]");
            }

#if UNITY_EDITOR
            if (!Directory.Exists(Application.streamingAssetsPath))
            {
                UnityEditor.AssetDatabase.CreateFolder("Assets", "StreamingAssets");
            }
#endif

            if (exportOBJ)
            {
                PhononCore.iplDumpSceneToObjFile(scene, Common.ConvertString(ObjFileName()));
                Debug.Log("Scene dumped to " + ObjFileName() + ".");
            }
            else
            {
                var dataSize = PhononCore.iplSaveFinalizedScene(scene, null);
                var data     = new byte[dataSize];
                PhononCore.iplSaveFinalizedScene(scene, data);

                var fileName = SceneFileName();
                File.WriteAllBytes(fileName, data);

                Debug.Log("Scene exported to " + fileName + ".");
            }

            PhononCore.iplDestroyStaticMesh(ref staticMesh);
            PhononCore.iplDestroyScene(ref scene);
            return(error);
        }
        //
        // Draws the inspector.
        //
        public override void OnInspectorGUI()
        {
            serializedObject.Update();

            // Audio Engine
            GUI.enabled = !EditorApplication.isPlayingOrWillChangePlaymode;
            EditorGUILayout.Space();
            EditorGUILayout.HelpBox("Do not manually add Steam Audio Manager component. " +
                                    "Click Window > Steam Audio.", MessageType.Info);
            EditorGUILayout.LabelField("Audio Engine Integration", EditorStyles.boldLabel);
            string[] engines             = { "Unity", "FMOD Studio" };
            var      audioEngineProperty = serializedObject.FindProperty("audioEngine");

            audioEngineProperty.enumValueIndex = EditorGUILayout.Popup("Audio Engine",
                                                                       audioEngineProperty.enumValueIndex, engines);

            EditorGUILayout.Space();
            EditorGUILayout.LabelField("HRTF Settings", EditorStyles.boldLabel);
            EditorGUILayout.PropertyField(serializedObject.FindProperty("sofaFiles"), new GUIContent("SOFA Files"), true);

            SteamAudioManager steamAudioManager = (SteamAudioManager)target;

            var wasGUIEnabled = GUI.enabled;

            GUI.enabled = true;

            var sofaFiles = steamAudioManager.GetSOFAFileNames();

            if (sofaFiles != null)
            {
                var sofaFileNames = new string[sofaFiles.Length];
                for (var i = 0; i < sofaFileNames.Length; ++i)
                {
                    sofaFileNames[i] = sofaFiles[i];
                    if (sofaFileNames[i].Length == 0 || sofaFileNames[i] == "")
                    {
                        sofaFileNames[i] = "Default";
                    }
                }
                var currentSOFAFileProperty = serializedObject.FindProperty("currentSOFAFile");
                if (currentSOFAFileProperty.intValue >= sofaFiles.Length)
                {
                    currentSOFAFileProperty.intValue = 0;
                }
                currentSOFAFileProperty.intValue = EditorGUILayout.Popup("Current SOFA File",
                                                                         currentSOFAFileProperty.intValue, sofaFileNames);
            }

            GUI.enabled = wasGUIEnabled;

            // Scene Settings
            EditorGUILayout.Space();
            EditorGUILayout.LabelField("Global Material Settings", EditorStyles.boldLabel);
            EditorGUILayout.PropertyField(serializedObject.FindProperty("materialPreset"));
            if (serializedObject.FindProperty("materialPreset").enumValueIndex < 11)
            {
                MaterialValue actualValue = steamAudioManager.materialValue;
                actualValue.CopyFrom(MaterialPresetList.PresetValue(
                                         serializedObject.FindProperty("materialPreset").enumValueIndex));
            }
            else
            {
                EditorGUILayout.PropertyField(serializedObject.FindProperty("materialValue"));
            }

            EditorGUILayout.Space();
            EditorGUILayout.LabelField("Scene Export", EditorStyles.boldLabel);
            if (GUILayout.Button("Pre-Export Scene"))
            {
                steamAudioManager.ExportScene(false);
            }
            if (GUILayout.Button("Export All Dynamic Objects"))
            {
                ExportAllDynamicObjects();
            }
            if (GUILayout.Button("Export to OBJ"))
            {
                steamAudioManager.ExportScene(true);
            }

            // Simulation Settings
            EditorGUILayout.Space();
            EditorGUILayout.LabelField("Simulation Settings", EditorStyles.boldLabel);
            EditorGUILayout.PropertyField(serializedObject.FindProperty("simulationPreset"));
            if (serializedObject.FindProperty("simulationPreset").enumValueIndex < 3)
            {
                SimulationSettingsValue actualValue = steamAudioManager.simulationValue;
                actualValue.CopyFrom(SimulationSettingsPresetList.PresetValue(
                                         serializedObject.FindProperty("simulationPreset").enumValueIndex));

                EditorGUILayout.Space();
                UnityRayTracerGUI(true);
            }
            else
            {
                EditorGUILayout.PropertyField(serializedObject.FindProperty("simulationValue"));

                SimulationSettingsValue actualValue = steamAudioManager.simulationValue;

                if (actualValue.AmbisonicsOrder > 2)
                {
                    var numChannels = (actualValue.AmbisonicsOrder + 1) * (actualValue.AmbisonicsOrder + 1);
                    EditorGUILayout.HelpBox("Ambisonics order " + actualValue.AmbisonicsOrder.ToString() +
                                            " uses " + numChannels.ToString() + " channels per source for processing indirect sound. " +
                                            "This may significantly increase CPU usage. Consider reducing this value unless necessary.",
                                            MessageType.Warning);
                }

                UnityRayTracerGUI(false);

                if (Application.isEditor && EditorApplication.isPlayingOrWillChangePlaymode)
                {
                    IntPtr environment = steamAudioManager.GameEngineState().Environment().GetEnvironment();
                    if (environment != IntPtr.Zero)
                    {
                        PhononCore.iplSetNumBounces(environment, actualValue.RealtimeBounces);
                    }
                }
            }

            // Display information message about CPU usage.
            int    realTimeThreads = (int)Mathf.Max(1, (steamAudioManager.simulationValue.RealtimeThreadsPercentage * SystemInfo.processorCount) / 100.0f);
            int    bakingThreads   = (int)Mathf.Max(1, (steamAudioManager.simulationValue.BakeThreadsPercentage * SystemInfo.processorCount) / 100.0f);
            string realTimeString  = (realTimeThreads == 1) ? "1 of " + SystemInfo.processorCount + " logical processor" :
                                     realTimeThreads + " of " + SystemInfo.processorCount + " logical processors";
            string bakingString = (bakingThreads == 1) ? "1 of " + SystemInfo.processorCount + " logical processor" :
                                  bakingThreads + " of " + SystemInfo.processorCount + " logical processors";

            EditorGUILayout.HelpBox("On this machine setting realtime CPU cores to " + steamAudioManager.simulationValue.RealtimeThreadsPercentage +
                                    "% will use " + realTimeString + " and setting baking CPU cores to " + steamAudioManager.simulationValue.BakeThreadsPercentage +
                                    "% will use " + bakingString + ". The number of logical processors used on an end user's machine might be different.", MessageType.Info);

            // Fold Out for Advanced Settings
            EditorGUILayout.Space();
            EditorGUILayout.LabelField("Advanced Options", EditorStyles.boldLabel);
            serializedObject.FindProperty("showLoadTimeOptions").boolValue =
                EditorGUILayout.Foldout(serializedObject.FindProperty("showLoadTimeOptions").boolValue,
                                        "Per Frame Query Optimization");

            if (steamAudioManager.showLoadTimeOptions)
            {
                EditorGUILayout.PropertyField(serializedObject.FindProperty("updateComponents"));

                var updateComponents = serializedObject.FindProperty("updateComponents").boolValue;
                if (!updateComponents)
                {
                    EditorGUILayout.PropertyField(serializedObject.FindProperty("currentAudioListener"));
                }
            }

            serializedObject.FindProperty("showMassBakingOptions").boolValue =
                EditorGUILayout.Foldout(serializedObject.FindProperty("showMassBakingOptions").boolValue,
                                        "Consolidated Baking Options");
            if (steamAudioManager.showMassBakingOptions)
            {
                bool noSettingMessage = false;
                noSettingMessage = ProbeGenerationGUI() || noSettingMessage;
                noSettingMessage = BakeAllGUI() || noSettingMessage;
                noSettingMessage = BakedSourcesGUI(steamAudioManager) || noSettingMessage;
                noSettingMessage = BakedStaticListenerNodeGUI(steamAudioManager) || noSettingMessage;
                noSettingMessage = BakedReverbGUI(steamAudioManager) || noSettingMessage;

                if (!noSettingMessage)
                {
                    EditorGUILayout.LabelField("Scene does not contain any baking related components.");
                }
            }

            GUI.enabled = true;
            EditorGUILayout.Space();
            serializedObject.ApplyModifiedProperties();
        }
Exemple #11
0
 //
 // Copy constructor.
 //
 public MaterialValue(MaterialValue other)
 {
     CopyFrom(other);
 }