Пример #1
0
        public WriteableBitmap GetBitmap()
        {
            DatabaseIndex imageIndex = DatabaseManager.Instance.Indices.Find(idx => idx.InstanceId == this.TextureInstanceId && (idx.TypeId == PropertyConstants.RasterImageType || idx.TypeId == PropertyConstants.RW4ImageType));

            if (imageIndex != null)
            {
                if (imageIndex.TypeId == PropertyConstants.RasterImageType)
                {
                    using (MemoryStream imageByteStream = new MemoryStream(imageIndex.GetIndexData(true)))
                    {
                        RasterImage img = RasterImage.CreateFromStream(imageByteStream, RasterChannel.All);
                        return(img.MipMaps[0]);
                    }
                }
                else if (imageIndex.TypeId == PropertyConstants.RW4ImageType)
                {
                    using (MemoryStream imageByteStream = new MemoryStream(imageIndex.GetIndexData(true)))
                    {
                        RW4Model mod = new RW4Model();
                        mod.Read(imageByteStream);
                        RW4Section texSection = mod.Sections.First(s => s.TypeCode == SectionTypeCodes.Texture);
                        return((texSection.obj as Texture).ToImage(false));
                    }
                }
            }
            return(null);
        }
Пример #2
0
        private void btnExport3dsMax_Click(object sender, RoutedEventArgs e)
        {
            if (this.DataContext != null && this.DataContext.GetType() == typeof(RW4Section))
            {
                section = this.DataContext as RW4Section;
                RW4Material vertexArraySection = section.obj as RW4Material;

                System.Windows.Forms.FolderBrowserDialog dlg = new System.Windows.Forms.FolderBrowserDialog();
                dlg.Description = "Select where to extract the files to...";
                if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                {
                    string selectedPath = dlg.SelectedPath;
                    foreach (MaterialTextureReference mat in vertexArraySection.Materials)
                    {
                        DatabaseIndex imageIndex = DatabaseManager.Instance.Indices.Find(idx => idx.InstanceId == mat.TextureInstanceId && idx.TypeId == PropertyConstants.RasterImageType);
                        if (imageIndex != null)
                        {
                            using (MemoryStream imageByteStream = new MemoryStream(imageIndex.GetIndexData(true)))
                            {
                                using (FileStream strm = File.Create(selectedPath + "\\" + imageIndex.InstanceId.ToHex() + ".png"))
                                {
                                    RasterImage img;
                                    if (mat.Unknown1 == 1)
                                    {
                                        img = RasterImage.CreateFromStream(imageByteStream, RasterChannel.FacadeColor);
                                    }
                                    else
                                    {
                                        img = RasterImage.CreateFromStream(imageByteStream, RasterChannel.All);
                                    }
                                    PngBitmapEncoder encoder = new PngBitmapEncoder();
                                    encoder.Frames.Add(BitmapFrame.Create(img.MipMaps[0] as BitmapSource));
                                    encoder.Save(strm);
                                }
                            }
                        }

                        imageIndex = DatabaseManager.Instance.Indices.Find(idx => idx.InstanceId == mat.TextureInstanceId && idx.TypeId == PropertyConstants.RW4ImageType);
                        if (imageIndex != null)
                        {
                            using (MemoryStream imageByteStream = new MemoryStream(imageIndex.GetIndexData(true)))
                            {
                                RW4Model model = new RW4Model();
                                model.Read(imageByteStream);
                                Texture         text   = model.Textures[0];
                                WriteableBitmap bitmap = text.ToImage(true);
                                using (FileStream strm = File.Create(selectedPath + "\\" + imageIndex.InstanceId.ToHex() + ".png"))
                                {
                                    //panelTextures.Children.Add(new Image() { Source = img.MipMaps[0], Stretch = Stretch.None });
                                    PngBitmapEncoder encoder = new PngBitmapEncoder();
                                    encoder.Frames.Add(BitmapFrame.Create(bitmap as BitmapSource));
                                    encoder.Save(strm);
                                }
                            }
                        }
                    }
                }
            }
        }
Пример #3
0
        public ViewWindow(MainWindow main, DatabaseIndex index)
        {
            InitializeComponent();
            this.Main = main;
            byte[] data = index.GetIndexData(true);
            contentHolder.Content = new DatabaseIndexData(index, data);
            this.Title            = string.Format("T:{0} G:{1} I:{2} ({3})", index.TypeName, index.GroupName, index.InstanceName, index.InstanceId.ToHex());
            this.WindowState      = System.Windows.WindowState.Normal;

            // scale window to a more decent size
            switch (index.TypeName.Substring(0, index.TypeName.IndexOf(' ')))
            {
            case "PNG": this.SizeToContent = System.Windows.SizeToContent.WidthAndHeight; break;

            case "TGA": this.SizeToContent = System.Windows.SizeToContent.WidthAndHeight; break;

            case "RASTER": this.SizeToContent = System.Windows.SizeToContent.WidthAndHeight; break;

            case "RW4": this.SizeToContent = System.Windows.SizeToContent.WidthAndHeight; break;    // System.Windows.SizeToContent.Manual; this.Width = 700; this.Height = 500; break;

            case "JS": this.SizeToContent = System.Windows.SizeToContent.Manual; this.Width = 600; this.Height = 500; break;

            default: this.SizeToContent = System.Windows.SizeToContent.Manual; this.Width = 600; this.Height = 350; break;
            }
        }
