예제 #1
0
 /// <summary>
 /// Expands the Composite Texture to a texture atlas friendly version and populates the Baked field. This method is called by the texture atlas managers.
 /// Won't have any effect if called after the texture atlasses have been created.
 /// </summary>
 public void Bake(IAssetManager assetManager)
 {
     // Some CompositeTextures are re-used multiple times in the same Block (e.g. if a blocktype specified a texture for key "all") so no need to re-bake
     if (Baked == null)
     {
         Baked = Bake(assetManager, this);
     }
 }
예제 #2
0
        /// <summary>
        /// Expands the Composite Texture to a texture atlas friendly version and populates the Baked field. This method can be called after the game world has loaded.
        /// </summary>
        /// <param name="capi">The Client API</param>
        /// <param name="intoAtlas">The atlas to insert the baked texture.</param>
        public void RuntimeBake(ICoreClientAPI capi, ITextureAtlasAPI intoAtlas)
        {
            Baked = Bake(capi.Assets, this);

            RuntimeInsert(capi, intoAtlas, Baked);

            if (Baked.BakedVariants != null)
            {
                foreach (var val in Baked.BakedVariants)
                {
                    RuntimeInsert(capi, intoAtlas, val);
                }
            }
        }
예제 #3
0
        bool RuntimeInsert(ICoreClientAPI capi, ITextureAtlasAPI intoAtlas, BakedCompositeTexture btex)
        {
            BitmapRef bmp = capi.Assets.Get(btex.BakedName).ToBitmap(capi);

            int textureSubId;
            TextureAtlasPosition texpos;

            if (intoAtlas.InsertTexture(bmp, out textureSubId, out texpos))
            {
                btex.TextureSubId = textureSubId;
                capi.Render.RemoveTexture(btex.BakedName);
                return(true);
            }

            bmp.Dispose();
            return(false);
        }
예제 #4
0
        /// <summary>
        /// Expands a CompositeTexture to a texture atlas friendly version and populates the Baked field
        /// </summary>
        /// <param name="assetManager"></param>
        /// <param name="ct"></param>
        /// <returns></returns>
        static BakedCompositeTexture Bake(IAssetManager assetManager, CompositeTexture ct)
        {
            BakedCompositeTexture bct = new BakedCompositeTexture();

            bct.BakedName = ct.Base.Clone();

            if (ct.Base.EndsWithWildCard)
            {
                List <IAsset> assets = assetManager.GetMany("textures/" + bct.BakedName.Path.Substring(0, bct.BakedName.Path.Length - 1), bct.BakedName.Domain);
                if (assets.Count == 0)
                {
                    ct.Base = bct.BakedName = new AssetLocation("unknown");
                }
                else
                if (assets.Count == 1)
                {
                    ct.Base       = assets[0].Location.CloneWithoutPrefixAndEnding("textures/".Length);
                    bct.BakedName = ct.Base.Clone();
                }
                else
                if (assets.Count > 1)
                {
                    int origLength = (ct.Alternates == null ? 0 : ct.Alternates.Length);
                    CompositeTexture[] alternates = new CompositeTexture[origLength + assets.Count - 1];
                    if (ct.Alternates != null)
                    {
                        Array.Copy(ct.Alternates, alternates, ct.Alternates.Length);
                    }

                    for (int i = 0; i < assets.Count; i++)
                    {
                        IAsset        asset       = assets[i];
                        AssetLocation newLocation = asset.Location.CloneWithoutPrefixAndEnding("textures/".Length);

                        if (i == 0)
                        {
                            ct.Base       = newLocation;
                            bct.BakedName = newLocation.Clone();
                        }
                        else
                        {
                            var act = alternates[origLength + i - 1] = new CompositeTexture(newLocation);
                            act.Rotation = ct.Rotation;
                            act.Alpha    = ct.Alpha;
                        }
                    }

                    ct.Alternates = alternates;
                }
            }


            if (ct.Overlays != null)
            {
                bct.TextureFilenames    = new AssetLocation[ct.Overlays.Length + 1];
                bct.TextureFilenames[0] = ct.Base.Clone();

                for (int i = 0; i < ct.Overlays.Length; i++)
                {
                    bct.TextureFilenames[i + 1] = ct.Overlays[i].Clone();
                    bct.BakedName.Path         += "++" + ct.Overlays[i].ToShortString();
                }
            }
            else
            {
                bct.TextureFilenames = new AssetLocation[] { ct.Base.Clone() };
            }

            if (ct.Rotation != 0)
            {
                if (ct.Rotation != 90 && ct.Rotation != 180 && ct.Rotation != 270)
                {
                    throw new Exception("Texture definition " + ct.Base + " has a rotation thats not 0, 90, 180 or 270. These are the only allowed values!");
                }
                bct.BakedName.Path += "@" + ct.Rotation;
            }

            if (ct.Alpha != 255)
            {
                if (ct.Alpha < 0 || ct.Alpha > 255)
                {
                    throw new Exception("Texture definition " + ct.Base + " has a alpha value outside the 0..255 range.");
                }

                bct.BakedName.Path += "å" + ct.Alpha;
            }

            if (ct.Alternates != null)
            {
                bct.BakedVariants    = new BakedCompositeTexture[ct.Alternates.Length + 1];
                bct.BakedVariants[0] = bct;
                for (int i = 0; i < ct.Alternates.Length; i++)
                {
                    bct.BakedVariants[i + 1] = Bake(assetManager, ct.Alternates[i]);
                }
            }

            return(bct);
        }
