Exemple #1
0
        public Error Export(ComputeDevice computeDevice, MaterialValue defaultMaterial,
                            IntPtr globalContext, bool exportOBJ = false)
        {
            SceneType sceneType           = SceneType.Phonon; // Scene type should always be Phonon when exporting.
            int       exportedScenesCount = 0;

            for (int i = 0; i < SceneManager.sceneCount; ++i)
            {
                var unityScene = SceneManager.GetSceneAt(i);
                if (!unityScene.isLoaded)
                {
                    Debug.LogWarning("Scene " + SceneManager.GetSceneAt(i).name + " is not loaded in the hierarchy.");
                    continue;
                }

                var objects = SceneExporter.GetStaticGameObjectsForExport(unityScene);

                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)
                {
                    Debug.LogWarning("Scene " + unityScene.name + " has no Steam Audio geometry.");
                    continue;
                }

                ++exportedScenesCount;

                Error 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)
                {
                    var fileName = ObjFileName(unityScene.name);
                    PhononCore.iplSaveSceneAsObj(scene, Common.ConvertString(fileName));
                    Debug.Log("Scene exported to " + fileName + ".");
                }
                else
                {
                    var dataSize = PhononCore.iplSaveScene(scene, null);
                    var data     = new byte[dataSize];
                    PhononCore.iplSaveScene(scene, data);

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

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

                PhononCore.iplDestroyStaticMesh(ref staticMesh);
                PhononCore.iplDestroyScene(ref scene);
            }

            if (exportedScenesCount == 0)
            {
                throw new Exception(
                          "No Steam Audio Geometry tagged in the scene hierarchy. Attach Steam Audio Geometry " +
                          "to one or more GameObjects that contain Mesh or Terrain geometry.");
            }

            return(Error.None);
        }
        public Error Export(ComputeDevice computeDevice, SimulationSettings simulationSettings,
                            MaterialValue defaultMaterial, IntPtr globalContext, bool exportOBJ = false)
        {
            var error = Error.None;

            simulationSettings.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(), 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 (" + 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);
        }
Exemple #3
0
        public Error Export(ComputeDevice computeDevice, SimulationSettings simulationSettings,
                            MaterialValue defaultMaterial, GlobalContext 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.

            error = PhononCore.iplCreateScene(globalContext, computeDevice.GetDevice(), simulationSettings,
                                              totalNumMaterials, 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, ref staticMesh);
            if (error != Error.None)
            {
                throw new Exception("Unable to create static mesh for export (" + totalNumVertices.ToString() +
                                    " vertices, " + totalNumTriangles.ToString() + " triangles): [" + error.ToString() + "]");
            }

            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);
            }

            for (var i = 0; i < totalNumMaterials; ++i)
            {
                PhononCore.iplSetSceneMaterial(scene, i, materials[i]);
            }

            PhononCore.iplSetStaticMeshVertices(scene, staticMesh, vertices);
            PhononCore.iplSetStaticMeshTriangles(scene, staticMesh, triangles);
            PhononCore.iplSetStaticMeshMaterials(scene, staticMesh, materialIndices);

            PhononCore.iplFinalizeScene(scene, null);

#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);
        }