private Task createTask(ThreadRequest tr)
        {
            Task tk = new Task();

            tk.task_uid       = taskGUIDCounter;
            tk.thread_request = tr;

            //Create and start Thread
            Thread t = null;

            switch (tr.type)
            {
            case THREAD_REQUEST_TYPE.LOAD_NMS_ARCHIVES_REQUEST:
                string          filepath = (string)tr.arguments[0];
                string          gameDir  = (string)tr.arguments[1];
                ResourceManager resMgr   = (ResourceManager)tr.arguments[2];
                t = new Thread(() => NMSUtils.loadNMSArchives(filepath, gameDir, ref resMgr, ref tk.thread_request.response));
                break;

            default:
                Console.WriteLine("DISPATCHER : Unsupported Thread Request");
                break;
            }

            tk.thread = t;
            tk.thread.IsBackground = true;
            tk.thread.Start();

            return(tk);
        }
        public AnimPoseComponent(TkAnimPoseComponentData apcd)
        {
            _poseFrameData = (TkAnimMetadata)NMSUtils.LoadNMSTemplate(apcd.Filename,
                                                                      ref Common.RenderState.activeResMgr);

            //Load PoseAnims
            for (int i = 0; i < apcd.PoseAnims.Count; i++)
            {
                AnimPoseData my_apd = new AnimPoseData(apcd.PoseAnims[i]);
                poseData.Add(my_apd);
            }
        }
Beispiel #3
0
        //Path Initializer
        public Texture(string path, bool isCustom = false)
        {
            Stream fs;

            byte[] image_data;
            int    data_length;

            try
            {
                if (!isCustom)
                {
                    try
                    {
                        fs = NMSUtils.LoadNMSFileStream(path, ref Common.RenderState.activeResMgr);
                    } catch (FileNotFoundException ex) {
                        //FileNotFoundExceptions during texture loading, are caught so that default textures are loaded
                        fs = null;
                    }
                }

                else
                {
                    fs = new FileStream(path, FileMode.Open);
                }

                if (fs == null)
                {
                    //throw new System.IO.FileNotFoundException();
                    Console.WriteLine("Texture {0} Missing. Using default.dds", path);

                    //Load default.dds from resources
                    image_data  = File.ReadAllBytes("default.dds");
                    data_length = image_data.Length;
                }
                else
                {
                    data_length = (int)fs.Length;
                    image_data  = new byte[data_length];
                }

                fs.Read(image_data, 0, data_length);
            } catch (FileNotFoundException e)
            {
                //Fallback to the default.dds
                image_data = WPFModelViewer.Properties.Resources._default;
            }

            textureInit(image_data, path);
        }
Beispiel #4
0
 private void fetchAnimMetaData()
 {
     if (Common.RenderState.activeResMgr.Animations.ContainsKey(Filename))
     {
         animMeta = Common.RenderState.activeResMgr.Animations[Filename];
     }
     else
     {
         TkAnimMetadata amd = NMSUtils.LoadNMSTemplate(Filename,
                                                       ref Common.RenderState.activeResMgr) as TkAnimMetadata;
         animMeta = new AnimMetadata(amd);
         animMeta.load(); //Load data as well
         Common.RenderState.activeResMgr.Animations[Filename] = animMeta;
     }
     NotifyPropertyChanged("FrameCount");
 }
Beispiel #5
0
        public static Material Parse(string path, textureManager input_texMgr)
        {
            //Load template
            //Try to use libMBIN to load the Material files
            TkMaterialData template = NMSUtils.LoadNMSTemplate(path, ref Common.RenderState.activeResMgr) as TkMaterialData;

#if DEBUG
            //Save NMSTemplate to exml
            template.WriteToExml("Temp\\" + template.Name + ".exml");
#endif

            //Make new material based on the template
            Material mat = new Material(template);

            mat.texMgr = input_texMgr;
            mat.init();
            return(mat);
        }
Beispiel #6
0
        private void loadData()
        {
            for (int j = 0; j < NodeCount; j++)
            {
                TkAnimNodeData node = NodeData[j];
                //Init dictionary entries

                anim_rotations[node.Node] = new OpenTK.Quaternion[FrameCount];
                anim_positions[node.Node] = new Vector3[FrameCount];
                anim_scales[node.Node]    = new Vector3[FrameCount];

                for (int i = 0; i < FrameCount; i++)
                {
                    NMSUtils.fetchRotQuaternion(node, this, i, ref anim_rotations[node.Node][i]); //use Ref
                    NMSUtils.fetchTransVector(node, this, i, ref anim_positions[node.Node][i]);   //use Ref
                    NMSUtils.fetchScaleVector(node, this, i, ref anim_scales[node.Node][i]);      //use Ref
                }
            }
        }
