Ejemplo n.º 1
0
        public void ReplaceTexture(int id, ImportedTexture image)
        {
            var oldTex = Parser.TextureList[id];

            var newTex = xx.CreateTexture(image);

            xx.CopyUnknowns(oldTex, newTex);

            Parser.TextureList.RemoveAt(id);
            Parser.TextureList.Insert(id, newTex);

            for (int i = 0; i < Parser.MaterialList.Count; i++)
            {
                var mat = Parser.MaterialList[i];
                for (int j = 0; j < mat.Textures.Length; j++)
                {
                    var matTex = mat.Textures[j];
                    if (matTex.Name == oldTex.Name)
                    {
                        matTex.Name = newTex.Name;
                    }
                }
            }
            Changed = true;
        }
Ejemplo n.º 2
0
        public static void ReplaceTexture(xxParser parser, ImportedTexture texture)
        {
            xxTexture tex = xx.CreateTexture(texture);

            bool found = false;

            for (int i = 0; i < parser.TextureList.Count; i++)
            {
                if (parser.TextureList[i].Name == texture.Name)
                {
                    CopyUnknowns(parser.TextureList[i], tex);

                    parser.TextureList.RemoveAt(i);
                    parser.TextureList.Insert(i, tex);
                    found = true;
                    break;
                }
            }

            if (!found)
            {
                CreateUnknowns(tex);
                parser.TextureList.Add(tex);
            }
        }
Ejemplo n.º 3
0
        public void AddTexture(ImportedTexture image)
        {
            xxTexture tex = xx.CreateTexture(image);

            xx.CreateUnknowns(tex);

            Parser.TextureList.Add(tex);
        }
Ejemplo n.º 4
0
        public ImportedTexture ImportedTexture()
        {
            string          ext         = Encoding.ASCII.GetString(Data, 8, 4).ToLowerInvariant();
            ImportedTexture importedTex = new ImportedTexture();

            importedTex.Name = Path.GetFileNameWithoutExtension(Name) + ext;
            importedTex.Data = CreateImageData();
            return(importedTex);
        }
Ejemplo n.º 5
0
        public static ImportedTexture ImportedTexture(xxTexture texture)
        {
            ImportedTexture importedTex = new ImportedTexture();

            importedTex.Name = texture.Name;
            importedTex.Data = (byte[])texture.ImageData.Clone();
            if (Path.GetExtension(texture.Name).ToLowerInvariant() == ".bmp")
            {
                importedTex.Data[0] = (byte)'B';
                importedTex.Data[1] = (byte)'M';
            }
            return(importedTex);
        }
Ejemplo n.º 6
0
        public static xxTexture CreateTexture(ImportedTexture texture)
        {
            var imgInfo = ImageInformation.FromMemory(texture.Data);

            xxTexture xxTex = new xxTexture();

            xxTex.Width           = imgInfo.Width;
            xxTex.Height          = imgInfo.Height;
            xxTex.Depth           = imgInfo.Depth;
            xxTex.Format          = (int)imgInfo.Format;
            xxTex.ImageFileFormat = (int)imgInfo.ImageFileFormat;
            xxTex.MipLevels       = imgInfo.MipLevels;
            xxTex.Name            = texture.Name;
            xxTex.ResourceType    = (int)imgInfo.ResourceType;

            byte checksum = 0;

            for (int i = 0; i < texture.Data.Length; i += 32)
            {
                checksum += texture.Data[i];
            }
            xxTex.Checksum = checksum;

            if (imgInfo.ImageFileFormat == ImageFileFormat.Bmp)
            {
                xxTex.ImageData    = (byte[])texture.Data.Clone();
                xxTex.ImageData[0] = 0;
                xxTex.ImageData[1] = 0;
            }
            else if (imgInfo.ImageFileFormat == ImageFileFormat.Tga)
            {
                byte[] tgaHeader = new byte[18];
                Array.Copy(texture.Data, tgaHeader, tgaHeader.Length);
                int imgdataLen = tgaHeader[16] / 8 * BitConverter.ToInt16(tgaHeader, 12) * BitConverter.ToInt16(tgaHeader, 14);
                if (imgdataLen > texture.Data.Length - (tgaHeader.Length + tgaHeader[0]))
                {
                    imgdataLen = texture.Data.Length - (tgaHeader.Length + tgaHeader[0]);
                }
                xxTex.ImageData = new byte[tgaHeader.Length + imgdataLen];
                Array.Copy(texture.Data, xxTex.ImageData, tgaHeader.Length);
                Array.Copy(texture.Data, tgaHeader.Length + tgaHeader[0], xxTex.ImageData, tgaHeader.Length, imgdataLen);
                xxTex.ImageData[0] = 0;
            }
            else
            {
                xxTex.ImageData = (byte[])texture.Data.Clone();
            }

            return(xxTex);
        }