Пример #4
0
        private void UserControl_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            if (this.DataContext != null && this.DataContext.GetType() == typeof(RW4Section))
            {
                section = this.DataContext as RW4Section;
                RW4Material vertexArraySection = section.obj as RW4Material;
                if (vertexArraySection != null)
                {
                    dataGridVertices.ItemsSource = vertexArraySection.Materials;

                    foreach (MaterialTextureReference mat in vertexArraySection.Materials)
                    {
                        DatabaseIndex imageIndex = DatabaseManager.Instance.Indices.Find(idx => idx.InstanceId == mat.TextureInstanceId && idx.TypeId == PropertyConstants.RasterImageType);
                        if (imageIndex != null)
                        {
                            using (MemoryStream imageByteStream = new MemoryStream(imageIndex.GetIndexData(true)))
                            {
                                RasterImage img = RasterImage.CreateFromStream(imageByteStream, RasterChannel.All);
                                panelTextures.Children.Add(new Image()
                                {
                                    Source = img.MipMaps[0], Stretch = Stretch.None
                                });
                            }
                        }
                    }
                }
            }
        }
Пример #5
0
        // WriteableBitmap bitmapCache = null;
        public WriteableBitmap GetBitmap(float x, float y, float width, float height)
        {
            DatabaseIndex imageIndex = DatabaseManager.Instance.Indices.Find(idx => idx.InstanceId == this.TextureInstanceId && idx.TypeId == PropertyConstants.RasterImageType);

            if (imageIndex != null)
            {
                using (MemoryStream imageByteStream = new MemoryStream(imageIndex.GetIndexData(true)))
                {
                    if (width != 0 && height != 0)
                    {
                        if (bitmapCache == null)
                        {
                            RasterImage img = RasterImage.CreateFromStream(imageByteStream, RasterChannel.All);
                            bitmapCache = img.MipMaps[0];
                        }

                        float pxX      = (float)bitmapCache.Width * x;
                        float pxY      = (float)bitmapCache.Width * y;
                        float pxWidth  = (float)bitmapCache.Width * width;
                        float pxHeight = (float)bitmapCache.Width * height;

                        WriteableBitmap croppedImage = bitmapCache.Crop(new System.Windows.Rect(pxX, pxY, Math.Abs(pxWidth), Math.Abs(pxHeight)));

                        return(croppedImage);
                    }
                }
            }
            return(null);
        }
Пример #6
0
        public static Dictionary <uint, Property> GetParentProperties(Dictionary <uint, Property> returnProperties, Dictionary <uint, Property> parentProperties)
        {
            if (returnProperties == null)
            {
                returnProperties = new Dictionary <uint, Property>();
            }

            //add keys that didn't exist yet
            foreach (uint index in parentProperties.Keys)
            {
                if (!returnProperties.ContainsKey(index) && index != 0x00B2CCCB)
                {
                    //This property specifies behavior bundles to exclude - instead of adding this property, remove all the bundles from the appropriate arrays
                    returnProperties.Add(index, parentProperties[index]);
                }
            }

            if (parentProperties.ContainsKey(0x00B2CCCB))
            {
                Property    parentProperty      = parentProperties[0x00B2CCCB];
                KeyProperty parentPropertyValue = parentProperty as KeyProperty;

                //search the currently opened package for this type/instance group id. if it doesn't exist, find it in the default simcity packages

                DatabaseIndex parentIndex = null;
                //if (DatabaseManager.Instance.Indices.Exists(i => i.InstanceId == parentPropertyValue.InstanceId && i.TypeId == parentPropertyValue.TypeId && i.GroupContainer == parentPropertyValue.GroupContainer))
                //{
                //    parentIndex = DatabaseManager.Instance.Indices.First(i => i.InstanceId == parentPropertyValue.InstanceId && i.TypeId == parentPropertyValue.TypeId && i.GroupContainer == parentPropertyValue.GroupContainer);
                //}
                //else
                //{
                parentIndex = DatabaseIndex.FindIndex(parentPropertyValue.TypeId, parentPropertyValue.GroupContainer, null, parentPropertyValue.InstanceId, true, true);
                //}

                // if(parentIndex != null)
                // {
                byte[] data = parentIndex.GetIndexData(true);

                using (Stream s = new MemoryStream(data, 0, data.Length))
                {
                    PropertyFile propertyFile = new PropertyFile();
                    propertyFile.Read(s);

                    return(GetParentProperties(returnProperties, propertyFile.Values));
                }
                throw new Exception("Inheritance not found!");
            }
            else
            {
                return(returnProperties);
            }
        }
Пример #7
0
        private void CreateUnitModel()
        {
            if (UnitFileEntry.LOD1 != null)
            {
                //get the 3d model to show in this scene
                KeyProperty   lod1  = UnitFileEntry.LOD1;
                DatabaseIndex index = DatabaseManager.Instance.Indices.Find(k => k.InstanceId == lod1.InstanceId && k.GroupContainer == lod1.GroupContainer && k.TypeId == lod1.TypeId);

                if (index != null)
                {
                    RW4Model _rw4model = new RW4Model();
                    using (Stream stream = new MemoryStream(index.GetIndexData(true)))
                    {
                        _rw4model.Read(stream);
                    }

                    RW4Section section = _rw4model.Sections.First(s => s.TypeCode == SectionTypeCodes.Mesh);
                    if (section != null)
                    {
                        SporeMaster.RenderWare4.RW4Mesh mesh = section.obj as SporeMaster.RenderWare4.RW4Mesh;

                        meshMain.TriangleIndices.Clear();
                        meshMain.Positions.Clear();
                        meshMain.Normals.Clear();
                        meshMain.TextureCoordinates.Clear();

                        try
                        {
                            foreach (var v in mesh.vertices.vertices)
                            {
                                meshMain.Positions.Add(new Point3D(v.Position.X, v.Position.Y, v.Position.Z));
                            }
                            foreach (var t in mesh.triangles.triangles)
                            {
                                meshMain.TriangleIndices.Add((int)t.i);
                                meshMain.TriangleIndices.Add((int)t.j);
                                meshMain.TriangleIndices.Add((int)t.k);
                            }
                        }
                        catch { }
                    }

                    if (UnitFileEntry.ModelSize != null)
                    {
                        ScaleTransform3D scaleTransform = new ScaleTransform3D(UnitFileEntry.ModelSize.Value, UnitFileEntry.ModelSize.Value, UnitFileEntry.ModelSize.Value);
                        modelMain.Transform = scaleTransform;
                    }
                }
            }
        }
