Esempio n. 1
0
        /// <summary>
        /// loads the bone nodes from a scene
        /// </summary>
        /// <param name="Scene"></param>
        public void LoadFromScene(SBScene Scene)
        {
            Nodes.Clear();
            Dictionary <SBBone, SBTreeNode> boneToNode = new Dictionary <SBBone, SBTreeNode>();

            if (Scene.Skeleton != null)
            {
                foreach (var bone in Scene.Skeleton.Bones)
                {
                    var node = new SBTreeNode(bone.Name)
                    {
                        Tag = bone
                    };
                    boneToNode.Add(bone, node);
                    if (bone.Parent == null)
                    {
                        Nodes.Add(node);
                    }
                    else if (boneToNode.ContainsKey(bone.Parent))
                    {
                        boneToNode[bone.Parent].Nodes.Add(node);
                    }
                }
            }
            if (Nodes.Count > 0)
            {
                Nodes[0].ExpandAll();
            }
        }
Esempio n. 2
0
        public static void Open(string FileName, SBScene Scene)
        {
            ISSBH_File File;

            if (SSBH.TryParseSSBHFile(FileName, out File))
            {
                if (File is MESH mesh)
                {
                    if (mesh.VersionMajor != 1 && mesh.VersionMinor != 10)
                    {
                        SBConsole.WriteLine($"Mesh Version {mesh.VersionMajor}.{mesh.VersionMinor} not supported");
                        return;
                    }
                    if (mesh.UnknownOffset != 0 || mesh.UnknownSize != 0)
                    {
                        SBConsole.WriteLine($"Warning: Unknown Mesh format detected");
                    }

                    SBUltimateModel model = new SBUltimateModel();
                    model.Name           = mesh.ModelName;
                    model.BoundingSphere = new Vector4(mesh.BoundingSphereX, mesh.BoundingSphereY, mesh.BoundingSphereZ, mesh.BoundingSphereRadius);

                    ((SBSceneSSBH)Scene).Model = model;

                    SSBHVertexAccessor accessor = new SSBHVertexAccessor(mesh);
                    {
                        foreach (var meshObject in mesh.Objects)
                        {
                            SBUltimateMesh sbMesh = new SBUltimateMesh();
                            foreach (var attr in meshObject.Attributes)
                            {
                                foreach (var atstring in attr.AttributeStrings)
                                {
                                    UltimateVertexAttribute at;
                                    if (Enum.TryParse(atstring.Name, out at))
                                    {
                                        sbMesh.EnableAttribute(at);
                                    }
                                }
                            }
                            sbMesh.Name       = meshObject.Name;
                            sbMesh.ParentBone = meshObject.ParentBoneName;

                            sbMesh.BoundingSphere = new BoundingSphere(meshObject.BoundingSphereX, meshObject.BoundingSphereY, meshObject.BoundingSphereZ, meshObject.BoundingSphereRadius);
                            sbMesh.AABoundingBox  = new AABoundingBox(new Vector3(meshObject.MinBoundingBoxX, meshObject.MinBoundingBoxY, meshObject.MinBoundingBoxZ),
                                                                      new Vector3(meshObject.MaxBoundingBoxX, meshObject.MaxBoundingBoxY, meshObject.MaxBoundingBoxZ));
                            sbMesh.OrientedBoundingBox = new OrientedBoundingBox(new Vector3(meshObject.OBBCenterX, meshObject.OBBCenterY, meshObject.OBBCenterZ),
                                                                                 new Vector3(meshObject.OBBSizeX, meshObject.OBBSizeY, meshObject.OBBSizeZ),
                                                                                 new Matrix3(meshObject.M11, meshObject.M12, meshObject.M13,
                                                                                             meshObject.M21, meshObject.M22, meshObject.M23,
                                                                                             meshObject.M31, meshObject.M32, meshObject.M33));

                            sbMesh.Indices  = new List <uint>(accessor.ReadIndices(0, meshObject.IndexCount, meshObject));
                            sbMesh.Vertices = CreateVertices(mesh, Scene.Skeleton, meshObject, accessor, sbMesh.Indices.ToArray());
                            model.Meshes.Add(sbMesh);
                        }
                    }
                }
            }
        }
Esempio n. 3
0
        public static void Open(string FileName, SBScene Scene)
        {
            ISSBH_File File;

            if (SSBH.TryParseSSBHFile(FileName, out File))
            {
                if (File is MESH mesh)
                {
                    if (mesh.VersionMajor != 1 && mesh.VersionMinor != 10)
                    {
                        SBConsole.WriteLine($"Mesh Version {mesh.VersionMajor}.{mesh.VersionMinor} not supported");
                        return;
                    }
                    if (mesh.UnknownOffset != 0 || mesh.UnknownSize != 0)
                    {
                        SBConsole.WriteLine($"Warning: Unknown Mesh format detected");
                    }

                    SBUltimateModel model = new SBUltimateModel();
                    model.Name           = mesh.ModelName;
                    model.BoundingSphere = new Vector4(mesh.BoundingSphereX, mesh.BoundingSphereY, mesh.BoundingSphereZ, mesh.BoundingSphereRadius / 2);

                    Vector3 min = new Vector3(mesh.MinBoundingBoxX, mesh.MinBoundingBoxY, mesh.MinBoundingBoxZ);
                    Vector3 max = new Vector3(mesh.MaxBoundingBoxX, mesh.MaxBoundingBoxY, mesh.MaxBoundingBoxZ);

                    model.VolumeCenter = (max + min) / 2;
                    model.VolumeSize   = (max - min) / 2;

                    ((SBSceneSSBH)Scene).Model = model;

                    using (SSBHVertexAccessor accessor = new SSBHVertexAccessor(mesh))
                    {
                        foreach (var meshObject in mesh.Objects)
                        {
                            SBUltimateMesh <UltimateVertex> sbMesh = new SBUltimateMesh <UltimateVertex>();
                            foreach (var attr in meshObject.Attributes)
                            {
                                foreach (var atstring in attr.AttributeStrings)
                                {
                                    MESHAttribute at;
                                    if (Enum.TryParse <MESHAttribute>(atstring.Name, out at))
                                    {
                                        //SBConsole.WriteLine("\tLoaded:" + at.ToString());
                                        sbMesh.ExportAttributes.Add(at);
                                    }
                                }
                            }
                            sbMesh.Name           = meshObject.Name;
                            sbMesh.BoundingSphere = new Vector4(meshObject.BoundingSphereX, meshObject.BoundingSphereY, meshObject.BoundingSphereZ, meshObject.BoundingSphereRadius);
                            sbMesh.ParentBone     = meshObject.ParentBoneName;

                            sbMesh.Indices  = new List <uint>(accessor.ReadIndices(0, meshObject.IndexCount, meshObject));
                            sbMesh.Vertices = CreateVertices(mesh, Scene.Skeleton, meshObject, accessor, sbMesh.Indices.ToArray());
                            model.Meshes.Add(sbMesh);
                        }
                    }
                }
            }
        }