Beispiel #7
0
        //Generate procedural textures
        private static void prepareTextures(textureManager texMgr, string path)
        {
            //At this point, at least one sampler exists, so for now I assume that the first sampler
            //is always the diffuse sampler and I can initiate the mixing process
            Console.WriteLine("Procedural Texture Detected: " + path);
            CallBacks.Log(string.Format("Parsing Procedural Texture"));

            TkProceduralTextureList template = NMSUtils.LoadNMSTemplate(path, ref Common.RenderState.activeResMgr) as TkProceduralTextureList;

            List <TkProceduralTexture> texList = new List <TkProceduralTexture>(8);

            for (int i = 0; i < 8; i++)
            {
                texList.Add(null);
            }
            ModelProcGen.parse_procTexture(ref texList, template, ref Common.RenderState.activeResMgr);

            Common.CallBacks.Log("Proc Texture Selection");
            for (int i = 0; i < 8; i++)
            {
                if (texList[i] != null)
                {
                    string partNameDiff = texList[i].Diffuse;
                    Common.CallBacks.Log(partNameDiff);
                }
            }

            Common.CallBacks.Log("Procedural Material. Trying to generate procTextures...");

            for (int i = 0; i < 8; i++)
            {
                TkProceduralTexture ptex = texList[i];
                //Add defaults
                if (ptex == null)
                {
                    baseLayersUsed[i]  = 0.0f;
                    alphaLayersUsed[i] = 0.0f;
                    continue;
                }

                string partNameDiff   = ptex.Diffuse;
                string partNameMask   = ptex.Mask;
                string partNameNormal = ptex.Normal;

                TkPaletteTexture paletteNode = ptex.Palette;
                string           paletteName = paletteNode.Palette.ToString();
                string           colorName   = paletteNode.ColourAlt.ToString();
                Vector4          palColor    = palette[paletteName][colorName];
                //Randomize palette Color every single time
                //Vector3 palColor = Model_Viewer.Palettes.get_color(paletteName, colorName);

                //Store pallete color to Recolouring List
                reColourings[i] = new float[] { palColor[0], palColor[1], palColor[2], palColor[3] };
                if (ptex.OverrideAverageColour)
                {
                    avgColourings[i] = new float[] { ptex.AverageColour.R, ptex.AverageColour.G, ptex.AverageColour.B, ptex.AverageColour.A }
                }
                ;

                //Create Palette Option
                PaletteOpt palOpt = new PaletteOpt();
                palOpt.PaletteName = paletteName;
                palOpt.ColorName   = colorName;
                palOpts[i]         = palOpt;
                Console.WriteLine("Index {0} Palette Selection {1} {2} ", i, palOpt.PaletteName, palOpt.ColorName);
                Console.WriteLine("Index {0} Color {1} {2} {3} {4}", i, palColor[0], palColor[1], palColor[2], palColor[3]);

                //DIFFUSE
                if (partNameDiff == "")
                {
                    //Add White
                    baseLayersUsed[i] = 0.0f;
                }
                else if (!texMgr.hasTexture(partNameDiff))
                {
                    //Configure the Diffuse Texture
                    try
                    {
                        Texture tex = new Texture(partNameDiff);
                        tex.palOpt    = palOpt;
                        tex.procColor = palColor;
                        //Store to master texture manager
                        Common.RenderState.activeResMgr.texMgr.addTexture(tex);

                        //Save Texture to material
                        difftextures[i]    = tex;
                        baseLayersUsed[i]  = 1.0f;
                        alphaLayersUsed[i] = 1.0f;
                    }
                    catch (System.IO.FileNotFoundException)
                    {
                        //Texture Not Found Continue
                        Console.WriteLine("Diffuse Texture " + partNameDiff + " Not Found, Appending White Tex");
                        CallBacks.Log(string.Format("Diffuse Texture {0} Not Found", partNameDiff));
                        baseLayersUsed[i] = 0.0f;
                    }
                }
                else
                //Load texture from dict
                {
                    Texture tex = texMgr.getTexture(partNameDiff);
                    //Save Texture to material
                    difftextures[i]   = tex;
                    baseLayersUsed[i] = 1.0f;
                }

                //MASK
                if (partNameMask == "")
                {
                    //Skip
                    alphaLayersUsed[i] = 0.0f;
                }
                else if (!texMgr.hasTexture(partNameMask))
                {
                    //Configure Mask
                    try
                    {
                        Texture texmask = new Texture(partNameMask);
                        //Store to master texture manager
                        Common.RenderState.activeResMgr.texMgr.addTexture(texmask);
                        //Store Texture to material
                        masktextures[i]    = texmask;
                        alphaLayersUsed[i] = 0.0f;
                    }
                    catch (System.IO.FileNotFoundException)
                    {
                        //Mask Texture not found
                        Console.WriteLine("Mask Texture " + partNameMask + " Not Found");
                        CallBacks.Log(string.Format("Mask Texture {0} Not Found", partNameMask));
                        alphaLayersUsed[i] = 0.0f;
                    }
                }
                else
                //Load texture from dict
                {
                    Texture tex = texMgr.getTexture(partNameMask);
                    //Store Texture to material
                    masktextures[i]    = tex;
                    alphaLayersUsed[i] = 1.0f;
                }


                //NORMALS
                if (partNameNormal == "")
                {
                    //Skip
                }
                else if (!texMgr.hasTexture(partNameNormal))
                {
                    try
                    {
                        Texture texnormal = new Texture(partNameNormal);
                        //Store to master texture manager
                        Common.RenderState.activeResMgr.texMgr.addTexture(texnormal);
                        //Store Texture to material
                        normaltextures[i] = texnormal;
                    }
                    catch (System.IO.FileNotFoundException)
                    {
                        //Normal Texture not found
                        CallBacks.Log(string.Format("Normal Texture {0} Not Found", partNameNormal));
                    }
                }
                else
                //Load texture from dict
                {
                    Texture tex = texMgr.getTexture(partNameNormal);
                    //Store Texture to material
                    normaltextures[i] = tex;
                }
            }
        }
