Skip to content

Commit

Permalink
Removed Lighting, Shadows and PostProcessRenderer
Browse files Browse the repository at this point in the history
  • Loading branch information
kaczy93 committed Nov 29, 2023
1 parent 8f43f9f commit 7f74b48
Show file tree
Hide file tree
Showing 10 changed files with 11 additions and 440 deletions.
59 changes: 1 addition & 58 deletions CentrED/Map/LandObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,32 +18,17 @@ public LandObject(CentrEDClient client, LandTile tile)
ref var tileData = ref TileDataLoader.Instance.LandData[tile.Id];

Vector4 cornerZ;
Vector3 normalTop;
Vector3 normalRight;
Vector3 normalLeft;
Vector3 normalBottom;

if ((tileData.Flags & TileFlag.Wet) != 0)
{
// Water tiles are always flat
cornerZ = new Vector4(tile.Z * TILE_Z_SCALE);
normalTop = normalRight = normalLeft = normalBottom = Vector3.UnitZ;
}
else
{
cornerZ = GetCornerZ(client, tile);
normalTop = ComputeNormal(client, tile.X, tile.Y);
normalRight = ComputeNormal(client, tile.X + 1, tile.Y);
normalLeft = ComputeNormal(client, tile.X, tile.Y + 1);
normalBottom = ComputeNormal(client, tile.X + 1, tile.Y + 1);
}

var normals = new Vector3[4];
normals[0] = normalTop;
normals[1] = normalRight;
normals[2] = normalLeft;
normals[3] = normalBottom;

var posX = (tile.X - 1) * TILE_SIZE;
var posY = (tile.Y - 1) * TILE_SIZE;

Expand Down Expand Up @@ -93,7 +78,7 @@ public LandObject(CentrEDClient client, LandTile tile)

for (int i = 0; i < 4; i++)
{
Vertices[i] = new MapVertex(coordinates[i], normals[i], texCoords[i], Vector3.Zero);
Vertices[i] = new MapVertex(coordinates[i], texCoords[i], Vector3.Zero);
}
}

Expand All @@ -115,48 +100,6 @@ private Vector4 GetCornerZ(CentrEDClient client, LandTile tile)
(top.Z * TILE_Z_SCALE, right.Z * TILE_Z_SCALE, left.Z * TILE_Z_SCALE, bottom.Z * TILE_Z_SCALE);
}

private static (Vector2, Vector2)[] _normalOffsets =
{
(new Vector2(1, 0), new Vector2(0, 1)), (new Vector2(0, 1), new Vector2(-1, 0)),
(new Vector2(-1, 0), new Vector2(0, -1)), (new Vector2(0, -1), new Vector2(1, 0))
};


private Vector3 ComputeNormal(CentrEDClient client, int tileX, int tileY)
{
var t = client.GetLandTile
(Math.Clamp(tileX, 0, client.Width * 8 - 1), Math.Clamp(tileY, 0, client.Height * 8 - 1));

Vector3 normal = Vector3.Zero;

for (int i = 0; i < _normalOffsets.Length; i++)
{
(var tu, var tv) = _normalOffsets[i];

var tx = client.GetLandTile
(
Math.Clamp((int)(tileX + tu.X), 0, client.Width * 8 - 1),
Math.Clamp((int)(tileY + tu.Y), 0, client.Height * 8 - 1)
);
var ty = client.GetLandTile
(
Math.Clamp((int)(tileX + tv.X), 0, client.Width * 8 - 1),
Math.Clamp((int)(tileY + tu.Y), 0, client.Height * 8 - 1)
);

if (tx.Id == 0 || ty.Id == 0)
continue;

Vector3 u = new Vector3(tu.X * TILE_SIZE, tu.Y * TILE_SIZE, tx.Z - t.Z);
Vector3 v = new Vector3(tv.X * TILE_SIZE, tv.Y * TILE_SIZE, ty.Z - t.Z);

var tmp = Vector3.Cross(u, v);
normal = Vector3.Add(normal, tmp);
}

return Vector3.Normalize(normal);
}