Esempio n. 4
0
 public void Clear()
 {
     meshName.Text = "";
     parentBoneSelector.Items.Clear();
     materialSelector.Items.Clear();
     SelectedScene  = null;
     SelectedMeshes = null;
 }
Esempio n. 5
0
        public static void Save(string fileName, SBScene scene, string sourceSkelPath)
        {
            var Skeleton = scene.Skeleton;

            var skelFile = new Skel();

            skelFile.MajorVersion = 1;
            skelFile.MinorVersion = 0;

            List <SkelBoneEntry> BoneEntries        = new List <SkelBoneEntry>();
            List <Matrix4x4>     Transforms         = new List <Matrix4x4>();
            List <Matrix4x4>     InvTransforms      = new List <Matrix4x4>();
            List <Matrix4x4>     WorldTransforms    = new List <Matrix4x4>();
            List <Matrix4x4>     InvWorldTransforms = new List <Matrix4x4>();

            // Attempt to match bone order to an existing SKEL if possible.
            var sortedBones = Skeleton.Bones;

            if (!string.IsNullOrEmpty(sourceSkelPath))
            {
                sortedBones = GetBoneOrderBasedOnReference(Skeleton.Bones, sourceSkelPath);
            }

            short index = 0;

            foreach (var bone in sortedBones)
            {
                var boneEntry = new SkelBoneEntry
                {
                    Name = bone.Name,
                    Type = bone.Type,
                    Id   = index++
                };
                boneEntry.Type     = 1;
                boneEntry.ParentId = -1;
                if (bone.Parent != null)
                {
                    boneEntry.ParentId = (short)Array.IndexOf(sortedBones, bone.Parent);
                }
                BoneEntries.Add(boneEntry);

                Transforms.Add(bone.Transform.ToSsbh());
                InvTransforms.Add(bone.Transform.Inverted().ToSsbh());
                WorldTransforms.Add(bone.WorldTransform.ToSsbh());
                InvWorldTransforms.Add(bone.InvWorldTransform.ToSsbh());
            }

            skelFile.BoneEntries       = BoneEntries.ToArray();
            skelFile.Transform         = Transforms.ToArray();
            skelFile.InvTransform      = InvTransforms.ToArray();
            skelFile.WorldTransform    = WorldTransforms.ToArray();
            skelFile.InvWorldTransform = InvWorldTransforms.ToArray();

            Ssbh.TrySaveSsbhFile(fileName, skelFile);
        }
Esempio n. 6
0
        public static void Open(string FileName, SBScene Scene)
        {
            SsbhFile File;

            if (Ssbh.TryParseSsbhFile(FileName, out File))
            {
                if (File is Mesh mesh)
                {
                    if (mesh.VersionMajor != 1 && mesh.VersionMinor != 10)
                    {
                        SBConsole.WriteLine($"Mesh Version {mesh.VersionMajor}.{mesh.VersionMinor} not supported");
                        return;
                    }
                    if (mesh.UnknownOffset != 0 || mesh.UnknownSize != 0)
                    {
                        SBConsole.WriteLine($"Warning: Unknown Mesh format detected");
                    }

                    SBUltimateModel model = new SBUltimateModel();
                    model.Name           = mesh.ModelName;
                    model.BoundingSphere = new Vector4(mesh.BoundingSphereCenter.X, mesh.BoundingSphereCenter.Y, mesh.BoundingSphereCenter.Z, mesh.BoundingSphereRadius);

                    ((SBSceneSSBH)Scene).Model = model;

                    SsbhVertexAccessor accessor = new SsbhVertexAccessor(mesh);
                    {
                        foreach (var meshObject in mesh.Objects)
                        {
                            SBUltimateMesh sbMesh = new SBUltimateMesh();
                            sbMesh.EnableAttributes(meshObject);

                            sbMesh.Name       = meshObject.Name;
                            sbMesh.ParentBone = meshObject.ParentBoneName;

                            sbMesh.BoundingSphere = new BoundingSphere(meshObject.BoundingSphereCenter.X, meshObject.BoundingSphereCenter.Y, meshObject.BoundingSphereCenter.Z, meshObject.BoundingSphereRadius);
                            sbMesh.AABoundingBox  = new AABoundingBox(new Vector3(meshObject.BoundingBoxMin.X, meshObject.BoundingBoxMin.Y, meshObject.BoundingBoxMin.Z),
                                                                      new Vector3(meshObject.BoundingBoxMax.X, meshObject.BoundingBoxMax.Y, meshObject.BoundingBoxMax.Z));
                            sbMesh.OrientedBoundingBox = new OrientedBoundingBox(
                                meshObject.OrientedBoundingBoxCenter.ToOpenTK(),
                                meshObject.OrientedBoundingBoxSize.ToOpenTK(),
                                meshObject.OrientedBoundingBoxTransform.ToOpenTK());

                            sbMesh.Indices  = new List <uint>(accessor.ReadIndices(0, meshObject.IndexCount, meshObject));
                            sbMesh.Vertices = CreateVertices(mesh, Scene.Skeleton, meshObject, accessor, sbMesh.Indices.ToArray());
                            model.Meshes.Add(sbMesh);
                        }
                    }
                }
            }
        }