Пример #8
0
        public Image GetImage()
        {
            DatabaseIndex imageIndex = DatabaseManager.Instance.Indices.Find(idx => idx.InstanceId == this.TextureInstanceId && idx.TypeId == PropertyConstants.RasterImageType);

            if (imageIndex != null)
            {
                using (MemoryStream imageByteStream = new MemoryStream(imageIndex.GetIndexData(true)))
                {
                    RasterImage img = RasterImage.CreateFromStream(imageByteStream, RasterChannel.All);
                    return(new Image()
                    {
                        Source = img.MipMaps[0], Stretch = Stretch.None
                    });
                }
            }
            return(null);
        }
 public void RefreshPreview()
 {
     try
     {
         DatabaseIndex imageIndex = DatabaseManager.Instance.Indices.Find(idx => idx.InstanceId == DecalID.InstanceId && idx.TypeId == PropertyConstants.RasterImageType);
         if (imageIndex != null)
         {
             using (MemoryStream imageByteStream = new MemoryStream(imageIndex.GetIndexData(true)))
             {
                 RasterImage img = RasterImage.CreateFromStream(imageByteStream, RasterChannel.Preview, Color4, Color3, Color2, Color1);
                 ImageSource = img.MipMaps[0];
                 OnPropertyChanged("ImageSource");
             }
         }
     }
     catch
     {
     }
 }