예제 #5
0
        /// <summary>
        /// Expands a CompositeTexture to a texture atlas friendly version and populates the Baked field
        /// </summary>
        /// <param name="assetManager"></param>
        /// <param name="ct"></param>
        /// <returns></returns>
        static BakedCompositeTexture Bake(IAssetManager assetManager, CompositeTexture ct)
        {
            BakedCompositeTexture bct = new BakedCompositeTexture();

            bct.BakedName = ct.Base.Clone();

            if (ct.Base.Path.EndsWith("*"))
            {
                List <IAsset> assets = assetManager.GetMany("textures/" + bct.BakedName.Path.Substring(0, bct.BakedName.Path.Length - 1), bct.BakedName.Domain);
                if (assets.Count == 0)
                {
                    ct.Base = bct.BakedName = new AssetLocation("unknown");
                }

                if (assets.Count == 1)
                {
                    ct.Base      = assets[0].Location.Clone();
                    ct.Base.Path = assets[0].Location.Path.Substring("textures/".Length);
                    ct.Base.RemoveEnding();
                    bct.BakedName = ct.Base.Clone();
                }

                if (assets.Count > 1)
                {
                    int origLength = (ct.Alternates == null ? 0 : ct.Alternates.Length);
                    CompositeTexture[] alternates = new CompositeTexture[origLength + assets.Count - 1];
                    if (ct.Alternates != null)
                    {
                        Array.Copy(ct.Alternates, alternates, ct.Alternates.Length);
                    }

                    int i = 0;
                    foreach (IAsset asset in assets)
                    {
                        AssetLocation newLocation = assets[0].Location.Clone();
                        newLocation.Path = asset.Location.Path.Substring("textures/".Length);
                        newLocation.RemoveEnding();

                        if (i == 0)
                        {
                            ct.Base       = newLocation;
                            bct.BakedName = ct.Base;
                        }
                        else
                        {
                            alternates[origLength + i - 1] = new CompositeTexture(newLocation);
                        }

                        i++;
                    }

                    ct.Alternates = alternates;
                }
            }


            if (ct.Overlays != null)
            {
                bct.TextureFilenames    = new AssetLocation[ct.Overlays.Length + 1];
                bct.TextureFilenames[0] = ct.Base.Clone();

                for (int i = 0; i < ct.Overlays.Length; i++)
                {
                    bct.TextureFilenames[i + 1] = ct.Overlays[i].Clone();
                    bct.BakedName.Path         += "++" + ct.Overlays[i].ToShortString();
                }
            }
            else
            {
                bct.TextureFilenames = new AssetLocation[] { ct.Base.Clone() };
            }

            if (ct.Rotation != 0)
            {
                if (ct.Rotation != 90 && ct.Rotation != 0 && ct.Rotation != 180 && ct.Rotation != 270)
                {
                    throw new Exception("Texture definition " + ct.Base + " has a rotation thats not 0, 90, 180 or 270. These are the only allowed values!");
                }
                bct.BakedName.Path += "@" + ct.Rotation;
            }

            if (ct.Alternates != null)
            {
                bct.BakedVariants    = new BakedCompositeTexture[ct.Alternates.Length + 1];
                bct.BakedVariants[0] = bct;
                for (int i = 0; i < ct.Alternates.Length; i++)
                {
                    bct.BakedVariants[i + 1] = Bake(assetManager, ct.Alternates[i]);
                }
            }

            return(bct);
        }
예제 #6
0
 /// <summary>
 /// Expands the Composite Texture to a texture atlas friendly version and populates the Baked field. This method is called by the texture atlas managers.
 /// Won't have any effect if called after the texture atlasses have been created.
 /// </summary>
 public void Bake(IAssetManager assetManager)
 {
     Baked = Bake(assetManager, this);
 }