Exemplo n.º 1
0
        static vtfheader ReadHeader(BinaryReader BR)
        {
            //BR.BaseStream.Seek (0, SeekOrigin.Begin);
            vtfheader header = new vtfheader();

            header.signature          = new string (BR.ReadChars(4));
            header.version            = new uint[] { BR.ReadUInt32(), BR.ReadUInt32() };
            header.headerSize         = BR.ReadUInt32();
            header.width              = (short)BR.ReadUInt16();
            header.height             = (short)BR.ReadUInt16();
            header.flags              = BR.ReadUInt32();
            header.frames             = BR.ReadUInt16();
            header.firstFrame         = BR.ReadUInt16();
            header.padding0           = BR.ReadBytes(4);
            header.reflectivity       = new Vector3(BR.ReadSingle(), BR.ReadSingle(), BR.ReadSingle());
            header.padding1           = BR.ReadBytes(4);
            header.bumpmapScale       = BR.ReadSingle();
            header.highResImageFormat = (VTFImageFormat)BR.ReadUInt32();
            header.mipmapCount        = BR.ReadByte();
            header.lowResImageFormat  = (VTFImageFormat)BR.ReadUInt32();
            header.lowResImageWidth   = BR.ReadByte();
            header.lowResImageHeight  = BR.ReadByte();
            header.depth              = BR.ReadUInt16();

            return(header);
        }
Exemplo n.º 2
0
        static void ReadData(BinaryReader BR, vtfheader header, out byte[] data, out uint dataSize, out byte[] ThumbData, out uint thumbSize)
        {
            dataSize = ComputeImageSize(header.width, header.height, header.mipmapCount, header.frames, header.highResImageFormat);

            thumbSize = ComputeImageSize(header.lowResImageWidth, header.lowResImageHeight, header.lowResImageFormat);

            uint ThumbnailDataOffset = 0;
            uint ImageDataOffset     = 0;

            ThumbnailDataOffset = header.headerSize;
            ImageDataOffset     = ThumbnailDataOffset + thumbSize;

            BR.BaseStream.Seek(ThumbnailDataOffset, SeekOrigin.Begin);
            ThumbData = BR.ReadBytes((int)thumbSize);


            BR.BaseStream.Seek(ImageDataOffset, SeekOrigin.Begin);
            data = BR.ReadBytes((int)dataSize);
        }
Exemplo n.º 3
0
        public static Texture2D LoadFile(string name)
        {
            BinaryReader BR;

            string path = "";

            if (!name.Contains(".vtf"))
            {
                name += ".vtf";
            }

            path = ResourceManager.GetPath("materials/" + name);

            if (path == null)
            {
                //	Debug.LogWarning("materials/"+name+".vtf: Not Found");
                return(null);
            }

            BR = new BinaryReader(File.Open(path, FileMode.Open));

            vtfheader header = ReadHeader(BR);

            byte[] ImageData;
            uint   ImageDataSize;

            byte[] ThumbnailImageData;
            uint   ThumbnailDataSize;

            ReadData(BR, header, out ImageData, out ImageDataSize, out ThumbnailImageData, out ThumbnailDataSize);

            BR.BaseStream.Dispose();
            //return CreateThumbnailTexture (name);


            return(CreateTexture(name, header, ImageData));
        }