Пример #10
0
 private void dataGridInstances_SelectionChanged(object sender, SelectionChangedEventArgs e)
 {
     if (e.AddedItems.Count == 1)
     {
         if (e.AddedItems[0].GetType() == typeof(DatabaseIndex))
         {
             DatabaseIndex index = (DatabaseIndex)e.AddedItems[0];
             byte[]        data;
             if (index.IsModified)
             {
                 data = index.ModifiedData.GetData();
             }
             else
             {
                 data = index.GetIndexData(true);
             }
             viewContainer.Content        = new DatabaseIndexData(index, data);
             viewHexContainer.DataContext = new DatabaseIndexData(index, data);
         }
     }
 }
        public RW4Mesh Import(RW4Mesh mesh, string fileName)
        {
            Collada141.COLLADA colladaFile = Collada141.COLLADA.Load(fileName);

            //Retrieve relevant information from the original RW4 Mesh
            uint         verticesSectionNumber  = mesh.vertices.vertices.section.Number;
            uint         trianglesSectionNumber = mesh.triangles.triangles.section.Number;
            VertexFormat vertexFormatSection    = (VertexFormat)mesh.model.Sections.First(s => s.TypeCode == SectionTypeCodes.VertexFormat).obj;

            //Get the visual_scene object that contains all the relevant scene information
            Collada141.library_visual_scenes scenes = colladaFile.Items.OfType <Collada141.library_visual_scenes>().First();
            Collada141.visual_scene          scene  = scenes.visual_scene[0];

            //Load the geometries container
            Collada141.library_geometries geometry = colladaFile.Items.OfType <Collada141.library_geometries>().First();

            //Define the lists to which the temporary items can be saved
            List <int>    triangleDefinitions = new List <int>();
            List <Vertex> vertexList          = new List <Vertex>();
            List <string> vertexDefinitions   = new List <string>();

            //create the materials - but only if the material IDs start with "SCP-"
            List <MaterialDefinition> materialDatas = new List <MaterialDefinition>();

            Collada141.library_materials materialLibrary = colladaFile.Items.OfType <Collada141.library_materials>().First();
            if (((Collada141.material)materialLibrary.material[0]).id.StartsWith("SCP-"))
            {
                int materialIndex = 0;
                foreach (Collada141.material mat in materialLibrary.material)
                {
                    string   materialString = mat.id;
                    string[] materialValues = materialString.Split(new char[] { '-' }, StringSplitOptions.RemoveEmptyEntries);

                    MaterialDefinition mater = new MaterialDefinition()
                    {
                        Index = int.Parse(materialValues[15], CultureInfo.InvariantCulture.NumberFormat),
                        Id    = mat.name,

                        ColorTop      = byte.Parse(materialValues[8], CultureInfo.InvariantCulture.NumberFormat),
                        ColorBottom   = byte.Parse(materialValues[1], CultureInfo.InvariantCulture.NumberFormat),
                        InteriorData1 = byte.Parse(materialValues[7], CultureInfo.InvariantCulture.NumberFormat),
                        InteriorData2 = byte.Parse(materialValues[2], CultureInfo.InvariantCulture.NumberFormat),

                        TopX      = float.Parse(materialValues[3], CultureInfo.InvariantCulture.NumberFormat),
                        TopY      = float.Parse(materialValues[4], CultureInfo.InvariantCulture.NumberFormat),
                        TopHeight = float.Parse(materialValues[5], CultureInfo.InvariantCulture.NumberFormat),
                        TopWidth  = float.Parse(materialValues[6], CultureInfo.InvariantCulture.NumberFormat),

                        BottomX      = float.Parse(materialValues[9], CultureInfo.InvariantCulture.NumberFormat),
                        BottomY      = float.Parse(materialValues[10], CultureInfo.InvariantCulture.NumberFormat),
                        BottomHeight = float.Parse(materialValues[11], CultureInfo.InvariantCulture.NumberFormat),
                        BottomWidth  = float.Parse(materialValues[12], CultureInfo.InvariantCulture.NumberFormat),

                        TilingTopX    = float.Parse(materialValues[13], CultureInfo.InvariantCulture.NumberFormat),
                        TilingTopY    = float.Parse(materialValues[14], CultureInfo.InvariantCulture.NumberFormat),
                        InteriorSizeX = 0, //1 / float.Parse(materialValues[9], CultureInfo.InvariantCulture.NumberFormat),
                        InteriorSizeY = 0  //1 / float.Parse(materialValues[10], CultureInfo.InvariantCulture.NumberFormat)
                    };

                    if (mater.InteriorData1 > 0)
                    {
                        mater.InteriorSizeX = 1;
                        mater.InteriorSizeY = 1;
                    }

                    materialDatas.Add(mater);

                    materialIndex++;
                }

                float[, ,] materialColors = new float[materialDatas.Max(m => m.Index + 1), 4, 4];

                foreach (MaterialDefinition def in materialDatas)
                {
                    materialColors[def.Index, 0, 0] = (float)def.ColorBottom / 256;
                    materialColors[def.Index, 0, 1] = (float)def.ColorTop / 256;
                    materialColors[def.Index, 0, 2] = (float)def.InteriorData1 / 256;
                    materialColors[def.Index, 0, 3] = (float)def.InteriorData2 / 256;

                    materialColors[def.Index, 1, 0] = def.TopX;
                    materialColors[def.Index, 1, 1] = def.TopY;
                    materialColors[def.Index, 1, 2] = def.TopHeight;
                    materialColors[def.Index, 1, 3] = def.TopWidth;

                    materialColors[def.Index, 2, 0] = def.BottomX;
                    materialColors[def.Index, 2, 1] = def.BottomY;
                    materialColors[def.Index, 2, 2] = def.BottomHeight;
                    materialColors[def.Index, 2, 3] = def.BottomWidth;

                    materialColors[def.Index, 3, 0] = def.TilingTopX;
                    materialColors[def.Index, 3, 1] = def.TilingTopY;
                    materialColors[def.Index, 3, 2] = def.InteriorSizeX;
                    materialColors[def.Index, 3, 3] = def.InteriorSizeY;
                }

                //save the materialsbitmap
                MaterialTextureReference texRef = mesh.model.Materials.Where(m => m.Unknown4 == 0).First();


                DatabaseIndex imageIndex = DatabaseManager.Instance.Indices.Find(idx => idx.InstanceId == texRef.TextureInstanceId && idx.TypeId == PropertyConstants.RW4ImageType);
                if (imageIndex != null)
                {
                    using (MemoryStream imageByteStream = new MemoryStream(imageIndex.GetIndexData(true)))
                    {
                        RW4Model model = new RW4Model();
                        model.Read(imageByteStream);

                        RW4Section textureSection = model.Sections.First(s => s.TypeCode == SectionTypeCodes.Texture);
                        Texture    oldSection     = textureSection.obj as Texture;
                        uint       texDataSection = oldSection.texData.section.Number;

                        Texture newTexture = MaterialTextureConverter.SetTexture(materialColors);

                        newTexture.texData.section = new RW4Section()
                        {
                            Number = texDataSection
                        };
                        newTexture.texData.section.obj = new TextureBlob()
                        {
                            blob = newTexture.texData.blob
                        };

                        textureSection.obj = newTexture;

                        SaveRW4Model(imageIndex, model);
                    }
                }
            }

            int elementIndex = 0;

            //Loop through all nodes in the scene to read the information from the geometry and
            foreach (Collada141.node node in scene.node)
            {
                Collada141.instance_geometry geometryInstance = null;
                if (node.instance_geometry != null)
                {
                    geometryInstance = node.instance_geometry[0];
                }
                else if (node.node1 != null)
                {
                    if (node.node1[0].instance_geometry != null)
                    {
                        geometryInstance = node.node1[0].instance_geometry[0];
                    }
                }

                //check if the node contains any geometry - lights will be ignored
                if (geometryInstance != null)
                {
                    Collada141.geometry geo = geometry.geometry.First(g => "#" + g.id == geometryInstance.url);
                    //Collada141.geometry geo = geometry.geometry.First(g => "#" + g.id == node.instance_geometry[0].url);

                    // Collada141.geometry geo = geometry.geometry[0];
                    Collada141.mesh geoMesh = geo.Item as Collada141.mesh;

                    ///get the array of positions for the vertices
                    Collada141.source      src       = geoMesh.source.First(s => "#" + s.id == geoMesh.vertices.input.First(g => g.semantic == "POSITION").source);
                    Collada141.float_array positions = src.Item as Collada141.float_array;

                    foreach (Collada141.triangles triangles in geoMesh.Items)
                    {
                        List <string> localVertexDefinitions = new List <string>();
                        //calculate how many input indices each triangle exists of
                        ulong    triangleIndexSize = triangles.input.Max(t => t.offset) + 1;
                        string[] triangleIndices   = triangles.p.Split(new char[1] {
                            ' '
                        }, StringSplitOptions.RemoveEmptyEntries);

                        for (int i = 0; i < triangleIndices.Length; i += (int)triangleIndexSize)
                        {
                            string readVertexDefinition = string.Empty;

                            for (int j = 0; j < (int)triangleIndexSize; j++)
                            {
                                readVertexDefinition += triangleIndices[i + j] + " ";
                            }
                            readVertexDefinition += elementIndex + " ";
                            if (!vertexDefinitions.Contains(readVertexDefinition))
                            {
                                vertexDefinitions.Add(readVertexDefinition);
                                localVertexDefinitions.Add(readVertexDefinition);
                            }
                            triangleDefinitions.Add(vertexDefinitions.IndexOf(readVertexDefinition));
                        }

                        //create a vertex for each definition
                        for (int i = 0; i < localVertexDefinitions.Count; i++)
                        {
                            Vertex v = new Vertex();
                            v.Element = elementIndex;

                            v.SetSize(vertexFormatSection.VertexSize);

                            //Create vertexcomponents in the new vertex
                            v.VertexComponents = new List <IVertexComponentValue>();
                            foreach (VertexUsage usage in vertexFormatSection.VertexElements)
                            {
                                IVertexComponentValue component = VertexComponentValueFactory.CreateComponent(usage.DeclarationType);
                                component.Usage = usage.Usage;
                                v.VertexComponents.Add(component);
                            }

                            string[] vdef = localVertexDefinitions[i].Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);


                            //create vertexcomponents based on
                            int positionIndex = int.Parse(vdef[0]);
                            VertexFloat3Value positionComponent = (VertexFloat3Value)v.VertexComponents.First(c => c.Usage == D3DDECLUSAGE.D3DDECLUSAGE_POSITION && c.DeclarationType == D3DDECLTYPE.D3DDECLTYPE_FLOAT3);
                            if (positionComponent != null)
                            {
                                positionComponent.X = (float)positions.Values[(positionIndex * 3)];
                                positionComponent.Y = (float)positions.Values[(positionIndex * 3) + 1];
                                positionComponent.Z = (float)positions.Values[(positionIndex * 3) + 2];
                            }

                            Collada141.source      normalSrc = geoMesh.source.First(s => "#" + s.id == triangles.input.First(g => g.semantic == "NORMAL").source);
                            Collada141.float_array normals   = normalSrc.Item as Collada141.float_array;
                            int normalIndex = int.Parse(vdef[1]);
                            VertexUByte4Value normalComponent = (VertexUByte4Value)v.VertexComponents.FirstOrDefault(c => c.Usage == D3DDECLUSAGE.D3DDECLUSAGE_NORMAL && c.DeclarationType == D3DDECLTYPE.D3DDECLTYPE_UBYTE4);
                            if (normalComponent != null)
                            {
                                normalComponent.X = (byte)((float)normals.Values[normalIndex * 3] * 127.5F + 127.5F);
                                normalComponent.Y = (byte)((float)normals.Values[(normalIndex * 3) + 1] * 127.5F + 127.5F);
                                normalComponent.Z = (byte)((float)normals.Values[(normalIndex * 3) + 2] * 127.5F + 127.5F);
                                normalComponent.W = (byte)(255);
                            }
                            else
                            {
                                //check if there is a different normal component
                                VertexFloat3Value normalComponent2 = (VertexFloat3Value)v.VertexComponents.FirstOrDefault(c => c.Usage == D3DDECLUSAGE.D3DDECLUSAGE_NORMAL && c.DeclarationType == D3DDECLTYPE.D3DDECLTYPE_FLOAT3);
                                if (normalComponent2 != null)
                                {
                                    normalComponent2.X = (float)normals.Values[normalIndex * 3];
                                    normalComponent2.Y = (float)normals.Values[(normalIndex * 3) + 1];
                                    normalComponent2.Z = (float)normals.Values[(normalIndex * 3) + 2];
                                }
                            }

                            //Get the external tangents
                            IEnumerable <Collada141.InputLocalOffset> tangentInputs = triangles.input.Where(g => g.semantic == "TEXTANGENT");
                            Collada141.source      bottomTangentSrc = geoMesh.source.First(s => "#" + s.id == tangentInputs.ElementAt(0).source);
                            Collada141.float_array bottomTangents   = bottomTangentSrc.Item as Collada141.float_array;
                            int interiorTangentIndex = int.Parse(vdef[3]);

                            IEnumerable <IVertexComponentValue> tangentComponents = v.VertexComponents.Where(c => c.Usage == D3DDECLUSAGE.D3DDECLUSAGE_TANGENT && c.DeclarationType == D3DDECLTYPE.D3DDECLTYPE_UBYTE4);
                            if (tangentComponents.Count() != 0)
                            {
                                VertexUByte4Value tangentComponent = (VertexUByte4Value)tangentComponents.ElementAt(0);

                                tangentComponent.X = (byte)((float)bottomTangents.Values[interiorTangentIndex * 3] * 127.5F + 127.5F);
                                tangentComponent.Y = (byte)((float)bottomTangents.Values[(interiorTangentIndex * 3) + 1] * 127.5F + 127.5F);
                                tangentComponent.Z = (byte)((float)bottomTangents.Values[(interiorTangentIndex * 3) + 2] * 127.5F + 127.5F);
                                tangentComponent.W = (byte)(255);

                                //Get the internal tangents
                                if (tangentComponents.Count() > 1)
                                {
                                    Collada141.source      internalTangentSrc = geoMesh.source.First(s => "#" + s.id == tangentInputs.ElementAt(1).source);
                                    Collada141.float_array internalTangents   = internalTangentSrc.Item as Collada141.float_array;
                                    interiorTangentIndex = int.Parse(vdef[5]);


                                    VertexUByte4Value internalTangentComponent = (VertexUByte4Value)tangentComponents.ElementAt(1);

                                    internalTangentComponent.X = (byte)((float)internalTangents.Values[interiorTangentIndex * 3] * 127.5F + 127.5F);
                                    internalTangentComponent.Y = (byte)((float)internalTangents.Values[(interiorTangentIndex * 3) + 1] * 127.5F + 127.5F);
                                    internalTangentComponent.Z = (byte)((float)internalTangents.Values[(interiorTangentIndex * 3) + 2] * 127.5F + 127.5F);
                                    internalTangentComponent.W = (byte)(255);
                                }
                            }
                            else
                            {
                                //check for other tangents
                                VertexFloat3Value tangentComponent2 = (VertexFloat3Value)v.VertexComponents.FirstOrDefault(c => c.Usage == D3DDECLUSAGE.D3DDECLUSAGE_TANGENT && c.DeclarationType == D3DDECLTYPE.D3DDECLTYPE_FLOAT3);
                                if (tangentComponent2 != null)
                                {
                                    tangentComponent2.X = (float)bottomTangents.Values[interiorTangentIndex * 3];
                                    tangentComponent2.Y = (float)bottomTangents.Values[(interiorTangentIndex * 3) + 1];
                                    tangentComponent2.Z = (float)bottomTangents.Values[(interiorTangentIndex * 3) + 2];
                                }
                            }

                            IEnumerable <Collada141.InputLocalOffset> textureCoordinateInputs = triangles.input.Where(g => g.semantic == "TEXCOORD");
                            Collada141.source bottomUVsource = geoMesh.source.First(s => "#" + s.id == textureCoordinateInputs.ElementAt(0).source);
                            Collada141.source topUVsource    = null;
                            if (textureCoordinateInputs.Count() > 1)
                            {
                                topUVsource = geoMesh.source.First(s => "#" + s.id == textureCoordinateInputs.ElementAt(1).source);
                            }
                            else
                            {
                                topUVsource = geoMesh.source.First(s => "#" + s.id == textureCoordinateInputs.ElementAt(0).source);
                            }

                            Collada141.float_array bottomTextureCoordinates = bottomUVsource.Item as Collada141.float_array;
                            Collada141.float_array topTextureCoordinates    = topUVsource.Item as Collada141.float_array;

                            int uvIndex    = int.Parse(vdef[textureCoordinateInputs.ElementAt(0).offset]);
                            int topUvIndex = 0;
                            if (textureCoordinateInputs.Count() > 1)
                            {
                                topUvIndex = int.Parse(vdef[textureCoordinateInputs.ElementAt(1).offset]);
                            }
                            else
                            {
                                topUvIndex = int.Parse(vdef[textureCoordinateInputs.ElementAt(0).offset]);
                            }

                            //Get all the texture elements (should be 2 in the case of a building)
                            IEnumerable <IVertexComponentValue> uvMapComponents = v.VertexComponents.Where(c => c.Usage == D3DDECLUSAGE.D3DDECLUSAGE_TEXCOORD && c.DeclarationType == D3DDECLTYPE.D3DDECLTYPE_FLOAT4);

                            if (uvMapComponents.Count() != 0)
                            {
                                VertexFloat4Value uvBottomComponent = (VertexFloat4Value)uvMapComponents.ElementAt(0);
                                uvBottomComponent.X = (float)bottomTextureCoordinates.Values[(uvIndex * 3)];
                                uvBottomComponent.Y = 1 - (float)bottomTextureCoordinates.Values[(uvIndex * 3) + 1];
                                uvBottomComponent.Z = (float)topTextureCoordinates.Values[(topUvIndex * 3)];
                                uvBottomComponent.W = 1 - (float)topTextureCoordinates.Values[(topUvIndex * 3) + 1];

                                if (uvMapComponents.Count() > 1)
                                {
                                    VertexFloat4Value uvTopComponent = (VertexFloat4Value)uvMapComponents.ElementAt(1);
                                    uvTopComponent.X = 0;
                                    uvTopComponent.Y = 0;
                                    uvTopComponent.Z = 1;
                                    uvTopComponent.W = 1;
                                }
                            }
                            else
                            {
                                //check for other UVs
                                VertexFloat2Value uvComponent2 = (VertexFloat2Value)v.VertexComponents.FirstOrDefault(c => c.Usage == D3DDECLUSAGE.D3DDECLUSAGE_TEXCOORD && c.DeclarationType == D3DDECLTYPE.D3DDECLTYPE_FLOAT2);
                                if (uvComponent2 != null)
                                {
                                    uvComponent2.X = (float)bottomTextureCoordinates.Values[uvIndex * 3];
                                    uvComponent2.Y = (float)bottomTextureCoordinates.Values[(uvIndex * 3) + 1];
                                }
                            }


                            VertexD3DColorValue colorComponent = (VertexD3DColorValue)v.VertexComponents.FirstOrDefault(c => c.Usage == D3DDECLUSAGE.D3DDECLUSAGE_COLOR && c.DeclarationType == D3DDECLTYPE.D3DDECLTYPE_D3DCOLOR);
                            if (triangles.material.StartsWith("SCP"))
                            {
                                //get the index of the material
                                MaterialDefinition  def             = materialDatas.First(m => m.Id == triangles.material);
                                VertexD3DColorValue colorComponent2 = (VertexD3DColorValue)v.VertexComponents.First(c => c.Usage == D3DDECLUSAGE.D3DDECLUSAGE_COLOR && c.DeclarationType == D3DDECLTYPE.D3DDECLTYPE_D3DCOLOR);
                                if (colorComponent != null)
                                {
                                    //Unused
                                    colorComponent2.A = 0;
                                    //Unused
                                    colorComponent2.R = 0;
                                    //Material index
                                    colorComponent2.G = (byte)def.Index;
                                    //RNG for windows
                                    colorComponent2.B = 84;
                                }

                                //if the item has a defined interior, recalculate the interior UV's
                                if (def.InteriorData1 > 0)
                                {
                                    // IEnumerable<IVertexComponentValue> uvMapComponentInterior = v.VertexComponents.Where(c => c.Usage == D3DDECLUSAGE.D3DDECLUSAGE_TEXCOORD && c.DeclarationType == D3DDECLTYPE.D3DDECLTYPE_FLOAT4);
                                    //  VertexFloat4Value uvInterior = (VertexFloat4Value)uvMapComponents.ElementAt(0);
                                    // uvInterior.X = (uvInterior.X / def.BottomX);
                                    // uvInterior.Y = (uvInterior.Y / def.TopX);
                                }
                            }

                            if (node.Items != null)
                            {
                                for (int transIndex = node.Items.Length - 1; transIndex >= 0; transIndex--)
                                {
                                    v = ApplyTransformation(v, node.Items[transIndex], node.ItemsElementName[transIndex]);
                                }
                            }

                            vertexList.Add(v);
                        }
                        elementIndex++;
                    }
                }
            }

            mesh.vertices.vertices         = new SporeMaster.RenderWare4.VertexBuffer(vertexList.Count);
            mesh.vertices.vertices.section = new RW4Section()
            {
                Number = verticesSectionNumber
            };
            mesh.vertices.vertexSize = vertexFormatSection.VertexSize;

            for (int i = 0; i < vertexList.Count; i++)
            {
                mesh.vertices.vertices[i] = vertexList[i];
            }

            //mesh.triangles = new RW4TriangleArray();
            mesh.triangles.triangles         = new Buffer <Triangle>(triangleDefinitions.Count / 3);
            mesh.triangles.triangles.section = new RW4Section()
            {
                Number = trianglesSectionNumber
            };


            for (int i = 0; i < triangleDefinitions.Count / 3; i++)
            {
                mesh.triangles.triangles[i] = new Triangle()
                {
                    i = (uint)triangleDefinitions[(i * 3)],
                    j = (uint)triangleDefinitions[(i * 3) + 1],
                    k = (uint)triangleDefinitions[(i * 3) + 2],
                };
            }


            return(mesh);
        }
