// 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); } } }
// 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); } } }
// Applies surface material to (the acoustic meshes) of the game objects whose Unity Materials // are identified by |guid|. private void ApplySurfaceMaterialToGameObjects( ResonanceAudioRoomManager.SurfaceMaterial surfaceMaterial, string guid) { UnityMaterialAcousticMeshData acosuticMeshesData = unityMaterialAcousticMeshDataFromGuid[guid]; if (acosuticMeshesData.acousticMeshes.Count != acosuticMeshesData.subMeshIndices.Count) { Debug.LogError("Number of acoustic meshes (" + acosuticMeshesData.acousticMeshes.Count + ") != number of sub-mesh indices (" + acosuticMeshesData.subMeshIndices.Count + ")"); } var acousticMeshes = acosuticMeshesData.acousticMeshes; var subMeshIndices = acosuticMeshesData.subMeshIndices; for (int i = 0; i < acousticMeshes.Count; ++i) { acousticMeshes[i].SetSurfaceMaterialToSubMesh(surfaceMaterial, subMeshIndices[i]); } }