Ejemplo n.º 1
0
 static void SetupLogicPhase <T0>(WorkerCycleCore workerCore, Action <float> callback, string name) where T0 : IPhaseId
 {
     workerCore.FindFirstLogicPhaseAndModify <T0>((ref WorkerPhase phase) =>
     {
         WorkerPhase[] subPhases = phase.GetPhases();
         WorkerPhase ecsPhase    = new WorkerPhase(EcsPhaseId.Default, name);
         ecsPhase.SetCallback(callback);
         Array.Resize(ref subPhases, subPhases.Length + 1);
         subPhases[^ 1] = ecsPhase;
Ejemplo n.º 2
0
        public EcsRunner AssignToWorker(WorkerCycleCore workerCore)
        {
            SetupLogicPhase <MainPhases.EarlyPhaseId>(workerCore, OnEarlyPhase, "Logic Ecs Execution");
            SetupLogicPhase <MainPhases.DefaultPhaseId>(workerCore, OnDefaultPhase, "Logic Ecs Execution");
            SetupLogicPhase <MainPhases.LatePhaseId>(workerCore, OnLatePhase, "Logic Ecs Execution");

            SetupRenderPhase <RootPhaseId>(workerCore, OnRenderPhase, "Render Ecs Execution");

            return(this);
Ejemplo n.º 3
0
        public static void LoadEngine()
        {
            if (loadedEngine)
            {
                return;
            }

            loadedEngine = true;

            Ogl.enableErrorCheck = true;

            InitConsole();

            DevConsole.Log(LogType.Info, "Creating Entity World...");

            coreWorker = new WorkerCycleCore();
            EntityWorld world = InitEcs();

            RenderPipelineCore.SetPipeline(new ForwardRenderPipeline());

            InitPhysics();
            InitSkybox();
            InitUI(world);
            InitLight();
            InitScene(world);

            DevConsole.Log(LogType.Info, "Entity world created.");

            Ogl.ClearColor(0.1f, 0.1f, 0.2f, 1.0f);
            Ogl.Enable(EnableCap.DepthTest);
            Ogl.Enable(EnableCap.CullFace);
            Ogl.Enable(EnableCap.ProgramPointSize);
            Ogl.PointSize(10f);

            EntityWorld InitEcs()
            {
                EntityWorld world = EntityWorld.CreateWorld();

                world.Runner.AssignToWorker(coreWorker).CreateSystemsAuto(world);
                EntityWorld.SetActive(world);
                return(world);
            }

            void InitPhysics()
            {
                PhysicsRunner physicsRunner = new PhysicsRunner();
                PhysicsWorld  world         = new PhysicsWorld();

                PhysicsWorld.SetDefaultWorld(world);
                physicsRunner.AddWorld(world);
                physicsRunner.AssignToWorker(coreWorker);
            }

            void InitScene(EntityWorld world)
            {
                Mesh   meshResource   = MeshPrimitives.CreateCube(1);
                Shader shaderResource = Shader.CreateShaderWithPath(AssetBrowser.Utilities.LocalToAbsolutePath(@"Shaders\standard.vert"),
                                                                    AssetBrowser.Utilities.LocalToAbsolutePath(@"Shaders\standard.frag"), "Standard Shader");

                Texture2D     texture          = new Texture2D(AssetBrowser.Utilities.LocalToAbsolutePath(@"Textures\Box.png"), "Box");
                Material      materialResource = new Material(shaderResource, texture);
                SC_RenderMesh renderMesh       = new SC_RenderMesh(meshResource, materialResource);

                EntityArchetype meshArchetype = new EntityArchetype(typeof(SC_RenderMesh), typeof(C_Transform), typeof(C_Position));
                EntityArchetype boxArchetype  = new EntityArchetype(typeof(SC_RenderMesh), typeof(C_Transform), typeof(C_Position), typeof(C_BoxTag));

                Entity planeEntity = world.EntityManager.CreateEntity(meshArchetype);

                world.EntityManager.SetComponent(planeEntity, new C_Position()
                {
                    value = new Vec3f(0, 0, 0)
                });
                //world.EntityManager.SetComponent(planeEntity, new C_PhysicsBody() { body = new PhysicBody() { isStatic = true } });
                Mesh     planeMesh     = MeshPrimitives.CreatePlaneXZ(50);
                Material planeMaterial = new Material(shaderResource, Texture2D.CreateWhiteTexture(64, 64));

                world.EntityManager.SetSharedComponent(planeEntity, new SC_RenderMesh(planeMesh, planeMaterial));

                Entity planeEntity2 = world.EntityManager.CreateEntity(meshArchetype);

                world.EntityManager.SetComponent(planeEntity2, new C_Position()
                {
                    value = new Vec3f(0, 5, -10)
                });
                //world.EntityManager.SetComponent(planeEntity2, new C_PhysicsBody() { body = new PhysicBody() { isStatic = true } });

                Mesh     planeMesh2     = MeshPrimitives.CreatePlaneXY(10);
                Material planeMaterial2 = new Material(shaderResource, texture);

                world.EntityManager.SetSharedComponent(planeEntity2, new SC_RenderMesh(planeMesh2, planeMaterial2));

                Entity boxEntity = world.EntityManager.CreateEntity(boxArchetype);

                world.EntityManager.SetComponent(boxEntity, new C_Position()
                {
                    value = new Vec3f(0, 2, 0)
                });
                //world.EntityManager.SetComponent(boxEntity, new C_PhysicsBody() { body = new PhysicBody() });
                world.EntityManager.SetSharedComponent(boxEntity, renderMesh);

                Entity boxEntity2 = world.EntityManager.CreateEntity(boxArchetype);

                world.EntityManager.SetComponent(boxEntity2, new C_Position()
                {
                    value = new Vec3f(2, 2, 0)
                });
                //world.EntityManager.SetComponent(boxEntity2, new C_PhysicsBody() { body = new PhysicBody() });
                world.EntityManager.SetSharedComponent(boxEntity2, renderMesh);

                Entity boxEntity3 = world.EntityManager.CreateEntity(boxArchetype);

                world.EntityManager.SetComponent(boxEntity3, new C_Position()
                {
                    value = new Vec3f(-2, 2, 0)
                });
                //world.EntityManager.SetComponent(boxEntity3, new C_PhysicsBody() { body = new PhysicBody() });

                world.EntityManager.SetSharedComponent(boxEntity3, renderMesh);

                EntityArchetype editorCameraArchetype = new EntityArchetype(typeof(C_Camera), typeof(C_Transform), typeof(C_EditorCamera));
                Entity          cameraEditorEntity    = world.EntityManager.CreateEntity(editorCameraArchetype);

                world.EntityManager.SetComponent(cameraEditorEntity, new C_Camera()
                {
                    cameraData = CameraData.CreatePerpectiveCamera(45f, 800f / 600f, 0.1f, 100f)
                });

                world.EntityManager.SetComponent(cameraEditorEntity, new C_EditorCamera()
                {
                    speed = 10f, focusPoint = new Vector3(0, 2, 0), focusDistance = 12, pitch = 20, yaw = 115, sensitivity = 20
                });
            }

            void InitLight()
            {
                if (RenderPipelineCore.TryGetContext(out LightsRenderData lightData))
                {
                    lightData.lights.Add(new DirectionalLight());
                }
            }

            void InitUI(EntityWorld world)
            {
                EntityArchetype canvasArch = new EntityArchetype(typeof(C_UICanvas));
                var             entity     = world.EntityManager.CreateEntity(canvasArch);

                world.EntityManager.SetComponent(entity, new C_UICanvas()
                {
                    canvas = new UICanvas()
                });
            }