private void backgroundRendering_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;

            //DeviceSettings settings = e.Argument as DeviceSettings;

            // create irrlicht device using provided settings
            if (panelRenderWindow.IsDisposed)
            {
                throw new Exception("Form closed!");
            }

            DeviceSettings s = new DeviceSettings(
                IntPtr.Zero,
                DriverType.Direct3D9,
                0,                  //antialias
                new Color(100, 101, 140),
                false);

            if (panelRenderWindow.InvokeRequired)
            {
                panelRenderWindow.Invoke(new MethodInvoker(delegate { s.WindowID = panelRenderWindow.Handle; }));
            }

            dev = IrrlichtDevice.CreateDevice(s);
            if (dev == null)
            {
                throw new NullReferenceException("Could not create device for engine!");
            }
            dev.OnEvent        += new IrrlichtDevice.EventHandler(this.device_OnEvent);
            dev.Logger.LogLevel = LogLevel.Warning;

            drv  = dev.VideoDriver;
            smgr = dev.SceneManager;
            gui  = dev.GUIEnvironment;

            _ = smgr.FileSystem.AddFileArchive(activeMod.FileDirectory);

            smgr.Attributes.AddValue("TW_TW3_LOAD_SKEL", true);
            smgr.Attributes.AddValue("TW_TW3_LOAD_BEST_LOD_ONLY", true);


            // added by vl
            if (meshFile != null && meshFile.Length > 0)
            {
                mesh = smgr.GetMesh(meshFile);
            }

            if (mesh == null)
            {
                throw new Exception("Failed to load mesh.");
            }

            mesh.Grab();
            smgr.MeshManipulator.RecalculateNormals(mesh);
            meshLoaded = true;

            float scaleMul = 1.0f;

            node   = smgr.AddAnimatedMeshSceneNode(mesh);
            helper = smgr.GetMeshLoader(smgr.MeshLoaderCount - 1).getMeshLoaderHelper();             // hacked to gat witcher3 loader
            if (node != null && meshLoaded)
            {
                node.Scale = new Vector3Df(3.0f);
                node.SetMaterialFlag(MaterialFlag.Lighting, false);

                scaleMul = node.BoundingBox.Radius / 4;

                if (!string.IsNullOrEmpty(rigFile))
                {
                    meshToAnimate = helper.loadRig(rigFile, mesh);
                    if (meshToAnimate == null)
                    {
                        throw new Exception("Failed to load rig.");
                    }
                    else
                    {
                        meshToAnimate.Grab();
                        rigLoaded = true;
                        Logger.LogString("Rig loaded!", Logtype.Success);
                        node.Mesh = meshToAnimate;
                    }
                }

                if (!string.IsNullOrEmpty(animFile) && rigLoaded)
                {
                    animList = helper.loadAnimation(animFile, meshToAnimate);
                    if (animList.Count > 0)
                    {
                        animLoaded = true;
                        Logger.LogString($"{animList.Count} animations loaded! Select animation to play", Logtype.Success);
                    }
                    else
                    {
                        Logger.LogString("No animations loaded!", Logtype.Important);
                    }
                }

                setMaterialsSettings(node);
            }

            var camera = smgr.AddCameraSceneNode(null,
                                                 new Vector3Df(node.BoundingBox.Radius * 8, node.BoundingBox.Radius, 0),
                                                 new Vector3Df(0, node.BoundingBox.Radius, 0));

            camera.NearValue = 0.001f;
            camera.FOV       = 45.0f * 3.14f / 180.0f;


            var animText = activeAnim;

            var mAnimText     = gui.AddStaticText(animText, new Recti(0, this.ClientSize.Height - 80, 100, this.ClientSize.Height - 70));
            var mPositionText = gui.AddStaticText("", new Recti(0, this.ClientSize.Height - 70, 100, this.ClientSize.Height - 60));
            var mRotationText = gui.AddStaticText("", new Recti(0, this.ClientSize.Height - 60, 100, this.ClientSize.Height - 50));
            var fpsText       = gui.AddStaticText("", new Recti(0, this.ClientSize.Height - 50, 100, this.ClientSize.Height - 40));
            var infoText      = gui.AddStaticText("[Space] - Reset\n[LMouse] - Rotate\n[MMouse] - Move\n[Wheel] - Zoom", new Recti(0, this.ClientSize.Height - 40, 100, this.ClientSize.Height));

            mAnimText.OverrideColor   = mPositionText.OverrideColor = mRotationText.OverrideColor = fpsText.OverrideColor = infoText.OverrideColor = new Color(255, 255, 255);
            mAnimText.BackgroundColor = mPositionText.BackgroundColor = mRotationText.BackgroundColor = fpsText.BackgroundColor = infoText.BackgroundColor = new Color(0, 0, 0);

            viewPort = drv.ViewPort;
            var lineMat = new Material
            {
                Lighting = false
            };

            while (dev.Run())
            {
                drv.ViewPort = viewPort;

                drv.BeginScene(ClearBufferFlag.Depth | ClearBufferFlag.Color, s.BackColor);

                node.Position = modelPosition;
                node.Rotation = modelAngle;

                //update info box
                mPositionText.Text = $"X: {modelPosition.X.ToString("F2")} Y: {modelPosition.Y.ToString("F2")} Z: {modelPosition.Z.ToString("F2")}";
                mRotationText.Text = $"Yaw: {modelAngle.Y.ToString("F2")} Roll: {modelAngle.Z.ToString("F2")}";
                fpsText.Text       = $"FPS: {drv.FPS}";

                smgr.DrawAll();
                gui.DrawAll();

                // draw xyz axis right bottom

                drv.ViewPort = new Recti(this.ClientSize.Width - 100, this.ClientSize.Height - 80, this.ClientSize.Width, this.ClientSize.Height);

                drv.SetMaterial(lineMat);
                var matrix = new Matrix(new Vector3Df(0, 0, 0), modelAngle);
                drv.SetTransform(TransformationState.World, matrix);
                matrix = matrix.BuildProjectionMatrixOrthoLH(100, 80, camera.NearValue, camera.FarValue);
                drv.SetTransform(TransformationState.Projection, matrix);
                matrix = matrix.BuildCameraLookAtMatrixLH(new Vector3Df(50, 0, 0), new Vector3Df(0, 0, 0), new Vector3Df(0, 1f, 0));
                drv.SetTransform(TransformationState.View, matrix);
                drv.Draw3DLine(0, 0, 0, 30f, 0, 0, Color.SolidGreen);
                drv.Draw3DLine(0, 0, 0, 0, 30f, 0, Color.SolidBlue);
                drv.Draw3DLine(0, 0, 0, 0, 0, 30f, Color.SolidRed);

                drv.EndScene();

                // if we requested to stop, we close the device
                if (worker.CancellationPending)
                {
                    dev.Close();
                }
            }
            // drop device
            dev.Drop();
        }
