Some stylistic cleanup in BlockInfo.Culling, use TileSide.Sides instead of an apparent magical constant, minor simplifications in mesh building code.

This commit is contained in:
UnknownShadow200 2015-08-29 08:16:49 +10:00
parent fece6e6da0
commit ffa96b651b
4 changed files with 134 additions and 134 deletions

View file

@ -4,7 +4,7 @@ namespace ClassicalSharp {
public partial class BlockInfo {
bool[] hidden = new bool[BlocksCount * BlocksCount * 6];
bool[] hidden = new bool[BlocksCount * BlocksCount * TileSide.Sides];
void SetupCullingCache() {
for( byte tile = 1; tile < BlocksCount; tile++ ) {
@ -29,11 +29,11 @@ namespace ClassicalSharp {
}
void SetHidden( byte tile, byte block, int tileSide, bool value ) {
hidden[( tile * BlocksCount + block ) * 6 + tileSide] = value;
hidden[( tile * BlocksCount + block ) * TileSide.Sides + tileSide] = value;
}
public bool IsFaceHidden( byte tile, byte block, int tileSide ) {
return hidden[( tile * BlocksCount + block ) * 6 + tileSide];
return hidden[( tile * BlocksCount + block ) * TileSide.Sides + tileSide];
}
}
}

View file

@ -4,123 +4,120 @@ namespace ClassicalSharp {
public partial class BlockInfo {
// The designation used is as follows:
// 0 - left 1 - right 2 - front
// 3 - back 4 - bottom 5 - top
int[] optimTextures = new int[BlocksCount * 6];
int[] textures = new int[BlocksCount * TileSide.Sides];
const int Row1 = 0, Row2 = 16, Row3 = 32, Row4 = 48,
Row5 = 64, Row6 = 80, Row7 = 96, Row8 = 112, Row9 = 128;
void SetupOptimTextures() {
SetOptimAllTextures( Block.Stone, Row1 + 1 );
SetOptimTopTexture( Block.Grass, Row1 );
SetOptimSideTextures( Block.Grass, Row1 + 3 );
SetOptimBottomTexture( Block.Grass, Row1 + 2 );
SetOptimAllTextures( Block.Dirt, Row1 + 2 );
SetOptimAllTextures( Block.Cobblestone, Row2 );
SetOptimAllTextures( Block.WoodenPlanks, Row1 + 4 );
SetOptimAllTextures( Block.Sapling, Row1 + 15 );
SetOptimAllTextures( Block.Bedrock, Row2 + 1 );
SetOptimAllTextures( Block.Water, Row1 + 14 );
SetOptimAllTextures( Block.StillWater, Row1 + 14 );
SetOptimAllTextures( Block.Lava, Row2 + 13 );
SetOptimAllTextures( Block.StillLava, Row2 + 13 );
SetOptimAllTextures( Block.Sand, Row2 + 2 );
SetOptimAllTextures( Block.Gravel, Row2 + 3 );
SetOptimAllTextures( Block.GoldOre, Row2 + 14 );
SetOptimAllTextures( Block.IronOre, Row2 + 15 );
SetOptimAllTextures( Block.CoalOre, Row3 );
SetOptimSideTextures( Block.Wood, Row2 + 4 );
SetOptimTopTexture( Block.Wood, Row2 + 5 );
SetOptimBottomTexture( Block.Wood, Row2 + 5 );
SetOptimAllTextures( Block.Leaves, Row2 + 6 );
SetOptimAllTextures( Block.Sponge, Row3 + 9);
SetOptimAllTextures( Block.Glass, Row3 + 10 );
SetOptimAllTextures( Block.RedCloth, Row4 + 4 );
SetOptimAllTextures( Block.OrangeCloth, Row4 + 5 );
SetOptimAllTextures( Block.YellowCloth, Row4 + 6 );
SetOptimAllTextures( Block.LimeCloth, Row4 + 7 );
SetOptimAllTextures( Block.GreenCloth, Row4 + 8 );
SetOptimAllTextures( Block.AquaCloth, Row4 + 9 );
SetOptimAllTextures( Block.CyanCloth, Row4 + 10 );
SetOptimAllTextures( Block.BlueCloth, Row4 + 11 );
SetOptimAllTextures( Block.PurpleCloth, Row4 + 12 );
SetOptimAllTextures( Block.IndigoCloth, Row4 + 13 );
SetOptimAllTextures( Block.VioletCloth, Row4 + 14 );
SetOptimAllTextures( Block.MagentaCloth, Row4 + 15 );
SetOptimAllTextures( Block.PinkCloth, Row5 );
SetOptimAllTextures( Block.BlackCloth, Row5 + 1 );
SetOptimAllTextures( Block.GrayCloth, Row5 + 2 );
SetOptimAllTextures( Block.WhiteCloth, Row5 + 3 );
SetOptimAllTextures( Block.Dandelion, Row1 + 13 );
SetOptimAllTextures( Block.Rose, Row1 + 12 );
SetOptimAllTextures( Block.BrownMushroom, Row2 + 12 );
SetOptimAllTextures( Block.RedMushroom, Row2 + 11 );
SetOptimTopTexture( Block.GoldBlock, Row2 + 8 );
SetOptimSideTextures( Block.GoldBlock, Row3 + 6 );
SetOptimBottomTexture( Block.GoldBlock, Row4 + 1 );
SetOptimTopTexture( Block.IronBlock, Row2 + 7 );
SetOptimSideTextures( Block.IronBlock, Row3 + 5 );
SetOptimBottomTexture( Block.IronBlock, Row4 );
SetOptimTopTexture( Block.DoubleSlab, Row1 + 6 );
SetOptimSideTextures( Block.DoubleSlab, Row1 + 5 );
SetOptimBottomTexture( Block.DoubleSlab, Row1 + 6 );
SetOptimTopTexture( Block.Slab, Row1 + 6 );
SetOptimSideTextures( Block.Slab, Row1 + 5 );
SetOptimBottomTexture( Block.Slab, Row1 + 6 );
SetOptimAllTextures( Block.Brick, Row1 + 7 );
SetOptimTopTexture( Block.TNT, Row1 + 9 );
SetOptimSideTextures( Block.TNT, Row1 + 8 );
SetOptimBottomTexture( Block.TNT, Row1 + 10 );
SetOptimTopTexture( Block.Bookshelf, Row1 + 4 );
SetOptimBottomTexture( Block.Bookshelf, Row1 + 4 );
SetOptimSideTextures( Block.Bookshelf, Row3 + 1 );
SetOptimAllTextures( Block.MossyCobblestone, Row3 + 2 );
SetOptimAllTextures( Block.Obsidian, Row3 + 3 );
SetOptimAll( Block.Stone, Row1 + 1 );
SetOptimTop( Block.Grass, Row1 );
SetOptimSide( Block.Grass, Row1 + 3 );
SetOptimBottom( Block.Grass, Row1 + 2 );
SetOptimAll( Block.Dirt, Row1 + 2 );
SetOptimAll( Block.Cobblestone, Row2 );
SetOptimAll( Block.WoodenPlanks, Row1 + 4 );
SetOptimAll( Block.Sapling, Row1 + 15 );
SetOptimAll( Block.Bedrock, Row2 + 1 );
SetOptimAll( Block.Water, Row1 + 14 );
SetOptimAll( Block.StillWater, Row1 + 14 );
SetOptimAll( Block.Lava, Row2 + 13 );
SetOptimAll( Block.StillLava, Row2 + 13 );
SetOptimAll( Block.Sand, Row2 + 2 );
SetOptimAll( Block.Gravel, Row2 + 3 );
SetOptimAll( Block.GoldOre, Row2 + 14 );
SetOptimAll( Block.IronOre, Row2 + 15 );
SetOptimAll( Block.CoalOre, Row3 );
SetOptimSide( Block.Wood, Row2 + 4 );
SetOptimTop( Block.Wood, Row2 + 5 );
SetOptimBottom( Block.Wood, Row2 + 5 );
SetOptimAll( Block.Leaves, Row2 + 6 );
SetOptimAll( Block.Sponge, Row3 + 9);
SetOptimAll( Block.Glass, Row3 + 10 );
SetOptimAll( Block.RedCloth, Row4 + 4 );
SetOptimAll( Block.OrangeCloth, Row4 + 5 );
SetOptimAll( Block.YellowCloth, Row4 + 6 );
SetOptimAll( Block.LimeCloth, Row4 + 7 );
SetOptimAll( Block.GreenCloth, Row4 + 8 );
SetOptimAll( Block.AquaCloth, Row4 + 9 );
SetOptimAll( Block.CyanCloth, Row4 + 10 );
SetOptimAll( Block.BlueCloth, Row4 + 11 );
SetOptimAll( Block.PurpleCloth, Row4 + 12 );
SetOptimAll( Block.IndigoCloth, Row4 + 13 );
SetOptimAll( Block.VioletCloth, Row4 + 14 );
SetOptimAll( Block.MagentaCloth, Row4 + 15 );
SetOptimAll( Block.PinkCloth, Row5 );
SetOptimAll( Block.BlackCloth, Row5 + 1 );
SetOptimAll( Block.GrayCloth, Row5 + 2 );
SetOptimAll( Block.WhiteCloth, Row5 + 3 );
SetOptimAll( Block.Dandelion, Row1 + 13 );
SetOptimAll( Block.Rose, Row1 + 12 );
SetOptimAll( Block.BrownMushroom, Row2 + 12 );
SetOptimAll( Block.RedMushroom, Row2 + 11 );
SetOptimTop( Block.GoldBlock, Row2 + 8 );
SetOptimSide( Block.GoldBlock, Row3 + 6 );
SetOptimBottom( Block.GoldBlock, Row4 + 1 );
SetOptimTop( Block.IronBlock, Row2 + 7 );
SetOptimSide( Block.IronBlock, Row3 + 5 );
SetOptimBottom( Block.IronBlock, Row4 );
SetOptimTop( Block.DoubleSlab, Row1 + 6 );
SetOptimSide( Block.DoubleSlab, Row1 + 5 );
SetOptimBottom( Block.DoubleSlab, Row1 + 6 );
SetOptimTop( Block.Slab, Row1 + 6 );
SetOptimSide( Block.Slab, Row1 + 5 );
SetOptimBottom( Block.Slab, Row1 + 6 );
SetOptimAll( Block.Brick, Row1 + 7 );
SetOptimTop( Block.TNT, Row1 + 9 );
SetOptimSide( Block.TNT, Row1 + 8 );
SetOptimBottom( Block.TNT, Row1 + 10 );
SetOptimTop( Block.Bookshelf, Row1 + 4 );
SetOptimBottom( Block.Bookshelf, Row1 + 4 );
SetOptimSide( Block.Bookshelf, Row3 + 1 );
SetOptimAll( Block.MossyCobblestone, Row3 + 2 );
SetOptimAll( Block.Obsidian, Row3 + 3 );
// CPE blocks
SetOptimAllTextures( Block.CobblestoneSlab, Row2 + 0 );
SetOptimAllTextures( Block.Rope, Row1 + 11 );
SetOptimTopTexture( Block.Sandstone, Row2 + 9 );
SetOptimSideTextures( Block.Sandstone, Row3 + 7 );
SetOptimBottomTexture( Block.Sandstone, Row4 + 2 );
SetOptimAllTextures( Block.Snow, Row3 + 11 );
SetOptimAllTextures( Block.Fire, Row3 + 4 );
SetOptimAllTextures( Block.LightPinkWool, Row5 + 4 );
SetOptimAllTextures( Block.ForestGreenWool, Row5 + 5 );
SetOptimAllTextures( Block.BrownWool, Row5 + 6 );
SetOptimAllTextures( Block.DeepBlueWool, Row5 + 7 );
SetOptimAllTextures( Block.TurquoiseWool, Row5 + 8 );
SetOptimAllTextures( Block.Ice, Row3 + 12 );
SetOptimAllTextures( Block.CeramicTile, Row3 + 15 );
SetOptimAllTextures( Block.Magma, Row5 + 9 );
SetOptimTopTexture( Block.Pillar, Row2 + 10 );
SetOptimSideTextures( Block.Pillar, Row3 + 8 );
SetOptimBottomTexture( Block.Pillar, Row4 + 3 );
SetOptimAllTextures( Block.Crate, Row3 + 14 );
SetOptimAllTextures( Block.StoneBrick, Row3 + 13 );
SetOptimAll( Block.CobblestoneSlab, Row2 + 0 );
SetOptimAll( Block.Rope, Row1 + 11 );
SetOptimTop( Block.Sandstone, Row2 + 9 );
SetOptimSide( Block.Sandstone, Row3 + 7 );
SetOptimBottom( Block.Sandstone, Row4 + 2 );
SetOptimAll( Block.Snow, Row3 + 11 );
SetOptimAll( Block.Fire, Row3 + 4 );
SetOptimAll( Block.LightPinkWool, Row5 + 4 );
SetOptimAll( Block.ForestGreenWool, Row5 + 5 );
SetOptimAll( Block.BrownWool, Row5 + 6 );
SetOptimAll( Block.DeepBlueWool, Row5 + 7 );
SetOptimAll( Block.TurquoiseWool, Row5 + 8 );
SetOptimAll( Block.Ice, Row3 + 12 );
SetOptimAll( Block.CeramicTile, Row3 + 15 );
SetOptimAll( Block.Magma, Row5 + 9 );
SetOptimTop( Block.Pillar, Row2 + 10 );
SetOptimSide( Block.Pillar, Row3 + 8 );
SetOptimBottom( Block.Pillar, Row4 + 3 );
SetOptimAll( Block.Crate, Row3 + 14 );
SetOptimAll( Block.StoneBrick, Row3 + 13 );
}
void SetOptimAllTextures( Block blockId, int textureId ) {
int index = (byte)blockId * 6;
for( int i = index; i < index + 6; i++ ) {
optimTextures[i] = textureId;
void SetOptimAll( Block blockId, int textureId ) {
int index = (byte)blockId * TileSide.Sides;
for( int i = index; i < index + TileSide.Sides; i++ ) {
textures[i] = textureId;
}
}
void SetOptimSideTextures( Block blockId, int textureId ) {
int index = (byte)blockId * 6;
for( int i = index; i < index + TileSide.Top; i++ ) {
optimTextures[i] = textureId;
void SetOptimSide( Block blockId, int textureId ) {
int index = (byte)blockId * TileSide.Sides;
for( int i = index; i < index + TileSide.Bottom; i++ ) {
textures[i] = textureId;
}
}
void SetOptimTopTexture( Block blockId, int textureId ) {
optimTextures[(byte)blockId * 6 + TileSide.Top] = textureId;
void SetOptimTop( Block blockId, int textureId ) {
textures[(byte)blockId * TileSide.Sides + TileSide.Top] = textureId;
}
void SetOptimBottomTexture( Block blockId, int textureId ) {
optimTextures[(byte)blockId * 6 + TileSide.Bottom] = textureId;
void SetOptimBottom( Block blockId, int textureId ) {
textures[(byte)blockId * TileSide.Sides + TileSide.Bottom] = textureId;
}
/// <summary> Gets the index in the ***optimised*** 2D terrain atlas for the
@ -129,7 +126,7 @@ namespace ClassicalSharp {
/// <param name="face">Face of the given block, see TileSide constants. </param>
/// <returns>The index of the texture within the terrain atlas.</returns>
public int GetOptimTextureLoc( byte block, int face ) {
return optimTextures[block * 6 + face];
return textures[block * TileSide.Sides + face];
}
}
}

View file

@ -24,7 +24,7 @@ namespace ClassicalSharp {
int width, length, height;
int maxX, maxY, maxZ;
byte[] counts = new byte[chunkSize3 * 6];
byte[] counts = new byte[chunkSize3 * TileSide.Sides];
byte[] chunk = new byte[extChunkSize3];
bool BuildChunk( int x1, int y1, int z1 ) {
@ -72,7 +72,7 @@ namespace ClassicalSharp {
if( z > maxZ ) break;
int index = ( y * length + z ) * width + ( x1 - 1 - 1 );
int chunkIndex = ( yy + 1 ) * extChunkSize2 + ( zz + 1 ) * extChunkSize + ( -1 );
int chunkIndex = ( yy + 1 ) * extChunkSize2 + ( zz + 1 ) * extChunkSize + ( -1 + 1 ) - 1;
for( int xx = -1; xx < 17; xx++ ) {
int x = xx + x1;
@ -112,11 +112,11 @@ namespace ClassicalSharp {
public void RenderTile( int chunkIndex, int xx, int yy, int zz, int x, int y, int z ) {
X = x; Y = y; Z = z;
blockHeight = -1;
int index = ( ( yy << 8 ) + ( zz << 4 ) + xx ) * 6;
int index = ( ( yy << 8 ) + ( zz << 4 ) + xx ) * TileSide.Sides;
int count = 0;
if( BlockInfo.IsSprite( tile ) ) {
count = counts[index + TileSide.Top];
count = counts[index];
if( count != 0 ) {
DrawSprite( count );
}
@ -165,25 +165,25 @@ namespace ClassicalSharp {
chunkIndex++;
byte tile = chunk[chunkIndex];
if( tile == 0 ) continue;
int countIndex = ( ( yy << 8 ) + ( zz << 4 ) + xx ) * 6;
int countIndex = ( ( yy << 8 ) + ( zz << 4 ) + xx ) * TileSide.Sides;
// Sprites only use the top face to indicate stretching count, so we can take a shortcut here.
// Sprites only use one face to indicate stretching count, so we can take a shortcut here.
// Note that sprites are not drawn with any of the DrawXFace, they are drawn using DrawSprite.
if( BlockInfo.IsSprite( tile ) ) {
if( counts[countIndex + TileSide.Top] != 0 ) {
if( counts[countIndex] != 0 ) {
X = x; Y = y; Z = z;
int count = StretchX( xx, countIndex, x, y, z, chunkIndex, tile, TileSide.Top );
int count = StretchX( xx, countIndex, x, y, z, chunkIndex, tile, 0 );
AddSpriteVertices( tile, count );
counts[countIndex + TileSide.Top] = (byte)count;
counts[countIndex] = (byte)count;
}
} else {
X = x; Y = y; Z = z;
TestAndStretchZ( zz, countIndex, tile, chunkIndex, x, 0, TileSide.Left, -1 );
TestAndStretchZ( zz, countIndex, tile, chunkIndex, x, maxX, TileSide.Right, 1 );
TestAndStretchX( xx, countIndex, tile, chunkIndex, z, 0, TileSide.Front, -18 );
TestAndStretchX( xx, countIndex, tile, chunkIndex, z, maxZ, TileSide.Back, 18 );
TestAndStretchX( xx, countIndex, tile, chunkIndex, y, 0, TileSide.Bottom, -324 );
TestAndStretchX( xx, countIndex, tile, chunkIndex, y, maxY + 2, TileSide.Top, 324 );
TestAndStretchX( xx, countIndex, tile, chunkIndex, z, 0, TileSide.Front, -extChunkSize );
TestAndStretchX( xx, countIndex, tile, chunkIndex, z, maxZ, TileSide.Back, extChunkSize );
TestAndStretchX( xx, countIndex, tile, chunkIndex, y, 0, TileSide.Bottom, -extChunkSize2 );
TestAndStretchX( xx, countIndex, tile, chunkIndex, y, maxY + 2, TileSide.Top, extChunkSize2 );
}
}
}
@ -191,29 +191,31 @@ namespace ClassicalSharp {
}
void TestAndStretchX( int xx, int index, byte tile, int chunkIndex, int value, int test, int tileSide, int offset ) {
index += tileSide;
if( value == test ) {
counts[index + tileSide] = 0;
} else if( counts[index + tileSide] != 0 ) {
counts[index] = 0;
} else if( counts[index] != 0 ) {
if( BlockInfo.IsFaceHidden( tile, chunk[chunkIndex + offset], tileSide ) ) {
counts[index + tileSide] = 0;
counts[index] = 0;
} else {
int count = StretchX( xx, index, X, Y, Z, chunkIndex, tile, tileSide );
AddVertices( tile, count, tileSide );
counts[index + tileSide] = (byte)count;
counts[index] = (byte)count;
}
}
}
void TestAndStretchZ( int zz, int index, byte tile, int chunkIndex, int value, int test, int tileSide, int offset ) {
index += tileSide;
if( value == test ) {
counts[index + tileSide] = 0;
} else if( counts[index + tileSide] != 0 ) {
counts[index] = 0;
} else if( counts[index] != 0 ) {
if( BlockInfo.IsFaceHidden( tile, chunk[chunkIndex + offset], tileSide ) ) {
counts[index + tileSide] = 0;
counts[index] = 0;
} else {
int count = StretchZ( zz, index, X, Y, Z, chunkIndex, tile, tileSide );
AddVertices( tile, count, tileSide );
counts[index + tileSide] = (byte)count;
counts[index] = (byte)count;
}
}
}
@ -245,14 +247,14 @@ namespace ClassicalSharp {
int count = 1;
x++;
chunkIndex++;
countIndex += 6;
countIndex += TileSide.Sides;
int max = chunkSize - xx;
while( count < max && x < width && CanStretch( tile, chunkIndex, x, y, z, face ) ) {
while( count < max && x < width && CanStretch( tile, chunkIndex, x, y, z, face ) ) {
counts[countIndex] = 0;
count++;
counts[countIndex + face] = 0;
x++;
chunkIndex++;
countIndex += 6;
countIndex += TileSide.Sides;
}
return count;
}
@ -261,14 +263,14 @@ namespace ClassicalSharp {
int count = 1;
z++;
chunkIndex += extChunkSize;
countIndex += chunkSize * 6;
countIndex += chunkSize * TileSide.Sides;
int max = chunkSize - zz;
while( count < max && z < length && CanStretch( tile, chunkIndex, x, y, z, face ) ) {
while( count < max && z < length && CanStretch( tile, chunkIndex, x, y, z, face ) ) {
counts[countIndex] = 0;
count++;
counts[countIndex + face] = 0;
z++;
chunkIndex += extChunkSize;
countIndex += chunkSize * 6;
countIndex += chunkSize * TileSide.Sides;
}
return count;
}

View file

@ -11,6 +11,7 @@ namespace ClassicalSharp {
public const int Back = 3;
public const int Bottom = 4;
public const int Top = 5;
public const int Sides = 6;
}
public class TerrainAtlas2D : IDisposable {