/// <summary> /// Import custom texture and label settings for buttons /// </summary> /// <param name="button">Button</param> /// <param name="colorName">Name of texture</param> static public bool TryCustomizeButton(ref Button button, string colorName) { Texture2D tex; if (!TryImportTexture(colorName, out tex)) { return(false); } // Load texture button.BackgroundTexture = tex; button.BackgroundTexture.filterMode = (FilterMode)DaggerfallUnity.Settings.GUIFilterMode; // Load settings from Xml XMLManager xml; if (XMLManager.TryReadXml(texturesPath, colorName, out xml)) { string value; if (xml.TryGetString("customtext", out value)) { if (value == "true") // Set custom color for text { button.Label.TextColor = xml.GetColor(button.Label.TextColor); } else if (value == "notext") // Disable text. This is useful if text is drawn on texture { button.Label.Text = string.Empty; } } } return(true); }
/// <summary> /// Get a safe size for a control based on resolution of img. /// </summary> public static Vector2 GetSize(Texture2D texture, string textureName, bool allowXml = false) { if (!DaggerfallUnity.Settings.AssetInjection) { return(new Vector2(texture.width, texture.height)); } if (allowXml) { // Get size from xml XMLManager xml; if (XMLManager.TryReadXml(imgPath, textureName, out xml)) { Vector2 size; if (xml.TryGetVector2("width", "height", out size)) { return(size); } } } // Get size from Daggerfall image ImageData imageData = ImageReader.GetImageData(textureName, createTexture: false); return(new Vector2(imageData.width, imageData.height)); }
/// <summary> /// Read configuration for a paperdoll item with custom rect. /// </summary> /// <param name="item">Target item or null.</param> /// <param name="imageData">Source image data.</param> /// <param name="rect">Rect for the item on paperdoll.</param> internal static void OverridePaperdollItemRect(DaggerfallUnityItem item, ImageData imageData, float paperdollScale, ref Rect rect) { DyeColors dyeColor = item != null ? item.dyeColor : DyeColors.Unchanged; string directory; string name; XMLManager xml; if (MakeName(imageData, dyeColor, out directory, out name) && XMLManager.TryReadXml(directory, name, out xml)) { rect = xml.GetRect("rect", rect, paperdollScale); } }
public static void SetEnemyScale(int archive, int record, ref Vector2 size) { if (!DaggerfallUnity.Settings.MeshAndTextureReplacement) { return; } XMLManager xml; if (XMLManager.TryReadXml(texturesPath, GetName(archive, record), out xml)) { Vector2 scale = xml.GetVector2("scaleX", "scaleY", Vector2.zero); size.x *= scale.x; size.y *= scale.y; } }
/// <summary> /// Read scale from xml and apply to given vector. /// </summary> public static void SetBillboardScale(int archive, int record, ref Vector2 size) { if (!DaggerfallUnity.Settings.AssetInjection) { return; } XMLManager xml; if (XMLManager.TryReadXml(texturesPath, GetName(archive, record), out xml)) { Vector2 scale = xml.GetVector2("scaleX", "scaleY", Vector2.zero); size.x *= scale.x; size.y *= scale.y; } }
/// <summary> /// Import textures and emission maps for all frames of this billboard. Also set other material properties from xml. /// </summary> public static void SetBillboardImportedTextures(GameObject go, ref DaggerfallBillboard.BillboardSummary summary) { if (!DaggerfallUnity.Settings.MeshAndTextureReplacement) { return; } MeshRenderer meshRenderer = go.GetComponent <MeshRenderer>(); int archive = summary.Archive; int record = summary.Record; int frame = 0; bool isEmissive = DaggerfallUnity.Instance.MaterialReader.TextureReader.IsEmissive(archive, record); // Check first frame Texture2D albedo, emission; bool hasImportedTextures = LoadFromCacheOrImport(archive, record, frame, isEmissive, out albedo, out emission); if (summary.ImportedTextures.HasImportedTextures = hasImportedTextures) { // Set texture on material meshRenderer.material.SetTexture(Uniforms.MainTex, albedo); if (isEmissive) { meshRenderer.material.SetTexture(Uniforms.EmissionMap, emission); } // Import animation frames var albedoTextures = new List <Texture2D>(); var emissionTextures = new List <Texture2D>(); do { albedoTextures.Add(albedo); if (isEmissive) { emissionTextures.Add(emission); } }while (LoadFromCacheOrImport(archive, record, ++frame, isEmissive, out albedo, out emission)); // Set scale and uv Vector2 uv = Vector2.zero; XMLManager xml; if (XMLManager.TryReadXml(texturesPath, GetName(archive, record), out xml)) { // Set billboard scale Transform transform = go.GetComponent <Transform>(); transform.localScale = xml.GetVector3("scaleX", "scaleY", transform.localScale); summary.Size.x *= transform.localScale.x; summary.Size.y *= transform.localScale.y; // Get UV uv = xml.GetVector2("uvX", "uvY", uv); } SetUv(go.GetComponent <MeshFilter>(), uv.x, uv.y); // Save results summary.ImportedTextures.FrameCount = frame; summary.ImportedTextures.IsEmissive = isEmissive; summary.ImportedTextures.Albedo = albedoTextures; summary.ImportedTextures.Emission = emissionTextures; } }
/// <summary> /// Gets a custom material for a mobile billboard with textures and configuration imported from mods. /// </summary> /// <param name="archive">Archive index.</param> /// <param name="meshFilter">The MeshFilter of the billboard object.</param> /// <param name="importedTextures">All the imported textures for the archive.</param> /// <remarks> /// Seek the texture for the first frame of the first record. If found, it imports the entire archive. /// If this texture has an emission map the material is considered emissive and all emission maps are imported. /// </remarks> /// <returns>A material or null.</returns> public static Material GetMobileBillboardMaterial(int archive, MeshFilter meshFilter, ref MobileBillboardImportedTextures importedTextures) { if (!DaggerfallUnity.Settings.AssetInjection) { return(null); } Texture2D tex, emission; if (importedTextures.HasImportedTextures = LoadFromCacheOrImport(archive, 0, 0, true, true, out tex, out emission)) { string renderMode = null; // Read xml configuration XMLManager xml; if (XMLManager.TryReadXml(ImagesPath, string.Format("{0:000}", archive), out xml)) { xml.TryGetString("renderMode", out renderMode); importedTextures.IsEmissive = xml.GetBool("emission"); } // Make material Material material = MakeBillboardMaterial(renderMode); // Enable emission ToggleEmission(material, importedTextures.IsEmissive |= emission != null); // Load texture file to get record and frame count string fileName = TextureFile.IndexToFileName(archive); var textureFile = new TextureFile(Path.Combine(DaggerfallUnity.Instance.Arena2Path, fileName), FileUsage.UseMemory, true); // Import all textures in this archive importedTextures.Albedo = new Texture2D[textureFile.RecordCount][]; importedTextures.EmissionMaps = importedTextures.IsEmissive ? new Texture2D[textureFile.RecordCount][] : null; for (int record = 0; record < textureFile.RecordCount; record++) { int frames = textureFile.GetFrameCount(record); var frameTextures = new Texture2D[frames]; var frameEmissionMaps = importedTextures.IsEmissive ? new Texture2D[frames] : null; for (int frame = 0; frame < frames; frame++) { if (record != 0 || frame != 0) { LoadFromCacheOrImport(archive, record, frame, importedTextures.IsEmissive, true, out tex, out emission); } frameTextures[frame] = tex ?? ImageReader.GetTexture(fileName, record, frame, true); if (frameEmissionMaps != null) { frameEmissionMaps[frame] = emission ?? frameTextures[frame]; } } importedTextures.Albedo[record] = frameTextures; if (importedTextures.EmissionMaps != null) { importedTextures.EmissionMaps[record] = frameEmissionMaps; } } // Update UV map SetUv(meshFilter); return(material); } return(null); }
/// <summary> /// Gets a custom material for a static billboard with textures and configuration imported from mods. /// </summary> /// <param name="go">The billboard object.</param> /// <param name="archive">Archive index.</param> /// <param name="record">Record index.</param> /// <param name="summary">Summary data of the billboard object.</param> /// <remarks> /// Seek the texture for the first frame of the given record. If found, it imports all other frames. /// Always creates an emission map for textures marked as emissive by TextureReader, import emission maps for others only if available. /// </remarks> /// <returns>A material or null.</returns> public static Material GetStaticBillboardMaterial(GameObject go, int archive, int record, ref DaggerfallBillboard.BillboardSummary summary) { if (!DaggerfallUnity.Settings.AssetInjection) { return(null); } //MeshRenderer meshRenderer = go.GetComponent<MeshRenderer>(); int frame = 0; Texture2D albedo, emission; if (summary.ImportedTextures.HasImportedTextures = LoadFromCacheOrImport(archive, record, frame, true, true, out albedo, out emission)) { bool isEmissive = emission || DaggerfallUnity.Instance.MaterialReader.TextureReader.IsEmissive(archive, record); // Read xml configuration Vector2 uv = Vector2.zero; string renderMode = null; XMLManager xml; if (XMLManager.TryReadXml(texturesPath, GetName(archive, record), out xml)) { xml.TryGetString("renderMode", out renderMode); isEmissive |= xml.GetBool("emission"); // Set billboard scale Transform transform = go.GetComponent <Transform>(); transform.localScale = xml.GetVector3("scaleX", "scaleY", transform.localScale); summary.Size.x *= transform.localScale.x; summary.Size.y *= transform.localScale.y; // Get UV uv = xml.GetVector2("uvX", "uvY", uv); } // Make material Material material = MakeBillboardMaterial(renderMode); summary.Rect = new Rect(uv.x, uv.y, 1 - 2 * uv.x, 1 - 2 * uv.y); // Set textures on material; emission is always overriden, with actual texture or null. material.SetTexture(Uniforms.MainTex, albedo); material.SetTexture(Uniforms.EmissionMap, isEmissive ? emission ?? albedo : null); ToggleEmission(material, isEmissive); // Import animation frames var albedoTextures = new List <Texture2D>(); var emissionTextures = isEmissive ? new List <Texture2D>() : null; do { albedoTextures.Add(albedo); if (isEmissive) { emissionTextures.Add(emission ?? albedo); } }while (LoadFromCacheOrImport(archive, record, ++frame, isEmissive, true, out albedo, out emission)); // Save results summary.ImportedTextures.FrameCount = frame; summary.ImportedTextures.IsEmissive = isEmissive; summary.ImportedTextures.Albedo = albedoTextures; summary.ImportedTextures.Emission = emissionTextures; return(material); } return(null); }