Exemplo n.º 1
0
        public Texture2D GetTexture(GraphicsDevice device)
        {
            DecodeIfRequired();
            if (PixelCache == null)
            {
                var mip = !Parent.WallStyle && FSOEnvironment.Enable3D && FSOEnvironment.EnableNPOTMip;
                var tc  = FSOEnvironment.TexCompress;

                if (Width * Height > 0)
                {
                    var w = Math.Max(1, Width);
                    var h = Math.Max(1, Height);
                    if (mip && TextureUtils.OverrideCompression(w, h))
                    {
                        tc = false;
                    }
                    if (tc)
                    {
                        PixelCache = new Texture2D(device, ((w + 3) / 4) * 4, ((h + 3) / 4) * 4, mip, SurfaceFormat.Dxt5);
                        if (mip)
                        {
                            TextureUtils.UploadDXT5WithMips(PixelCache, w, h, device, Data);
                        }
                        else
                        {
                            PixelCache.SetData <byte>(TextureUtils.DXT5Compress(Data, w, h).Item1);
                        }
                    }
                    else
                    {
                        PixelCache = new Texture2D(device, w, h, mip, SurfaceFormat.Color);
                        if (mip)
                        {
                            TextureUtils.UploadWithMips(PixelCache, device, Data);
                        }
                        else
                        {
                            PixelCache.SetData <Color>(this.Data);
                        }
                    }
                }
                else
                {
                    PixelCache = new Texture2D(device, Math.Max(1, Width), Math.Max(1, Height), mip, SurfaceFormat.Color);
                    PixelCache.SetData <Color>(new Color[] { Color.Transparent });
                }

                PixelCache.Tag = new TextureInfo(PixelCache, Width, Height);
                if (!IffFile.RETAIN_CHUNK_DATA)
                {
                    Data = null;
                }
            }
            return(PixelCache);
        }
Exemplo n.º 2
0
        private byte[] TexToData(Texture2D tex, bool compressed)
        {
            var data = new Color[tex.Width * tex.Height];

            tex.GetData(data);
            if (compressed)
            {
                //let's assume the width and height didn't change. the default settings should always result in textures divisible by 4.
                return(TextureUtils.DXT5Compress(data, tex.Width, tex.Height).Item1);
            }
            return(ToByteArray(data.Select(x => x.PackedValue).ToArray()));
        }
Exemplo n.º 3
0
        private Texture2D GetTexture(GraphicsDevice device, bool onlyThis)
        {
            if (ContainsNothing)
            {
                return(null);
            }
            Texture2D result = null;

            if (!PixelCache.TryGetTarget(out result) || ((CachableTexture2D)result).BeingDisposed || result.IsDisposed)
            {
                DecodeIfRequired(false);
                if (this.Width == 0 || this.Height == 0)
                {
                    ContainsNothing = true;
                    return(null);
                }
                var tc  = FSOEnvironment.TexCompress;
                var mip = FSOEnvironment.Enable3D && (FSOEnvironment.EnableNPOTMip || (Width == 128 && Height == 64));
                if (mip && TextureUtils.OverrideCompression(Width, Height))
                {
                    tc = false;
                }
                if (tc)
                {
                    result = new CachableTexture2D(device, ((Width + 3) / 4) * 4, ((Height + 3) / 4) * 4, mip, SurfaceFormat.Dxt5);
                    if (mip)
                    {
                        TextureUtils.UploadDXT5WithMips(result, Width, Height, device, this.PixelData);
                    }
                    else
                    {
                        var dxt = TextureUtils.DXT5Compress(this.PixelData, this.Width, this.Height);
                        result.SetData <byte>(dxt.Item1);
                    }
                }
                else
                {
                    result = new CachableTexture2D(device, this.Width, this.Height, mip, SurfaceFormat.Color);
                    if (mip)
                    {
                        TextureUtils.UploadWithMips(result, device, this.PixelData);
                    }
                    else
                    {
                        result.SetData <Color>(this.PixelData);
                    }
                }
                result.Tag = new TextureInfo(result, Width, Height);
                PixelCache = new WeakReference <Texture2D>(result);
                if (TimedReferenceController.CurrentType == CacheType.PERMANENT)
                {
                    PermaRefP = result;
                }
                if (!IffFile.RETAIN_CHUNK_DATA)
                {
                    PixelData = null;
                    //if (onlyThis && !FSOEnvironment.Enable3D) ZBufferData = null;
                }
            }
            if (TimedReferenceController.CurrentType != CacheType.PERMANENT)
            {
                TimedReferenceController.KeepAlive(result, KeepAliveType.ACCESS);
            }
            return(result);
        }