fix tinting

This commit is contained in:
UnknownShadow200 2017-02-07 16:33:39 +11:00
parent 73d4cf227e
commit a2aa233759
5 changed files with 73 additions and 63 deletions

View File

@ -18,7 +18,7 @@ namespace ClassicalSharp {
VertexP3fT2fC4b[] vertices;
int vb;
NormalBlockBuilder drawer = new NormalBlockBuilder();
CuboidDrawer drawer = new CuboidDrawer();
public void BeginBatch(Game game, VertexP3fT2fC4b[] vertices, int vb) {
this.game = game;
@ -51,7 +51,6 @@ namespace ClassicalSharp {
atlas = game.TerrainAtlas1D;
drawer.elementsPerAtlas1D = atlas.elementsPerAtlas1D;
drawer.invVerElementSize = atlas.invElementSize;
drawer.info = game.BlockInfo;
bright = info.FullBright[block];
if (info.Draw[block] == DrawType.Gas) return;
@ -74,17 +73,20 @@ namespace ClassicalSharp {
SpriteZQuad(block, false);
SpriteXQuad(block, false);
} else {
drawer.minBB = info.MinBB[block]; drawer.maxBB = info.MaxBB[block];
drawer.minBB.Y = 1 - drawer.minBB.Y; drawer.maxBB.Y = 1 - drawer.maxBB.Y;
drawer.minBB = info.MinBB[block]; drawer.minBB.Y = 1 - drawer.minBB.Y;
drawer.maxBB = info.MaxBB[block]; drawer.maxBB.Y = 1 - drawer.maxBB.Y;
Vector3 min = game.BlockInfo.MinBB[block], max = game.BlockInfo.MaxBB[block];
Vector3 min = info.MinBB[block], max = info.MaxBB[block];
drawer.x1 = scale * (1 - min.X * 2) + pos.X; drawer.x2 = scale * (1 - max.X * 2) + pos.X;
drawer.y1 = scale * (1 - min.Y * 2) + pos.Y; drawer.y2 = scale * (1 - max.Y * 2) + pos.Y;
drawer.z1 = scale * (1 - min.Z * 2) + pos.Z; drawer.z2 = scale * (1 - max.Z * 2) + pos.Z;
drawer.DrawRightFace(1, bright ? colNormal : colXSide, GetTex(block, Side.Right), vertices, ref index);
drawer.DrawFrontFace(1, bright ? colNormal : colZSide, GetTex(block, Side.Front), vertices, ref index);
drawer.DrawTopFace(1, colNormal , GetTex(block, Side.Top), vertices, ref index);
drawer.Tinted = info.Tinted[block];
drawer.TintColour = info.FogColour[block];
drawer.Right(1, bright ? colNormal : colXSide, GetTex(block, Side.Right), vertices, ref index);
drawer.Front(1, bright ? colNormal : colZSide, GetTex(block, Side.Front), vertices, ref index);
drawer.Top(1, colNormal , GetTex(block, Side.Top), vertices, ref index);
}
}

View File

@ -204,7 +204,7 @@
<Compile Include="Entities\Model\ModelBuilder.cs" />
<Compile Include="MeshBuilder\Builder.cs" />
<Compile Include="MeshBuilder\FloodFill.cs" />
<Compile Include="MeshBuilder\NormalBlockBuilder.cs" />
<Compile Include="MeshBuilder\CuboidDrawer.cs" />
<Compile Include="MeshBuilder\NormalBuilder.cs" />
<Compile Include="MeshBuilder\AdvLightingBuilder.cs" />
<Compile Include="MeshBuilder\TileDrawer.cs" />

View File

@ -139,7 +139,7 @@ namespace ClassicalSharp.Model {
}
}
NormalBlockBuilder drawer = new NormalBlockBuilder();
CuboidDrawer drawer = new CuboidDrawer();
void DrawParts(bool sprite) {
// SwitchOrder is needed for held block, which renders without depth testing
if (sprite) {
@ -161,31 +161,31 @@ namespace ClassicalSharp.Model {
} else {
drawer.elementsPerAtlas1D = atlas.elementsPerAtlas1D;
drawer.invVerElementSize = atlas.invElementSize;
drawer.info = game.BlockInfo;
drawer.minBB = game.BlockInfo.MinBB[block];
drawer.maxBB = game.BlockInfo.MaxBB[block];
drawer.minBB.Y = 1 - drawer.minBB.Y;
drawer.maxBB.Y = 1 - drawer.maxBB.Y;
drawer.minBB = game.BlockInfo.MinBB[block]; drawer.minBB.Y = 1 - drawer.minBB.Y;
drawer.maxBB = game.BlockInfo.MaxBB[block]; drawer.maxBB.Y = 1 - drawer.maxBB.Y;
Vector3 min = game.BlockInfo.RenderMinBB[block];
Vector3 max = game.BlockInfo.RenderMaxBB[block];
drawer.x1 = min.X - 0.5f; drawer.y1 = min.Y; drawer.z1 = min.Z - 0.5f;
drawer.x2 = max.X - 0.5f; drawer.y2 = max.Y; drawer.z2 = max.Z - 0.5f;
drawer.DrawBottomFace(1, Colour(FastColour.ShadeYBottom), GetTex(Side.Bottom), cache.vertices, ref index);
drawer.Tinted = game.BlockInfo.Tinted[block];
drawer.TintColour = game.BlockInfo.FogColour[block];
drawer.Bottom(1, Colour(FastColour.ShadeYBottom), GetTex(Side.Bottom), cache.vertices, ref index);
if (SwitchOrder) {
drawer.DrawRightFace(1, Colour(FastColour.ShadeX), GetTex(Side.Right), cache.vertices, ref index);
drawer.DrawBackFace(1, Colour(FastColour.ShadeZ), GetTex(Side.Back), cache.vertices, ref index);
drawer.DrawLeftFace(1, Colour(FastColour.ShadeX), GetTex(Side.Left), cache.vertices, ref index);
drawer.DrawFrontFace(1, Colour(FastColour.ShadeZ), GetTex(Side.Front), cache.vertices, ref index);
drawer.Right(1, Colour(FastColour.ShadeX), GetTex(Side.Right), cache.vertices, ref index);
drawer.Back(1, Colour(FastColour.ShadeZ), GetTex(Side.Back), cache.vertices, ref index);
drawer.Left(1, Colour(FastColour.ShadeX), GetTex(Side.Left), cache.vertices, ref index);
drawer.Front(1, Colour(FastColour.ShadeZ), GetTex(Side.Front), cache.vertices, ref index);
} else {
drawer.DrawFrontFace(1, Colour(FastColour.ShadeZ), GetTex(Side.Front), cache.vertices, ref index);
drawer.DrawRightFace(1, Colour(FastColour.ShadeX), GetTex(Side.Right), cache.vertices, ref index);
drawer.DrawBackFace(1, Colour(FastColour.ShadeZ), GetTex(Side.Back), cache.vertices, ref index);
drawer.DrawLeftFace(1, Colour(FastColour.ShadeX), GetTex(Side.Left), cache.vertices, ref index);
drawer.Front(1, Colour(FastColour.ShadeZ), GetTex(Side.Front), cache.vertices, ref index);
drawer.Right(1, Colour(FastColour.ShadeX), GetTex(Side.Right), cache.vertices, ref index);
drawer.Back(1, Colour(FastColour.ShadeZ), GetTex(Side.Back), cache.vertices, ref index);
drawer.Left(1, Colour(FastColour.ShadeX), GetTex(Side.Left), cache.vertices, ref index);
}
drawer.DrawTopFace(1, Colour(1.0f), GetTex(Side.Top), cache.vertices, ref index);
drawer.Top(1, Colour(1.0f), GetTex(Side.Top), cache.vertices, ref index);
}
}

View File

@ -5,24 +5,29 @@ using OpenTK;
namespace ClassicalSharp {
public unsafe sealed class NormalBlockBuilder {
public BlockInfo info;
/// <summary> Draws the vertices for a cuboid region. </summary>
public unsafe sealed class CuboidDrawer {
public int elementsPerAtlas1D;
public float invVerElementSize;
public byte curBlock;
public bool tinted;
/// <summary> Whether a colour tinting effect should be applied to all faces. </summary>
public bool Tinted;
/// <summary> The tint colour to multiply colour of faces by. </summary>
public FastColour TintColour;
public Vector3 minBB, maxBB;
public float x1, y1, z1, x2, y2, z2;
public void DrawLeftFace(int count, int col, int texId, VertexP3fT2fC4b[] vertices, ref int index) {
/// <summary> Draws the left face of the given cuboid region. </summary>
public void Left(int count, int col, int texId, VertexP3fT2fC4b[] vertices, ref int index) {
float vOrigin = (texId % elementsPerAtlas1D) * invVerElementSize;
float u1 = minBB.Z, u2 = (count - 1) + maxBB.Z * 15.99f/16f;
float v1 = vOrigin + maxBB.Y * invVerElementSize;
float v2 = vOrigin + minBB.Y * invVerElementSize * 15.99f/16f;
if (tinted) col = TintBlock(curBlock, col);
if (Tinted) col = TintBlock(col);
vertices[index++] = new VertexP3fT2fC4b(x1, y2, z2 + (count - 1), u2, v1, col);
vertices[index++] = new VertexP3fT2fC4b(x1, y2, z1, u1, v1, col);
@ -30,12 +35,13 @@ namespace ClassicalSharp {
vertices[index++] = new VertexP3fT2fC4b(x1, y1, z2 + (count - 1), u2, v2, col);
}
public void DrawRightFace(int count, int col, int texId, VertexP3fT2fC4b[] vertices, ref int index) {
/// <summary> Draws the right face of the given cuboid region. </summary>
public void Right(int count, int col, int texId, VertexP3fT2fC4b[] vertices, ref int index) {
float vOrigin = (texId % elementsPerAtlas1D) * invVerElementSize;
float u1 = (count - minBB.Z), u2 = (1 - maxBB.Z) * 15.99f/16f;
float v1 = vOrigin + maxBB.Y * invVerElementSize;
float v2 = vOrigin + minBB.Y * invVerElementSize * 15.99f/16f;
if (tinted) col = TintBlock(curBlock, col);
if (Tinted) col = TintBlock(col);
vertices[index++] = new VertexP3fT2fC4b(x2, y2, z1, u1, v1, col);
vertices[index++] = new VertexP3fT2fC4b(x2, y2, z2 + (count - 1), u2, v1, col);
@ -43,12 +49,13 @@ namespace ClassicalSharp {
vertices[index++] = new VertexP3fT2fC4b(x2, y1, z1, u1, v2, col);
}
public void DrawFrontFace(int count, int col, int texId, VertexP3fT2fC4b[] vertices, ref int index) {
/// <summary> Draws the front face of the given cuboid region. </summary>
public void Front(int count, int col, int texId, VertexP3fT2fC4b[] vertices, ref int index) {
float vOrigin = (texId % elementsPerAtlas1D) * invVerElementSize;
float u1 = (count - minBB.X), u2 = (1 - maxBB.X) * 15.99f/16f;
float v1 = vOrigin + maxBB.Y * invVerElementSize;
float v2 = vOrigin + minBB.Y * invVerElementSize * 15.99f/16f;
if (tinted) col = TintBlock(curBlock, col);
if (Tinted) col = TintBlock(col);
vertices[index++] = new VertexP3fT2fC4b(x2 + (count - 1), y1, z1, u2, v2, col);
vertices[index++] = new VertexP3fT2fC4b(x1, y1, z1, u1, v2, col);
@ -56,12 +63,13 @@ namespace ClassicalSharp {
vertices[index++] = new VertexP3fT2fC4b(x2 + (count - 1), y2, z1, u2, v1, col);
}
public void DrawBackFace(int count, int col, int texId, VertexP3fT2fC4b[] vertices, ref int index) {
/// <summary> Draws the back face of the given cuboid region. </summary>
public void Back(int count, int col, int texId, VertexP3fT2fC4b[] vertices, ref int index) {
float vOrigin = (texId % elementsPerAtlas1D) * invVerElementSize;
float u1 = minBB.X, u2 = (count - 1) + maxBB.X * 15.99f/16f;
float v1 = vOrigin + maxBB.Y * invVerElementSize;
float v2 = vOrigin + minBB.Y * invVerElementSize * 15.99f/16f;
if (tinted) col = TintBlock(curBlock, col);
if (Tinted) col = TintBlock(col);
vertices[index++] = new VertexP3fT2fC4b(x2 + (count - 1), y2, z2, u2, v1, col);
vertices[index++] = new VertexP3fT2fC4b(x1, y2, z2, u1, v1, col);
@ -69,12 +77,13 @@ namespace ClassicalSharp {
vertices[index++] = new VertexP3fT2fC4b(x2 + (count - 1), y1, z2, u2, v2, col);
}
public void DrawBottomFace(int count, int col, int texId, VertexP3fT2fC4b[] vertices, ref int index) {
/// <summary> Draws the bottom face of the given cuboid region. </summary>
public void Bottom(int count, int col, int texId, VertexP3fT2fC4b[] vertices, ref int index) {
float vOrigin = (texId % elementsPerAtlas1D) * invVerElementSize;
float u1 = minBB.X, u2 = (count - 1) + maxBB.X * 15.99f/16f;
float v1 = vOrigin + minBB.Z * invVerElementSize;
float v2 = vOrigin + maxBB.Z * invVerElementSize * 15.99f/16f;
if (tinted) col = TintBlock(curBlock, col);
if (Tinted) col = TintBlock(col);
vertices[index++] = new VertexP3fT2fC4b(x2 + (count - 1), y1, z2, u2, v2, col);
vertices[index++] = new VertexP3fT2fC4b(x1, y1, z2, u1, v2, col);
@ -82,12 +91,13 @@ namespace ClassicalSharp {
vertices[index++] = new VertexP3fT2fC4b(x2 + (count - 1), y1, z1, u2, v1, col);
}
public void DrawTopFace(int count, int col, int texId, VertexP3fT2fC4b[] vertices, ref int index) {
/// <summary> Draws the top face of the given cuboid region. </summary>
public void Top(int count, int col, int texId, VertexP3fT2fC4b[] vertices, ref int index) {
float vOrigin = (texId % elementsPerAtlas1D) * invVerElementSize;
float u1 = minBB.X, u2 = (count - 1) + maxBB.X * 15.99f/16f;
float v1 = vOrigin + minBB.Z * invVerElementSize;
float v2 = vOrigin + maxBB.Z * invVerElementSize * 15.99f/16f;
if (tinted) col = TintBlock(curBlock, col);
if (Tinted) col = TintBlock(col);
vertices[index++] = new VertexP3fT2fC4b(x2 + (count - 1), y2, z1, u2, v1, col);
vertices[index++] = new VertexP3fT2fC4b(x1, y2, z1, u1, v1, col);
@ -95,11 +105,10 @@ namespace ClassicalSharp {
vertices[index++] = new VertexP3fT2fC4b(x2 + (count - 1), y2, z2, u2, v2, col);
}
int TintBlock(byte curBlock, int col) {
FastColour fogCol = info.FogColour[curBlock];
FastColour newCol = FastColour.Unpack(col);
newCol *= fogCol;
return newCol.Pack();
int TintBlock(int col) {
FastColour rgbCol = FastColour.Unpack(col);
rgbCol *= TintColour;
return rgbCol.Pack();
}
}
}

View File

@ -8,7 +8,7 @@ namespace ClassicalSharp {
public unsafe sealed class NormalMeshBuilder : ChunkMeshBuilder {
NormalBlockBuilder drawer = new NormalBlockBuilder();
CuboidDrawer drawer = new CuboidDrawer();
protected override int StretchXLiquid(int xx, int countIndex, int x, int y, int z, int chunkIndex, byte block) {
if (OccludedLiquid(chunkIndex)) return 0;
@ -76,13 +76,12 @@ namespace ClassicalSharp {
base.PreStretchTiles(x1, y1, z1);
drawer.invVerElementSize = invVerElementSize;
drawer.elementsPerAtlas1D = elementsPerAtlas1D;
drawer.info = game.BlockInfo;
}
protected override void RenderTile(int index) {
if (info.Draw[curBlock] == DrawType.Sprite) {
fullBright = info.FullBright[curBlock];
tinted = info.Tinted[curBlock];
this.fullBright = info.FullBright[curBlock];
this.tinted = info.Tinted[curBlock];
int count = counts[index + Side.Top];
if (count != 0) DrawSprite(count);
return;
@ -94,19 +93,19 @@ namespace ClassicalSharp {
if (leftCount == 0 && rightCount == 0 && frontCount == 0 &&
backCount == 0 && bottomCount == 0 && topCount == 0) return;
fullBright = info.FullBright[curBlock];
bool fullBright = info.FullBright[curBlock];
bool isTranslucent = info.Draw[curBlock] == DrawType.Translucent;
int lightFlags = info.LightOffset[curBlock];
tinted = info.Tinted[curBlock];
drawer.minBB = info.MinBB[curBlock]; drawer.minBB.Y = 1 - drawer.minBB.Y;
drawer.maxBB = info.MaxBB[curBlock]; drawer.maxBB.Y = 1 - drawer.maxBB.Y;
Vector3 min = info.RenderMinBB[curBlock], max = info.RenderMaxBB[curBlock];
drawer.x1 = X + min.X; drawer.y1 = Y + min.Y; drawer.z1 = Z + min.Z;
drawer.x2 = X + max.X; drawer.y2 = Y + max.Y; drawer.z2 = Z + max.Z;
drawer.minBB = info.MinBB[curBlock];
drawer.maxBB = info.MaxBB[curBlock];
drawer.minBB.Y = 1 - drawer.minBB.Y;
drawer.maxBB.Y = 1 - drawer.maxBB.Y;
drawer.Tinted = game.BlockInfo.Tinted[curBlock];
drawer.TintColour = game.BlockInfo.FogColour[curBlock];
if (leftCount != 0) {
int texId = info.textures[curBlock * Side.Sides + Side.Left];
@ -116,7 +115,7 @@ namespace ClassicalSharp {
DrawInfo part = isTranslucent ? translucentParts[i] : normalParts[i];
int col = fullBright ? FastColour.WhitePacked :
X >= offset ? lighting.LightCol_XSide_Fast(X - offset, Y, Z) : lighting.OutsideXSide;
drawer.DrawLeftFace(leftCount, col, texId, part.vertices, ref part.vIndex[Side.Left]);
drawer.Left(leftCount, col, texId, part.vertices, ref part.vIndex[Side.Left]);
}
if (rightCount != 0) {
@ -127,7 +126,7 @@ namespace ClassicalSharp {
DrawInfo part = isTranslucent ? translucentParts[i] : normalParts[i];
int col = fullBright ? FastColour.WhitePacked :
X <= (maxX - offset) ? lighting.LightCol_XSide_Fast(X + offset, Y, Z) : lighting.OutsideXSide;
drawer.DrawRightFace(rightCount, col, texId, part.vertices, ref part.vIndex[Side.Right]);
drawer.Right(rightCount, col, texId, part.vertices, ref part.vIndex[Side.Right]);
}
if (frontCount != 0) {
@ -138,7 +137,7 @@ namespace ClassicalSharp {
DrawInfo part = isTranslucent ? translucentParts[i] : normalParts[i];
int col = fullBright ? FastColour.WhitePacked :
Z >= offset ? lighting.LightCol_ZSide_Fast(X, Y, Z - offset) : lighting.OutsideZSide;
drawer.DrawFrontFace(frontCount, col, texId, part.vertices, ref part.vIndex[Side.Front]);
drawer.Front(frontCount, col, texId, part.vertices, ref part.vIndex[Side.Front]);
}
if (backCount != 0) {
@ -149,7 +148,7 @@ namespace ClassicalSharp {
DrawInfo part = isTranslucent ? translucentParts[i] : normalParts[i];
int col = fullBright ? FastColour.WhitePacked :
Z <= (maxZ - offset) ? lighting.LightCol_ZSide_Fast(X, Y, Z + offset) : lighting.OutsideZSide;
drawer.DrawBackFace(backCount, col, texId, part.vertices, ref part.vIndex[Side.Back]);
drawer.Back(backCount, col, texId, part.vertices, ref part.vIndex[Side.Back]);
}
if (bottomCount != 0) {
@ -159,7 +158,7 @@ namespace ClassicalSharp {
DrawInfo part = isTranslucent ? translucentParts[i] : normalParts[i];
int col = fullBright ? FastColour.WhitePacked : lighting.LightCol_YBottom_Fast(X, Y - offset, Z);
drawer.DrawBottomFace(bottomCount, col, texId, part.vertices, ref part.vIndex[Side.Bottom]);
drawer.Bottom(bottomCount, col, texId, part.vertices, ref part.vIndex[Side.Bottom]);
}
if (topCount != 0) {
@ -169,7 +168,7 @@ namespace ClassicalSharp {
DrawInfo part = isTranslucent ? translucentParts[i] : normalParts[i];
int col = fullBright ? FastColour.WhitePacked : lighting.LightCol_YTop_Fast(X, Y - offset, Z);
drawer.DrawTopFace(topCount, col, texId, part.vertices, ref part.vIndex[Side.Top]);
drawer.Top(topCount, col, texId, part.vertices, ref part.vIndex[Side.Top]);
}
}
}