Beispiel #1
0
        public unsafe void ResetBox(int index)
        {
            if (index == 2 || index == 4 || index == 5 || index == 8)
            {
                return;
            }

            NumericInputBox     box = _transBoxes[index];
            MDL0MaterialRefNode mr  = TargetTexRef;
            SRT0TextureNode     entry;

            if (TargetTexRef != null)
            {
                if ((SelectedAnimation != null) && (CurrentFrame > 0) && ((entry = SelectedAnimation.FindChild(mr.Parent.Name + "/Texture" + mr.Index, true) as SRT0TextureNode) != null))
                {
                    KeyframeEntry e = entry.Keyframes.GetKeyframe((KeyFrameMode)index + 0x10, CurrentFrame - 1);
                    if (e == null)
                    {
                        box.Value     = entry.Keyframes[KeyFrameMode.ScaleX + index, CurrentFrame - 1];
                        box.BackColor = Color.White;
                    }
                    else
                    {
                        box.Value     = e._value;
                        box.BackColor = Color.Yellow;
                    }
                }
                else
                {
                    FrameState state = mr._bindState;
                    box.Value     = ((float *)&state)[index];
                    box.BackColor = Color.White;
                }
            }
        }
Beispiel #2
0
        private void ResetTexture()
        {
            MDL0MaterialRefNode mr = TargetTexRef;
            PAT0TextureNode     node;

            if (mr == null)
            {
                return;
            }

            _updating = true;
            if ((SelectedAnimation != null) && (CurrentFrame > 0) && ((node = SelectedAnimation.FindChild(mr._parent.Name + "/Texture" + mr.Index, true) as PAT0TextureNode) != null))
            {
                bool   kf;
                string e = node.GetTexture(CurrentFrame - 1, out kf);
                texBox.Value = e;
                if (!kf)
                {
                    texBox.BackColor = Color.White;
                }
                else
                {
                    texBox.BackColor = Color.Yellow;
                }
            }
            else
            {
                texBox.Value     = null;
                texBox.BackColor = Color.White;
            }
            _updating = false;
        }
Beispiel #3
0
        internal unsafe void TexChanged(object sender, EventArgs e)
        {
            MDL0MaterialRefNode mr = TargetTexRef;
            PAT0TextureNode     node;

            if (mr == null || _updating)
            {
                return;
            }

            if (SelectedAnimation != null && CurrentFrame > 0)
            {
                node = SelectedAnimation.FindChild(mr._parent.Name + "/Texture" + mr.Index, true) as PAT0TextureNode;

                if (node != null)
                {
                    PAT0TextureEntryNode tex = node.GetEntry(CurrentFrame - 1);
                    if (tex == null)
                    {
                        if (!String.IsNullOrEmpty(texBox.Text) || !String.IsNullOrEmpty(pltBox.Text))
                        {
                            tex        = new PAT0TextureEntryNode();
                            tex._frame = CurrentFrame - 1;
                            if (node.Children.Count > 0)
                            {
                                node.InsertChild(tex, true, node.GetPrevious(CurrentFrame - 1).Index + 1);
                            }
                            else
                            {
                                node.AddChild(tex, true);
                            }
                            tex.Texture = texBox.Text;
                            tex.Palette = pltBox.Text;
                        }
                    }
                    else
                    {
                        if (!String.IsNullOrEmpty(texBox.Text) || !String.IsNullOrEmpty(pltBox.Text))
                        {
                            tex.Texture = texBox.Text;
                            tex.Palette = pltBox.Text;
                        }
                        else
                        {
                            tex.Remove();
                        }
                    }
                }
            }

            TargetModel.ApplyPAT(SelectedAnimation, CurrentFrame);
            ResetTexture();
            ResetPalette();
            _mainWindow.ModelPanel.Invalidate();
        }