Пример #12
0
        private void CreateLotModel()
        {
            //Create the plane representing the lot (texture & size)
            if (UnitFileEntry.LotOverlayBoxSize != null)
            {
                Vector2Property lotSize = UnitFileEntry.LotOverlayBoxSize;

                RectangleVisual3D groundPlane = new RectangleVisual3D();

                groundPlane.Width     = (lotSize.X);
                groundPlane.Length    = (lotSize.Y);
                groundPlane.Transform = new RotateTransform3D(new AxisAngleRotation3D(new Vector3D(0, 0, 1), -90));
                if (UnitFileEntry.LotOverlayBoxOffset != null)
                {
                    //    groundPlane.Transform = Transform3DHelper.CombineTransform(groundPlane.Transform, new TranslateTransform3D(-1 * UnitFileEntry.LotOverlayBoxOffset.X, -1 * UnitFileEntry.LotOverlayBoxOffset.Y, 0));
                }
                if (UnitFileEntry.LotPlacementTransform != null && UnitFileEntry.LotPlacementTransform.Count > 0)
                {
                    groundPlane.Transform = Transform3DHelper.CombineTransform(groundPlane.Transform, (Transform3D) new MatrixTransform3D(UnitFileEntry.LotPlacementTransform[0].GetAsMatrix3D()).Inverse);
                }

                if (UnitFileEntry.LotMask != null)
                {
                    DatabaseIndex index = DatabaseManager.Instance.Indices.Find(k => k.InstanceId == UnitFileEntry.LotMask.InstanceId && k.TypeId == 0x2f4e681c);
                    if (index != null)
                    {
                        Color color1 = Colors.Black;
                        Color color2 = Colors.Red;
                        Color color3 = Colors.Green;
                        Color color4 = Colors.Blue;

                        if (UnitFileEntry.LotColor1 != null)
                        {
                            color1 = Color.FromRgb((byte)(255 * UnitFileEntry.LotColor1.R), (byte)(255 * UnitFileEntry.LotColor1.G), (byte)(255 * UnitFileEntry.LotColor1.B));
                        }
                        if (UnitFileEntry.LotColor2 != null)
                        {
                            color2 = Color.FromRgb((byte)(255 * UnitFileEntry.LotColor2.R), (byte)(255 * UnitFileEntry.LotColor2.G), (byte)(255 * UnitFileEntry.LotColor2.B));
                        }
                        if (UnitFileEntry.LotColor3 != null)
                        {
                            color3 = Color.FromRgb((byte)(255 * UnitFileEntry.LotColor3.R), (byte)(255 * UnitFileEntry.LotColor3.G), (byte)(255 * UnitFileEntry.LotColor3.B));
                        }
                        if (UnitFileEntry.LotColor4 != null)
                        {
                            color4 = Color.FromRgb((byte)(255 * UnitFileEntry.LotColor4.R), (byte)(255 * UnitFileEntry.LotColor4.G), (byte)(255 * UnitFileEntry.LotColor4.B));
                        }

                        using (MemoryStream byteStream = new MemoryStream(index.GetIndexData(true)))
                        {
                            RasterImage rasterImage = RasterImage.CreateFromStream(byteStream, RasterChannel.Preview, color4, color3, color2, color1);
                            groundPlane.Material = new DiffuseMaterial(new ImageBrush(rasterImage.MipMaps[0]));
                        }
                    }

                    groundPlane.BackMaterial = new DiffuseMaterial(new SolidColorBrush(Colors.Transparent));
                }

                viewPort.Children.Add(groundPlane);
            }
        }
