public void Iteration(ref EntityChunk chunk) { if (!chunk.TryGetComponents <C_Camera>(out ComponentArray cameras) || !chunk.TryGetComponents <C_Transform>(out ComponentArray transforms)) { return; } CameraData[] cameraDatas = new CameraData[chunk.Count]; Matrix4[] cameraTransforms = new Matrix4[chunk.Count]; for (int c = 0; c < chunk.Count; c++) { C_Camera cam = cameras.Get <C_Camera>(c); if (cam.cameraData.Framebuffer == null) { Vec2i res = AppScreen.Resolution; var fb = new Framebuffer(res, "Camera FBO"); fb.AddColorBuffer(true); fb.AddDepthBuffer(true); cam.cameraData.SetFramebuffer(fb); var ffb = new Framebuffer(res, "Camera Final FBO"); ffb.AddColorBuffer(false); cam.cameraData.SetFinalFramebuffer(ffb); chunk.SetComponent(c, cam); } else if (cam.cameraData.Framebuffer.Size != AppScreen.Resolution) { cam.cameraData.Framebuffer.ChangeSize(AppScreen.Resolution); cam.cameraData.FinalFramebuffer.ChangeSize(AppScreen.Resolution); } //if(cam.cameraData.ColorTargetTexture == null) //{ // Vec2i res = AppScreen.Resolution; // cam.cameraData.SetColorTargetTexture(new Texture2D(res.x, res.y, "Camera Color FBO")); // cam.cameraData.SetDepthTargetTexture(new DepthTexture(res.x, res.y, "Camera Depth")); // chunk.SetComponent(c, cam); //} //else if (cam.cameraData.ColorTargetTexture.Size != AppScreen.Resolution) //{ // cam.cameraData.ColorTargetTexture.SetSize(AppScreen.Resolution); // cam.cameraData.DepthTargetTexture.SetSize(AppScreen.Resolution); //} cameraDatas[c] = cam.cameraData; cameraTransforms[c] = transforms.Get <C_Transform>(c).value; } RenderPipelineCore.Draw(cameraDatas, cameraTransforms); }
static void InitSkybox() { Cubemap skyboxCubemap = new Cubemap(new string[] { AssetBrowser.Utilities.LocalToAbsolutePath(@"Skybox\right.png"), AssetBrowser.Utilities.LocalToAbsolutePath(@"Skybox\left.png"), AssetBrowser.Utilities.LocalToAbsolutePath(@"Skybox\bottom.png"), AssetBrowser.Utilities.LocalToAbsolutePath(@"Skybox\top.png"), AssetBrowser.Utilities.LocalToAbsolutePath(@"Skybox\front.png"), AssetBrowser.Utilities.LocalToAbsolutePath(@"Skybox\back.png"), }); Shader skyboxShader = Shader.CreateShaderWithPath(AssetBrowser.Utilities.LocalToAbsolutePath(@"Shaders\skybox.vert"), AssetBrowser.Utilities.LocalToAbsolutePath(@"Shaders\skybox.frag"), "Skybox"); Skybox skybox = new Skybox(new Material(skyboxShader, skyboxCubemap)); RenderPipelineCore.SetSkybox(skybox); }
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() }); }