Exemplo n.º 4
0
        static Texture2D CreateTexture(string name, vtfheader header, byte[] ImageData, bool asNormalMap, bool useCompression)
        {
            bool isLinear    = false;
            bool usesMipMaps = true;

            Texture2D temp;

            if (asNormalMap == true)
            {
                if ((header.flags & IMAGE_FLAG_NORMAL_MAP) != 0)
                {
                    isLinear = true;
                }
                else
                {
                    return(null);
                }
            }

            if (header.highResImageFormat == VTFImageFormat.IMAGE_FORMAT_DXT5)
            {
                temp = new Texture2D((int)header.width, (int)header.height, TextureFormat.DXT5, false);
                int    offset = (header.width * header.height);
                byte[] buf    = new byte[offset];
                Buffer.BlockCopy(ImageData, ImageData.Length - offset, buf, 0, offset);
                temp.LoadRawTextureData(buf);
                temp.Apply(true);
                //if(remipmap)
                //{
                Color32[] tempCol = temp.GetPixels32();
                if (asNormalMap)
                {
                    for (int i = 0; i < tempCol.Length; i++)
                    {
                        tempCol[i] = new Color32(tempCol[i].r, (byte)(0xff - tempCol[i].g), tempCol[i].b, tempCol[i].a);
                    }
                }
                temp = new Texture2D((int)header.width, (int)header.height, TextureFormat.RGBA32, usesMipMaps, isLinear);
                temp.SetPixels32(tempCol);
                temp.Apply(true);
                if (useCompression)
                {
                    temp.Compress(true);
                }
                //}
                temp.name = name;

                //if ((header.flags & 4) != 0)
                //	temp.
                //Debug.Log("Texture loaded "+header.width+"x"+header.height);
                return(temp);
            }
            if (header.highResImageFormat == VTFImageFormat.IMAGE_FORMAT_DXT1)
            {
                temp = new Texture2D((int)header.width, (int)header.height, TextureFormat.DXT1, false);
                int    offset = ((header.width * header.height) / 2) * header.frames;
                byte[] buf    = new byte[offset];
                Buffer.BlockCopy(ImageData, ImageData.Length - offset, buf, 0, offset);
                temp.LoadRawTextureData(buf);
                temp.Apply();
                //if(remipmap)
                //{
                Color32[] tempCol = temp.GetPixels32();
                if (asNormalMap)
                {
                    for (int i = 0; i < tempCol.Length; i++)
                    {
                        tempCol[i] = new Color32(tempCol[i].r, tempCol[i].g, tempCol[i].b, tempCol[i].a);
                    }
                }
                temp = new Texture2D((int)header.width, (int)header.height, TextureFormat.RGBA32, usesMipMaps, isLinear);
                temp.SetPixels32(tempCol);
                temp.Apply(true);
                if (useCompression)
                {
                    temp.Compress(true);
                }
                //}
                temp.name = name;
                //Debug.Log("Texture loaded "+header.width+"x"+header.height);
                return(temp);
            }
            if (header.highResImageFormat == VTFImageFormat.IMAGE_FORMAT_RGBA8888 ||
                header.highResImageFormat == VTFImageFormat.IMAGE_FORMAT_ABGR8888 ||
                header.highResImageFormat == VTFImageFormat.IMAGE_FORMAT_RGB888 ||
                header.highResImageFormat == VTFImageFormat.IMAGE_FORMAT_BGR888 ||
                header.highResImageFormat == VTFImageFormat.IMAGE_FORMAT_BGRA8888 ||
                header.highResImageFormat == VTFImageFormat.IMAGE_FORMAT_ARGB8888 ||
                header.highResImageFormat == VTFImageFormat.IMAGE_FORMAT_A8)
            {
                int    mipOffset    = CalculateVTFMipOffset(header.width, header.height, header.mipmapCount);
                int    byteCount    = 0;
                byte[] pixelIndices = new byte[4];

                switch (header.highResImageFormat)
                {
                case VTFImageFormat.IMAGE_FORMAT_RGBA8888:
                    byteCount       = 4;
                    pixelIndices[0] = 0; pixelIndices[1] = 1; pixelIndices[2] = 2; pixelIndices[3] = 3;
                    break;

                case VTFImageFormat.IMAGE_FORMAT_ABGR8888:
                    byteCount       = 4;
                    pixelIndices[0] = 3; pixelIndices[1] = 2; pixelIndices[2] = 1; pixelIndices[3] = 0;
                    break;

                case VTFImageFormat.IMAGE_FORMAT_RGB888:
                    byteCount       = 3;
                    pixelIndices[0] = 0; pixelIndices[1] = 1; pixelIndices[2] = 2; pixelIndices[3] = 0xff;
                    break;

                case VTFImageFormat.IMAGE_FORMAT_BGR888:
                    byteCount       = 3;
                    pixelIndices[0] = 2; pixelIndices[1] = 1; pixelIndices[2] = 0; pixelIndices[3] = 0xff;
                    break;

                case VTFImageFormat.IMAGE_FORMAT_BGRA8888:
                    byteCount       = 4;
                    pixelIndices[0] = 2; pixelIndices[1] = 1; pixelIndices[2] = 0; pixelIndices[3] = 3;
                    break;

                case VTFImageFormat.IMAGE_FORMAT_ARGB8888:
                    byteCount       = 4;
                    pixelIndices[0] = 1; pixelIndices[1] = 2; pixelIndices[2] = 3; pixelIndices[3] = 0;
                    break;

                case VTFImageFormat.IMAGE_FORMAT_A8:
                    byteCount       = 1;
                    pixelIndices[0] = 0xff; pixelIndices[1] = 0xff; pixelIndices[2] = 0xff; pixelIndices[3] = 0;
                    break;

                default:
                    return(null);
                }

                temp = new Texture2D((int)header.width, (int)header.height, TextureFormat.RGB24, usesMipMaps, isLinear);
                Color32[] colors = new Color32[header.width * header.height];
                if (asNormalMap)
                {
                    for (int i = 0; i < header.width * header.height; i++)
                    {
                        colors[i] = new Color32(
                            pixelIndices[0] != 255 ?
                            ImageData[(mipOffset * byteCount) + (i * byteCount) + pixelIndices[0]] : (byte)0xff,
                            pixelIndices[1] != 255 ?
                            (byte)(0xff - ImageData[(mipOffset * byteCount) + (i * byteCount) + pixelIndices[1]]) : (byte)0xff,
                            pixelIndices[2] != 255 ?
                            ImageData[(mipOffset * byteCount) + (i * byteCount) + pixelIndices[2]] : (byte)0xff,
                            pixelIndices[3] != 255 ?
                            ImageData[(mipOffset * byteCount) + (i * byteCount) + pixelIndices[3]] : (byte)0xff);
                    }
                }
                else
                {
                    for (int i = 0; i < header.width * header.height; i++)
                    {
                        colors[i] = new Color32(
                            pixelIndices[0] != 255 ?
                            ImageData[(mipOffset * byteCount) + (i * byteCount) + pixelIndices[0]] : (byte)0xff,
                            pixelIndices[2] != 255 ?
                            ImageData[(mipOffset * byteCount) + (i * byteCount) + pixelIndices[1]] : (byte)0xff,
                            pixelIndices[2] != 255 ?
                            ImageData[(mipOffset * byteCount) + (i * byteCount) + pixelIndices[2]] : (byte)0xff,
                            pixelIndices[3] != 255 ?
                            ImageData[(mipOffset * byteCount) + (i * byteCount) + pixelIndices[3]] : (byte)0xff);
                    }
                }
                temp.SetPixels32(colors);
                temp.Apply();
                temp.name = name;
                //Debug.Log("Texture loaded "+header.width+"x"+header.height);
                return(temp);
            }
            Debug.LogWarning(name + " Unsuported Texture Format" + header.highResImageFormat);
            return(null);
        }