Esempio n. 7
0
        /// <summary>
        /// Binds the material to shader
        /// </summary>
        /// <param name="scene"></param>
        /// <param name="shader"></param>
        public void Bind(SBScene scene, Shader shader)
        {
            var ssbhScene = (SBSceneSSBH)scene;

            BindRasterizerState();

            int TextureUnit = 0;

            foreach (var p in Properties)
            {
                if (shader.GetUniformLocation(p.Name) != -1 || shader.GetAttribLocation(p.Name) != -1)
                {
                    if (p.PropertyType == typeof(SBMatAttrib <string>))
                    {
                        var       value       = ((SBMatAttrib <string>)MatAttribs[p.Name]).AnimatedValue;
                        SBSurface surface     = ssbhScene.nameToSurface.ContainsKey(value) ? ssbhScene.nameToSurface[value] : null;
                        var       surfaceInfo = nameToDefaultTexture[p.Name];
                        BindSurface(shader, ssbhScene, surface, surfaceInfo, p.Name, TextureUnit++);
                    }
                    else
                    if (p.PropertyType == typeof(SBMatAttrib <Vector4>))
                    {
                        shader.SetVector4(p.Name, ((SBMatAttrib <Vector4>)MatAttribs[p.Name]).AnimatedValue);
                    }
                    else
                    if (p.PropertyType == typeof(SBMatAttrib <float>))
                    {
                        shader.SetFloat(p.Name, ((SBMatAttrib <float>)MatAttribs[p.Name]).AnimatedValue);
                    }
                    else
                    if (p.PropertyType == typeof(SBMatAttrib <bool>))
                    {
                        shader.SetBoolToInt(p.Name, ((SBMatAttrib <bool>)MatAttribs[p.Name]).AnimatedValue);
                    }
                    else
                    if (p.PropertyType == typeof(bool))
                    {
                        var value = p.GetValue(this); // slow
                        shader.SetBoolToInt(p.Name, (bool)value);
                    }
                }
            }

            shader.SetTexture("diffusePbrCube", DefaultTextures.Instance.diffusePbr, TextureUnit++);
            shader.SetTexture("specularPbrCube", DefaultTextures.Instance.specularPbr, TextureUnit++);
            shader.SetTexture("iblLut", DefaultTextures.Instance.iblLut, TextureUnit++);
            shader.SetTexture("uvPattern", DefaultTextures.Instance.uvPattern, TextureUnit++);
        }
Esempio n. 8
0
        public static void Save(string FileName, SBScene Scene)
        {
            var Skeleton = Scene.Skeleton;

            var skelFile = new Skel();

            skelFile.MajorVersion = 1;
            skelFile.MinorVersion = 0;

            List <SkelBoneEntry> BoneEntries        = new List <SkelBoneEntry>();
            List <SkelMatrix>    Transforms         = new List <SkelMatrix>();
            List <SkelMatrix>    InvTransforms      = new List <SkelMatrix>();
            List <SkelMatrix>    WorldTransforms    = new List <SkelMatrix>();
            List <SkelMatrix>    InvWorldTransforms = new List <SkelMatrix>();

            short index = 0;
            Dictionary <SBBone, short> BoneToIndex = new Dictionary <SBBone, short>();
            var OrderedBones = SortBones(Skeleton.Bones);

            foreach (var bone in OrderedBones)
            {
                BoneToIndex.Add(bone, index);
                var boneentry = new SkelBoneEntry();
                boneentry.Name     = bone.Name;
                boneentry.Type     = bone.Type;
                boneentry.Id       = index++;
                boneentry.Type     = 1;
                boneentry.ParentId = -1;
                if (bone.Parent != null)// && BoneToIndex.ContainsKey(bone.Parent))
                {
                    boneentry.ParentId = (short)OrderedBones.IndexOf(bone.Parent);
                }
                BoneEntries.Add(boneentry);

                Transforms.Add(TKMatrix_to_Skel(bone.Transform));
                InvTransforms.Add(TKMatrix_to_Skel(bone.Transform.Inverted()));
                WorldTransforms.Add(TKMatrix_to_Skel(bone.WorldTransform));
                InvWorldTransforms.Add(TKMatrix_to_Skel(bone.InvWorldTransform));
            }

            skelFile.BoneEntries       = BoneEntries.ToArray();
            skelFile.Transform         = Transforms.ToArray();
            skelFile.InvTransform      = InvTransforms.ToArray();
            skelFile.WorldTransform    = WorldTransforms.ToArray();
            skelFile.InvWorldTransform = InvWorldTransforms.ToArray();

            Ssbh.TrySaveSsbhFile(FileName, skelFile);
        }
Esempio n. 9
0
        /// <summary>
        /// loads the bone nodes from a scene
        /// </summary>
        /// <param name="Scene"></param>
        private void LoadFromScene(SBScene Scene)
        {
            meshObjectList.Items.Clear();
            var meshes = Scene.GetMeshObjects();

            if (meshes != null)
            {
                foreach (var mesh in meshes)
                {
                    ListViewItem item = new ListViewItem
                    {
                        Text    = mesh.ToString(),
                        Tag     = mesh,
                        Checked = mesh.Visible
                    };
                    meshObjectList.Items.Add(item);
                }
            }
        }
