Пример #1
0
    /// Generates an acoustic mesh from a source object's mesh filter. Returns null if the
    /// generation failed.
    public static ResonanceAudioAcousticMesh GenerateFromMeshFilter(MeshFilter meshFilter,
                                                                    Shader surfaceMaterialShader)
    {
        var sourceObject       = meshFilter.gameObject;
        var sourceMesh         = meshFilter.sharedMesh;
        var sourceTriangles    = sourceMesh.triangles;
        int numTriangleIndices = sourceTriangles.Length;
        int numVertices        = sourceMesh.vertexCount;

        ResonanceAudioAcousticMesh acousticMesh = new ResonanceAudioAcousticMesh();

        int[]     triangles = null;
        Vector3[] vertices  = null;
        acousticMesh.InitializeMesh(numTriangleIndices, numVertices, out triangles, out vertices);

        // Duplicate the source object's mesh. The vertices are transformed to world space.
        acousticMesh.FillVerticesAndTrianglesFromMesh(sourceMesh, sourceObject.transform, ref vertices,
                                                      ref triangles);

        acousticMesh.mesh.vertices  = vertices;
        acousticMesh.mesh.triangles = triangles;
        acousticMesh.mesh.RecalculateNormals();

        acousticMesh.InitializeSubMeshMaterials();
        acousticMesh.InitializeVisualizationMaterial(surfaceMaterialShader);

        acousticMesh.sourceObject = sourceObject;
        return(acousticMesh);
    }
    // Creates acoustic meshes from terrain objects and builds a mapping from GUIDs of the terrain
    // data to the generated acoustic meshes.
    private void BuildTerrainData(Terrain[] activeTerrains, string[] guidsForTerrains,
                                  Shader surfaceMaterialShader)
    {
        terrainAcousticMeshDataFromGuid.Clear();
        for (int terrainIndex = 0; terrainIndex < activeTerrains.Length; ++terrainIndex)
        {
            var    terrain = activeTerrains[terrainIndex];
            string guid    = guidsForTerrains[terrainIndex];

            // Generate an acoustic mesh for the terrain object.
            var acousticMesh = ResonanceAudioAcousticMesh.GenerateFromTerrain(terrain,
                                                                              surfaceMaterialShader);

            // If this guid is not mapped to a surface material yet, map it to the default surface
            // material.
            if (!surfaceMaterialFromGuid.ContainsKey(guid))
            {
                surfaceMaterialFromGuid.Add(guid, defaultSurfaceMaterial);
            }

            if (!terrainAcousticMeshDataFromGuid.ContainsKey(guid))
            {
                terrainAcousticMeshDataFromGuid[guid] = new TerrainAcousticMeshData();
            }
            TerrainAcousticMeshData data = terrainAcousticMeshDataFromGuid[guid];
            data.terrainData = terrain.terrainData;
            data.acousticMeshes.Add(acousticMesh);
        }
    }
    // Creates acoustic meshes from game objects and builds a mapping from GUIDs of the Unity
    // Materials used by these game objects to the generated acoustic meshes.
    private void BuildUnityMaterialData(MeshRenderer[] meshRenderers,
                                        List <string>[] guidsForMeshRenderers,
                                        Shader surfaceMaterialShader)
    {
        unityMaterialAcousticMeshDataFromGuid.Clear();
        for (int meshRendererIndex = 0; meshRendererIndex < meshRenderers.Length; ++meshRendererIndex)
        {
            var meshRenderer = meshRenderers[meshRendererIndex];
            var gameObject   = meshRenderer.gameObject;

            // Skip if the mesh renderer does not have Unity Materials.
            var unityMaterials = meshRenderer.sharedMaterials;
            if (unityMaterials.Length == 0)
            {
                continue;
            }

            // Exclude inactive game objects.
            if (!gameObject.activeInHierarchy)
            {
                continue;
            }

            // Generate an acoustic mesh for the game object. Skip if failed.
            var acousticMesh = ResonanceAudioAcousticMesh.GenerateFromMeshFilter(
                gameObject.GetComponent <MeshFilter>(), surfaceMaterialShader);
            if (acousticMesh == null)
            {
                Debug.LogError("acousticMesh == null");
                continue;
            }

            // Each Unity Material of a mesh renderer correspondes to a sub-mesh.
            var guidsForMeshRenderer = guidsForMeshRenderers[meshRendererIndex];
            for (int subMeshIndex = 0; subMeshIndex < unityMaterials.Length; ++subMeshIndex)
            {
                // Find the GUID that identifies this Unity Material.
                var    unityMaterial = unityMaterials[subMeshIndex];
                string guid          = guidsForMeshRenderer[subMeshIndex];

                // If this guid is not mapped to a surface material yet, map it to the default surface
                // material.
                if (!surfaceMaterialFromGuid.ContainsKey(guid))
                {
                    surfaceMaterialFromGuid.Add(guid, defaultSurfaceMaterial);
                }

                if (!unityMaterialAcousticMeshDataFromGuid.ContainsKey(guid))
                {
                    unityMaterialAcousticMeshDataFromGuid[guid] = new UnityMaterialAcousticMeshData();
                }
                UnityMaterialAcousticMeshData data = unityMaterialAcousticMeshDataFromGuid[guid];
                data.unityMaterial = unityMaterial;
                data.acousticMeshes.Add(acousticMesh);
                data.subMeshIndices.Add(subMeshIndex);
            }
        }
    }
