/// <summary> /// Assign texture filtermode and override materials if found in loose files. /// </summary> /// <remarks> /// Filtermode is assigned to all supported texture properties. When a material follows /// Daggerfall nomenclature a corrispective is seeked from loose files for consistency. /// </remarks> private static void FinaliseMaterials(GameObject go) { // Get MaterialReader MaterialReader materialReader = DaggerfallUnity.Instance.MaterialReader; // Check object and all children foreach (var meshRenderer in go.GetComponentsInChildren <MeshRenderer>()) { if (meshRenderer.gameObject.GetComponent <RuntimeMaterials>()) { continue; } // Check all materials Material[] materials = meshRenderer.sharedMaterials; for (int i = 0; i < materials.Length; i++) { if (materials[i]) { // Override Daggerfall material with textures from loose files int archive, record; if (TextureReplacement.IsDaggerfallTexture(materials[i].name, out archive, out record) && TextureReplacement.TextureExistsAmongLooseFiles(archive, record, 0)) { CachedMaterial cachedMaterialOut; if (materialReader.GetCachedMaterial(archive, record, 0, out cachedMaterialOut)) { materials[i] = cachedMaterialOut.material; continue; } } // Assign filtermode to textures TextureReplacement.AssignFiltermode(materials[i]); } else { if (go == meshRenderer.gameObject) { Debug.LogWarningFormat("{0} is missing material {1}.", go.name, i.ToString()); } else { Debug.LogWarningFormat("{0} (child {1}) is missing material {2}.", go.name, meshRenderer.name, i.ToString()); } } } // Confirm finalised materials meshRenderer.sharedMaterials = materials; } }
/// <summary> /// Assign texture filtermode as user settings and check integrity of materials. /// Import textures from disk if available. This is for consistency when custom models /// use vanilla textures (or improved versions of them) and the user has installed a texture pack. /// </summary> /// <param name="object3D">Custom prefab</param> static private void FinaliseMaterials(GameObject object3D) { // Get MaterialReader MaterialReader materialReader = DaggerfallUnity.Instance.MaterialReader; // Check all MeshRenderers MeshRenderer[] meshRenderers = object3D.GetComponentsInChildren <MeshRenderer>(); foreach (var meshRenderer in meshRenderers) { // Check all materials Material[] materials = meshRenderer.sharedMaterials; for (int i = 0; i < materials.Length; i++) { if (!materials[i]) { if (object3D == meshRenderer.gameObject) { Debug.LogErrorFormat("{0} is missing material {1}.", object3D.name, i.ToString()); } else { Debug.LogErrorFormat("{0} (child {1}) is missing material {2}.", object3D.name, meshRenderer.name, i.ToString()); } continue; } if (materials[i].mainTexture) { // Get name of texture string textureName = materials[i].mainTexture.name; // Textures inside StreamingAssets/Textures have the priority // over the material included inside the assetbundle. if (TextureReplacement.CustomTextureExist(textureName)) { // Check that is a daggerfall texture // These textures should follow the nomenclature (archive_record-frame.png) while unique // textures should have unique names (MyModelPack_WoodChair2.png) to avoid involuntary replacements. int archive, record; if (TextureReplacement.IsDaggerfallTexture(textureName, out archive, out record)) { // Use material from Daggerfall Unity cache CachedMaterial cachedMaterialOut; if (materialReader.GetCachedMaterial(archive, record, 0, out cachedMaterialOut)) { materials[i] = cachedMaterialOut.material; continue; } } } // Assign filtermode FilterMode filterMode = (FilterMode)DaggerfallUnity.Settings.MainFilterMode; materials[i].mainTexture.filterMode = filterMode; if (materials[i].HasProperty("_BumpMap") && materials[i].GetTexture("_BumpMap")) { materials[i].GetTexture("_BumpMap").filterMode = filterMode; } if (materials[i].HasProperty("_EmissionMap") && materials[i].GetTexture("_EmissionMap")) { materials[i].GetTexture("_EmissionMap").filterMode = filterMode; } if (materials[i].HasProperty("_MetallicGlossMap") && materials[i].GetTexture("_MetallicGlossMap")) { materials[i].GetTexture("_MetallicGlossMap").filterMode = filterMode; } } } // Confirm finalised materials meshRenderer.sharedMaterials = materials; } }