Esempio n. 10
0
        /// <summary>
        /// Adds the selected mesh from the Scene to the editor
        /// </summary>
        /// <param name="scene"></param>
        public void SetSelectedMeshFromScene(SBScene scene)
        {
            // clear currently selected meshes
            SelectedMeshes = null;

            // loads the materials from the scene
            materialSelector.Items.Clear();
            foreach (var material in scene.GetMaterials())
            {
                materialSelector.Items.Add(material);
            }

            // loads the bone names from the scene
            parentBoneSelector.Items.Clear();
            foreach (var bone in scene.Skeleton.Bones)
            {
                parentBoneSelector.Items.Add(bone.Name);
            }

            // get selected meshes in scene
            List <ISBMesh> selected = new List <ISBMesh>();

            foreach (var mesh in scene.GetMeshObjects())
            {
                if (mesh.Selected)
                {
                    selected.Add(mesh);
                }
            }

            // for now just get the first selected
            if (selected.Count > 0)
            {
                meshName.Text = selected[0].Name + (selected.Count > 1 ? $" + {selected.Count - 1} Others" : "");
                materialSelector.SelectedItem   = selected[0].Material;
                parentBoneSelector.SelectedItem = selected[0].ParentBone;
            }

            //
            SelectedMeshes = selected.ToArray();
            PropertyGrid.SelectedObjects = SelectedMeshes;
            SelectedScene = scene;
        }
Esempio n. 11
0
        /// <summary>
        /// Sets up the controls for the scene
        /// </summary>
        /// <param name="scene"></param>
        private void SetupScene(SBScene scene)
        {
            Viewport.Scene = scene;

            BoneTree.LoadFromScene(scene);
            MeshList.LoadFromScene(scene);

            RightPane.Contents.Clear();
            if (scene.HasBones)
            {
                RightPane.Contents.Add(BoneEditor);
                RightPane.Contents.Add(new Splitter()
                {
                    Dock = DockStyle.Top
                });
            }
            if (scene.HasMesh)
            {
                RightPane.Contents.Add(MeshPanel);
                RightPane.Contents.Add(new Splitter()
                {
                    Dock = DockStyle.Top
                });
            }
            if (scene.HasMesh)
            {
                RightPane.Contents.Add(MeshList);
                RightPane.Contents.Add(new Splitter()
                {
                    Dock = DockStyle.Top
                });
            }
            if (scene.HasBones)
            {
                RightPane.Contents.Add(BoneTree);
                RightPane.Contents.Add(new Splitter()
                {
                    Dock = DockStyle.Top
                });
            }
        }
Esempio n. 12
0
        public void UpdateScene(float Frame, SBScene scene)
        {
            if (scene == null)
            {
                return;
            }

            // Materials
            foreach (SBMaterialAnimation a in MaterialNodes)
            {
                foreach (var material in scene.GetMaterials())
                {
                    if (material.Label.Equals(a.MaterialName))
                    {
                        material.AnimateParam(a.AttributeName, a.Keys.GetValue(Frame));
                    }
                }
            }
            // Visibility
            foreach (SBVisibilityAnimation a in VisibilityNodes)
            {
                foreach (var mesh in scene.GetMeshObjects())
                {
                    // names match with start ignoreing the _VIS tags
                    if (mesh.Name.StartsWith(a.MeshName))
                    {
                        mesh.Visible = a.Visibility.GetValue(Frame);
                    }
                }
            }
            // Bones
            foreach (SBTransformAnimation a in TransformNodes)
            {
                var bone = scene.Skeleton[a.Name];
                if (bone != null)
                {
                    bone.AnimatedTransform = a.Transform.GetValue(Frame);
                }
            }
        }
Esempio n. 13
0
        public static SBSkeleton Open(string FileName, SBScene Scene)
        {
            SsbhFile File;

            if (Ssbh.TryParseSsbhFile(FileName, out File))
            {
                if (File is Skel skel)
                {
                    var Skeleton = new SBSkeleton();
                    Scene.Skeleton = Skeleton;

                    Dictionary <int, SBBone> idToBone   = new Dictionary <int, SBBone>();
                    Dictionary <SBBone, int> needParent = new Dictionary <SBBone, int>();
                    foreach (var b in skel.BoneEntries)
                    {
                        SBBone bone = new SBBone();
                        bone.Name      = b.Name;
                        bone.Type      = b.Type;
                        bone.Transform = Skel_to_TKMatrix(skel.Transform[b.Id]);
                        idToBone.Add(b.Id, bone);
                        if (b.ParentId == -1)
                        {
                            Skeleton.AddRoot(bone);
                        }
                        else
                        {
                            needParent.Add(bone, b.ParentId);
                        }
                    }
                    foreach (var v in needParent)
                    {
                        v.Key.Parent = idToBone[v.Value];
                    }

                    return(Skeleton);
                }
            }
            return(null);
        }
        /// <summary>
        /// sets and binds the material to an editor
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="material"></param>
        public void SetMaterialsFromScene(SBScene scene)
        {
            ISBMaterial[] material = scene.GetMaterials();

            if (material == null || material.Length == 0)
            {
                ClearMaterial();
                return;
            }
            typeLabel.Text = "Material Type: " + material[0].GetType().Name;

            SetMaterial(material[0]);

            materialList.Clear();
            foreach (var mat in material)
            {
                var listitem = new ListViewItem();
                listitem.Tag  = mat;
                listitem.Text = mat.Label;
                materialList.Items.Add(listitem);
            }
        }
Esempio n. 15
0
        public static void Open(string FileName, SBScene Scene)
        {
            ISSBH_File File;

            if (SSBH.TryParseSSBHFile(FileName, out File))
            {
                if (File is SKEL skel)
                {
                    var Skeleton = new SBSkeleton();
                    Scene.Skeleton = Skeleton;

                    Dictionary <int, SBBone> idToBone   = new Dictionary <int, SBBone>();
                    Dictionary <SBBone, int> needParent = new Dictionary <SBBone, int>();
                    foreach (var b in skel.BoneEntries)
                    {
                        SBBone bone = new SBBone();
                        bone.Name      = b.Name;
                        bone.Type      = b.Type;
                        bone.Transform = Skel_to_TKMatrix(skel.Transform[b.ID]);
                        idToBone.Add(b.ID, bone);
                        if (b.ParentID == -1)
                        {
                            Skeleton.AddRoot(bone);
                        }
                        else
                        {
                            needParent.Add(bone, b.ParentID);
                        }
                    }
                    foreach (var v in needParent)
                    {
                        v.Key.Parent = idToBone[v.Value];
                    }
                }
            }
        }