Exemplo n.º 5
0
        public static Texture2D LoadFile(string name, bool asNormalMap)
        {
            BinaryReader BR;

            string path     = "";
            string fullName = "";

            bool isTTFile = IsTTFileLoad(name);

            if (!isTTFile)
            {
                if (!name.Contains(".vtf"))
                {
                    fullName = name + ".vtf";
                }

                path = ResourceManager.GetPath("materials/" + fullName);

                if (path == null)
                {
                    //	Debug.LogWarning("materials/"+name+".vtf: Not Found");
                    return(null);
                }

                BR = new BinaryReader(File.Open(path, FileMode.Open));

                BR.BaseStream.Seek(0, SeekOrigin.Begin);
                vtfheader header = ReadHeader(BR);

                byte[] ImageData;
                uint   ImageDataSize;
                byte[] ThumbnailImageData;
                uint   ThumbnailDataSize;

                ReadData(BR, header, out ImageData, out ImageDataSize, out ThumbnailImageData, out ThumbnailDataSize);

                BR.BaseStream.Dispose();
                //return CreateThumbnailTexture (name);


                return(CreateTexture(fullName, header, ImageData, asNormalMap, true));
            }
            else
            {
                if (!name.Contains(".tth"))
                {
                    fullName = name + ".tth";
                }

                path = ResourceManager.GetPath("materials/" + fullName);

                if (path == null)
                {
                    //	Debug.LogWarning("materials/"+name+".tth: Not Found");
                    return(null);
                }

                BR = new BinaryReader(File.Open(path, FileMode.Open));

                BR.BaseStream.Seek(0, SeekOrigin.Begin);
                troikaTextureHeader tkheader = ReadTroikaTextureHeader(BR);

                BR.BaseStream.Seek(-tkheader.VTFSize, SeekOrigin.End);
                vtfheader header = ReadHeader(BR);

                BR.BaseStream.Dispose();

                if (!name.Contains(".ttz"))
                {
                    fullName = name + ".ttz";
                }

                path = ResourceManager.GetPath("materials/" + fullName);

                if (path == null)
                {
                    //	Debug.LogWarning("materials/"+name+".tth: Not Found");
                    return(null);
                }

                MemoryStream outputMemoryStream = new MemoryStream();

                byte[]        queueBuffer = new byte[4096];
                DeflateStream zstream     = new DeflateStream(File.Open(path, FileMode.Open), CompressionMode.Decompress);
                while (true)
                {
                    int readBytes = zstream.Read(queueBuffer, 0, 4096);
                    if (readBytes == 0)
                    {
                        break;
                    }
                    else
                    {
                        outputMemoryStream.Write(queueBuffer, 0, readBytes);
                    }
                }
                zstream.Dispose();

                BR = new BinaryReader(outputMemoryStream);

                BR.BaseStream.Seek(0, SeekOrigin.Begin);

                byte[] ImageData;
                uint   ImageDataSize;
                byte[] ThumbnailImageData;
                uint   ThumbnailDataSize;

                ReadData(BR, header, out ImageData, out ImageDataSize, out ThumbnailImageData, out ThumbnailDataSize);

                BR.BaseStream.Dispose();
                //return CreateThumbnailTexture (name);

                return(CreateTexture(fullName, header, ImageData, asNormalMap, true));
            }
        }