Beispiel #8
0
        public static Dictionary <string, Dictionary <string, Vector4> > createPalettefromBasePalettes()
        {
            Dictionary <string, Dictionary <string, Vector4> > newPal;

            newPal = new Dictionary <string, Dictionary <string, Vector4> >();

            GcPaletteList template;

            try {
                template = NMSUtils.LoadNMSTemplate("METADATA\\SIMULATION\\SOLARSYSTEM\\COLOURS\\BASECOLOURPALETTES.MBIN",
                                                    ref MVCore.Common.RenderState.activeResMgr) as GcPaletteList;
            } catch (Exception ex) {
                CallBacks.Log("Using Default Palettes");
                return(createPalette());
            }

            TkPaletteTexture tkpt = new TkPaletteTexture();
            GcPaletteData    gcpd = new GcPaletteData();

            for (int i = 0; i < template.Palettes.Length; i++)
            {
                string pal_name = ((TkPaletteTexture.PaletteEnum)i).ToString();
                CallBacks.Log(string.Format("Palette {0} NumColors {1}", pal_name, template.Palettes[i].NumColours));
                newPal[pal_name] = new Dictionary <string, Vector4>();

                //Generate Bitmap for palette
                Bitmap bmp = new Bitmap(64, 1);

                for (int j = 0; j < template.Palettes[i].Colours.Length; j++)
                {
                    Colour colour = template.Palettes[i].Colours[j];

                    //Console.WriteLine("Color {0} {1} {2} {3} {4}",
                    //j, colour.R, colour.G, colour.B, colour.A);

                    //bmp.SetPixel(j, 0, Color.FromArgb((int)(colour.A * 255),
                    //                                    (int)(colour.R * 255),
                    //                                    (int)(colour.G * 255),
                    //                                    (int)(colour.B * 255)));
                }

                Vector4 primary, alt1, alt2, alt3, alt4, matchg, unique, none;
                int     index        = 0;
                int     index1       = 0;
                int     index2       = 0;
                int     index3       = 0;
                int     index4       = 0;
                int     unique_index = 0;
                none = new Vector4(1.0f, 1.0f, 1.0f, 0.0f);

                switch (template.Palettes[i].NumColours)
                {
                case GcPaletteData.NumColoursEnum.Inactive:     //Inactive
                    //Test by not saving anything
                    break;

                case GcPaletteData.NumColoursEnum._1:     //1 Color - All colors should be the same
                    index        = 0;
                    index1       = index;
                    index2       = index;
                    index3       = index;
                    index4       = index;
                    unique_index = 0;
                    break;

                case GcPaletteData.NumColoursEnum._4:     //4 Color
                case GcPaletteData.NumColoursEnum._8:     //8 Color NOTE: Not enough samples for that
                    index        = get_active_palette_index(4);
                    index1       = index + 1;
                    index2       = index + 2;
                    index3       = index + 3;
                    unique_index = get_active_palette_index(1);
                    break;

                case GcPaletteData.NumColoursEnum._16:     //16 Color
                    //Align to groups of 2
                    index        = get_active_palette_index(2);
                    index1       = index + 1;
                    index2       = get_active_palette_index(2);
                    index3       = index2 + 1;
                    index4       = get_active_palette_index(2);
                    unique_index = get_active_palette_index(1);
                    break;

                case GcPaletteData.NumColoursEnum.All:     //All
                    index        = get_active_palette_index(1);
                    index1       = get_active_palette_index(1);
                    index2       = get_active_palette_index(1);
                    index3       = get_active_palette_index(1);
                    index4       = get_active_palette_index(1);
                    unique_index = get_active_palette_index(1);
                    break;
                }

                //Set Colors
                primary = colour_to_vec4(template.Palettes[i].Colours[index]);
                alt1    = colour_to_vec4(template.Palettes[i].Colours[index1]);
                alt2    = colour_to_vec4(template.Palettes[i].Colours[index2]);
                alt3    = colour_to_vec4(template.Palettes[i].Colours[index3]);
                alt4    = colour_to_vec4(template.Palettes[i].Colours[index4]);
                matchg  = primary;
                unique  = colour_to_vec4(template.Palettes[i].Colours[unique_index]);
                none    = primary;

                //save the colors to the dictionary
                newPal[pal_name]["Primary"]      = primary;
                newPal[pal_name]["Alternative1"] = alt1;
                newPal[pal_name]["Alternative2"] = alt2;
                newPal[pal_name]["Alternative3"] = alt3;
                newPal[pal_name]["Alternative4"] = alt4;
                newPal[pal_name]["Unique"]       = unique;
                //Add MatchGround option
                newPal[pal_name]["MatchGround"] = new Vector4(0.5f, 0.427f, 0.337f, 0.0f);
                newPal[pal_name]["None"]        = none;

                //bmp.Save("Temp\\" + pal_name + ".bmp", ImageFormat.Bmp);
            }

            return(newPal);
        }