Ejemplo n.º 7
0
        void LoadImage(ImportedTexture tex)
        {
            try
            {
                if (tex == null)
                {
                    pictureBox1.Image = null;
                    textBoxName.Text  = String.Empty;
                    textBoxSize.Text  = String.Empty;
                }
                else
                {
                    textBoxName.Text = tex.Name;

                    if (tex.Data.Length > 0x12)
                    {
                        ImageInformation imageInfo;
                        Texture          renderTexture = Texture.FromMemory(Gui.Renderer.Device, tex.Data, 0, 0, -1, Usage.None, Format.Unknown, Pool.Managed, Filter.Default, Filter.Default, 0, out imageInfo);
                        DataStream       stream        = Texture.ToStream(renderTexture, ImageFileFormat.Bmp);
                        Bitmap           bitmap        = new Bitmap(stream);
                        stream.Dispose();
                        string format = renderTexture.GetLevelDescription(0).Format.GetDescription();
                        int    bpp    = (format.Contains("A8") ? 8 : 0)
                                        + (format.Contains("R8") ? 8 : 0) + (format.Contains("G8") ? 8 : 0) + (format.Contains("B8") ? 8 : 0);
                        renderTexture.Dispose();
                        pictureBox1.Image = bitmap;

                        ResizeImage();
                        if (!this.IsHidden)
                        {
                            Enabled = false;
                            Activate();
                            Enabled = true;
                        }
                        textBoxSize.Text = imageInfo.Width + "x" + imageInfo.Height + (bpp > 0 ? "x" + bpp : String.Empty);
                    }
                    else
                    {
                        pictureBox1.Image = null;
                        textBoxSize.Text  = "0x0";
                    }
                }

                image = tex;
            }
            catch (Exception ex)
            {
                Utility.ReportException(ex);
            }
        }
Ejemplo n.º 8
0
            public static Texture2D ToImage(ImportedTexture tex, ImageLoadInformation loadInfo, out byte pixelDepth)
            {
                ushort width, height;

                byte[] data;
                using (Stream stream = new MemoryStream(tex.Data))
                {
                    byte  idLen, descriptor;
                    bool  compressed;
                    short originY;
                    GetImageInfo(stream, out idLen, out compressed, out originY, out width, out height, out pixelDepth, out descriptor);
                    if (compressed)
                    {
                        throw new Exception("Warning! Compressed TGAs are not supported");
                    }
                    stream.Position = idLen + 0x12;
                    BinaryReader reader = new BinaryReader(stream);
                    data = reader.ReadToEnd();
                    if (originY == 0)
                    {
                        Flip((short)height, width, height, (byte)(pixelDepth >> 3), data);
                    }
                }
                byte[] header = DDS.CreateHeader(width, height, pixelDepth, 0, false, pixelDepth == 32 ? DDS.UnityCompatibleFormat.ARGB32 : DDS.UnityCompatibleFormat.RGB24);
                using (BinaryWriter writer = new BinaryWriter(new MemoryStream()))
                {
                    writer.Write(header);
                    writer.Write(data);

                    try
                    {
                        writer.BaseStream.Position = 0;
                        return(Texture2D.FromStream(Gui.Renderer.Device, writer.BaseStream, (int)writer.BaseStream.Length, loadInfo));
                    }
                    catch (Exception e)
                    {
                        Direct3D11Exception d3de = e as Direct3D11Exception;
                        if (d3de != null)
                        {
                            Report.ReportLog("Direct3D11 Exception name=\"" + d3de.ResultCode.Name + "\" desc=\"" + d3de.ResultCode.Description + "\" code=0x" + ((uint)d3de.ResultCode.Code).ToString("X"));
                        }
                        else
                        {
                            Utility.ReportException(e);
                        }
                        return(null);
                    }
                }
            }
Ejemplo n.º 9
0
            public static Texture2D ToImage(ImportedTexture tex, out byte pixelDepth)
            {
                ImageLoadInformation loadInfo = new ImageLoadInformation()
                {
                    BindFlags      = BindFlags.None,
                    CpuAccessFlags = CpuAccessFlags.Read,
                    FilterFlags    = FilterFlags.None,
                    Format         = Format.R8G8B8A8_UNorm,
                    MipFilterFlags = FilterFlags.None,
                    MipLevels      = 1,
                    OptionFlags    = ResourceOptionFlags.None,
                    Usage          = ResourceUsage.Staging,
                };

                return(ToImage(tex, loadInfo, out pixelDepth));
            }