Пример #4
0
    // Creates acoustic meshes from game objects and builds a mapping from GUIDs of the Unity
    // Materials used by these game objects to the generated acoustic meshes.
    private void BuildUnityMaterialData(MeshRenderer[] meshRenderers,
                                        List <string>[] guidsForMeshRenderers,
                                        Shader surfaceMaterialShader)
    {
        unityMaterialAcousticMeshDataFromGuid.Clear();
        for (int meshRendererIndex = 0; meshRendererIndex < meshRenderers.Length; ++meshRendererIndex)
        {
            var meshRenderer = meshRenderers[meshRendererIndex];
            var gameObject   = meshRenderer.gameObject;

            // Skip if the mesh renderer does not have Unity Materials.
            var unityMaterials = meshRenderer.sharedMaterials;
            if (unityMaterials.Length == 0)
            {
                continue;
            }

            // Exclude inactive game objects.
            if (!gameObject.activeInHierarchy)
            {
                continue;
            }

            // Generate an acoustic mesh for the game object. Skip if failed.
            var acousticMesh = ResonanceAudioAcousticMesh.GenerateFromMeshFilter(
                gameObject.GetComponent <MeshFilter>(), surfaceMaterialShader);
            if (acousticMesh == null)
            {
                continue;
            }

            // Each Unity Material of a mesh renderer correspondes to a sub-mesh.
            var guidsForMeshRenderer = guidsForMeshRenderers[meshRendererIndex];
            for (int subMeshIndex = 0; subMeshIndex < unityMaterials.Length; ++subMeshIndex)
            {
                // Skip materials that are used by non-triangular sub-meshes (points, lines, etc.).
                if (!acousticMesh.IsSubMeshTriangular(subMeshIndex))
                {
                    continue;
                }

                // Find the GUID that identifies this Unity Material.
                string guid = guidsForMeshRenderer[subMeshIndex];
                materialMap.AddDefaultMaterialIfGuidUnmapped(guid);

                if (!unityMaterialAcousticMeshDataFromGuid.ContainsKey(guid))
                {
                    unityMaterialAcousticMeshDataFromGuid[guid] = new UnityMaterialAcousticMeshData();
                }
                UnityMaterialAcousticMeshData data = unityMaterialAcousticMeshDataFromGuid[guid];
                data.acousticMeshes.Add(acousticMesh);
                data.subMeshIndices.Add(subMeshIndex);
            }
        }
    }
    // Creates acoustic meshes from terrain objects and builds a mapping from GUIDs of the terrain
    // data to the generated acoustic meshes.
    private void BuildTerrainData(Terrain[] activeTerrains, string[] guidsForTerrains,
                                  Shader surfaceMaterialShader)
    {
        terrainAcousticMeshDataFromGuid.Clear();
        for (int terrainIndex = 0; terrainIndex < activeTerrains.Length; ++terrainIndex)
        {
            var    terrain = activeTerrains[terrainIndex];
            string guid    = guidsForTerrains[terrainIndex];

            // Generate an acoustic mesh for the terrain object.
            var acousticMesh = ResonanceAudioAcousticMesh.GenerateFromTerrain(terrain,
                                                                              surfaceMaterialShader);
            materialMap.AddDefaultMaterialIfGuidUnmapped(guid);

            if (!terrainAcousticMeshDataFromGuid.ContainsKey(guid))
            {
                terrainAcousticMeshDataFromGuid[guid] = new TerrainAcousticMeshData();
            }
            TerrainAcousticMeshData data = terrainAcousticMeshDataFromGuid[guid];
            data.acousticMeshes.Add(acousticMesh);
        }
    }
