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

Commit

Permalink
-Add shader preprocessor
Browse files Browse the repository at this point in the history
-Port most shaders to new format
-Fix Entity movement code
-Add LivingEntity
  • Loading branch information
Cassunshine committed Dec 12, 2023
1 parent 5433db9 commit 36527c3
Show file tree
Hide file tree
Showing 43 changed files with 671 additions and 92 deletions.
16 changes: 12 additions & 4 deletions Client/ClientConfig.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ namespace Voxel.Client;

public class ClientConfig {
public static ClientConfig instance { get; } = ConfigHelper.LoadFile<ClientConfig>("Voxel.Client.toml") ?? new();

public General general = new();

[DataMember(Name = "keybindings")]
public Dictionary<string, string[]> _keybindings = new();

Expand Down Expand Up @@ -37,16 +37,19 @@ public class General {
public double _snapRight = 0.25;
[DataMember(Name = "snap_left")]
public double _snapLeft = 0.25;

[DataMember(Name = "fov")]
public float _fov = 45;

[DataMember(Name = "render_distance")]
public int _renderDistance = 4;
[DataMember(Name = "MSAA")]
public int _msaa = 2;

[DataMember(Name = "chunk_build_thread_count")]
public int _chunkBuildThreadCount = 3;


public static double deadzoneRight {
get => instance.general._deadzoneRight;
set => instance.general._deadzoneRight = value;
Expand All @@ -56,7 +59,7 @@ public static double deadzoneLeft {
get => instance.general._deadzoneLeft;
set => instance.general._deadzoneLeft = value;
}

public static double snapRight {
get => instance.general._snapRight;
set => instance.general._snapRight = value;
Expand All @@ -77,6 +80,11 @@ public static int renderDistance {
set => instance.general._renderDistance = value;
}

public static int msaaLevel {
get => instance.general._msaa;
set => instance.general._msaa = value;
}

public static int chunkBuildThreadCount {
get => instance.general._chunkBuildThreadCount;
set => instance.general._chunkBuildThreadCount = value;
Expand Down
19 changes: 19 additions & 0 deletions Client/Content/shaders/blit.v.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
layout (set = 0, binding = 0) uniform sampler TextureSampler;
layout (set = 0, binding = 1) uniform texture2D Texture;
layout (set = 1, binding = 0) uniform TextureDrawParams {
bool flip;
};

void vert(vec3 position, out vec2 uv){
gl_Position = vec4((position.xy * 2) - 1, 0, 1);

//Sample from source texture.

uv = position.xy;
if (flip)
uv = vec2(uv.x, 1 - uv.y);
}

void frag(vec2 uv, out vec4 o_color){
o_color = texture(sampler2D(Texture, TextureSampler), uv);
}
3 changes: 3 additions & 0 deletions Client/Content/shaders/common/camera.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
layout (set = 0, binding = 0) uniform CameraData {
mat4 VPMatrix;
};
10 changes: 10 additions & 0 deletions Client/Content/shaders/common/camera_and_model.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#include "common/camera.glsl"
#include "common/model.glsl"

mat4 GetMVP(){
return ModelMatrix * VPMatrix;
}

vec4 ModelVertex(vec3 pos){
return vec4(pos, 1) * GetMVP();
}
30 changes: 30 additions & 0 deletions Client/Content/shaders/common/filtering.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#AREA FRAGMENT

vec4 interpolatePixels(vec2 uv, vec2 uvMin, vec2 uvMax, texture2D tex, sampler sam) {
vec2 inverseTexSize = textureSize(sampler2D(tex, sam), 0);
vec2 texSize = 1 / inverseTexSize;

vec2 oldUv = uv;

vec2 boxSize = (abs(dFdx(oldUv)) + abs(dFdy(oldUv))) * inverseTexSize * 0.8;

vec2 tx = oldUv * inverseTexSize - 0.5 * boxSize;
vec2 tfract = fract(tx);
vec2 txOffset = smoothstep(1 - boxSize, vec2(1), tfract);

vec2 tmin = uvMin + 0.5 * texSize;
vec2 tmax = uvMax - 0.5 * texSize;

vec2 newUvMin = clamp((tx - tfract + 0.5) * texSize, tmin, tmax);
vec2 newUvMax = clamp((tx - tfract + 0.5 + txOffset) * texSize, tmin, tmax);

vec4 sampledColor = (
texture(sampler2D(tex, sam), newUvMin) +
texture(sampler2D(tex, sam), newUvMax) +
texture(sampler2D(tex, sam), vec2(newUvMin.x, newUvMax.y)) +
texture(sampler2D(tex, sam), vec2(newUvMax.x, newUvMin.y))
) / 4;
return sampledColor;
}

#END
3 changes: 3 additions & 0 deletions Client/Content/shaders/common/model.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
layout (set = 1, binding = 0) uniform ModelData {
mat4 ModelMatrix;
};
3 changes: 3 additions & 0 deletions Client/Content/shaders/common/test.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
void doStuff(){

}
12 changes: 12 additions & 0 deletions Client/Content/shaders/debug.v.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "common/camera.glsl"

void vert(vec3 position, vec4 color, vec2 uv, out vec4 o_color){
vec4 pos = vec4(position, 1) * VPMatrix;
gl_Position = pos;

o_color = color;
}

void frag(vec4 color, out vec4 o_color){
o_color = color;
}
33 changes: 33 additions & 0 deletions Client/Content/shaders/stitcher.v.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
layout (set = 0, binding = 0) uniform sampler TextureSampler;
layout (set = 0, binding = 1) uniform texture2D Texture;

layout (set = 1, binding = 0) uniform TextureDrawParams {
vec2 SrcMin;
vec2 SrcMax;
vec2 DstMin;
vec2 DstMax;
vec2 SrcSize;
vec2 DstSize;
};


void vert(vec3 Position, out vec2 o_uv){
vec2 scaledDstMin = DstMin / DstSize;
vec2 scaledDstMax = DstMax / DstSize;

//Move vertex to correct place on texture.
vec2 uv = (scaledDstMin + (scaledDstMax - scaledDstMin) * Position.xy);
uv.y = 1-uv.y;

gl_Position = vec4((uv * 2) - 1, 0, 1);

vec2 scaledSrcMin = SrcMin / SrcSize;
vec2 scaledSrcMax = SrcMax / SrcSize;

//Sample from source texture.
o_uv = scaledSrcMin + (scaledSrcMax - scaledSrcMin) * Position.xy;
}

void frag(vec2 uv, out vec4 o_color){
o_color = texture(sampler2D(Texture, TextureSampler), uv);
}
49 changes: 49 additions & 0 deletions Client/Content/shaders/terrain.v.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include "common/camera_and_model.glsl"
#include "common/filtering.glsl"

layout (set = 2, binding = 0) uniform sampler TextureSampler;
layout (set = 2, binding = 1) uniform texture2D Texture;

void Unpack(int packedColor, int packedUV, out vec4 color, out vec2 uv){
color = vec4(
(packedColor & 255) / 255.0f,
((packedColor >> 8) & 255) / 255.0f,
((packedColor >> 16) & 255) / 255.0f,
((packedColor >> 24) & 255) / 255.0f
);

uv = vec2(
(packedUV & 65535) / 65535.0f,
((packedUV >> 16) & 65535) / 65535.0f
);
}

float singleInterp(float strength, float baseColor) {
float min = 0.95 * baseColor * baseColor;
return min + (baseColor - min) * strength;
}

vec3 getColorMultiplier(float strength, vec3 baseColor) {
return vec3(
singleInterp(strength, baseColor.x),
singleInterp(strength, baseColor.y),
singleInterp(strength, baseColor.z)
);
}

void vert(vec3 position, int packedColor, int packedUV, float ao, vec2 uvMin, vec2 uvMax, out vec4 o_color, out vec2 o_uv, out float o_distance, out vec2 o_uvMin, out vec2 o_uvMax){
Unpack(packedColor, packedUV, o_color, o_uv);
o_color = o_color * vec4(getColorMultiplier(1-ao, vec3(0.9, 0.9, 1)), 1);

vec4 pos = ModelVertex(position);
gl_Position = pos;
o_distance = pos.z;

o_uvMin = uvMin;
o_uvMax = uvMax;
}

void frag(vec4 color, vec2 uv, float distance, vec2 uvMin, vec2 uvMax, out vec4 o_color, out vec4 o_gbuffer){
o_color = interpolatePixels(uv, uvMin, uvMax, Texture, TextureSampler) * color;
o_gbuffer = vec4(1, 1, 0, 1);
}
5 changes: 3 additions & 2 deletions Client/Rendering/Debug/DebugRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,11 @@ public override void CreatePipeline(MainFramebuffer framebuffer) {
}

public override void Render(double delta) {
//Flush();
Flush();
}

private void Flush() {
return;
//return;
if (vertexIndex == 0)
return;

Expand All @@ -78,6 +78,7 @@ public override void Dispose() {
}

private void AddPoint(dvec3 pos) {
//return;
Instance.DebugVertices[Instance.vertexIndex++] = new DebugVertex {
color = color, position = (matrix * new vec4((vec3)(pos - Client.GameRenderer.MainCamera.position), 1)).xyz
};
Expand Down
2 changes: 1 addition & 1 deletion Client/Rendering/GameRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ namespace Voxel.Client.Rendering;
public class GameRenderer : Renderer {

public MainFramebuffer Framebuffer { get; private set; }
private uint msaaLevel = 1;
private uint msaaLevel = (uint)ClientConfig.General.msaaLevel;
private bool needMainBufferRefresh = true;

/// <summary>
Expand Down
4 changes: 2 additions & 2 deletions Client/Rendering/Gui/GuiRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public GuiRenderer(VoxelClient client) : base(client) {
}

public override void CreatePipeline(MainFramebuffer framebuffer) {
if (!Client.RenderSystem.ShaderManager.GetShaders("shaders/gui", out var shaders))
/*if (!Client.RenderSystem.ShaderManager.GetShaders("shaders/gui", out var shaders))
throw new("Shaders not present.");
GuiPipeline = framebuffer.AddDependency(ResourceFactory.CreateGraphicsPipeline(new() {
Expand All @@ -38,7 +38,7 @@ public override void CreatePipeline(MainFramebuffer framebuffer) {
},
Shaders = shaders
}
}));
}));*/
}
public override void Render(double delta) {
//CommandList.SetPipeline(GuiPipeline);
Expand Down
2 changes: 1 addition & 1 deletion Client/Rendering/World/ChunkRenderSlot.cs
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ public void Render() {
UniformBuffer.SetValue(new() {
modelMatrix = mat4.Translate((vec3)(WorldPosition - CameraStateManager.currentCameraPosition)).Transposed
});
RenderSystem.MainCommandList.SetGraphicsResourceSet(2, UniformResourceSet);
RenderSystem.MainCommandList.SetGraphicsResourceSet(1, UniformResourceSet);

RenderSystem.MainCommandList.SetVertexBuffer(0, Buffer);
RenderSystem.MainCommandList.DrawIndexed(IndexCount);
Expand Down
10 changes: 5 additions & 5 deletions Client/Rendering/World/ChunkRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public ChunkRenderer(VoxelClient client) : base(client) {
}

public override void CreatePipeline(MainFramebuffer framebuffer) {
if (!Client.RenderSystem.ShaderManager.GetShaders("shaders/simple", out var shaders))
if (!Client.RenderSystem.ShaderManager.GetShaders("shaders/terrain", out var shaders))
throw new("Shaders not present.");

ChunkPipeline = framebuffer.AddDependency(ResourceFactory.CreateGraphicsPipeline(new() {
Expand All @@ -82,8 +82,8 @@ public override void CreatePipeline(MainFramebuffer framebuffer) {
},
ResourceLayouts = new[] {
Client.GameRenderer.CameraStateManager.CameraResourceLayout,
ChunkResourceLayout,
RenderSystem.TextureManager.TextureResourceLayout,
ChunkResourceLayout
},
ShaderSet = new() {
VertexLayouts = new[] {
Expand Down Expand Up @@ -112,10 +112,10 @@ public override void Render(double delta) {

SetRenderPosition(Client.GameRenderer.MainCamera.position);

RenderSystem.MainCommandList.SetPipeline(ChunkPipeline);
CommandList.SetPipeline(ChunkPipeline);

RenderSystem.MainCommandList.SetGraphicsResourceSet(0, Client.GameRenderer.CameraStateManager.CameraResourceSet);
CommandList.SetGraphicsResourceSet(1, TerrainAtlas.AtlasResourceSet);
CommandList.SetGraphicsResourceSet(0, Client.GameRenderer.CameraStateManager.CameraResourceSet);
CommandList.SetGraphicsResourceSet(2, TerrainAtlas.AtlasResourceSet);

CommandList.SetIndexBuffer(RenderSystem.CommonIndexBuffer, IndexFormat.UInt32);
foreach (var slot in createdRenderSlots)
Expand Down
1 change: 1 addition & 0 deletions Client/VoxelClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using Voxel.Client.World.Entity;
using Voxel.Common.Util;
using Voxel.Common.World.Entity;
using Voxel.Common.World.Entity.Player;
using Voxel.Core;

namespace Voxel.Client;
Expand Down
3 changes: 3 additions & 0 deletions Client/World/ClientWorld.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,7 @@ protected override Chunk CreateChunk(ivec3 pos) {
var c = new ClientChunk(pos, this);
return c;
}


public override bool IsChunkLoadedRaw(ivec3 chunkPos) => TryGetChunkRaw(chunkPos, out var c) && c is ClientChunk cc && cc.isFilled;
}
2 changes: 1 addition & 1 deletion Client/World/Entity/ClientPlayerEntity.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using Voxel.Common.World.Entity;
using Voxel.Common.World.Entity.Player;

namespace Voxel.Client.World.Entity;

Expand Down
16 changes: 10 additions & 6 deletions Client/World/Entity/ControlledClientPlayerEntity.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,18 @@ public override void Tick() {
// movement3d += new dvec3(0, 1, 0) * Keybinds.Jump.strength;
// movement3d += new dvec3(0, -1, 0) * Keybinds.Crouch.strength;

rotation += new dvec2((float)(looking.y * Constants.SecondsPerTick) * 1, (float)(looking.x * Constants.SecondsPerTick) * 1);
rotation += new dvec2((float)(looking.y * Constants.SecondsPerTick) * 1, (float)(looking.x * Constants.SecondsPerTick) * 1);

movement3d = new dvec2(0, rotation.y).RotationVecToQuat() * movement3d * 4;
var localVel = dvec2.Lerp(velocity.xz, movement3d.xz, 0.9);
velocity = velocity.WithXZ(localVel);

movement3d = new dvec2(0, rotation.y).RotationVecToQuat() * movement3d;

velocity = dvec3.Lerp(velocity, movement3d * 4, 0.5);
velocity += new dvec3(0, -1, 0);
if (Keybinds.Jump.justPressed)
velocity += new dvec3(0, 5, 0);
Jump();

if (Keybinds.Crouch.justPressed) {
position -= new dvec3(0, 1, 0);
}

var transformUpdate = PacketPool.GetPacket<PlayerUpdated>();
transformUpdate.Position = position;
Expand Down
1 change: 1 addition & 0 deletions Common/Collision/AABB.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ public bool Raycast(Ray ray, out RaycastHit hit) {
point = ray.Position,
normal = -ray.Direction,
distance = 0,
startedInside = true,
};
return true;
}
Expand Down
Loading

0 comments on commit 36527c3

Please sign in to comment.