Skip to content

Commit

Permalink
Compare light colour instead of isLit, allows for custom lighting imp…
Browse files Browse the repository at this point in the history
…lementations.
  • Loading branch information
UnknownShadow200 committed Feb 25, 2017
1 parent fe7b494 commit c916785
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 25 deletions.
5 changes: 4 additions & 1 deletion ClassicalSharp/MeshBuilder/AdvLightingBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,8 @@ int ComputeLightFlags(int x, int y, int z, int cIndex) {

int Lit(int x, int y, int z, int cIndex) {
if (x < 0 || y < 0 || z < 0
|| x >= width || y >= height || z >= length) return 7;
|| x >= width || y >= height || z >= length) return 7; // all faces lit

int flags = 0;
BlockID block = chunk[cIndex];
int lightHeight = light.heightmap[(z * width) + x];
Expand All @@ -426,8 +427,10 @@ int Lit(int x, int y, int z, int cIndex) {
// Use fact Light(Y.Bottom) == Light((Y - 1).Top)
int offset = (lightFlags >> Side.Bottom) & 1;
flags |= ((y - offset) > lightHeight ? 1 : 0);

// Light is same for all the horizontal faces
flags |= (y > lightHeight ? 2 : 0);

// Use fact Light((Y + 1).Bottom) == Light(Y.Top)
offset = (lightFlags >> Side.Top) & 1;
flags |= ((y - offset) >= lightHeight ? 4 : 0);
Expand Down
19 changes: 0 additions & 19 deletions ClassicalSharp/MeshBuilder/Builder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -297,25 +297,6 @@ protected bool OccludedLiquid(int chunkIndex) {
&& info.Draw[chunk[chunkIndex + 324 + 18]] != DrawType.Gas;
}

protected bool IsLit(int x, int y, int z, int face, BlockID block) {
int offset = (info.LightOffset[block] >> face) & 1;
switch (face) {
case Side.Left:
return x < offset || y > light.heightmap[(z * width) + (x - offset)];
case Side.Right:
return x > (maxX - offset) || y > light.heightmap[(z * width) + (x + offset)];
case Side.Front:
return z < offset || y > light.heightmap[((z - offset) * width) + x];
case Side.Back:
return z > (maxZ - offset) || y > light.heightmap[((z + offset) * width) + x];
case Side.Bottom:
return y <= 0 || (y - 1 - offset) >= (light.heightmap[(z * width) + x]);
case Side.Top:
return y >= maxY || (y - offset) >= (light.heightmap[(z * width) + x]);
}
return true;
}

public void OnNewMapLoaded() {
map = game.World;
env = game.World.Env;
Expand Down
29 changes: 24 additions & 5 deletions ClassicalSharp/MeshBuilder/NormalBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,30 @@ protected override int StretchZ(int countIndex, int x, int y, int z, int chunkIn
return count;
}

bool CanStretch(BlockID initialBlock, int chunkIndex, int x, int y, int z, int face) {
BlockID rawBlock = chunk[chunkIndex];
return rawBlock == initialBlock
&& !info.IsFaceHidden(rawBlock, chunk[chunkIndex + offsets[face]], face)
&& (fullBright || IsLit(X, Y, Z, face, initialBlock) == IsLit(x, y, z, face, rawBlock));
bool CanStretch(BlockID initial, int chunkIndex, int x, int y, int z, int face) {
BlockID cur = chunk[chunkIndex];
return cur == initial
&& !info.IsFaceHidden(cur, chunk[chunkIndex + offsets[face]], face)
&& (fullBright || (LightCol(X, Y, Z, face, initial) == LightCol(x, y, z, face, cur)));
}

int LightCol(int x, int y, int z, int face, BlockID block) {
int offset = (info.LightOffset[block] >> face) & 1;
switch (face) {
case Side.Left:
return x < offset ? light.OutsideXSide : light.LightCol_XSide_Fast(x, y, z);
case Side.Right:
return x > (maxX - offset) ? light.OutsideXSide : light.LightCol_XSide_Fast(x, y, z);
case Side.Front:
return z < offset ? light.OutsideZSide : light.LightCol_ZSide_Fast(x, y, z);
case Side.Back:
return z > (maxZ - offset) ? light.OutsideZSide : light.LightCol_ZSide_Fast(x, y, z);
case Side.Bottom:
return y <= 0 ? light.OutsideYBottom : light.LightCol_YBottom_Fast(x, y, z);
case Side.Top:
return y >= maxY ? light.Outside : light.LightCol_YTop_Fast(x, y, z);
}
return 0;
}

protected override void PreStretchTiles(int x1, int y1, int z1) {
Expand Down

0 comments on commit c916785

Please sign in to comment.