Beispiel #9
0
        //TODO: It would be nice if I didn't have to do make the method public, but it needs a lot of work on the
        //AnimPoseComponent class to temporarily store the selected pose frames, while also in the model.update method

        //Locator Animation Stuff

        public Dictionary <string, Matrix4> loadPose()
        {
            if (animPoseComponentID < 0)
            {
                return(new Dictionary <string, Matrix4>());
            }

            AnimPoseComponent            apc          = _components[animPoseComponentID] as AnimPoseComponent;
            Dictionary <string, Matrix4> posematrices = new Dictionary <string, Matrix4>();

            foreach (TkAnimNodeData node in apc._poseFrameData.NodeData)
            {
                List <OpenTK.Quaternion> quats        = new List <OpenTK.Quaternion>();
                List <Vector3>           translations = new List <Vector3>();
                List <Vector3>           scales       = new List <Vector3>();

                //We should interpolate frame shit over all the selected Pose Data

                //Gather all the transformation data for all the pose factors
                for (int i = 0; i < apc._poseData.Count; i++)
                //for (int i = 0; i < 1; i++)
                {
                    //Get Pose Frame
                    int poseFrameIndex = apc._poseData[i].PActivePoseFrame;

                    Vector3           v_t, v_s;
                    OpenTK.Quaternion lq;
                    //Fetch Rotation Quaternion
                    lq  = NMSUtils.fetchRotQuaternion(node, apc._poseFrameData, poseFrameIndex);
                    v_t = NMSUtils.fetchTransVector(node, apc._poseFrameData, poseFrameIndex);
                    v_s = NMSUtils.fetchScaleVector(node, apc._poseFrameData, poseFrameIndex);

                    quats.Add(lq);
                    translations.Add(v_t);
                    scales.Add(v_s);
                }

                float      fact = 1.0f / quats.Count;
                Quaternion fq   = new Quaternion();
                Vector3    f_vt = new Vector3();
                Vector3    f_vs = new Vector3();


                fq   = quats[0];
                f_vt = translations[0];
                f_vs = scales[0];

                //Interpolate all data
                for (int i = 1; i < quats.Count; i++)
                {
                    //Method A: Interpolate
                    //Quaternion.Slerp(fq, quats[i], 0.5f);
                    //Vector3.Lerp(f_vt, translations[i], 0.5f);
                    //Vector3.Lerp(f_vs, scales[i], 0.5f);

                    //Addup
                    f_vs *= scales[i];
                }

                //Generate Transformation Matrix
                //Matrix4 poseMat = Matrix4.CreateScale(f_vs) * Matrix4.CreateFromQuaternion(fq) * Matrix4.CreateTranslation(f_vt);
                //Matrix4 poseMat = Matrix4.CreateScale(f_vs) * Matrix4.CreateFromQuaternion(fq);
                Matrix4 poseMat = Matrix4.CreateScale(f_vs);
                posematrices[node.Node] = poseMat;
            }

            return(posematrices);
        }
        public static void loadSettingsStatic()
        {
            //Load jsonstring
            try
            {
                string       jsonstring = File.ReadAllText("settings.json");
                JSONSettings lSettings  = JsonConvert.DeserializeObject <JSONSettings>(jsonstring);
                saveSettingsToEnv(lSettings);
            }
            catch (FileNotFoundException)
            {
                //Generating new settings file

                string gamedir = NMSUtils.getGameInstallationDir();
                string unpackdir;

                if (gamedir == "" || gamedir is null)
                {
                    Util.showInfo("NMS Installation not found. Please choose your unpacked files folder...", "Info");
                    FolderBrowserDialog openFileDlg = new FolderBrowserDialog();
                    var res = openFileDlg.ShowDialog();

                    if (res == System.Windows.Forms.DialogResult.Cancel)
                    {
                        unpackdir = "";
                    }
                    else
                    {
                        unpackdir = openFileDlg.SelectedPath;
                    }
                    openFileDlg.Dispose();
                    //Store paths
                    RenderState.settings.GameDir   = unpackdir;
                    RenderState.settings.UnpackDir = unpackdir;
                    return;
                }

                //Ask if the user has files unpacked
                MessageBoxResult result = MessageBox.Show("Do you have unpacked game files?", "", MessageBoxButton.YesNo);

                if (result == MessageBoxResult.No)
                {
                    unpackdir = gamedir;
                }
                else
                {
                    FolderBrowserDialog dialog = new FolderBrowserDialog();
                    dialog.Description = "Select the unpacked GAMEDATA folder";
                    DialogResult res = dialog.ShowDialog();

                    if (res == System.Windows.Forms.DialogResult.OK)
                    {
                        unpackdir = dialog.SelectedPath;
                    }
                    else
                    {
                        unpackdir = "";
                    }
                }

                //Save path settings to the environment
                RenderState.settings.GameDir   = gamedir;
                RenderState.settings.UnpackDir = unpackdir;

                saveSettingsStatic(); //Save Settings right away
            }
        }