Ejemplo n.º 10
0
        void LoadImage(ImportedTexture tex)
        {
            try
            {
                if (tex == null)
                {
                    pictureBox1.Image = null;
                    textBoxName.Text  = String.Empty;
                    textBoxSize.Text  = String.Empty;
                }
                else
                {
                    Texture renderTexture = Texture.FromMemory(Gui.Renderer.Device, tex.Data);
                    Bitmap  bitmap        = new Bitmap(Texture.ToStream(renderTexture, ImageFileFormat.Bmp));
                    renderTexture.Dispose();
                    pictureBox1.Image = bitmap;

                    ResizeImage();
                    if (!this.IsHidden)
                    {
                        Enabled = false;
                        Activate();
                        Enabled = true;
                    }

                    textBoxName.Text = tex.Name;
                    textBoxSize.Text = bitmap.Width + "x" + bitmap.Height;
                }

                image = tex;
            }
            catch (Exception ex)
            {
                Utility.ReportException(ex);
            }
        }
Ejemplo n.º 11
0
        private AnimationFrame CreateFrame(xxFrame frame, xxParser parser, HashSet <string> extractFrames, HashSet <string> meshNames, Device device, Matrix combinedParent, List <AnimationFrame> meshFrames)
        {
            AnimationFrame animationFrame = new AnimationFrame();

            animationFrame.Name = frame.Name;
            animationFrame.TransformationMatrix = frame.Matrix;
            animationFrame.OriginalTransform    = animationFrame.TransformationMatrix;
            animationFrame.CombinedTransform    = combinedParent * animationFrame.TransformationMatrix;

            xxMesh mesh = frame.Mesh;

            if (meshNames.Contains(frame.Name) && (mesh != null))
            {
                List <xxBone> boneList = mesh.BoneList;

                string[] boneNames   = new string[boneList.Count];
                Matrix[] boneOffsets = new Matrix[boneList.Count];
                for (int i = 0; i < boneList.Count; i++)
                {
                    xxBone bone = boneList[i];
                    boneNames[i]   = bone.Name;
                    boneOffsets[i] = bone.Matrix;
                }

                AnimationMeshContainer[] meshContainers = new AnimationMeshContainer[mesh.SubmeshList.Count];
                Vector3 min = new Vector3(Single.MaxValue);
                Vector3 max = new Vector3(Single.MinValue);
                for (int i = 0; i < mesh.SubmeshList.Count; i++)
                {
                    xxSubmesh       submesh    = mesh.SubmeshList[i];
                    List <xxFace>   faceList   = submesh.FaceList;
                    List <xxVertex> vertexList = submesh.VertexList;

                    Mesh animationMesh = new Mesh(device, faceList.Count, vertexList.Count, MeshFlags.Managed, PositionBlendWeightsIndexedNormalTexturedColoured.Format);

                    using (DataStream indexStream = animationMesh.LockIndexBuffer(LockFlags.None))
                    {
                        for (int j = 0; j < faceList.Count; j++)
                        {
                            ushort[] indices = faceList[j].VertexIndices;
                            indexStream.Write(indices[0]);
                            indexStream.Write(indices[2]);
                            indexStream.Write(indices[1]);
                        }
                        animationMesh.UnlockIndexBuffer();
                    }

                    FillVertexBuffer(animationMesh, vertexList, -1);

                    var normalLines = new PositionBlendWeightsIndexedColored[vertexList.Count * 2];
                    for (int j = 0; j < vertexList.Count; j++)
                    {
                        xxVertex vertex = vertexList[j];

                        normalLines[j * 2]       = new PositionBlendWeightsIndexedColored(vertex.Position, vertex.Weights3, vertex.BoneIndices, Color.Yellow.ToArgb());
                        normalLines[(j * 2) + 1] = new PositionBlendWeightsIndexedColored(vertex.Position + (vertex.Normal / 16), vertex.Weights3, vertex.BoneIndices, Color.Yellow.ToArgb());

                        min = Vector3.Minimize(min, vertex.Position);
                        max = Vector3.Maximize(max, vertex.Position);
                    }

                    AnimationMeshContainer meshContainer = new AnimationMeshContainer();
                    meshContainer.Name        = animationFrame.Name;
                    meshContainer.MeshData    = new MeshData(animationMesh);
                    meshContainer.NormalLines = normalLines;
                    meshContainers[i]         = meshContainer;

                    int matIdx = submesh.MaterialIndex;
                    if ((matIdx >= 0) && (matIdx < parser.MaterialList.Count))
                    {
                        int texIdx;
                        if (!MatTexIndices.TryGetValue(matIdx, out texIdx))
                        {
                            texIdx = -1;

                            xxMaterial mat         = parser.MaterialList[matIdx];
                            Material   materialD3D = new Material();
                            materialD3D.Ambient  = mat.Ambient;
                            materialD3D.Diffuse  = mat.Diffuse;
                            materialD3D.Emissive = mat.Emissive;
                            materialD3D.Specular = mat.Specular;
                            materialD3D.Power    = mat.Power;
                            Materials[matIdx]    = materialD3D;

                            xxMaterialTexture matTex     = mat.Textures[0];
                            string            matTexName = matTex.Name;
                            if (matTexName != String.Empty)
                            {
                                for (int j = 0; j < parser.TextureList.Count; j++)
                                {
                                    xxTexture tex = parser.TextureList[j];
                                    if (tex.Name == matTexName)
                                    {
                                        texIdx = j;
                                        if (Textures[j] == null)
                                        {
                                            ImportedTexture importedTex = xx.ImportedTexture(tex);
                                            Textures[j] = Texture.FromMemory(device, importedTex.Data);
                                        }
                                        break;
                                    }
                                }
                            }

                            MatTexIndices.Add(matIdx, texIdx);
                        }

                        meshContainer.MaterialIndex = matIdx;
                        meshContainer.TextureIndex  = texIdx;
                    }
                }

                for (int i = 0; i < (meshContainers.Length - 1); i++)
                {
                    meshContainers[i].NextMeshContainer = meshContainers[i + 1];
                }
                for (int i = 0; i < meshContainers.Length; i++)
                {
                    meshContainers[i].BoneNames   = boneNames;
                    meshContainers[i].BoneOffsets = boneOffsets;
                }

                min = Vector3.TransformCoordinate(min, animationFrame.CombinedTransform);
                max = Vector3.TransformCoordinate(max, animationFrame.CombinedTransform);
                animationFrame.Bounds        = new BoundingBox(min, max);
                animationFrame.MeshContainer = meshContainers[0];
                meshFrames.Add(animationFrame);
            }

            for (int i = 0; i < frame.Count; i++)
            {
                xxFrame child = frame[i];
                if (extractFrames.Contains(child.Name))
                {
                    AnimationFrame childAnimationFrame = CreateFrame(child, parser, extractFrames, meshNames, device, animationFrame.CombinedTransform, meshFrames);
                    childAnimationFrame.Parent = animationFrame;
                    animationFrame.AppendChild(childAnimationFrame);
                }
            }

            numFrames++;
            return(animationFrame);
        }
