// 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);
            }
        }
    }
예제 #2
0
 // If |guid| is not mapped to a surface material yet, map it to the default surface material.
 public void AddDefaultMaterialIfGuidUnmapped(string guid)
 {
     if (!surfaceMaterialFromGuid.ContainsKey(guid))
     {
         surfaceMaterialFromGuid.Add(guid, defaultSurfaceMaterial);
     }
 }