public void UpdateId(ushort newId)
{
Texture2D? tileTex = null;
Expand Down
106 changes: 1 addition & 105 deletions CentrED/Map/MapManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,20 @@ public class MapManager
private readonly SpriteBatch _spriteBatch;
private readonly Texture2D _background;

private RenderTarget2D _shadowsBuffer;
private RenderTarget2D _selectionBuffer;

public Tool? ActiveTool;

private readonly PostProcessRenderer _postProcessRenderer;

public CentrEDClient Client;

public bool ShowLand = true;
public bool ShowStatics = true;
public bool ShowShadows = true;
public bool ShowVirtualLayer = false;
public int VirtualLayerZ = 0;
public Vector3 VirtualLayerTilePos = Vector3.Zero;

public readonly Camera Camera = new();
private Camera _lightSourceCamera = new();

private LightingState _lightingState = new();
private DepthStencilState _depthStencilState = new()
{
DepthBufferEnable = true,
Expand Down Expand Up @@ -87,15 +81,6 @@ public MapManager(GraphicsDevice gd)
_gfxDevice = gd;
_mapEffect = new MapEffect(gd);
_mapRenderer = new MapRenderer(gd);
_shadowsBuffer = new RenderTarget2D
(
gd,
gd.PresentationParameters.BackBufferWidth * 2,
gd.PresentationParameters.BackBufferHeight * 2,
false,
SurfaceFormat.Single,
DepthFormat.Depth24
);

_selectionBuffer = new RenderTarget2D
(
Expand All @@ -106,7 +91,6 @@ public MapManager(GraphicsDevice gd)
SurfaceFormat.Color,
DepthFormat.Depth24
);
_postProcessRenderer = new PostProcessRenderer(gd);
_spriteBatch = new SpriteBatch(gd);
_background = CEDGame.Content.Load<Texture2D>("background");

Expand Down Expand Up @@ -191,23 +175,6 @@ public MapManager(GraphicsDevice gd)
Camera.ScreenSize.Y = 0;
Camera.ScreenSize.Width = gd.PresentationParameters.BackBufferWidth;
Camera.ScreenSize.Height = gd.PresentationParameters.BackBufferHeight;

// This has to match the LightDirection below
_lightSourceCamera.Position = Camera.Position;
_lightSourceCamera.Zoom = Camera.Zoom;
_lightSourceCamera.Rotation = 45;
_lightSourceCamera.ScreenSize.Width = Camera.ScreenSize.Width * 2;
_lightSourceCamera.ScreenSize.Height = Camera.ScreenSize.Height * 2;

_lightingState.LightDirection = new Vector3(0, -1, -1f);
_lightingState.LightDiffuseColor = Vector3.Normalize(new Vector3(1, 1, 1));
_lightingState.LightSpecularColor = Vector3.Zero;
_lightingState.AmbientLightColor = new Vector3
(
1f - _lightingState.LightDiffuseColor.X,
1f - _lightingState.LightDiffuseColor.Y,
1f - _lightingState.LightDiffuseColor.Z
);
}

public void ReloadShader()
Expand Down Expand Up @@ -484,14 +451,6 @@ public void Update(GameTime gameTime, bool isActive, bool processMouse, bool pro
}
}
}

_lightSourceCamera.Position = Camera.Position;
_lightSourceCamera.Zoom = Camera.Zoom;
_lightSourceCamera.Rotation = 45;
_lightSourceCamera.ScreenSize.Width = Camera.ScreenSize.Width * 2;
_lightSourceCamera.ScreenSize.Height = Camera.ScreenSize.Height * 2;
_lightSourceCamera.Update();

ViewRange = newViewRange;
}

Expand Down Expand Up @@ -529,7 +488,7 @@ private void CalculateViewRange(Camera camera, out Rectangle rect)
int screenHeight = camera.ScreenSize.Height;

/* Calculate the size of the drawing diamond in pixels */
float screenDiamondDiagonal = (screenWidth + screenHeight) / zoom / 3f;
float screenDiamondDiagonal = (screenWidth + screenHeight) / zoom / 2.6f;

Vector3 center = camera.Position;

Expand Down Expand Up @@ -734,9 +693,6 @@ public void Draw()
_gfxDevice.PresentationParameters.BackBufferWidth,
_gfxDevice.PresentationParameters.BackBufferHeight
);
Metrics.Start("DrawShadows");
DrawShadows();
Metrics.Stop("DrawShadows");
Metrics.Start("DrawSelection");
DrawSelectionBuffer();
Metrics.Stop("DrawSelection");
Expand Down Expand Up @@ -769,41 +725,6 @@ private void DrawBackground()
_spriteBatch.End();
}