Ejemplo n.º 12
0
        void LoadImage(ImportedTexture tex)
        {
            try
            {
                if (tex == null || Gui.Docking.DockRenderer == null || Gui.Docking.DockRenderer.IsHidden)
                {
                    pictureBox1.Image = null;
                    textBoxName.Text  = String.Empty;
                    textBoxSize.Text  = String.Empty;
                    if (Gui.Docking.DockRenderer != null && !Gui.Docking.DockRenderer.IsHidden)
                    {
                        Gui.Docking.DockRenderer.Enabled = false;
                        Gui.Docking.DockRenderer.Activate();
                        Gui.Docking.DockRenderer.Enabled = true;
                    }
                }
                else
                {
                    textBoxName.Text = tex.Name;

                    if (tex.Data.Length > 0x12)
                    {
                        if (Path.GetExtension(tex.Name).ToUpper() == ".TGA")
                        {
                            byte      pixelDepth;
                            Texture2D renderTexture = Utility.TGA.ToImage(tex, out pixelDepth);
                            if (renderTexture != null)
                            {
                                ToPictureBox(renderTexture, pixelDepth);
                                renderTexture.Dispose();
                            }
                            else
                            {
                                pictureBox1.Image = pictureBox1.ErrorImage;
                            }
                        }
                        else
                        {
                            try
                            {
                                Image img = System.Drawing.Image.FromStream(new MemoryStream(tex.Data));
                                pictureBox1.Image = img;
                                int bpp = 0;
                                if (img.PixelFormat.ToString().IndexOf("Format") >= 0)
                                {
                                    bpp = img.PixelFormat.ToString().IndexOf("bpp");
                                    if (!int.TryParse(img.PixelFormat.ToString().Substring(6, bpp - 6), out bpp))
                                    {
                                        bpp = 0;
                                    }
                                }
                                textBoxSize.Text = img.Width + "x" + img.Height + (bpp > 0 ? "x" + bpp : String.Empty);
                            }
                            catch
                            {
                                try
                                {
                                    int  width, height, bpp;
                                    bool cubemap;
                                    using (Texture2D renderTexture = Utility.DDS.ScaleWhenNeeded(tex.Data, out width, out height, out bpp, out cubemap))
                                    {
                                        ToPictureBox(renderTexture, bpp, width, height, cubemap);
                                    }
                                }
                                catch (Exception e)
                                {
                                    pictureBox1.Image = pictureBox1.ErrorImage;
                                    Direct3D11Exception d3de = e as Direct3D11Exception;
                                    if (d3de != null)
                                    {
                                        Report.ReportLog("Direct3D11 Exception name=\"" + d3de.ResultCode.Name + "\" desc=\"" + d3de.ResultCode.Description + "\" code=0x" + ((uint)d3de.ResultCode.Code).ToString("X"));
                                    }
                                    else
                                    {
                                        Utility.ReportException(e);
                                    }
                                }
                            }
                        }

                        ResizeImage();
                        if (!this.IsHidden)
                        {
                            Enabled = false;
                            Activate();
                            Enabled = true;
                        }
                    }
                    else
                    {
                        pictureBox1.Image = null;
                        textBoxSize.Text  = "0x0";
                    }
                }

                image = tex;
            }
            catch (Exception ex)
            {
                Utility.ReportException(ex);
            }
        }