Beispiel #11
0
        public void Cleanup()
        {
            //Cleanup global texture manager
            texMgr.cleanup();
            fontMgr.cleanup();
            txtMgr.cleanup();
            //procTextureLayerSelections.Clear();

            foreach (Scene p in GLScenes.Values)
            {
                p.Dispose();
            }
            GLScenes.Clear();

            //Cleanup Geom Objects
            foreach (GeomObject p in GLgeoms.Values)
            {
                p.Dispose();
            }
            GLgeoms.Clear();

            //Cleanup GLVaos
            foreach (GLVao p in GLVaos.Values)
            {
                p.Dispose();
            }
            GLVaos.Clear();

            //Cleanup Animations
            Animations.Clear();

            //Cleanup Materials
            foreach (Material p in GLmaterials.Values)
            {
                p.Dispose();
            }
            GLmaterials.Clear();

            //Cleanup Material Shaders
            opaqueMeshShaderMap.Clear();
            defaultMeshShaderMap.Clear();
            transparentMeshShaderMap.Clear();
            decalMeshShaderMap.Clear();

            activeGLDeferredLITShaders.Clear();
            activeGLDeferredUNLITShaders.Clear();
            activeGLDeferredDecalShaders.Clear();
            activeGLForwardTransparentShaders.Clear();

            GLDeferredLITShaderMap.Clear();
            GLDeferredUNLITShaderMap.Clear();
            GLForwardShaderMapTransparent.Clear();
            GLDeferredShaderMapDecal.Clear();
            GLDefaultShaderMap.Clear();

            //Cleanup archives
            NMSUtils.unloadNMSArchives(this);

            //Cleanup Lights
            foreach (Light p in GLlights)
            {
                p.Dispose();
            }
            GLlights.Clear();

            //Cleanup Cameras
            //TODO: Make Camera Disposable
            //foreach (GMDL.Camera p in GLCameras)
            //    p.Dispose();
            GLCameras.Clear();
        }