protected override Scene3D LoadEntry(FileSystemEntry entry, ContentManager contentManager, Game game, LoadOptions loadOptions) { switch (contentManager.SageGame) { case SageGame.Ra3: case SageGame.Ra3Uprising: case SageGame.Cnc4: // TODO break; default: contentManager.IniDataContext.LoadIniFile(@"Data\INI\Terrain.ini"); break; } var mapFile = MapFile.FromFileSystemEntry(entry); var heightMap = new HeightMap(mapFile.HeightMapData); var indexBufferCache = AddDisposable(new TerrainPatchIndexBufferCache(contentManager.GraphicsDevice)); var tileDataTexture = AddDisposable(CreateTileDataTexture( contentManager.GraphicsDevice, mapFile, heightMap)); var cliffDetailsBuffer = AddDisposable(CreateCliffDetails( contentManager.GraphicsDevice, mapFile)); CreateTextures( contentManager, mapFile.BlendTileData, out var textureArray, out var textureDetails); var textureDetailsBuffer = AddDisposable(contentManager.GraphicsDevice.CreateStaticStructuredBuffer(textureDetails)); var terrainMaterial = AddDisposable(new TerrainMaterial(contentManager, contentManager.EffectLibrary.Terrain)); terrainMaterial.SetTileData(tileDataTexture); terrainMaterial.SetCliffDetails(cliffDetailsBuffer); terrainMaterial.SetTextureDetails(textureDetailsBuffer); terrainMaterial.SetTextureArray(textureArray); var terrainPatches = CreatePatches( contentManager.GraphicsDevice, heightMap, mapFile.BlendTileData, terrainMaterial, indexBufferCache); var cloudTextureName = mapFile.EnvironmentData?.CloudTexture ?? "tscloudmed.dds"; var cloudTexture = contentManager.Load <Texture>(Path.Combine("Art", "Textures", cloudTextureName)); var macroTextureName = mapFile.EnvironmentData?.MacroTexture ?? "tsnoiseurb.dds"; var macroTexture = contentManager.Load <Texture>(Path.Combine("Art", "Textures", macroTextureName)); var materialConstantsBuffer = AddDisposable(contentManager.GraphicsDevice.CreateStaticBuffer( new TerrainMaterial.TerrainMaterialConstants { MapBorderWidth = new Vector2(mapFile.HeightMapData.BorderWidth, mapFile.HeightMapData.BorderWidth) * HeightMap.HorizontalScale, MapSize = new Vector2(mapFile.HeightMapData.Width, mapFile.HeightMapData.Height) * HeightMap.HorizontalScale, IsMacroTextureStretched = false // TODO: This must be one of the EnvironmentData unknown values. }, BufferUsage.UniformBuffer)); terrainMaterial.SetMaterialConstants(materialConstantsBuffer); var terrain = new Terrain.Terrain( heightMap, terrainPatches, cloudTexture, macroTexture, contentManager.SolidWhiteTexture); var players = Player.FromMapData(mapFile.SidesList.Players, contentManager).ToArray(); var teams = (mapFile.SidesList.Teams ?? mapFile.Teams.Items) .Select(team => Team.FromMapData(team, players)) .ToArray(); LoadObjects( contentManager, heightMap, mapFile.ObjectsList.Objects, teams, out var waypoints, out var gameObjects); var lighting = new WorldLighting( mapFile.GlobalLighting.LightingConfigurations.ToLightSettingsDictionary(), mapFile.GlobalLighting.Time); var waypointPaths = new WaypointPathCollection(mapFile.WaypointsList.WaypointPaths .Select(path => { var start = waypoints[path.StartWaypointID]; var end = waypoints[path.EndWaypointID]; return(new Settings.WaypointPath(start, end)); })); // TODO: Don't hardcode this. // Perhaps add one ScriptComponent for the neutral player, // and one for the active player. var scriptList = mapFile.GetPlayerScriptsList().ScriptLists[0]; var mapScripts = CreateScripts(scriptList); var cameraController = new RtsCameraController(contentManager) { TerrainPosition = terrain.HeightMap.GetPosition( terrain.HeightMap.Width / 2, terrain.HeightMap.Height / 2) }; contentManager.GraphicsDevice.WaitForIdle(); return(new Scene3D( game, cameraController, mapFile, terrain, mapScripts, gameObjects, waypoints, waypointPaths, lighting, players, teams)); }
protected override Scene3D LoadEntry(FileSystemEntry entry, ContentManager contentManager, Game game, LoadOptions loadOptions) { switch (contentManager.SageGame) { case SageGame.Ra3: case SageGame.Ra3Uprising: case SageGame.Cnc4: // TODO break; default: contentManager.IniDataContext.LoadIniFile(@"Data\INI\Terrain.ini"); contentManager.IniDataContext.LoadIniFile(@"Data\INI\Roads.ini"); break; } var mapFile = MapFile.FromFileSystemEntry(entry); var heightMap = new HeightMap(mapFile.HeightMapData); var indexBufferCache = AddDisposable(new TerrainPatchIndexBufferCache(contentManager.GraphicsDevice)); var tileDataTexture = AddDisposable(CreateTileDataTexture( contentManager.GraphicsDevice, mapFile, heightMap)); var cliffDetailsBuffer = AddDisposable(CreateCliffDetails( contentManager.GraphicsDevice, mapFile)); CreateTextures( contentManager, mapFile.BlendTileData, out var textureArray, out var textureDetails); var textureDetailsBuffer = AddDisposable(contentManager.GraphicsDevice.CreateStaticStructuredBuffer(textureDetails)); var terrainPipeline = contentManager.ShaderResources.Terrain.Pipeline; Texture LoadTexture(string name) { var texture = contentManager.Load <Texture>(Path.Combine("Art", "Textures", name), fallbackToPlaceholder: false); if (texture == null) { texture = contentManager.Load <Texture>(Path.Combine("Art", "CompiledTextures", name.Substring(0, 2), name)); } return(texture); } var materialConstantsBuffer = AddDisposable(contentManager.GraphicsDevice.CreateStaticBuffer( new TerrainShaderResources.TerrainMaterialConstants { MapBorderWidth = new Vector2(mapFile.HeightMapData.BorderWidth, mapFile.HeightMapData.BorderWidth) * HeightMap.HorizontalScale, MapSize = new Vector2(mapFile.HeightMapData.Width, mapFile.HeightMapData.Height) * HeightMap.HorizontalScale, IsMacroTextureStretched = false // TODO: This must be one of the EnvironmentData unknown values. }, BufferUsage.UniformBuffer)); var macroTexture = LoadTexture(mapFile.EnvironmentData?.MacroTexture ?? "tsnoiseurb.dds"); var materialResourceSet = AddDisposable(contentManager.ShaderResources.Terrain.CreateMaterialResourceSet( materialConstantsBuffer, tileDataTexture, cliffDetailsBuffer ?? contentManager.GetNullStructuredBuffer(TerrainShaderResources.CliffInfo.Size), textureDetailsBuffer, textureArray, macroTexture)); var terrainPatches = CreatePatches( contentManager.GraphicsDevice, heightMap, mapFile.BlendTileData, indexBufferCache, materialResourceSet); var cloudTexture = LoadTexture(mapFile.EnvironmentData?.CloudTexture ?? "tscloudmed.dds"); cloudTexture.Name = "Cloud texture"; var cloudResourceLayout = AddDisposable(contentManager.GraphicsDevice.ResourceFactory.CreateResourceLayout( new ResourceLayoutDescription( new ResourceLayoutElementDescription("Global_CloudTexture", ResourceKind.TextureReadOnly, ShaderStages.Fragment)))); var cloudResourceSet = AddDisposable(contentManager.GraphicsDevice.ResourceFactory.CreateResourceSet( new ResourceSetDescription( cloudResourceLayout, cloudTexture))); cloudResourceSet.Name = "Cloud resource set"; var terrain = new Terrain.Terrain( heightMap, terrainPatches, contentManager.ShaderResources.Terrain.ShaderSet, terrainPipeline, cloudResourceSet); var players = Player.FromMapData(mapFile.SidesList.Players, contentManager).ToArray(); var teams = (mapFile.SidesList.Teams ?? mapFile.Teams.Items) .Select(team => Team.FromMapData(team, players)) .ToArray(); LoadObjects( contentManager, heightMap, mapFile.ObjectsList.Objects, teams, out var waypoints, out var gameObjects, out var roads, out var bridges); var waterAreas = new List <WaterArea>(); if (mapFile.PolygonTriggers != null) { foreach (var polygonTrigger in mapFile.PolygonTriggers.Triggers) { switch (polygonTrigger.TriggerType) { case PolygonTriggerType.Water: case PolygonTriggerType.River: // TODO: Handle this differently. Water texture should be animated "downstream". case PolygonTriggerType.WaterAndRiver: if (WaterArea.TryCreate(contentManager, polygonTrigger, out var waterArea)) { waterAreas.Add(AddDisposable(waterArea)); } break; } } } var lighting = new WorldLighting( mapFile.GlobalLighting.LightingConfigurations.ToLightSettingsDictionary(), mapFile.GlobalLighting.Time); var waypointPaths = new WaypointPathCollection(mapFile.WaypointsList.WaypointPaths .Select(path => { var start = waypoints[path.StartWaypointID]; var end = waypoints[path.EndWaypointID]; return(new Settings.WaypointPath(start, end)); })); // TODO: Don't hardcode this. // Perhaps add one ScriptComponent for the neutral player, // and one for the active player. var scriptList = mapFile.GetPlayerScriptsList().ScriptLists[0]; var mapScripts = CreateScripts(scriptList); var cameraController = new RtsCameraController(contentManager) { TerrainPosition = terrain.HeightMap.GetPosition( terrain.HeightMap.Width / 2, terrain.HeightMap.Height / 2) }; contentManager.GraphicsDevice.WaitForIdle(); return(new Scene3D( game, game.InputMessageBuffer, () => game.Viewport, cameraController, mapFile, terrain, waterAreas.ToArray(), roads, bridges, mapScripts, gameObjects, waypoints, waypointPaths, lighting, players, teams)); }