Ejemplo n.º 13
0
 public void MergeTexture(ImportedTexture tex)
 {
     xx.ReplaceTexture(Parser, tex);
 }
Ejemplo n.º 14
0
 public void MergeTexture(ImportedTexture tex)
 {
     xx.ReplaceTexture(Parser, tex);
     Changed = true;
 }
Ejemplo n.º 15
0
        void LoadImage(ImportedTexture tex)
        {
            try
            {
                if (tex == null)
                {
                    pictureBox1.Image = null;
                    textBoxName.Text  = String.Empty;
                    textBoxSize.Text  = String.Empty;
                }
                else
                {
                    textBoxName.Text = tex.Name;

                    if (tex.Data.Length > 0x12)
                    {
                        Texture renderTexture = null;
                        try
                        {
                            ImageInformation imageInfo;
                            renderTexture = Texture.FromMemory(Gui.Renderer.Device, tex.Data, 0, 0, -1, Usage.None, Format.Unknown, Pool.Managed, Filter.Default, Filter.Default, 0, out imageInfo);
                            using (Image img = System.Drawing.Image.FromStream(Texture.ToStream(renderTexture, imageInfo.Height <= 512 && imageInfo.Width <= 512 ? ImageFileFormat.Png : ImageFileFormat.Bmp)))
                            {
                                int shift = 0;
                                for (int max = imageInfo.Width > imageInfo.Height ? imageInfo.Width : imageInfo.Height; max > 256; max >>= 1)
                                {
                                    shift++;
                                }
                                pictureBox1.Image = new Bitmap(img, new Size(imageInfo.Width >> shift, imageInfo.Height >> shift));
                            }
                            string format = renderTexture.GetLevelDescription(0).Format.GetDescription();
                            int    bpp    = (format.Contains("A8") ? 8 : 0)
                                            + (format.Contains("R8") ? 8 : 0) + (format.Contains("G8") ? 8 : 0) + (format.Contains("B8") ? 8 : 0);
                            textBoxSize.Text = imageInfo.Width + "x" + imageInfo.Height + (bpp > 0 ? "x" + bpp : String.Empty);
                        }
                        catch (Exception e)
                        {
                            pictureBox1.Image = pictureBox1.ErrorImage;
                            Utility.ReportException(e);
                        }
                        finally
                        {
                            if (renderTexture != null)
                            {
                                renderTexture.Dispose();
                            }
                        }

                        ResizeImage();
                        if (!this.IsHidden)
                        {
                            Enabled = false;
                            Activate();
                            Enabled = true;
                        }
                    }
                    else
                    {
                        pictureBox1.Image = null;
                        textBoxSize.Text  = "0x0";
                    }
                }

                image = tex;
            }
            catch (Exception ex)
            {
                Utility.ReportException(ex);
            }
        }