示例#2
0
        private void backgroundRendering_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker   = sender as BackgroundWorker;
            DeviceSettings   settings = e.Argument as DeviceSettings;

            // create irrlicht device using provided settings

            IrrlichtDevice dev = IrrlichtDevice.CreateDevice(settings);

            if (dev == null)
            {
                throw new Exception("Failed to create Irrlicht device.");
            }

            dev.Logger.LogLevel = LogLevel.Warning;
            VideoDriver  drv  = dev.VideoDriver;
            SceneManager smgr = dev.SceneManager;

            smgr.FileSystem.AddFileArchive("E:/WitcherMods/modTest/Test/files/Mod/Bundle");
            smgr.FileSystem.AddFileArchive("E:/WitcherMods/modTest/Test/files/Raw/Mod/TextureCache");

            smgr.Attributes.AddValue("TW_TW3_LOAD_SKEL", true);
            smgr.Attributes.AddValue("TW_TW3_LOAD_BEST_LOD_ONLY", true);

            // setup a simple 3d scene

            //CameraSceneNode cam = smgr.AddCameraSceneNode();
            //cam.Target = new Vector3Df(0);

            // added by vl
            AnimatedMesh mesh = smgr.GetMesh("E:/WitcherMods/modTest/Test/files/Mod/Bundle/characters/models/animals/cat/model/t_01__cat.w2mesh");

            if (mesh == null)
            {
                throw new Exception("Failed to load mesh.");
            }

            smgr.MeshManipulator.RecalculateNormals(mesh);

            List <String> animList = null;

            float scaleMul               = 1.0f;
            AnimatedMeshSceneNode node   = smgr.AddAnimatedMeshSceneNode(mesh);
            MeshLoaderHelper      helper = smgr.GetMeshLoader(smgr.MeshLoaderCount - 1).getMeshLoaderHelper();

            if (node != null)
            {
                scaleMul   = node.BoundingBox.Radius / 4;
                node.Scale = new Vector3Df(3.0f);
                node.SetMaterialFlag(MaterialFlag.Lighting, false);

                SkinnedMesh sm = helper.loadRig("E:/WitcherMods/modTest/Test/files/Mod/Bundle/characters/base_entities/cat_base/cat_base.w2rig", mesh);
                if (sm == null)
                {
                    throw new Exception("Failed to load rig.");
                }

                animList = helper.loadAnimation("E:/WitcherMods/modTest/Test/files/Mod/Bundle/animations/animals/cat/cat_animation.w2anims", sm);
                if (animList.Count > 0)
                {
                    AnimatedMesh am = helper.applyAnimation(animList[0], sm);
                    node.Mesh = am;
                }
                //scaleSkeleton(sm, 3.0f);
                //sm.SkinMesh();

                mesh.Drop();
                sm.Drop();
                setMaterialsSettings(node);

                /*
                 * animList = helper.loadAnimation("E:/WitcherMods/modTest/Test/files/Mod/Bundle/animations/animals/cat/cat_animation.w2anims", node);
                 * if (animList.Count > 0)
                 * {
                 *      helper.applyAnimation(animList[0]);
                 * }
                 */
            }

            var camera = smgr.AddCameraSceneNode(null,
                                                 new Vector3Df(node.BoundingBox.Radius * 8, node.BoundingBox.Radius, 0),
                                                 new Vector3Df(0, node.BoundingBox.Radius, 0)
                                                 );

            camera.NearValue = 0.001f;
            camera.FOV       = 45.0f * 3.14f / 180.0f;

            node.DebugDataVisible ^= DebugSceneType.BBox | DebugSceneType.Skeleton;

            node.Position = new Vector3Df(0.0f);

            /*
             * SceneNodeAnimator anim = smgr.CreateFlyCircleAnimator(new Vector3Df(0, 15, 0), 30.0f);
             * cam.AddAnimator(anim);
             * anim.Drop();
             *
             * SceneNode cube = smgr.AddCubeSceneNode(20);
             * cube.SetMaterialTexture(0, drv.GetTexture("../../media/wall.bmp"));
             * cube.SetMaterialTexture(1, drv.GetTexture("../../media/water.jpg"));
             * cube.SetMaterialFlag(MaterialFlag.Lighting, false);
             * cube.SetMaterialType(MaterialType.Reflection2Layer);
             */

            if (settings.BackColor == null)
            {
                smgr.AddSkyBoxSceneNode(
                    "../../media/irrlicht2_up.jpg",
                    "../../media/irrlicht2_dn.jpg",
                    "../../media/irrlicht2_lf.jpg",
                    "../../media/irrlicht2_rt.jpg",
                    "../../media/irrlicht2_ft.jpg",
                    "../../media/irrlicht2_bk.jpg");
            }

            dev.GUIEnvironment.AddImage(
                drv.GetTexture("../../media/lime_logo_alpha.png"),
                new Vector2Di(30, 0));

            // draw all

            int lastFPS = -1;

            while (dev.Run())
            {
                if (settings.BackColor == null)
                {
                    // indeed, we do not need to spend time on cleaning color buffer if we use skybox
                    drv.BeginScene(ClearBufferFlag.Depth);
                }
                else
                {
                    drv.BeginScene(ClearBufferFlag.Depth | ClearBufferFlag.Color, settings.BackColor);
                }

                smgr.DrawAll();
                dev.GUIEnvironment.DrawAll();
                drv.EndScene();

                int fps = drv.FPS;
                if (lastFPS != fps)
                {
                    // report progress using common BackgroundWorker' method
                    // note: we cannot do just labelRenderingStatus.Text = "...",
                    // because we are running another thread
                    worker.ReportProgress(fps, drv.Name);
                    lastFPS = fps;
                }

                // if we requested to stop, we close the device
                if (worker.CancellationPending)
                {
                    dev.Close();
                }
            }

            // drop device
            dev.Drop();
        }