Пример #13
0
        private void ReloadDisplayProperties(Dictionary <uint, Property> propertyCollection)
        {
            try
            {
                displayProperties = new List <PropertyModel>();
                foreach (KeyValuePair <uint, Property> prop in propertyCollection)
                {
                    if (prop.Value is ArrayProperty)
                    {
                        int           arrIndex = 0;
                        ArrayProperty arr      = prop.Value as ArrayProperty;
                        foreach (Property subProp in arr.Values)
                        {
                            subProp.PropertyChanged += new EventHandler(Value_PropertyChanged);
                            displayProperties.Add(new PropertyModel(prop.Key, subProp, true, arrIndex));
                            arrIndex++;
                        }
                    }
                    else
                    {
                        displayProperties.Add(new PropertyModel(prop.Key, prop.Value, false, 0));
                    }
                    if (prop.Value is KeyProperty)
                    {
                        KeyProperty kp = prop.Value as KeyProperty;
                        if (kp.TypeId == 0x2f7d0004 || kp.TypeId == 0x3f8662ea)//PNG
                        {
                            DatabaseIndex imageIndex = DatabaseManager.Instance.Indices.Find(idx => idx.InstanceId == kp.InstanceId && idx.GroupContainer == kp.GroupContainer && idx.TypeId == kp.TypeId);
                            if (imageIndex != null)
                            {
                                Image  imageControl = new Image();
                                byte[] imgdata      = imageIndex.GetIndexData(true);
                                using (MemoryStream imageByteStream = new MemoryStream(imgdata))
                                {
                                    try
                                    {
                                        BitmapImage image = new BitmapImage();
                                        image.BeginInit();
                                        image.StreamSource = imageByteStream;
                                        image.CacheOption  = BitmapCacheOption.OnLoad;
                                        image.EndInit();
                                        imageControl.Source  = image;
                                        imageControl.Width   = 80;
                                        imageControl.Height  = 80;
                                        imageControl.Stretch = Stretch.Fill;
                                        pnlImages.Children.Add(imageControl);
                                    }
                                    catch
                                    {
                                    }
                                }
                            }
                        }
                    }
                    prop.Value.PropertyChanged += new EventHandler(Value_PropertyChanged);
                }
                ((Expander)pnlImages.Parent).Visibility = (pnlImages.Children.Count == 0) ? System.Windows.Visibility.Collapsed : System.Windows.Visibility.Visible;

                dataGrid1.ItemsSource = displayProperties;

                txtStatus.Text = string.Format("Loaded {0} of {1} properties", propertyFile.Values.Count, propertyFile.PropertyCount);
            }
            catch
            {
            }
        }
