Ejemplo n.º 1
0
        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    = null;

            // Custom texture
            if (TextureReplacement.CustomCifExist(name, record, frame))
            {
                texture = TextureReplacement.LoadCustomCif(name, record, frame);
            }
            // Daggerfall texture
            else
            {
                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);
        }
Ejemplo n.º 2
0
        /// <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(SpellTypes spellType, int border = 0, bool dilate = false)
        {
            // Attempt to get current anims
            if (castAnims.ContainsKey(spellType))
            {
                currentAnims = castAnims[spellType];
                return;
            }

            // Load spellcast file
            string     filename = WeaponBasics.GetSpellAnimFilename(spellType);
            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 textures - spells have a single frame per record unlike weapons
            Texture2D[] frames = new Texture2D[cifFile.RecordCount];
            for (int record = 0; record < cifFile.RecordCount; record++)
            {
                Texture2D texture = null;

                // Import custom texture or load classic texture
                if (TextureReplacement.CustomCifExist(filename, record, 0, MetalTypes.None))
                {
                    texture = TextureReplacement.LoadCustomCif(filename, record, 0, MetalTypes.None);
                }
                else
                {
                    // 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.RGBA32, false);
                    texture.SetPixels32(colors);
                    texture.Apply(true);
                }

                // Set filter mode and store in frames array
                if (texture)
                {
                    texture.filterMode = (FilterMode)DaggerfallUnity.Settings.MainFilterMode;
                    frames[record]     = texture;
                }
            }

            // Add frames array to dictionary
            castAnims.Add(spellType, frames);

            // Use as current anims
            currentAnims = frames;
        }
Ejemplo n.º 3
0
        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 = new Dictionary <string, Texture2D>();
            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));

                    // Import custom texture
                    if (TextureReplacement.CustomCifExist(filename, record, frame, metalType))
                    {
                        Texture2D tex = TextureReplacement.LoadCustomCif(filename, record, frame, metalType);
                        tex.filterMode = (FilterMode)DaggerfallUnity.Settings.MainFilterMode;
                        CustomTextures.Add(record + "-" + frame, tex);
                    }
                }
            }

            // Pack textures into atlas
            Texture2D atlas = new Texture2D(2048, 2048, TextureFormat.RGBA32, 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);
        }