Skip to content
This repository has been archived by the owner on Nov 25, 2024. It is now read-only.

Commit

Permalink
Pass in uvmin and uvmax to shader to keep within atlas texture
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver-makes-code committed Nov 27, 2023
1 parent 8945c35 commit 82ab3dc
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 53 deletions.
5 changes: 4 additions & 1 deletion Client/Content/shaders/simple.frag.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
layout(location = 0) in vec2 fsin_texCoords;
layout(location = 1) in vec4 fsin_Color;
layout(location = 2) in float fsin_Distance;
layout(location = 3) in vec2 fsin_UvMin;
layout(location = 4) in vec2 fsin_UvMax;

layout(location = 0) out vec4 fsout_Color;

Expand All @@ -21,7 +23,8 @@ void main() {
vec2 tfract = fract(tx);
vec2 txOffset = smoothstep(1 - boxSize, vec2(1), tfract);

vec2 newUv = clamp((tx - tfract + 0.5 + txOffset) * texSize, 0, 1);
vec2 newUv = clamp((tx - tfract + 0.5 + txOffset) * texSize, fsin_UvMin + texSize * 0.5, fsin_UvMax - texSize * 0.5);

vec4 sampledColor = textureGrad(sampler2D(Texture, TextureSampler), newUv, dFdx(newUv), dFdy(newUv));
fsout_Color = sampledColor * fsin_Color;
}
6 changes: 6 additions & 0 deletions Client/Content/shaders/simple.vert.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,14 @@ layout(location = 0) in vec3 Position;
layout(location = 1) in int PackedColor;
layout(location = 2) in int PackedUV;
layout(location = 3) in float AmbientOcclusion;
layout(location = 4) in vec2 UvMin;
layout(location = 5) in vec2 UvMax;

layout(location = 0) out vec2 fsin_texCoords;
layout(location = 1) out vec4 fsin_Color;
layout(location = 2) out float fsin_Distance;
layout(location = 3) out vec2 fsin_UvMin;
layout(location = 4) out vec2 fsin_UvMax;