Пример #14
0
        public Dictionary <uint, Property> GetParentProperties(Dictionary <uint, Property> returnProperties, Dictionary <uint, Property> parentProperties)
        {
            if (returnProperties == null)
            {
                returnProperties = new Dictionary <uint, Property>();
            }

            //add keys that didn't exist yet
            foreach (uint index in parentProperties.Keys)
            {
                if (!returnProperties.ContainsKey(index) && index != 0x00B2CCCB)
                {
                    //This property specifies behavior bundles to exclude - instead of adding this property, remove all the bundles from the appropriate arrays

                    returnProperties.Add(index, parentProperties[index]);
                }
            }

            if (parentProperties.ContainsKey(0x00B2CCCB))
            {
                Property    parentProperty      = parentProperties[0x00B2CCCB];
                KeyProperty parentPropertyValue = parentProperty as KeyProperty;

                //search the currently opened package for this type/instance group id. if it doesn't exist, find it in the default simcity packages

                DatabaseIndex parentIndex = null;
                if (DatabaseManager.Instance.Indices.Exists(i => i.InstanceId == parentPropertyValue.InstanceId && i.TypeId == parentPropertyValue.TypeId && i.GroupContainer == parentPropertyValue.GroupContainer))
                {
                    parentIndex = DatabaseManager.Instance.Indices.First(i => i.InstanceId == parentPropertyValue.InstanceId && i.TypeId == parentPropertyValue.TypeId && i.GroupContainer == parentPropertyValue.GroupContainer);
                }
                else
                {
                    string        folderEcoGamePath = Properties.Settings.Default.SimCityFolder + @"\SimCityUserData\EcoGame\";
                    DirectoryInfo folderEcoGame     = new DirectoryInfo(folderEcoGamePath);

                    //search the simcity data packages for this type/instance group id. if it doesn't exist, find it in the default simcity packages
                    FileInfo[]         scriptsFiles   = folderEcoGame.GetFiles("SimCity-Scripts_*.package", SearchOption.TopDirectoryOnly);
                    FileInfo           scriptsFile    = scriptsFiles.First(sf => sf.LastWriteTime == scriptsFiles.Max(sf2 => sf2.LastWriteTime));
                    DatabasePackedFile ScriptsPackage = DatabasePackedFile.LoadFromFile(scriptsFile.FullName);

                    parentIndex = ScriptsPackage.Indices.FirstOrDefault(i => i.InstanceId == parentPropertyValue.InstanceId && i.TypeId == parentPropertyValue.TypeId && i.GroupContainer == parentPropertyValue.GroupContainer);
                }

                // if(parentIndex != null)
                // {
                byte[] data = parentIndex.GetIndexData(true);

                using (Stream s = new MemoryStream(data, 0, data.Length))
                {
                    PropertyFile propertyFile = new PropertyFile();
                    propertyFile.Read(s);

                    return(GetParentProperties(returnProperties, propertyFile.Values));
                }



                throw new Exception("Inheritance not found!");
                return(returnProperties);
            }
            else
            {
                return(returnProperties);
            }
        }