Esempio n. 16
0
        public static void Open(string FilePath, SBScene Scene)
        {
            using (BinaryReader reader = new BinaryReader(new FileStream(FilePath, FileMode.Open)))
            {
                // TODO: Why are there empty streams?
                if (reader.BaseStream.Length == 0)
                {
                    return;
                }

                SBSurface surface = new SBSurface();

                reader.BaseStream.Position = reader.BaseStream.Length - 0xB0;

                int[] mipmapSizes = new int[16];
                for (int i = 0; i < mipmapSizes.Length; i++)
                {
                    mipmapSizes[i] = reader.ReadInt32();
                }

                reader.ReadChars(4); // TNX magic

                string texName = ReadTexName(reader);
                surface.Name = texName;

                surface.Width  = reader.ReadInt32();
                surface.Height = reader.ReadInt32();
                surface.Depth  = reader.ReadInt32();

                var Format = (NUTEX_FORMAT)reader.ReadByte();

                // hack
                surface.IsSRGB = Format.ToString().ToLower().Contains("srgb");

                SBConsole.WriteLine($"Loaded NUTEX: {surface.Name} Format: {Format.ToString()}");

                if (pixelFormatByNuTexFormat.ContainsKey(Format))
                {
                    surface.PixelFormat = pixelFormatByNuTexFormat[Format];
                }
                if (internalFormatByNuTexFormat.ContainsKey(Format))
                {
                    surface.InternalFormat = internalFormatByNuTexFormat[Format];
                }

                reader.ReadByte();

                ushort Padding = reader.ReadUInt16();
                reader.ReadUInt32();

                int    MipCount     = reader.ReadInt32();
                int    Alignment    = reader.ReadInt32();
                int    ArrayCount   = reader.ReadInt32();
                int    ImageSize    = reader.ReadInt32();
                char[] Magic        = reader.ReadChars(4);
                int    MajorVersion = reader.ReadInt16();
                int    MinorVersion = reader.ReadInt16();

                uint blkWidth  = (uint)blkDims[Format].X;
                uint blkHeight = (uint)blkDims[Format].Y;

                uint blockHeight     = Tools.SwitchSwizzler.GetBlockHeight(Tools.SwitchSwizzler.DivRoundUp((uint)surface.Height, blkHeight));
                uint BlockHeightLog2 = (uint)Convert.ToString(blockHeight, 2).Length - 1;
                uint tileMode        = 0;

                uint bpp = GetBpps(Format);

                //TODO: Read mipmaps
                reader.BaseStream.Position = 0;
                int blockHeightShift = 0;
                for (int i = 0; i < 1; i++)
                {
                    int size = mipmapSizes[i];

                    if (i == 0 && size % Alignment != 0)
                    {
                        size += Alignment - (size % Alignment);
                    }

                    byte[] deswiz  = Tools.SwitchSwizzler.Deswizzle((uint)surface.Width, (uint)surface.Height, blkWidth, blkHeight, 0, bpp, tileMode, (int)Math.Max(0, BlockHeightLog2 - blockHeightShift), reader.ReadBytes(ImageSize));
                    byte[] trimmed = new byte[mipmapSizes[0]];
                    Array.Copy(deswiz, 0, trimmed, 0, trimmed.Length);

                    surface.Mipmaps.Add(trimmed);
                }

                Scene.Surfaces.Add(surface);
            }
        }
Esempio n. 17
0
 /// <summary>
 /// loads the bone nodes from a scene
 /// </summary>
 /// <param name="Scene"></param>
 private void LoadFromScene(SBScene Scene)
 {
     this.Scene       = Scene;
     SelectedSkeleton = Scene.Skeleton as SBSkeleton;
     RefreshBoneList();
 }