Пример #6
0
    /// Generates an acoustic mesh from a terrain.
    public static ResonanceAudioAcousticMesh GenerateFromTerrain(Terrain terrain,
                                                                 Shader surfaceMaterialShader)
    {
        var terrainData = terrain.terrainData;
        var heightMap   = terrainData.GetHeights(0, 0, terrainData.heightmapResolution,
                                                 terrainData.heightmapResolution);

        // First sub-sample the height map.
        int m;
        int n;
        int subSampledNumTriangleIndices;
        int subSampleStep;

        SubSampleHeightMap(heightMap.GetLength(0), heightMap.GetLength(1), out m, out n,
                           out subSampleStep, out subSampledNumTriangleIndices);

        ResonanceAudioAcousticMesh acousticMesh = new ResonanceAudioAcousticMesh();

        int[]     triangles;
        Vector3[] vertices;
        acousticMesh.InitializeMesh(subSampledNumTriangleIndices, subSampledNumTriangleIndices,
                                    out triangles, out vertices);

        // Create triangles and vertices from the height map. The vertices are transformed to world
        // space.
        acousticMesh.FillTrianglesAndVerticesFromHeightMap(
            terrain.transform.position, terrainData.size, heightMap, m, n, subSampleStep,
            ref triangles, ref vertices);

        acousticMesh.mesh.vertices  = vertices;
        acousticMesh.mesh.triangles = triangles;
        acousticMesh.mesh.RecalculateNormals();

        acousticMesh.InitializeSubMeshMaterials();
        acousticMesh.InitializeVisualizationMaterial(surfaceMaterialShader);

        acousticMesh.sourceObject = terrain.gameObject;
        return(acousticMesh);
    }
Пример #7
0
    /// Generates an acoustic mesh from a source object's mesh filter. Returns null if the
    /// generation failed.
    public static ResonanceAudioAcousticMesh GenerateFromMeshFilter(MeshFilter meshFilter,
                                                                    Shader surfaceMaterialShader)
    {
        var sourceObject = meshFilter.gameObject;
        var sourceMesh   = meshFilter.sharedMesh;

        if (sourceMesh == null)
        {
            Debug.LogWarning("GameObject: " + sourceObject.name + " has no mesh and will not be " +
                             "included in reverb baking.");
            return(null);
        }

        int numTriangleIndices = CountTriangleIndices(sourceMesh);
        int numVertices        = sourceMesh.vertexCount;

        ResonanceAudioAcousticMesh acousticMesh = new ResonanceAudioAcousticMesh();

        int[]     triangles = null;
        Vector3[] vertices  = null;
        acousticMesh.InitializeMesh(numTriangleIndices, numVertices, out triangles, out vertices);

        // Duplicate the source object's mesh. The vertices are transformed to world space.
        acousticMesh.FillVerticesAndTrianglesFromMesh(sourceMesh, sourceObject.transform, ref vertices,
                                                      ref triangles);

        acousticMesh.mesh.vertices  = vertices;
        acousticMesh.mesh.triangles = triangles;
        acousticMesh.mesh.RecalculateNormals();

        acousticMesh.InitializeSubMeshMaterials();
        acousticMesh.InitializeVisualizationMaterial(surfaceMaterialShader);

        acousticMesh.sourceObject = sourceObject;
        return(acousticMesh);
    }