struct UnpackedVertex{
vec4 color;
Expand Down Expand Up @@ -63,4 +67,6 @@ void main() {

fsin_Color = up.color * vec4(getColorMultiplier(1-AmbientOcclusion, vec3(0.9, 0.9, 1)), 1);
fsin_Distance = pos.z;
fsin_UvMin = UvMin;
fsin_UvMax = UvMax;
}
96 changes: 48 additions & 48 deletions Client/Rendering/Models/BlockModelManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,79 +33,79 @@ public static class BlockModelManager {
public static BlockModel GetDefault(Atlas.Sprite sprite) {
return new BlockModel.Builder()
//Left
.AddVertex(0, new(new(0, 0, 0), LeftColor, sprite.GetTrueUV(new vec2(0, 1)), new(0, 0)))
.AddVertex(0, new(new(0, 0, 1), LeftColor, sprite.GetTrueUV(new vec2(1, 1)), new(1, 0)))
.AddVertex(0, new(new(0, 1, 1), LeftColor, sprite.GetTrueUV(new vec2(1, 0)), new(1, 1)))
.AddVertex(0, new(new(0, 1, 0), LeftColor, sprite.GetTrueUV(new vec2(0, 0)), new(0, 1)))
.AddVertex(0, new(new(0, 0, 0), LeftColor, sprite.GetTrueUV(new vec2(0, 1)), new(0, 0), sprite))
.AddVertex(0, new(new(0, 0, 1), LeftColor, sprite.GetTrueUV(new vec2(1, 1)), new(1, 0), sprite))
.AddVertex(0, new(new(0, 1, 1), LeftColor, sprite.GetTrueUV(new vec2(1, 0)), new(1, 1), sprite))
.AddVertex(0, new(new(0, 1, 0), LeftColor, sprite.GetTrueUV(new vec2(0, 0)), new(0, 1), sprite))

//Right
.AddVertex(1, new(new(1, 0, 0), RightColor, sprite.GetTrueUV(new vec2(1, 1)), new(0, 0)))
.AddVertex(1, new(new(1, 1, 0), RightColor, sprite.GetTrueUV(new vec2(1, 0)), new(1, 0)))
.AddVertex(1, new(new(1, 1, 1), RightColor, sprite.GetTrueUV(new vec2(0, 0)), new(1, 1)))
.AddVertex(1, new(new(1, 0, 1), RightColor, sprite.GetTrueUV(new vec2(0, 1)), new(0, 1)))
.AddVertex(1, new(new(1, 0, 0), RightColor, sprite.GetTrueUV(new vec2(1, 1)), new(0, 0), sprite))
.AddVertex(1, new(new(1, 1, 0), RightColor, sprite.GetTrueUV(new vec2(1, 0)), new(1, 0), sprite))
.AddVertex(1, new(new(1, 1, 1), RightColor, sprite.GetTrueUV(new vec2(0, 0)), new(1, 1), sprite))
.AddVertex(1, new(new(1, 0, 1), RightColor, sprite.GetTrueUV(new vec2(0, 1)), new(0, 1), sprite))

//Bottom
.AddVertex(2, new(new(0, 0, 0), BottomColor, sprite.GetTrueUV(new vec2(0, 1)), new(0, 0)))
.AddVertex(2, new(new(1, 0, 0), BottomColor, sprite.GetTrueUV(new vec2(1, 1)), new(1, 0)))
.AddVertex(2, new(new(1, 0, 1), BottomColor, sprite.GetTrueUV(new vec2(1, 0)), new(1, 1)))
.AddVertex(2, new(new(0, 0, 1), BottomColor, sprite.GetTrueUV(new vec2(0, 0)), new(0, 1)))
.AddVertex(2, new(new(0, 0, 0), BottomColor, sprite.GetTrueUV(new vec2(0, 1)), new(0, 0), sprite))
.AddVertex(2, new(new(1, 0, 0), BottomColor, sprite.GetTrueUV(new vec2(1, 1)), new(1, 0), sprite))
.AddVertex(2, new(new(1, 0, 1), BottomColor, sprite.GetTrueUV(new vec2(1, 0)), new(1, 1), sprite))
.AddVertex(2, new(new(0, 0, 1), BottomColor, sprite.GetTrueUV(new vec2(0, 0)), new(0, 1), sprite))

//Top
.AddVertex(3, new(new(0, 1, 0), vec4.Ones, sprite.GetTrueUV(new vec2(0, 0)), new(0, 0)))
.AddVertex(3, new(new(0, 1, 1), vec4.Ones, sprite.GetTrueUV(new vec2(1, 0)), new(1, 0)))
.AddVertex(3, new(new(1, 1, 1), vec4.Ones, sprite.GetTrueUV(new vec2(1, 1)), new(1, 1)))
.AddVertex(3, new(new(1, 1, 0), vec4.Ones, sprite.GetTrueUV(new vec2(0, 1)), new(0, 1)))
.AddVertex(3, new(new(0, 1, 0), vec4.Ones, sprite.GetTrueUV(new vec2(0, 0)), new(0, 0), sprite))
.AddVertex(3, new(new(0, 1, 1), vec4.Ones, sprite.GetTrueUV(new vec2(1, 0)), new(1, 0), sprite))
.AddVertex(3, new(new(1, 1, 1), vec4.Ones, sprite.GetTrueUV(new vec2(1, 1)), new(1, 1), sprite))
.AddVertex(3, new(new(1, 1, 0), vec4.Ones, sprite.GetTrueUV(new vec2(0, 1)), new(0, 1), sprite))

//Backward
.AddVertex(4, new(new(0, 0, 0), BackwardColor, sprite.GetTrueUV(new vec2(1, 1)), new(0, 0)))
.AddVertex(4, new(new(0, 1, 0), BackwardColor, sprite.GetTrueUV(new vec2(1, 0)), new(1, 0)))
.AddVertex(4, new(new(1, 1, 0), BackwardColor, sprite.GetTrueUV(new vec2(0, 0)), new(1, 1)))
.AddVertex(4, new(new(1, 0, 0), BackwardColor, sprite.GetTrueUV(new vec2(0, 1)), new(0, 1)))
.AddVertex(4, new(new(0, 0, 0), BackwardColor, sprite.GetTrueUV(new vec2(1, 1)), new(0, 0), sprite))
.AddVertex(4, new(new(0, 1, 0), BackwardColor, sprite.GetTrueUV(new vec2(1, 0)), new(1, 0), sprite))
.AddVertex(4, new(new(1, 1, 0), BackwardColor, sprite.GetTrueUV(new vec2(0, 0)), new(1, 1), sprite))
.AddVertex(4, new(new(1, 0, 0), BackwardColor, sprite.GetTrueUV(new vec2(0, 1)), new(0, 1), sprite))

//Forward
.AddVertex(5, new(new(0, 0, 1), ForwardColor, sprite.GetTrueUV(new vec2(0, 1)), new(0, 0)))
.AddVertex(5, new(new(1, 0, 1), ForwardColor, sprite.GetTrueUV(new vec2(1, 1)), new(1, 0)))
.AddVertex(5, new(new(1, 1, 1), ForwardColor, sprite.GetTrueUV(new vec2(1, 0)), new(1, 1)))
.AddVertex(5, new(new(0, 1, 1), ForwardColor, sprite.GetTrueUV(new vec2(0, 0)), new(0, 1)))
.AddVertex(5, new(new(0, 0, 1), ForwardColor, sprite.GetTrueUV(new vec2(0, 1)), new(0, 0), sprite))
.AddVertex(5, new(new(1, 0, 1), ForwardColor, sprite.GetTrueUV(new vec2(1, 1)), new(1, 0), sprite))
.AddVertex(5, new(new(1, 1, 1), ForwardColor, sprite.GetTrueUV(new vec2(1, 0)), new(1, 1), sprite))
.AddVertex(5, new(new(0, 1, 1), ForwardColor, sprite.GetTrueUV(new vec2(0, 0)), new(0, 1), sprite))
.Build();
}
public static BlockModel GetGrass(Atlas.Sprite top, Atlas.Sprite bottom, Atlas.Sprite side) {
return new BlockModel.Builder()
//Left
.AddVertex(0, new(new(0, 0, 0), LeftColor, side.GetTrueUV(new vec2(0, 1)), new(0, 0)))
.AddVertex(0, new(new(0, 0, 1), LeftColor, side.GetTrueUV(new vec2(1, 1)), new(1, 0)))
.AddVertex(0, new(new(0, 1, 1), LeftColor, side.GetTrueUV(new vec2(1, 0)), new(1, 1)))
.AddVertex(0, new(new(0, 1, 0), LeftColor, side.GetTrueUV(new vec2(0, 0)), new(0, 1)))
.AddVertex(0, new(new(0, 0, 0), LeftColor, side.GetTrueUV(new vec2(0, 1)), new(0, 0), side))
.AddVertex(0, new(new(0, 0, 1), LeftColor, side.GetTrueUV(new vec2(1, 1)), new(1, 0), side))
.AddVertex(0, new(new(0, 1, 1), LeftColor, side.GetTrueUV(new vec2(1, 0)), new(1, 1), side))
.AddVertex(0, new(new(0, 1, 0), LeftColor, side.GetTrueUV(new vec2(0, 0)), new(0, 1), side))

//Right
.AddVertex(1, new(new(1, 0, 0), RightColor, side.GetTrueUV(new vec2(1, 1)), new(0, 0)))
.AddVertex(1, new(new(1, 1, 0), RightColor, side.GetTrueUV(new vec2(1, 0)), new(1, 0)))
.AddVertex(1, new(new(1, 1, 1), RightColor, side.GetTrueUV(new vec2(0, 0)), new(1, 1)))
.AddVertex(1, new(new(1, 0, 1), RightColor, side.GetTrueUV(new vec2(0, 1)), new(0, 1)))
.AddVertex(1, new(new(1, 0, 0), RightColor, side.GetTrueUV(new vec2(1, 1)), new(0, 0), side))
.AddVertex(1, new(new(1, 1, 0), RightColor, side.GetTrueUV(new vec2(1, 0)), new(1, 0), side))
.AddVertex(1, new(new(1, 1, 1), RightColor, side.GetTrueUV(new vec2(0, 0)), new(1, 1), side))
.AddVertex(1, new(new(1, 0, 1), RightColor, side.GetTrueUV(new vec2(0, 1)), new(0, 1), side))

//Bottom
.AddVertex(2, new(new(0, 0, 0), BottomColor, bottom.GetTrueUV(new vec2(0, 1)), new(0, 0)))
.AddVertex(2, new(new(1, 0, 0), BottomColor, bottom.GetTrueUV(new vec2(1, 1)), new(1, 0)))
.AddVertex(2, new(new(1, 0, 1), BottomColor, bottom.GetTrueUV(new vec2(1, 0)), new(1, 1)))
.AddVertex(2, new(new(0, 0, 1), BottomColor, bottom.GetTrueUV(new vec2(0, 0)), new(0, 1)))
.AddVertex(2, new(new(0, 0, 0), BottomColor, bottom.GetTrueUV(new vec2(0, 1)), new(0, 0), bottom))
.AddVertex(2, new(new(1, 0, 0), BottomColor, bottom.GetTrueUV(new vec2(1, 1)), new(1, 0), bottom))
.AddVertex(2, new(new(1, 0, 1), BottomColor, bottom.GetTrueUV(new vec2(1, 0)), new(1, 1), bottom))
.AddVertex(2, new(new(0, 0, 1), BottomColor, bottom.GetTrueUV(new vec2(0, 0)), new(0, 1), bottom))

//Top
.AddVertex(3, new(new(0, 1, 0), vec4.Ones, top.GetTrueUV(new vec2(0, 0)), new(0, 0)))
.AddVertex(3, new(new(0, 1, 1), vec4.Ones, top.GetTrueUV(new vec2(1, 0)), new(1, 0)))
.AddVertex(3, new(new(1, 1, 1), vec4.Ones, top.GetTrueUV(new vec2(1, 1)), new(1, 1)))
.AddVertex(3, new(new(1, 1, 0), vec4.Ones, top.GetTrueUV(new vec2(0, 1)), new(0, 1)))
.AddVertex(3, new(new(0, 1, 0), vec4.Ones, top.GetTrueUV(new vec2(0, 0)), new(0, 0), top))
.AddVertex(3, new(new(0, 1, 1), vec4.Ones, top.GetTrueUV(new vec2(1, 0)), new(1, 0), top))
.AddVertex(3, new(new(1, 1, 1), vec4.Ones, top.GetTrueUV(new vec2(1, 1)), new(1, 1), top))
.AddVertex(3, new(new(1, 1, 0), vec4.Ones, top.GetTrueUV(new vec2(0, 1)), new(0, 1), top))

//Backward
.AddVertex(4, new(new(0, 0, 0), BackwardColor, side.GetTrueUV(new vec2(1, 1)), new(0, 0)))
.AddVertex(4, new(new(0, 1, 0), BackwardColor, side.GetTrueUV(new vec2(1, 0)), new(1, 0)))
.AddVertex(4, new(new(1, 1, 0), BackwardColor, side.GetTrueUV(new vec2(0, 0)), new(1, 1)))
.AddVertex(4, new(new(1, 0, 0), BackwardColor, side.GetTrueUV(new vec2(0, 1)), new(0, 1)))
.AddVertex(4, new(new(0, 0, 0), BackwardColor, side.GetTrueUV(new vec2(1, 1)), new(0, 0), side))
.AddVertex(4, new(new(0, 1, 0), BackwardColor, side.GetTrueUV(new vec2(1, 0)), new(1, 0), side))
.AddVertex(4, new(new(1, 1, 0), BackwardColor, side.GetTrueUV(new vec2(0, 0)), new(1, 1), side))
.AddVertex(4, new(new(1, 0, 0), BackwardColor, side.GetTrueUV(new vec2(0, 1)), new(0, 1), side))

//Forward
.AddVertex(5, new(new(0, 0, 1), ForwardColor, side.GetTrueUV(new vec2(0, 1)), new(0, 0)))
.AddVertex(5, new(new(1, 0, 1), ForwardColor, side.GetTrueUV(new vec2(1, 1)), new(1, 0)))
.AddVertex(5, new(new(1, 1, 1), ForwardColor, side.GetTrueUV(new vec2(1, 0)), new(1, 1)))
.AddVertex(5, new(new(0, 1, 1), ForwardColor, side.GetTrueUV(new vec2(0, 0)), new(0, 1)))
.AddVertex(5, new(new(0, 0, 1), ForwardColor, side.GetTrueUV(new vec2(0, 1)), new(0, 0), side))
.AddVertex(5, new(new(1, 0, 1), ForwardColor, side.GetTrueUV(new vec2(1, 1)), new(1, 0), side))
.AddVertex(5, new(new(1, 1, 1), ForwardColor, side.GetTrueUV(new vec2(1, 0)), new(1, 1), side))
.AddVertex(5, new(new(0, 1, 1), ForwardColor, side.GetTrueUV(new vec2(0, 0)), new(0, 1), side))
.Build();
}

Expand Down
25 changes: 21 additions & 4 deletions Client/Rendering/VertexTypes/BasicVertex.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
using GlmSharp;
using Veldrid;
using Voxel.Client.Rendering.Texture;
using Voxel.Common.Util;

namespace Voxel.Client.Rendering.VertexTypes;
Expand All @@ -10,13 +11,17 @@ public struct BasicVertex {
new VertexElementDescription("Position", VertexElementFormat.Float3, VertexElementSemantic.Position),
new VertexElementDescription("Color", VertexElementFormat.Float4, VertexElementSemantic.Color),
new VertexElementDescription("UV", VertexElementFormat.Float2, VertexElementSemantic.TextureCoordinate),
new VertexElementDescription("AO", VertexElementFormat.Float2, VertexElementSemantic.TextureCoordinate)
new VertexElementDescription("AO", VertexElementFormat.Float2, VertexElementSemantic.TextureCoordinate),
new VertexElementDescription("UVMin", VertexElementFormat.Float2, VertexElementSemantic.TextureCoordinate),
new VertexElementDescription("UVMax", VertexElementFormat.Float2, VertexElementSemantic.TextureCoordinate)
);

public vec3 position;
public vec4 color;
public vec2 uv;
public vec2 ao;
public vec2 uvmin;
public vec2 uvmax;

public BasicVertex() {}

Expand All @@ -26,30 +31,42 @@ public BasicVertex(vec3 pos, vec4 color) : this(pos, color, vec2.Zero, vec2.Zero

public BasicVertex(vec3 pos, vec4 color, vec2 uv) : this(pos, color, uv, vec2.Zero) {}

public BasicVertex(vec3 pos, vec4 color, vec2 uv, vec2 ao) {
public BasicVertex(vec3 pos, vec4 color, vec2 uv, vec2 ao) : this(pos, color, uv, ao, vec2.Zero, vec2.Ones) {}

public BasicVertex(vec3 pos, vec4 color, vec2 uv, vec2 ao, Atlas.Sprite sprite) : this(pos, color, uv, ao, sprite.uvPosition, sprite.uvPosition + sprite.uvSize) {}

public BasicVertex(vec3 pos, vec4 color, vec2 uv, vec2 ao, vec2 uvmin, vec2 uvmax) {
position = pos;
this.color = color;
this.uv = uv;
this.ao = ao;
this.uvmin = uvmin;
this.uvmax = uvmax;
}

public static implicit operator Packed(BasicVertex vertex) => new() {
Position = vertex.position,
Color = vertex.color.Packed(),
UV = ((int)(vertex.uv.x * ushort.MaxValue)) | ((int)(vertex.uv.y * ushort.MaxValue)) << 16
UV = ((int)(vertex.uv.x * ushort.MaxValue)) | ((int)(vertex.uv.y * ushort.MaxValue)) << 16,
UVMin = vertex.uvmin,
UVMax = vertex.uvmax
};

public struct Packed {
public static readonly VertexLayoutDescription Layout = new(
new VertexElementDescription("Position", VertexElementFormat.Float3, VertexElementSemantic.Position),
new VertexElementDescription("Color", VertexElementFormat.Int1, VertexElementSemantic.Color),
new VertexElementDescription("UV", VertexElementFormat.Int1, VertexElementSemantic.TextureCoordinate),
new VertexElementDescription("AO", VertexElementFormat.Float1, VertexElementSemantic.TextureCoordinate)
new VertexElementDescription("AO", VertexElementFormat.Float1, VertexElementSemantic.TextureCoordinate),
new VertexElementDescription("UVMin", VertexElementFormat.Float2, VertexElementSemantic.TextureCoordinate),
new VertexElementDescription("UVMax", VertexElementFormat.Float2, VertexElementSemantic.TextureCoordinate)
);

public vec3 Position;
public int Color;
public int UV;
public float AO;
public vec2 UVMin;
public vec2 UVMax;
}
}

0 comments on commit 82ab3dc

Please sign in to comment.