Beispiel #4
0
 public GLTextureRef(GLMaterial mat, MDL0MaterialRefNode texRef)
 {
     _name = texRef.Name;
     foreach (GLTexture tex in mat._model._textures)
     {
         if (tex._name.Equals(_name))
         {
             _tex = tex;
             break;
         }
     }
 }
        public void NewMaterial()
        {
            MDL0Node model = (MDL0Node)_resource;

            if (model._matGroup == null)
            {
                MDL0GroupNode g = model._matGroup;
                if (g == null)
                {
                    model.AddChild(g = new MDL0GroupNode(MDLResourceType.Materials), true);
                    model._matGroup  = g;
                    model._matList   = g.Children;
                }
            }

            MDL0MaterialNode mat = new MDL0MaterialNode();

            model._matGroup.AddChild(mat);
            mat.Name = "Material" + mat.Index;

            if (model._shadGroup == null)
            {
                MDL0GroupNode g = model._shadGroup;
                if (g == null)
                {
                    model.AddChild(g = new MDL0GroupNode(MDLResourceType.Shaders), true);
                    model._shadGroup = g;
                    model._shadList  = g.Children;
                }
            }

            if (model._shadList.Count == 0)
            {
                NewShader();
            }

            mat.ShaderNode = (MDL0ShaderNode)model._shadList[0];
            MDL0MaterialRefNode mr = new MDL0MaterialRefNode();

            mat.AddChild(mr);
            mr.Name = "MatRef0";
            mat.Rebuild(true);

            BaseWrapper b = FindResource(mat, true);

            if (b != null)
            {
                b.EnsureVisible();
            }
        }
        private void CreateRef()
        {
            if (_resource.Children.Count < 8)
            {
                MDL0MaterialRefNode node = new MDL0MaterialRefNode();
                _resource.AddChild(node);
                node.Default();
                _resource.SignalPropertyChange();

                //if (node.Model.AutoMetalMaterials && ((MDL0MaterialNode)node.Parent).MetalMaterial != null)
                //    ((MDL0MaterialNode)node.Parent).MetalMaterial.UpdateAsMetal();

                Nodes[Nodes.Count - 1].EnsureVisible();
                //TreeView.SelectedNode = Nodes[Nodes.Count - 1];
            }
        }
        public override ResourceNode Duplicate()
        {
            if (_resource is MDL0MaterialRefNode matRef && matRef.Model != null)
            {
                MDL0MaterialRefNode matRef2 = new MDL0MaterialRefNode();
                matRef.Parent.InsertChild(matRef2, true, matRef.Index + 1);
                matRef2.Default();
                // Misc
                matRef2.Name    = matRef.Name;
                matRef2.Texture = matRef.Texture;
                matRef2.Palette = matRef.Palette;
                // Texture Coordinates
                matRef2.Scale       = matRef.Scale;
                matRef2.Rotation    = matRef.Rotation;
                matRef2.Translation = matRef.Translation;
                // Texture Matrix Effect
                matRef2.HasTextureMatrix = matRef.HasTextureMatrix;
                matRef2.SCN0RefCamera    = matRef.SCN0RefCamera;
                matRef2.SCN0RefLight     = matRef.SCN0RefLight;
                // Texture Reference
                matRef2.UWrapMode        = matRef.UWrapMode;
                matRef2.VWrapMode        = matRef.VWrapMode;
                matRef2.MinFilter        = matRef.MinFilter;
                matRef2.MagFilter        = matRef.MagFilter;
                matRef2.LODBias          = matRef.LODBias;
                matRef2.MaxAnisotropy    = matRef.MaxAnisotropy;
                matRef2.ClampBias        = matRef.ClampBias;
                matRef2.TexelInterpolate = matRef.TexelInterpolate;
                // XF TexGen Flags
                matRef2.Projection   = matRef.Projection;
                matRef2.InputForm    = matRef.InputForm;
                matRef2.Type         = matRef.Type;
                matRef2.Coordinates  = matRef.Coordinates;
                matRef2.EmbossSource = matRef.EmbossSource;
                matRef2.EmbossLight  = matRef.EmbossLight;
                matRef2.Normalize    = matRef.Normalize;

                return(matRef2);
            }

            return(null);
        }
        public IModel ImportModel(string filePath, ImportType type)
        {
            IModel model = null;

            ModelType = type;

            BoneType = ModelType == ImportType.MDL0 ? typeof(MDL0BoneNode) : null;

            //TransformMatrix = Matrix.TransformMatrix(_importOptions._modifyScale, _importOptions._modifyRotation, new Vector3());

            switch (type)
            {
            case ImportType.MDL0:
                MDL0Node m = new MDL0Node()
                {
                    _name    = Path.GetFileNameWithoutExtension(filePath),
                    _version = _importOptions._modelVersion.Clamp(8, 11)
                };
                if (_importOptions._setOrigPath)
                {
                    m._originalPath = filePath;
                }
                m.BeginImport();
                model = m;
                break;
            }

            CurrentModel = model;

            Error = "There was a problem reading the model.";
            using (DecoderShell shell = DecoderShell.Import(filePath))
                try
                {
                    Error = "There was a problem reading texture entries.";

                    //Extract images, removing duplicates
                    foreach (ImageEntry img in shell._images)
                    {
                        string name = img._path != null?
                                      Path.GetFileNameWithoutExtension(img._path) :
                                          img._name != null ? img._name : img._id;

                        switch (type)
                        {
                        case ImportType.MDL0:
                            img._node = ((MDL0Node)model).FindOrCreateTexture(name);
                            break;
                        }
                    }

                    Error = "There was a problem creating a default shader.";

                    //Create a shader
                    ResourceNode shader = null;
                    switch (type)
                    {
                    case ImportType.MDL0:
                        MDL0Node       m        = (MDL0Node)model;
                        MDL0ShaderNode shadNode = new MDL0ShaderNode()
                        {
                            _ref0 = 0,
                            _ref1 = -1,
                            _ref2 = -1,
                            _ref3 = -1,
                            _ref4 = -1,
                            _ref5 = -1,
                            _ref6 = -1,
                            _ref7 = -1,
                        };

                        shadNode._parent = m._shadGroup;
                        m._shadList.Add(shadNode);

                        switch (_importOptions._mdlType)
                        {
                        case ImportOptions.MDLType.Character:
                            for (int i = 0; i < 3; i++)
                            {
                                switch (i)
                                {
                                case 0:
                                    shadNode.AddChild(new MDL0TEVStageNode(0x28F8AF, 0x08F2F0, 0, TevKColorSel.ConstantColor0_RGB, TevKAlphaSel.ConstantColor0_Alpha, TexMapID.TexMap0, TexCoordID.TexCoord0, ColorSelChan.LightChannel0, true));
                                    break;

                                case 1:
                                    shadNode.AddChild(new MDL0TEVStageNode(0x08FEB0, 0x081FF0, 0, TevKColorSel.ConstantColor1_RGB, TevKAlphaSel.ConstantColor0_Alpha, TexMapID.TexMap7, TexCoordID.TexCoord7, ColorSelChan.LightChannel0, false));
                                    break;

                                case 2:
                                    shadNode.AddChild(new MDL0TEVStageNode(0x0806EF, 0x081FF0, 0, TevKColorSel.ConstantColor0_RGB, TevKAlphaSel.ConstantColor0_Alpha, TexMapID.TexMap7, TexCoordID.TexCoord7, ColorSelChan.Zero, false));
                                    break;
                                }
                            }
                            break;

                        case ImportOptions.MDLType.Stage:
                            shadNode.AddChild(new MDL0TEVStageNode(0x28F8AF, 0x08F2F0, 0, TevKColorSel.ConstantColor0_RGB, TevKAlphaSel.ConstantColor0_Alpha, TexMapID.TexMap0, TexCoordID.TexCoord0, ColorSelChan.LightChannel0, true));
                            break;
                        }

                        shader = shadNode;

                        break;
                    }

                    Error = "There was a problem extracting materials.";

                    //Extract materials
                    foreach (MaterialEntry mat in shell._materials)
                    {
                        List <ImageEntry> imgEntries = new List <ImageEntry>();

                        //Find effect
                        if (mat._effect != null)
                        {
                            foreach (EffectEntry eff in shell._effects)
                            {
                                if (eff._id == mat._effect) //Attach textures and effects to material
                                {
                                    if (eff._shader != null)
                                    {
                                        foreach (LightEffectEntry l in eff._shader._effects)
                                        {
                                            if (l._type == LightEffectType.diffuse && l._texture != null)
                                            {
                                                string path = l._texture;
                                                foreach (EffectNewParam p in eff._newParams)
                                                {
                                                    if (p._sid == l._texture)
                                                    {
                                                        path = p._sampler2D._url;
                                                        if (!String.IsNullOrEmpty(p._sampler2D._source))
                                                        {
                                                            foreach (EffectNewParam p2 in eff._newParams)
                                                            {
                                                                if (p2._sid == p._sampler2D._source)
                                                                {
                                                                    path = p2._path;
                                                                }
                                                            }
                                                        }
                                                    }
                                                }

                                                foreach (ImageEntry img in shell._images)
                                                {
                                                    if (img._id == path)
                                                    {
                                                        imgEntries.Add(img);
                                                        break;
                                                    }
                                                }
                                            }
                                        }
                                    }
                                }
                            }
                        }
                        switch (type)
                        {
                        case ImportType.MDL0:
                            MDL0MaterialNode matNode = new MDL0MaterialNode();

                            MDL0Node m = (MDL0Node)model;
                            matNode._parent = m._matGroup;
                            m._matList.Add(matNode);

                            matNode._name      = mat._name != null ? mat._name : mat._id;
                            matNode.ShaderNode = shader as MDL0ShaderNode;

                            mat._node     = matNode;
                            matNode._cull = _importOptions._culling;

                            foreach (ImageEntry img in imgEntries)
                            {
                                MDL0MaterialRefNode mr = new MDL0MaterialRefNode();
                                (mr._texture = img._node as MDL0TextureNode)._references.Add(mr);
                                mr._name = mr._texture.Name;
                                matNode._children.Add(mr);
                                mr._parent  = matNode;
                                mr._minFltr = mr._magFltr = 1;
                                mr._uWrap   = mr._vWrap = (int)_importOptions._wrap;
                            }
                            break;
                        }
                    }

                    Say("Extracting scenes...");

                    List <ObjectInfo> _objects  = new List <ObjectInfo>();
                    ResourceNode      boneGroup = null;
                    switch (type)
                    {
                    case ImportType.MDL0:
                        boneGroup = ((MDL0Node)model)._boneGroup;
                        break;
                    }

                    //Extract bones and objects and create bone tree
                    foreach (SceneEntry scene in shell._scenes)
                    {
                        foreach (NodeEntry node in scene._nodes)
                        {
                            EnumNode(node, boneGroup, scene, model, shell, _objects, TransformMatrix, Matrix.Identity);
                        }
                    }

                    //Add root bone if there are no bones
                    if (boneGroup.Children.Count == 0)
                    {
                        switch (type)
                        {
                        case ImportType.MDL0:
                            MDL0BoneNode bone = new MDL0BoneNode();
                            bone.Scale = new Vector3(1);
                            bone.RecalcBindState(false, false);
                            bone._name   = "TopN";
                            TempRootBone = bone;
                            break;
                        }
                    }

                    //Create objects
                    foreach (ObjectInfo obj in _objects)
                    {
                        NodeEntry node = obj._node;
                        string    w    = obj._weighted ? "" : "un";
                        string    w2   = obj._weighted ? "\nOne or more vertices may not be weighted correctly." : "";
                        string    n    = node._name != null ? node._name : node._id;

                        Error = String.Format("There was a problem decoding {0}weighted primitives for the object {1}.{2}", w, n, w2);

                        Say(String.Format("Decoding {0}weighted primitives for {1}...", w, n));

                        obj.Initialize(model, shell);
                    }

                    //Finish
                    switch (type)
                    {
                    case ImportType.MDL0:
                        MDL0Node mdl0 = (MDL0Node)model;
                        if (TempRootBone != null)
                        {
                            mdl0._boneGroup._children.Add(TempRootBone);
                            TempRootBone._parent = mdl0._boneGroup;
                        }
                        FinishMDL0(mdl0);
                        break;
                    }
                }
#if !DEBUG
            catch (Exception x)
            {
                MessageBox.Show("Cannot continue importing this model.\n" + Error + "\n\nException:\n" + x.ToString());
                model = null;
                Close();
            }
#endif
            finally
            {
                //Clean up the mess we've made
                GC.Collect(GC.MaxGeneration, GCCollectionMode.Forced);
            }

            CurrentModel = null;
            Error        = null;

            return(model);
        }
        private void SetTarget(MDL0MaterialRefNode texture)
        {
            if (_targetMatRef != texture && texture != null)
            {
                CurrentViewport.Camera.Reset();
            }

            if (_attached == null)
            {
                _attached = new List <ResourceNode>();
            }

            if (Tex0 != null)
            {
                Tex0.Unbind();
            }

            _attached.Clear();

            _targetMatRef = texture;
            if (_targetMatRef == null)
            {
                return;
            }

            _attached.Add(_targetMatRef);

            if (Tex0 != null)
            {
                Tex0.Prepare(_targetMatRef, -1);
            }

            //Dispose of all old UV buffers
            if (_renderInfo != null)
            {
                foreach (RenderInfo info in _renderInfo)
                {
                    if (info._renderBuffer != null)
                    {
                        info._renderBuffer.Dispose();
                    }
                }
            }

            //Recreate lists
            _objIndices   = new List <int>();
            _uvSetIndices = new List <int>();
            _renderInfo   = new List <RenderInfo>();
            _objNames.Clear();
            _uvSetNames.Clear();

            int coordID = _targetMatRef.TextureCoordId;

            if (coordID < 0)
            {
                _objNames.Add("None");
                _uvSetNames.Add("None");
                Invalidate();
                return;
            }

            if (_targetMatRef.Material.Objects.Length > 1)
            {
                _objNames.Add("All");

                //TODO: need to change the UV dropdown box to show only texcoords 0-7 that exist
                //not the uv set names
                coordID = -1;
            }

            Vector2 min = new Vector2(float.MaxValue);
            Vector2 max = new Vector2(float.MinValue);

            foreach (MDL0ObjectNode obj in _targetMatRef.Material.Objects)
            {
                _objNames.Add(obj.Name);
                RenderInfo info = new RenderInfo(obj._manager);
                _renderInfo.Add(info);
                min.Min(info._min);
                max.Max(info._max);
            }

            if (_objNames.Count == 0)
            {
                _objNames.Add("None");
                _uvSetNames.Add("None");
                Invalidate();
                return;
            }

            //TODO: zoom the camera out to the nearest whole texture repetition
            //to display all texture coordinates.

            //Change from [0, 1] to [-0.5, 0.5]
            //min -= 0.5f;
            //max -= 0.5f;

            //float xCorrect = 1.0f, yCorrect = 1.0f;
            //GLTexture t = _targetMatRef.TextureNode.Texture;
            //float texWidth = t.Width;
            //float texHeight = t.Height;
            //float tAspect = (float)texWidth / texHeight;
            //float wAspect = (float)Width / Height;

            //int minXmult = (int)(min._x + (min._x < 0 ? -1 : 0));
            //int maxXmult = (int)(max._x + (max._x < 0 ? 0 : 1));
            //int minYmult = (int)(min._y + (min._y < 0 ? -1 : 0));
            //int maxYmult = (int)(max._y + (max._y < 0 ? 0 : 1));

            //if (tAspect > wAspect)
            //{
            //    yCorrect = tAspect / wAspect;
            //}
            //else
            //{
            //    xCorrect = wAspect / tAspect;
            //}

            //CurrentViewport.Camera.Translate(
            //    ((float)(minXmult + maxXmult) / 2.0f - 0.5f) / xCorrect * Width,
            //    ((float)(minYmult + maxYmult) / 2.0f - 0.5f) / yCorrect * -Height, 0);
            //float scale = Math.Max(maxXmult - minXmult, maxYmult - minYmult);
            //CurrentViewport.Camera.Scale(scale, scale, scale);

            SetObjectIndex(-1);
            SetUVIndex(coordID);
        }