Esempio n. 18
0
        /// <summary>
        /// Load an ultimate mesh from file to the given scene
        /// </summary>
        /// <param name="FileName"></param>
        /// <param name="Scene"></param>
        public static void Open(string FileName, SBScene Scene)
        {
            SsbhFile File;

            if (Ssbh.TryParseSsbhFile(FileName, out File))
            {
                if (File is Matl matl)
                {
                    if (matl.MajorVersion != 1 && matl.MinorVersion != 6)
                    {
                        SBConsole.WriteLine($"Warning: Mesh Version {matl.MajorVersion}.{matl.MinorVersion} not supported");
                    }

                    var MaterialProps = typeof(UltimateMaterial).GetProperties();
                    // use dictionary for faster lookup
                    Dictionary <string, PropertyInfo> NameToProperty = new Dictionary <string, PropertyInfo>();
                    foreach (var prop in MaterialProps)
                    {
                        var attrName = prop.Name;
                        if (attrName != null)
                        {
                            NameToProperty.Add(attrName, prop);
                        }
                    }

                    foreach (var entry in matl.Entries)
                    {
                        UltimateMaterial material = new UltimateMaterial();
                        material.Name  = entry.ShaderLabel;
                        material.Label = entry.MaterialLabel;

                        Scene.Materials.Add(material);

                        foreach (var attr in entry.Attributes)
                        {
                            if (NameToProperty.ContainsKey(attr.ParamId.ToString()))
                            {
                                var prop = NameToProperty[attr.ParamId.ToString()];
                                if (prop.PropertyType == typeof(SBMatAttrib <string>))
                                {
                                    material.SetProperty(attr.ParamId.ToString(), attr.DataObject.ToString());
                                }
                                else
                                if (prop.PropertyType == typeof(SBMatAttrib <Vector4>))
                                {
                                    material.SetProperty(attr.ParamId.ToString(), MATLVectorToGLVector((MatlAttribute.MatlVector4)attr.DataObject));
                                }
                                else
                                {
                                    material.SetProperty(attr.ParamId.ToString(), attr.DataObject);
                                }
                            }
                            else
                            {
                                switch (attr.ParamId)
                                {
                                case MatlEnums.ParamId.RasterizerState0:
                                    material.RasterizerState = (MatlAttribute.MatlRasterizerState)attr.DataObject;
                                    break;

                                case MatlEnums.ParamId.BlendState0:
                                    material.BlendState = (MatlAttribute.MatlBlendState)attr.DataObject;
                                    break;

                                default:
                                    //SBConsole.WriteLine("Extra Param: " + attr.ParamID.ToString() + " = " + attr.DataObject.ToString());
                                    material.extraParams.Add(attr.ParamId, attr.DataObject);
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
Esempio n. 19
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public static SBScene LoadScene(string filePath, SBScene loadedScene)
        {
            DialogResult Result = DialogResult.OK;

            var settings = new IONET.ImportSettings();

            using (SBCustomDialog d = new SBCustomDialog(settings))
                Result = d.ShowDialog();

            if (Result == DialogResult.OK)
            {
                var ioModel = IONET.IOManager.LoadScene(filePath, settings);

                SBScene scene;
                if (loadedScene == null)
                {
                    SBConsole.WriteLine("No scene loaded, defaulted to Smash Ultimate scene");
                    scene = new SBSceneSSBH();

                    using (var dialog = new SBCustomDialog(SBSceneSSBH.NewImportSettings))
                        Result = dialog.ShowDialog();

                    if (Result == DialogResult.OK)
                    {
                        if (SBSceneSSBH.NewImportSettings.NUMATLB != null && SBSceneSSBH.NewImportSettings.NUMATLB != "")
                        {
                            MATL_Loader.Open(SBSceneSSBH.NewImportSettings.NUMATLB, scene);
                        }

                        if (SBSceneSSBH.NewImportSettings.NUSKTFile != null && SBSceneSSBH.NewImportSettings.NUSKTFile != "")
                        {
                            ioModel.Models[0].Skeleton = SKEL_Loader.Open(SBSceneSSBH.NewImportSettings.NUSKTFile, scene).ToIOSkeleton();
                        }
                    }
                    else
                    {
                        MessageBox.Show("Failed to import model");
                        return(null);
                    }
                }
                else
                {
                    scene = loadedScene;

                    using (var dialog = new SBCustomDialog(SBSceneSSBH.ImportSettings))
                        Result = dialog.ShowDialog();

                    if (Result == DialogResult.OK)
                    {
                        if (SBSceneSSBH.ImportSettings.UseExistingSkeleton)
                        {
                            ioModel.Models[0].Skeleton = ((SBSkeleton)scene.Skeleton).ToIOSkeleton();
                            //ioModel.ConvertToSkeleton((SBSkeleton)scene.Skeleton);
                            // ionet uses bone names so no need to convert bone mapping indices
                        }
                    }
                    else
                    {
                        MessageBox.Show("Failed to import model");
                        return(null);
                    }
                }

                scene.FromIOModel(ioModel);
                return(scene);
            }
            else
            {
                return(null);
            }
        }
Esempio n. 20
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public static SBScene LoadScene(string filePath, SBScene loadedScene)
        {
            DialogResult Result = DialogResult.OK;

            var settings = new IONET.ImportSettings();

            using (SBCustomDialog d = new SBCustomDialog(settings))
                Result = d.ShowDialog();

            if (Result == DialogResult.OK)
            {
                IONET.Core.IOScene ioModel;
                try
                {
                    ioModel = IONET.IOManager.LoadScene(filePath, settings);
                }
                catch (ArgumentNullException e)
                {
                    throw new ArgumentNullException("This error is usually caused by missing textures. Make sure you can correctly preview your model with textures before exporting.\nOriginal Error Msg: \n" + e.Message, e);
                }
                catch (NullReferenceException e)
                {
                    throw new NullReferenceException("This error is usually caused by a not exporting the Skeleton along with the Meshes. \nOriginal Message: \n" + e.Message, e);
                }

                SBScene scene;
                if (loadedScene == null)
                {
                    SBConsole.WriteLine("No scene loaded, defaulted to Smash Ultimate scene");
                    scene = new SBSceneSSBH();

                    using (var dialog = new SBCustomDialog(SBSceneSSBH.NewImportSettings))
                        Result = dialog.ShowDialog();

                    if (Result == DialogResult.OK)
                    {
                        if (SBSceneSSBH.NewImportSettings.NumatbFile != null && SBSceneSSBH.NewImportSettings.NumatbFile != "")
                        {
                            MATL_Loader.Open(SBSceneSSBH.NewImportSettings.NumatbFile, scene);
                        }

                        if (SBSceneSSBH.NewImportSettings.NusktbFile != null && SBSceneSSBH.NewImportSettings.NusktbFile != "")
                        {
                            ioModel.Models[0].Skeleton = SKEL_Loader.Open(SBSceneSSBH.NewImportSettings.NusktbFile, scene).ToIOSkeleton();
                        }
                    }
                    else
                    {
                        MessageBox.Show("Failed to import model");
                        return(null);
                    }
                }
                else
                {
                    scene = loadedScene;

                    using (var dialog = new SBCustomDialog(SBSceneSSBH.ImportSettings))
                        Result = dialog.ShowDialog();

                    if (Result == DialogResult.OK)
                    {
                        if (SBSceneSSBH.ImportSettings.UseExistingSkeleton)
                        {
                            ioModel.Models[0].Skeleton = ((SBSkeleton)scene.Skeleton).ToIOSkeleton();
                            //ioModel.ConvertToSkeleton((SBSkeleton)scene.Skeleton);
                            // ionet uses bone names so no need to convert bone mapping indices
                        }
                    }
                    else
                    {
                        MessageBox.Show("Failed to import model");
                        return(null);
                    }
                }

                scene.FromIOModel(ioModel);
                return(scene);
            }
            else
            {
                return(null);
            }
        }
Esempio n. 21
0
        /// <summary>
        /// Load an ultimate mesh from file to the given scene
        /// </summary>
        /// <param name="FileName"></param>
        /// <param name="Scene"></param>
        public static void Open(string FileName, SBScene Scene)
        {
            ISSBH_File File;

            if (SSBH.TryParseSSBHFile(FileName, out File))
            {
                if (File is MATL matl)
                {
                    if (matl.MajorVersion != 1 && matl.MinorVersion != 6)
                    {
                        SBConsole.WriteLine($"Warning: Mesh Version {matl.MajorVersion}.{matl.MinorVersion} not supported");
                    }

                    var MaterialProps = typeof(UltimateMaterial).GetProperties();
                    // use dictionary for faster lookup
                    Dictionary <string, PropertyInfo> NameToProperty = new Dictionary <string, PropertyInfo>();
                    foreach (var prop in MaterialProps)
                    {
                        var attrName = (MATLLoaderAttributeName)prop.GetCustomAttribute(typeof(MATLLoaderAttributeName));
                        if (attrName != null)
                        {
                            NameToProperty.Add(attrName.Name, prop);
                        }
                    }

                    foreach (var entry in matl.Entries)
                    {
                        UltimateMaterial material = new UltimateMaterial();
                        material.Name  = entry.MaterialName;
                        material.Label = entry.MaterialLabel;

                        Scene.Materials.Add(material);

                        foreach (var attr in entry.Attributes)
                        {
                            if (NameToProperty.ContainsKey(attr.ParamID.ToString()))
                            {
                                var prop = NameToProperty[attr.ParamID.ToString()];
                                if (prop.PropertyType == typeof(SBMatAttrib <string>))
                                {
                                    ((SBMatAttrib <string>)prop.GetValue(material)).Value = attr.DataObject.ToString();
                                    //foreach (var surface in Scene.Surfaces)
                                    //    if (surface.Name == attr.DataObject.ToString())
                                    //        prop.SetValue(material, surface);
                                }
                                if (prop.PropertyType == typeof(SBMatAttrib <Vector4>))
                                {
                                    ((SBMatAttrib <Vector4>)prop.GetValue(material)).Value = MATLVectorToGLVector((MatlAttribute.MatlVector4)attr.DataObject);
                                }
                                if (prop.PropertyType == typeof(SBMatAttrib <float>))
                                {
                                    ((SBMatAttrib <float>)prop.GetValue(material)).Value = (float)attr.DataObject;
                                }
                                if (prop.PropertyType == typeof(SBMatAttrib <bool>))
                                {
                                    ((SBMatAttrib <bool>)prop.GetValue(material)).Value = (bool)attr.DataObject;
                                }
                            }
                            else
                            {
                                switch (attr.ParamID)
                                {
                                case MatlEnums.ParamId.RasterizerState0:
                                    material.RasterizerState = (MatlAttribute.MatlRasterizerState)attr.DataObject;
                                    break;

                                case MatlEnums.ParamId.BlendState0:
                                    material.BlendState = (MatlAttribute.MatlBlendState)attr.DataObject;
                                    break;

                                default:
                                    SBConsole.WriteLine("Extra Param: " + attr.ParamID.ToString() + " = " + attr.DataObject.ToString());
                                    material.extraParams.Add(attr.ParamID, attr.DataObject);
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
Esempio n. 22
0
        /// <summary>
        ///
        /// </summary>
        /// <returns></returns>
        public static SBScene LoadScene(string filePath, SBScene loadedScene)
        {
            IImportableModelType modelType = GetModelTypeFromPath(filePath);

            if (modelType == null)
            {
                return(null);
            }

            DialogResult Result = DialogResult.OK;

            if (modelType.Settings != null)
            {
                using (var dialog = new SBCustomDialog(modelType.Settings))
                    Result = dialog.ShowDialog();
            }

            if (Result == DialogResult.OK)
            {
                var ioModel = modelType.ImportIOModel(filePath);

                SBScene scene;
                if (loadedScene == null)
                {
                    SBConsole.WriteLine("No scene loaded, defaulted to Smash Ultimate scene");
                    scene = new SBSceneSSBH();

                    using (var dialog = new SBCustomDialog(SBSceneSSBH.NewImportSettings))
                        Result = dialog.ShowDialog();

                    if (Result == DialogResult.OK)
                    {
                        if (SBSceneSSBH.NewImportSettings.NUMATLB != null && SBSceneSSBH.NewImportSettings.NUMATLB != "")
                        {
                            MATL_Loader.Open(SBSceneSSBH.NewImportSettings.NUMATLB, scene);
                        }

                        if (SBSceneSSBH.NewImportSettings.NUSKTFile != null && SBSceneSSBH.NewImportSettings.NUSKTFile != "")
                        {
                            ioModel.Skeleton = SKEL_Loader.Open(SBSceneSSBH.NewImportSettings.NUSKTFile, scene);
                        }
                    }
                    else
                    {
                        MessageBox.Show("Failed to import model");
                        return(null);
                    }
                }
                else
                {
                    scene = loadedScene;

                    using (var dialog = new SBCustomDialog(SBSceneSSBH.ImportSettings))
                        Result = dialog.ShowDialog();

                    if (Result == DialogResult.OK)
                    {
                        if (SBSceneSSBH.ImportSettings.UseExistingSkeleton)
                        {
                            ioModel.ConvertToSkeleton((SBSkeleton)scene.Skeleton);
                        }
                    }
                    else
                    {
                        MessageBox.Show("Failed to import model");
                        return(null);
                    }
                }

                scene.FromIOModel(ioModel);
                return(scene);
            }
            else
            {
                return(null);
            }
        }
Esempio n. 23
0
        public void Bind(SBScene scene, Shader shader)
        {
            int TextureUnit = 1;

            shader.SetTexture("uvPattern", DefaultTextures.Instance.uvPattern, TextureUnit++);

            if (_mobj == null)
            {
                return;
            }

            if (_mobj.RenderFlags.HasFlag(RENDER_MODE.XLU))
            {
                GL.Enable(EnableCap.Blend);
                GL.BlendFunc(BlendingFactor.SrcAlpha, BlendingFactor.OneMinusSrcAlpha);
            }
            else
            {
                GL.Disable(EnableCap.Blend);
            }

            shader.SetBoolToInt("hasDF", _mobj.RenderFlags.HasFlag(RENDER_MODE.DF_NONE));
            shader.SetBoolToInt("hasDiffuseMaterial", _mobj.RenderFlags.HasFlag(RENDER_MODE.DIFFUSE_MAT));
            shader.SetBoolToInt("enableDiffuseLighting", _mobj.RenderFlags.HasFlag(RENDER_MODE.DIFFUSE) || _mobj.RenderFlags.HasFlag(RENDER_MODE.DIFFUSE_VTX));
            shader.SetBoolToInt("enableDiffuseVertex", _mobj.RenderFlags.HasFlag(RENDER_MODE.DIFFUSE_VTX));
            shader.SetBoolToInt("enableSpecular", _mobj.RenderFlags.HasFlag(RENDER_MODE.SPECULAR));

            shader.SetFloat("glossiness", 0);
            shader.SetFloat("transparency", 1);
            shader.SetVector4("ambientColor", Vector4.Zero);
            shader.SetVector4("diffuseColor", Vector4.One);
            shader.SetVector4("specularColor", Vector4.One);

            shader.SetInt("TEX0Flag", _mobj.RenderFlags.HasFlag(RENDER_MODE.TEX0) ? 1 : 0);

            if (_mobj.Material != null)
            {
                var matcol = _mobj.Material;

                shader.SetFloat("glossiness", matcol.Shininess);
                shader.SetFloat("transparency", matcol.Alpha);

                shader.SetVector4("ambientColor", matcol.AMB_R / 255f, matcol.AMB_G / 255f, matcol.AMB_B / 255f, matcol.AMB_A / 255f);
                shader.SetVector4("diffuseColor", matcol.DIF_R / 255f, matcol.DIF_G / 255f, matcol.DIF_B / 255f, matcol.DIF_A / 255f);
                shader.SetVector4("specularColor", matcol.SPC_R / 255f, matcol.SPC_G / 255f, matcol.SPC_B / 255f, matcol.SPC_A / 255f);
            }

            bool hasDiffuse  = false;
            bool hasSpecular = false;
            bool hasExt      = false;
            bool hasBumpMap  = false;

            if (_mobj.Textures != null)
            {
                foreach (var texture in _mobj.Textures.List)
                {
                    var rTexture = ((HSDScene)scene).TOBJtoRenderTexture(texture);

                    int coordType = 0;
                    if (texture.Flags.HasFlag(TOBJ_FLAGS.COORD_REFLECTION))
                    {
                        coordType = 1;
                    }

                    if (mapIdToAnimSurface.ContainsKey(texture.TexMapID))
                    {
                        rTexture = mapIdToAnimSurface[texture.TexMapID];
                    }

                    var texScale = new Vector2(texture.WScale, texture.HScale);

                    // bugged
                    GL.ActiveTexture(OpenTK.Graphics.OpenGL.TextureUnit.Texture0);

                    GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapS, (int)GXtoGL.GLWrapMode(texture.WrapS));
                    GL.TexParameter(TextureTarget.Texture2D, TextureParameterName.TextureWrapT, (int)GXtoGL.GLWrapMode(texture.WrapT));

                    rTexture.TextureWrapS = GXtoGL.GLWrapMode(texture.WrapS);
                    rTexture.TextureWrapT = GXtoGL.GLWrapMode(texture.WrapT);

                    if (texture.Flags.HasFlag(TOBJ_FLAGS.LIGHTMAP_DIFFUSE))
                    {
                        hasDiffuse = true;
                        shader.SetInt("diffuseCoordType", coordType);
                        shader.SetVector2("diffuseScale", texScale);
                        shader.SetFloat("diffuseBlending", texture.Blending);
                        shader.SetBoolToInt("hasDiffuseAlphaBlend", texture.Flags.HasFlag(TOBJ_FLAGS.ALPHAMAP_BLEND) || texture.Flags.HasFlag(TOBJ_FLAGS.ALPHAMAP_REPLACE));
                        shader.SetTexture("diffuseTex", rTexture, TextureUnit++);
                    }
                    if (texture.Flags.HasFlag(TOBJ_FLAGS.LIGHTMAP_SPECULAR))
                    {
                        hasSpecular = true;
                        shader.SetInt("specularCoordType", coordType);
                        shader.SetVector2("specularScale", texScale);
                        shader.SetTexture("specularTex", rTexture, TextureUnit++);
                    }
                    if (texture.Flags.HasFlag(TOBJ_FLAGS.LIGHTMAP_EXT))
                    {
                        hasExt = true;
                        shader.SetInt("extCoordType", coordType);
                        shader.SetVector2("extScale", texScale);
                        shader.SetTexture("extTex", rTexture, TextureUnit++);
                    }
                    if (texture.Flags.HasFlag(TOBJ_FLAGS.BUMP))
                    {
                        hasBumpMap = true;
                        shader.SetInt("bumpMapWidth", texture.ImageData.Width);
                        shader.SetInt("bumpMapHeight", texture.ImageData.Height);
                        shader.SetVector2("bumpMapTexScale", texScale);
                        shader.SetTexture("bumpMapTex", rTexture, TextureUnit++);
                    }
                }
            }

            shader.SetBoolToInt("hasDiffuse", hasDiffuse);
            shader.SetBoolToInt("hasSpecular", hasSpecular);
            shader.SetBoolToInt("hasBumpMap", hasBumpMap);
            shader.SetBoolToInt("hasExt", hasExt);
        }