Example #1
0
        private static void _client_Started()
        {
            var world           = new World();
            var commandRecorder = new EntityCommandRecorder();

            var scene = new Scene("Client", world, commandRecorder);

            var factory = _client.GraphicsDevice.ResourceFactory;

            var materialInputLayouts = new MaterialInputLayouts();

            var textureLayout = factory.CreateResourceLayout(
                new ResourceLayoutDescription(
                    new ResourceLayoutElementDescription("SurfaceTexture", ResourceKind.TextureReadOnly, ShaderStages.Fragment),
                    new ResourceLayoutElementDescription("SurfaceSampler", ResourceKind.Sampler, ShaderStages.Fragment),
                    new ResourceLayoutElementDescription("TextureColour", ResourceKind.UniformBuffer, ShaderStages.Fragment)));

            materialInputLayouts.ResourceLayouts["Texture"] = textureLayout;

            materialInputLayouts.ResourceLayouts["WorldTransform"] = factory.CreateResourceLayout(
                new ResourceLayoutDescription(
                    new ResourceLayoutElementDescription("WorldBuffer", ResourceKind.UniformBuffer, ShaderStages.Vertex)));

            materialInputLayouts.ResourceLayouts["SceneInputs"] = factory.CreateResourceLayout(
                new ResourceLayoutDescription(
                    new ResourceLayoutElementDescription("ProjectionBuffer", ResourceKind.UniformBuffer, ShaderStages.Vertex),
                    new ResourceLayoutElementDescription("ViewBuffer", ResourceKind.UniformBuffer, ShaderStages.Vertex),
                    new ResourceLayoutElementDescription("SceneLighting", ResourceKind.UniformBuffer, ShaderStages.Fragment)));

            materialInputLayouts.ResourceLayouts["CameraInputs"] = factory.CreateResourceLayout(
                new ResourceLayoutDescription(
                    new ResourceLayoutElementDescription("CameraInfo", ResourceKind.UniformBuffer, ShaderStages.Vertex)));

            materialInputLayouts.VertexLayouts["Model"] = new VertexLayoutDescription(
                new VertexElementDescription("Position", VertexElementSemantic.Position, VertexElementFormat.Float3),
                new VertexElementDescription("TexCoords", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2),
                new VertexElementDescription("Normal", VertexElementSemantic.Normal, VertexElementFormat.Float3));

            materialInputLayouts.VertexLayouts["Lighting"] = new VertexLayoutDescription(
                new VertexElementDescription("Light", VertexElementSemantic.Color, VertexElementFormat.Float1));

            var mesh3dMaterial = new Material(_client.GraphicsDevice, _client.MainSceneFramebuffer, _client.Resources.LoadText("Shaders\\Mesh.vs"), _client.Resources.LoadText("Shaders\\Mesh.fg"),
                                              new string[] { "Model" }, new string[] { "SceneInputs", "WorldTransform", "Texture" }, materialInputLayouts);

            var lightMeshMaterial = new Material(_client.GraphicsDevice, _client.MainSceneFramebuffer, _client.Resources.LoadText("Shaders\\LightMesh.vs"), _client.Resources.LoadText("Shaders\\LightMesh.fg"),
                                                 new string[] { "Model", "Lighting" }, new string[] { "SceneInputs", "WorldTransform", "Texture", "CameraInputs" }, materialInputLayouts);

            var voxelTexturesResource = _client.Resources.LoadImage("Textures\\spritesheet_tiles.png");
            var voxelTexture          = new MaterialTexture(_client.GraphicsDevice, textureLayout, voxelTexturesResource, RgbaFloat.White);
            var redVoxelTexture       = new MaterialTexture(_client.GraphicsDevice, textureLayout, voxelTexturesResource, RgbaFloat.Red);
            var semiTransVoxelColour  = new MaterialTexture(_client.GraphicsDevice, textureLayout, voxelTexturesResource, new RgbaFloat(1.0f, 1.0f, 1.0f, 0.8f));

            Action <Entity> setVoxelRender = (Entity e) =>
            {
                e.Set(new LightVertexResources());
                e.Set(lightMeshMaterial);
                e.Set(voxelTexture);
            };

            var networkedEntities = new NetworkedEntities(world);

            var clientSystem = new ClientSystem(world, _messageTargetMap, _clientMessagingChannel);

            clientSystem.AddListener(new TransformMessageApplier(networkedEntities));
            clientSystem.AddListener(new ClientEntityAssignmentApplier(networkedEntities));
            clientSystem.AddListener(new VoxelSpaceMessageApplier(networkedEntities));
            clientSystem.AddListener(new VoxelGridMessageApplier(setVoxelRender, networkedEntities));
            clientSystem.AddListener(new VoxelGridChangeMessageApplier(networkedEntities));
            clientSystem.AddListener(new EntityRemover(networkedEntities));
            clientSystem.AddListener(new ComponentSyncMessageApplier <EntityMetaData>(networkedEntities));

            scene.AddSystem(clientSystem);

            var parallelRunner = new DefaultParallelRunner(8);

            var px = Image.Load("Assets\\Textures\\cloudtop_rt.png");
            var nx = Image.Load("Assets\\Textures\\cloudtop_lf.png");
            var py = Image.Load("Assets\\Textures\\cloudtop_up.png");
            var ny = Image.Load("Assets\\Textures\\cloudtop_dn.png");
            var pz = Image.Load("Assets\\Textures\\cloudtop_bk.png");
            var nz = Image.Load("Assets\\Textures\\cloudtop_ft.png");

            scene.AddSystem(new SkyboxRenderer(_client.GraphicsDevice, _client.MainSceneFramebuffer, px, nx, py, ny, pz, nz));

            scene.AddSystem(new MeshGeometryRenderer(_client.GraphicsDevice, materialInputLayouts, world));
            scene.AddSystem(new LightMeshGeometryRenderer(_client.GraphicsDevice, materialInputLayouts, world));

            var voxelTypes = LoadVoxelTypes();

            var editors = new List <IEditor>()
            {
                new SelectedEntitySystem(world),
                new EntityInspector(world),
                new EntityList(world),
                new SystemList(scene),
                new VoxelEditor(_clientMessagingChannel, world, voxelTypes.Select((t, i) => (t.Name, new Voxel()
                {
                    Exists = true, BlockType = (ushort)i
                })).ToArray()),
                new VoxelSpaceLoader(_clientMessagingChannel, world)
            };

            _editorMenu.AddEditorSet("Client", editors);

            scene.AddSystem(_editorMenu);
            scene.AddSystems(editors);

            scene.AddSystem(new SunLightPropogationSystem(world, new VoxelTypes(voxelTypes)));

            scene.AddSystem(new VoxelGridMesher(commandRecorder, world, new VoxelTypes(voxelTypes), _client.GraphicsDevice, parallelRunner));

            scene.AddSystem(new MeshGeometryCleaner(world));
            scene.AddSystem(new LightVertexCleaner(world));

            scene.AddSystem(new TransformLerper(networkedEntities, world));

            scene.AddSystem(new FlagClearingSystem <NeighbourMemberChanged>(world));

            scene.AddSystem(new InputForceApplierInputSystem(_clientMessagingChannel, world));
            scene.AddSystem(new SimpleCameraMoverInputSystem(_clientMessagingChannel, world));

            world.SubscribeEntityDisposed((in Entity e) =>
            {
                if (e.Has <VoxelGrid>())
                {
                    var voxelGrid = e.Get <VoxelGrid>();
                    voxelGrid.VoxelSpace.Remove(voxelGrid.MemberIndex);
                }
            });
Example #2
0
        protected override void Initialize()
        {
            var factory = GraphicsDevice.ResourceFactory;

            var materialInputLayouts = new MaterialInputLayouts();

            var textureLayout = factory.CreateResourceLayout(
                new ResourceLayoutDescription(
                    new ResourceLayoutElementDescription("SurfaceTexture", ResourceKind.TextureReadOnly, ShaderStages.Fragment),
                    new ResourceLayoutElementDescription("SurfaceSampler", ResourceKind.Sampler, ShaderStages.Fragment),
                    new ResourceLayoutElementDescription("TextureColour", ResourceKind.UniformBuffer, ShaderStages.Fragment)));

            materialInputLayouts.ResourceLayouts["Texture"] = textureLayout;

            materialInputLayouts.ResourceLayouts["WorldTransform"] = factory.CreateResourceLayout(
                new ResourceLayoutDescription(
                    new ResourceLayoutElementDescription("WorldBuffer", ResourceKind.UniformBuffer, ShaderStages.Vertex)));

            materialInputLayouts.ResourceLayouts["SceneInputs"] = factory.CreateResourceLayout(
                new ResourceLayoutDescription(
                    new ResourceLayoutElementDescription("ProjectionBuffer", ResourceKind.UniformBuffer, ShaderStages.Vertex),
                    new ResourceLayoutElementDescription("ViewBuffer", ResourceKind.UniformBuffer, ShaderStages.Vertex),
                    new ResourceLayoutElementDescription("SceneLighting", ResourceKind.UniformBuffer, ShaderStages.Fragment)));

            materialInputLayouts.ResourceLayouts["CameraInputs"] = factory.CreateResourceLayout(
                new ResourceLayoutDescription(
                    new ResourceLayoutElementDescription("CameraInfo", ResourceKind.UniformBuffer, ShaderStages.Vertex)));

            materialInputLayouts.VertexLayouts["Model"] = new VertexLayoutDescription(
                new VertexElementDescription("Position", VertexElementSemantic.Position, VertexElementFormat.Float3),
                new VertexElementDescription("TexCoords", VertexElementSemantic.TextureCoordinate, VertexElementFormat.Float2),
                new VertexElementDescription("Normal", VertexElementSemantic.Normal, VertexElementFormat.Float3));

            materialInputLayouts.VertexLayouts["Lighting"] = new VertexLayoutDescription(
                new VertexElementDescription("Light", VertexElementSemantic.Color, VertexElementFormat.Float1));

            var mesh3dMaterial = new Material(GraphicsDevice, MainSceneFramebuffer, Resources.LoadText("Shaders\\Mesh.vs"), Resources.LoadText("Shaders\\Mesh.fg"),
                                              new string[] { "Model" }, new string[] { "SceneInputs", "WorldTransform", "Texture" }, materialInputLayouts);

            var lightMeshMaterial = new Material(GraphicsDevice, MainSceneFramebuffer, Resources.LoadText("Shaders\\LightMesh.vs"), Resources.LoadText("Shaders\\LightMesh.fg"),
                                                 new string[] { "Model", "Lighting" }, new string[] { "SceneInputs", "WorldTransform", "Texture", "CameraInputs" }, materialInputLayouts);

            var voxelTexturesResource = Resources.LoadImage("Textures\\spritesheet_tiles.png");
            var voxelTexture          = new MaterialTexture(GraphicsDevice, textureLayout, voxelTexturesResource, RgbaFloat.White);
            var redVoxelTexture       = new MaterialTexture(GraphicsDevice, textureLayout, voxelTexturesResource, RgbaFloat.Red);
            var semiTransVoxelColour  = new MaterialTexture(GraphicsDevice, textureLayout, voxelTexturesResource, new RgbaFloat(1.0f, 1.0f, 1.0f, 0.8f));

            Action <Entity> setVoxelRender = (Entity e) =>
            {
                e.Set(lightMeshMaterial);
                e.Set(voxelTexture);
            };

            var parallelRunner = new DefaultParallelRunner(4);

            var player          = Scene.World.CreateEntity();
            var playerTransform = new Transform();

            playerTransform.Position = new Vector3(0, 40, 0);
            player.Set(playerTransform);
            player.Set(new Camera());

            var worldVoxelSpace = Scene.World.CreateEntity();

            worldVoxelSpace.Set(new Transform());
            worldVoxelSpace.Set(new VoxelSpace(32, 1, worldVoxelSpace));

            var px = Image.Load("Assets\\Textures\\cloudtop_rt.png");
            var nx = Image.Load("Assets\\Textures\\cloudtop_lf.png");
            var py = Image.Load("Assets\\Textures\\cloudtop_up.png");
            var ny = Image.Load("Assets\\Textures\\cloudtop_dn.png");
            var pz = Image.Load("Assets\\Textures\\cloudtop_bk.png");
            var nz = Image.Load("Assets\\Textures\\cloudtop_ft.png");

            Scene.RendererSystems.Add(new SkyboxRenderer(GraphicsDevice, MainSceneFramebuffer, px, nx, py, ny, pz, nz));

            Scene.RendererSystems.Add(new MeshGeometryRenderer(GraphicsDevice, materialInputLayouts, Scene.World));
            Scene.RendererSystems.Add(new LightMeshGeometryRenderer(GraphicsDevice, materialInputLayouts, Scene.World));

            var physicsSystem = new PhysicsSystem();

            var voxelTypes = LoadVoxelTypes();

            var tools = new List <ITool>()
            {
                new RemoveVoxelEditingTool((e) => { e.Set(lightMeshMaterial); e.Set(redVoxelTexture); }, Scene.World, physicsSystem, player)
            };

            tools.AddRange(voxelTypes.Select((type, i) => new BasicVoxelAddingTool(type.Name, (ushort)i, (e) => { e.Set(lightMeshMaterial); e.Set(semiTransVoxelColour); }, Scene.World, physicsSystem, player)));

            Scene.LogicSystems.Add(new EditorMenu(Scene, new List <IEditor>()
            {
                new EditorConsole(Scene),
                new Toolbar(tools.ToArray()),
                new SelectedEntitySystem(Scene.World),
                new PhysicsEntitySelector(Scene.World, physicsSystem, playerTransform),
                new EntityInspector(Scene.World),
                new EntityList(Scene.World),
                new SystemList(Scene),
                new VoxelSpaceLoader(Scene.World, playerTransform, setVoxelRender)
            }));

            Scene.LogicSystems.Add(new SimpleCameraMover(player, physicsSystem, Scene.World));
            Scene.LogicSystems.Add(new WorldSpaceLoader(setVoxelRender, Scene.World, playerTransform, worldVoxelSpace, 10, 3, 32));
            Scene.LogicSystems.Add(new ChunkGeneratorSystem(Scene, parallelRunner, new ChunkGenerator()));

            Scene.LogicSystems.Add(new VoxelSpaceExpanderSystem(setVoxelRender, Scene.World));

            Scene.LogicSystems.Add(new InputForceApplier(physicsSystem, Scene.World));

            Scene.LogicSystems.Add(new PhysicsBlockFinder(Scene.World, parallelRunner));

            Scene.LogicSystems.Add(new ParallelSystem <double>(parallelRunner,
                                                               new SequentialSystem <double>(
                                                                   new VoxelSpaceChangePropogator(Scene.World),
                                                                   new VoxelStaticBodyGenerator(physicsSystem, Scene.World),
                                                                   new VoxelSpaceDynamicBodyGenerator(physicsSystem, Scene.World),
                                                                   physicsSystem,
                                                                   new DynamicBodyPositionSync(Scene.World)),
                                                               new SunLightPropogationSystem(new VoxelTypes(voxelTypes), Scene)));

            Scene.LogicSystems.Add(new VoxelGridMesher(Scene, new VoxelTypes(voxelTypes), GraphicsDevice, parallelRunner));

            Scene.LogicSystems.Add(new CharacterInputSystem(physicsSystem, Scene.World));

            Scene.LogicSystems.Add(new MeshGeometryCleaner(Scene.World));
            Scene.LogicSystems.Add(new LightVertexCleaner(Scene.World));

            Scene.LogicSystems.Add(new FlagClearingSystem <NeighbourMemberChanged>(Scene.World));

            //var cylinder = Scene.World.CreateEntity();
            //AddCylinder(cylinder, mesh3dMaterial, voxelTexture);
        }