Beispiel #10
0
        internal unsafe void BoxChanged(object sender, EventArgs e)
        {
            if (TargetTexRef == null || sender == null)
            {
                return;
            }

            NumericInputBox box   = sender as NumericInputBox;
            int             index = (int)box.Tag;

            if (index == 2 || index == 4 || index == 5 || index == 8)
            {
                return;
            }

            MDL0MaterialRefNode mr = TargetTexRef;

            if ((SelectedAnimation != null) && (CurrentFrame > 0))
            {
                SRT0TextureNode entry = SelectedAnimation.FindChild(mr.Parent.Name + "/Texture" + mr.Index, true) as SRT0TextureNode;

                if (entry == null)
                {
                    if (!float.IsNaN(box.Value))
                    {
                        entry = SelectedAnimation.FindOrCreateEntry(mr.Parent.Name, mr.Index);

                        //Set initial values (so they aren't null)
                        FrameState state = mr._bindState; //Get the texture's bindstate
                        float *    p     = (float *)&state;
                        for (int i = 0; i < 3; i++)       //Get the scale
                        {
                            if (p[i] != 1.0f)             //Check for default values
                            {
                                entry.SetKeyframe(KeyFrameMode.ScaleX + i, 0, p[i]);
                            }
                        }
                        for (int i = 3; i < 9; i++) //Get rotation and translation respectively
                        {
                            if (p[i] != 0.0f)       //Check for default values
                            {
                                entry.SetKeyframe(KeyFrameMode.ScaleX + i, 0, p[i]);
                            }
                        }
                        if (p[10] != 0.0f)
                        {
                            entry.SetKeyframe(KeyFrameMode.ScaleX + 10, 0, p[10]);
                        }

                        //Finally, replace with the changed value
                        entry.SetKeyframe(KeyFrameMode.ScaleX + index, CurrentFrame - 1, box.Value);
                    }
                }
                else //Set to existing SRT0 texture
                if (float.IsNaN(box.Value))
                {
                    entry.RemoveKeyframe(KeyFrameMode.ScaleX + index, CurrentFrame - 1);
                }
                else
                {
                    entry.SetKeyframe(KeyFrameMode.ScaleX + index, CurrentFrame - 1, box.Value);
                }
            }
            else
            {
                //Change base transform
                FrameState state = mr._bindState;
                float *    p     = (float *)&state;
                p[index]      = float.IsNaN(box.Value) ? (index > 2 ? 0.0f : 1.0f) : box.Value;
                mr._bindState = state;
                //mr.RecalcBindState();
                mr.SignalPropertyChange();
            }
            TargetModel.ApplySRT(SelectedAnimation, CurrentFrame);
            ResetBox(index);
            _mainWindow.modelPanel1.Invalidate();
        }