Exemplo n.º 6
0
        static Texture2D CreateTexture(string name, vtfheader header, byte[] ImageData)
        {
            Texture2D temp;

            if (header.highResImageFormat == VTFImageFormat.IMAGE_FORMAT_DXT5)
            {
                temp = new Texture2D((int)header.width, (int)header.height, TextureFormat.DXT5, false);
                int    offset = (header.width * header.height);
                byte[] buf    = new byte[offset];
                Buffer.BlockCopy(ImageData, ImageData.Length - offset, buf, 0, offset);
                temp.LoadRawTextureData(buf);
                temp.Apply(true);
                //if(remipmap)
                //{
                //	Color32[] tempCol = temp.GetPixels32();
                //	temp = new Texture2D((int)header.width, (int)header.height, TextureFormat.RGBA32, true);
                //	temp.SetPixels32(tempCol);
                //	temp.Apply(true);
                //	temp.Compress(true);
                //}
                temp.name = name;

                //if ((header.flags & 4) != 0)
                //	temp.
                //Debug.Log("Texture loaded "+header.width+"x"+header.height);
                return(temp);
            }
            if (header.highResImageFormat == VTFImageFormat.IMAGE_FORMAT_DXT1)
            {
                temp = new Texture2D((int)header.width, (int)header.height, TextureFormat.DXT1, false);
                int    offset = ((header.width * header.height) / 2) * header.frames;
                byte[] buf    = new byte[offset];
                Buffer.BlockCopy(ImageData, ImageData.Length - offset, buf, 0, offset);
                temp.LoadRawTextureData(buf);
                temp.Apply();
                //if(remipmap)
                //{
                Color32[] tempCol = temp.GetPixels32();
                temp = new Texture2D((int)header.width, (int)header.height, TextureFormat.RGBA32, true);
                temp.SetPixels32(tempCol);
                temp.Apply(true);
                temp.Compress(true);
                //}
                temp.name = name;
                //Debug.Log("Texture loaded "+header.width+"x"+header.height);
                return(temp);
            }
            if (header.highResImageFormat == VTFImageFormat.IMAGE_FORMAT_BGR888)
            {
                temp = new Texture2D((int)header.width, (int)header.height, TextureFormat.RGB24, true);
                Color32[] colors = new Color32[header.width * header.height];
                for (int i = 0; i < header.width * header.height; i++)
                {
                    colors[i] = new Color32(ImageData[i * 3 + 2], ImageData[i * 3 + 1], ImageData[i * 3], 255);
                }
                temp.SetPixels32(colors);
                temp.Apply();
                temp.name = name;
                //Debug.Log("Texture loaded "+header.width+"x"+header.height);
                return(temp);
            }
            if (header.highResImageFormat == VTFImageFormat.IMAGE_FORMAT_BGRA8888)
            {
                temp = new Texture2D((int)header.width, (int)header.height, TextureFormat.BGRA32, false);

                /*Color32[] colors = new Color32[header.width * header.height];
                 * for(int i=0; i<header.width * header.height; i++)
                 * {
                 *      colors[i] = new Color32(ImageData[i*3+2],ImageData[i*3+1],ImageData[i*3],ImageData[i*3+3]);
                 * }
                 * temp.SetPixels32(colors);*/
                int imageSize = header.width * header.height * 4;
                //int offset = imageSize*header.frames;
                Debug.Log("ImageData length " + ImageData.Length /*+" offset "+offset*/ + " Image size " + imageSize + " frames " + header.frames);
                byte[] buf = new byte[imageSize];
                Buffer.BlockCopy(ImageData, ImageData.Length - imageSize, buf, 0, imageSize);
                temp.LoadRawTextureData(buf);
                temp.Apply();
                temp.name = name;
                //Debug.Log("Texture loaded "+header.width+"x"+header.height);
                return(temp);
            }
            Debug.LogWarning(name + " Unsuported Texture Format" + header.highResImageFormat);
            return(null);
        }