private void DrawShadows()
{
_mapRenderer.SetRenderTarget(_shadowsBuffer);
if (!ShowShadows)
{
return;
}
_mapEffect.WorldViewProj = _lightSourceCamera.WorldViewProj;
_mapEffect.LightSource.Enabled = false;
_mapEffect.CurrentTechnique = _mapEffect.Techniques["ShadowMap"];
_mapRenderer.Begin
(
_mapEffect,
_lightSourceCamera,
RasterizerState.CullNone,
SamplerState.PointClamp,
_depthStencilState,
BlendState.AlphaBlend,
null,
null
);
foreach (var staticTile in StaticTiles)
{
if (!IsRock(staticTile.Tile.Id) && !IsTree(staticTile.Tile.Id) &&
!TileDataLoader.Instance.StaticData[staticTile.Tile.Id].IsFoliage)
continue;
DrawStatic(staticTile);
}
foreach (var landTile in LandTiles)
{
DrawLand(landTile);
}
_mapRenderer.End();
}

private void DrawSelectionBuffer()
{
_mapEffect.WorldViewProj = Camera.WorldViewProj;
Expand All @@ -812,12 +733,10 @@ private void DrawSelectionBuffer()
_mapRenderer.Begin
(
_mapEffect,
Camera,
RasterizerState.CullNone,
SamplerState.PointClamp,
_depthStencilState,
BlendState.AlphaBlend,
null,
null
);
//0 is no tile in selection buffer
Expand Down Expand Up @@ -854,12 +773,10 @@ private void DrawStatics()
_mapRenderer.Begin
(
_mapEffect,
Camera,
RasterizerState.CullNone,
SamplerState.PointClamp,
_depthStencilState,
BlendState.AlphaBlend,
_shadowsBuffer,
HuesManager.Instance.Texture
);
foreach (var tile in StaticTiles)
Expand All @@ -881,22 +798,14 @@ private void DrawLand()
return;
}
_mapEffect.WorldViewProj = Camera.WorldViewProj;
_mapEffect.LightWorldViewProj = _lightSourceCamera.WorldViewProj;
_mapEffect.AmbientLightColor = _lightingState.AmbientLightColor;
_mapEffect.LightSource.Direction = _lightingState.LightDirection;
_mapEffect.LightSource.DiffuseColor = _lightingState.LightDiffuseColor;
_mapEffect.LightSource.SpecularColor = _lightingState.LightSpecularColor;
_mapEffect.LightSource.Enabled = true;
_mapEffect.CurrentTechnique = _mapEffect.Techniques["Terrain"];
_mapRenderer.Begin
(
_mapEffect,
Camera,
RasterizerState.CullNone,
SamplerState.PointClamp,
_depthStencilState,
BlendState.AlphaBlend,
_shadowsBuffer,
HuesManager.Instance.Texture
);

Expand All @@ -918,17 +827,14 @@ public void DrawVirtualLayer()
{
return;
}
_mapEffect.LightSource.Enabled = false;
_mapEffect.CurrentTechnique = _mapEffect.Techniques["VirtualLayer"];
_mapRenderer.Begin
(
_mapEffect,
Camera,
RasterizerState.CullNone,
SamplerState.PointClamp,
_depthStencilState,
BlendState.AlphaBlend,
null,
null
);
VirtualLayer.Z = (sbyte)VirtualLayerZ;
Expand Down Expand Up @@ -991,16 +897,6 @@ public void OnWindowsResized(GameWindow window)
Camera.ScreenSize = window.ClientBounds;
Camera.Update();

_shadowsBuffer = new RenderTarget2D
(
_gfxDevice,
_gfxDevice.PresentationParameters.BackBufferWidth * 2,
_gfxDevice.PresentationParameters.BackBufferHeight * 2,
false,
SurfaceFormat.Single,
DepthFormat.Depth24
);

_selectionBuffer = new RenderTarget2D
(
_gfxDevice,
Expand Down
2 changes: 1 addition & 1 deletion CentrED/Map/StaticObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public StaticObject(StaticTile tile)
var hue = HuesManager.Instance.GetHueVector(tile);
for (int i = 0; i < 4; i++)
{
Vertices[i] = new MapVertex(coordinates[i], Vector3.UnitZ, texCoords[i], hue);
Vertices[i] = new MapVertex(coordinates[i], texCoords[i], hue);
}
}
}
2 changes: 1 addition & 1 deletion CentrED/Map/VirtualLayerObject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ private VirtualLayerObject()
{
for (int i = 0; i < 4; i++)
{
Vertices[i] = new MapVertex(Vector3.Zero, Vector3.Zero, Vector3.Zero, Vector3.Zero);
Vertices[i] = new MapVertex(Vector3.Zero,Vector3.Zero, Vector3.Zero);
}
}
}
Loading

0 comments on commit 7f74b48

Please sign in to comment.