/// <summary> /// Creates a simple texture from any base API image. /// </summary> public static Texture2D CreateFromAPIImage(BaseImageFile image, int record = 0, int frame = 0, int alphaIndex = -1, bool createMipMaps = false, bool makeNoLongerReadable = true) { // Override readable flag when user has set preference in material reader if (DaggerfallUnity.Instance.MaterialReader.ReadableTextures) { makeNoLongerReadable = false; } Texture2D texture; if (TextureReplacement.TryImportImage(image.FileName, out texture)) { return(texture); } if (TextureReplacement.TryImportCifRci(image.FileName, record, frame, out texture)) { return(texture); } DFSize sz; Color32[] colors = image.GetColor32(record, frame, alphaIndex, 0, out sz); texture = new Texture2D(sz.Width, sz.Height, TextureFormat.RGBA32, createMipMaps); texture.SetPixels32(colors); texture.Apply(createMipMaps, makeNoLongerReadable); return(texture); }
public static Texture2D GetTextureFromCifRci(string name, int record, out DFPosition offset, int frame = 0, TextureFormat format = TextureFormat.ARGB32) { offset = new DFPosition(); DaggerfallUnity dfUnity = DaggerfallUnity.Instance; if (!dfUnity.IsReady) { return(null); } CifRciFile cifRciFile = new CifRciFile(Path.Combine(dfUnity.Arena2Path, name), FileUsage.UseMemory, true); Texture2D texture; if (!TextureReplacement.TryImportCifRci(name, record, frame, out texture)) { cifRciFile.LoadPalette(Path.Combine(dfUnity.Arena2Path, cifRciFile.PaletteName)); DFBitmap bitmap = cifRciFile.GetDFBitmap(record, frame); texture = new Texture2D(bitmap.Width, bitmap.Height, format, false); texture.SetPixels32(cifRciFile.GetColor32(bitmap, 0)); texture.Apply(false, true); } texture.filterMode = DaggerfallUI.Instance.GlobalFilterMode; offset = cifRciFile.GetOffset(record); return(texture); }
// Initialise. void Start() { playerMotor = GetComponent <PlayerMotor>(); if (!playerMotor) { throw new Exception("PlayerMotor not found."); } transportManager = GetComponent <TransportManager>(); if (!transportManager) { throw new Exception("TransportManager not found."); } transportManager.DrawHorse = false; playerMouseLook = GameManager.Instance.PlayerMouseLook; if (!playerMouseLook) { throw new Exception("PlayerMouseLook not found."); } GameManager.Instance.SpeedChanger.CanRun = CanRunUnlessRidingCart; // Setup appropriate neck textures if availiable. for (int i = 0; i < 4; i++) { TextureReplacement.TryImportCifRci(horseNeckTextureName, 0, i, false, out horseNeckTextures[i].texture); } for (int i = 0; i < 4; i++) { TextureReplacement.TryImportCifRci(cartNeckTextureName, 0, i, false, out cartNeckTextures[i].texture); } }
private Texture2D GetWeaponTextureAtlas( string filename, MetalTypes metalType, out Rect[] rectsOut, out RecordIndex[] indicesOut, int padding, int border, bool dilate = false) { // Load texture file cifFile.Load(Path.Combine(dfUnity.Arena2Path, filename), FileUsage.UseMemory, true); // Read every image in archive Rect rect; List <Texture2D> textures = new List <Texture2D>(); List <RecordIndex> indices = new List <RecordIndex>(); customTextures.Clear(); for (int record = 0; record < cifFile.RecordCount; record++) { int frames = cifFile.GetFrameCount(record); DFSize size = cifFile.GetSize(record); RecordIndex ri = new RecordIndex() { startIndex = textures.Count, frameCount = frames, width = size.Width, height = size.Height, }; indices.Add(ri); for (int frame = 0; frame < frames; frame++) { textures.Add(GetWeaponTexture2D(filename, record, frame, metalType, out rect, border, dilate)); Texture2D tex; if (TextureReplacement.TryImportCifRci(filename, record, frame, metalType, true, out tex)) { tex.filterMode = dfUnity.MaterialReader.MainFilterMode; customTextures.Add(MaterialReader.MakeTextureKey(0, (byte)record, (byte)frame), tex); } } } // Pack textures into atlas Texture2D atlas = new Texture2D(2048, 2048, TextureFormat.ARGB32, false); rectsOut = atlas.PackTextures(textures.ToArray(), padding, 2048); indicesOut = indices.ToArray(); // Shrink UV rect to compensate for internal border float ru = 1f / atlas.width; float rv = 1f / atlas.height; for (int i = 0; i < rectsOut.Length; i++) { Rect rct = rectsOut[i]; rct.xMin += border * ru; rct.xMax -= border * ru; rct.yMin += border * rv; rct.yMax -= border * rv; rectsOut[i] = rct; } return(atlas); }
/// <summary> /// Get animations for current spellcast. /// This happens the first time a spell is cast and stored for re-casting. /// It's likely player will use a wide variety of spell types in normal play. /// </summary> void SetCurrentAnims(ElementTypes elementType, int border = 0, bool dilate = false) { // Attempt to get current anims if (castAnims.ContainsKey(elementType)) { currentAnimType = elementType; currentAnims = castAnims[elementType]; return; } // Load spellcast file string filename = WeaponBasics.GetMagicAnimFilename(elementType); string path = Path.Combine(DaggerfallUnity.Instance.Arena2Path, filename); CifRciFile cifFile = new CifRciFile(); if (!cifFile.Load(path, FileUsage.UseMemory, true)) { throw new Exception(string.Format("Could not load spell anims file {0}", path)); } // Load CIF palette cifFile.Palette.Load(Path.Combine(DaggerfallUnity.Instance.Arena2Path, cifFile.PaletteName)); // Load textures - spells have a single frame per record unlike weapons AnimationRecord[] animationRecords = new AnimationRecord[cifFile.RecordCount]; for (int record = 0; record < cifFile.RecordCount; record++) { Texture2D texture; if (!TextureReplacement.TryImportCifRci(filename, record, 0, false, out texture)) { // Get Color32 array DFSize sz; Color32[] colors = cifFile.GetColor32(record, 0, 0, border, out sz); // Dilate edges if (border > 0 && dilate) { ImageProcessing.DilateColors(ref colors, sz); } // Create Texture2D texture = new Texture2D(sz.Width, sz.Height, TextureFormat.ARGB32, false); texture.SetPixels32(colors); texture.Apply(true); } // Set filter mode and store in frames array if (texture) { texture.filterMode = (FilterMode)DaggerfallUnity.Settings.MainFilterMode; animationRecords[record].Texture = texture; animationRecords[record].Size = cifFile.GetSize(record); } } // Add frames array to dictionary castAnims.Add(elementType, animationRecords); // Use as current anims currentAnimType = elementType; currentAnims = animationRecords; }