Smaller matrices in uniforms, texcoord transform to rotate voxel faces

This commit is contained in:
2026-09-23 15:42:31 +02:00
parent a6d2538352
commit ea94b65f06
11 changed files with 127 additions and 45 deletions

View File

@@ -1,22 +1,28 @@
{ {
"walls": { "walls": {
"positive_x": { "positive_x": {
"material": "OakLogTop.json" "material": "OakLogTop.json",
"transform": "identity"
}, },
"negative_x": { "negative_x": {
"material": "OakLogTop.json" "material": "OakLogTop.json",
"transform": "mirror"
}, },
"positive_y": { "positive_y": {
"material": "OakLog.json" "material": "OakLog.json",
"transform": "rotate_ccw"
}, },
"negative_y": { "negative_y": {
"material": "OakLog.json" "material": "OakLog.json",
"transform": "rotate_cw"
}, },
"positive_z": { "positive_z": {
"material": "OakLog.json" "material": "OakLog.json",
"transform": "rotate_cw"
}, },
"negative_z": { "negative_z": {
"material": "OakLog.json" "material": "OakLog.json",
"transform": "rotate_cw"
} }
} }
} }

View File

@@ -1,22 +1,28 @@
{ {
"walls": { "walls": {
"positive_x": { "positive_x": {
"material": "OakLog.json" "material": "OakLog.json",
"transform": "rotate_cw"
}, },
"negative_x": { "negative_x": {
"material": "OakLog.json" "material": "OakLog.json",
"transform": "rotate_ccw"
}, },
"positive_y": { "positive_y": {
"material": "OakLogTop.json" "material": "OakLogTop.json",
"transform": "identity"
}, },
"negative_y": { "negative_y": {
"material": "OakLogTop.json" "material": "OakLogTop.json",
"transform": "mirror"
}, },
"positive_z": { "positive_z": {
"material": "OakLog.json" "material": "OakLog.json",
"transform": "identity"
}, },
"negative_z": { "negative_z": {
"material": "OakLog.json" "material": "OakLog.json",
"transform": "rotate_180"
} }
} }
} }

View File

@@ -1,22 +1,28 @@
{ {
"walls": { "walls": {
"positive_x": { "positive_x": {
"material": "OakLog.json" "material": "OakLog.json",
"transform": "identity"
}, },
"negative_x": { "negative_x": {
"material": "OakLog.json" "material": "OakLog.json",
"transform": "identity"
}, },
"positive_y": { "positive_y": {
"material": "OakLog.json" "material": "OakLog.json",
"transform": "identity"
}, },
"negative_y": { "negative_y": {
"material": "OakLog.json" "material": "OakLog.json",
"transform": "identity"
}, },
"positive_z": { "positive_z": {
"material": "OakLogTop.json" "material": "OakLogTop.json",
"transform": "identity"
}, },
"negative_z": { "negative_z": {
"material": "OakLogTop.json" "material": "OakLogTop.json",
"transform": "mirror_rotate_180"
} }
} }
} }

View File

@@ -1,5 +1,5 @@
struct GlobalUniforms { struct GlobalUniforms {
float4x4 matrixWStoVS; float3x4 matrixWStoVS;
float4x4 matrixVStoCS; float4x4 matrixVStoCS;
float2x3 matrixSSPXtoCS; float2x3 matrixSSPXtoCS;
@@ -12,10 +12,10 @@ struct PointLight {
float3 positionWS; float3 positionWS;
float3 color; float3 color;
IncomingLight getIncoming(float4x4 matrixWStoVS, float3 positionVS) { IncomingLight getIncoming(float3x4 matrixWStoVS, float3 positionVS) {
IncomingLight light; IncomingLight light;
let lightPositionVS = mul(matrixWStoVS, float4(positionWS, 1.0)).xyz; let lightPositionVS = mul(matrixWStoVS, float4(positionWS, 1.0));
let lightDirectionVS = normalize(lightPositionVS - positionVS); let lightDirectionVS = normalize(lightPositionVS - positionVS);
let lightDistance = distance(positionVS, lightPositionVS); let lightDistance = distance(positionVS, lightPositionVS);
let lightAttenuation = 1.0 / (lightDistance * lightDistance); let lightAttenuation = 1.0 / (lightDistance * lightDistance);
@@ -31,11 +31,11 @@ struct DirectionalLight {
float3 directionWS; float3 directionWS;
float3 color; float3 color;
IncomingLight getIncoming(float4x4 matrixWStoVS) { IncomingLight getIncoming(float3x4 matrixWStoVS) {
IncomingLight light; IncomingLight light;
light.radiance = color; light.radiance = color;
light.directionVS = normalize(mul(matrixWStoVS, float4(-directionWS, 0.0)).xyz); light.directionVS = normalize(mul(matrixWStoVS, float4(-directionWS, 0.0)));
return light; return light;
} }
} }
@@ -56,8 +56,9 @@ struct Material {
} }
struct ObjectUniforms { struct ObjectUniforms {
float4x4 matrixOStoWS; // Limited to a composition of translation and rotation
float4x4 matrixOStoWSNormal; float3x4 matrixOStoWS;
float2x3 texCoordTransform;
uint16_t material; uint16_t material;
} }

View File

@@ -33,22 +33,24 @@ Varyings vertexMain(Vertex vert, uint instanceID : SV_VulkanInstanceID) {
let object = _Objects[instanceID]; let object = _Objects[instanceID];
let positionWS = mul(object.matrixOStoWS, float4(vert.positionOS, 1.0)).xyz; let positionWS = mul(object.matrixOStoWS, float4(vert.positionOS, 1.0));
let positionVS = mul(_Global.matrixWStoVS, float4(positionWS, 1.0)).xyz; let positionVS = mul(_Global.matrixWStoVS, float4(positionWS, 1.0));
let positionCS = mul(_Global.matrixVStoCS, float4(positionVS, 1.0)); let positionCS = mul(_Global.matrixVStoCS, float4(positionVS, 1.0));
let normalWS = normalize(mul(object.matrixOStoWSNormal, float4(vert.normalOS, 0.0)).xyz); let texCoord = mul(object.texCoordTransform, float3(vert.texCoord, 1.0));
let normalVS = normalize(mul(_Global.matrixWStoVS, float4(normalWS, 0.0)).xyz);
let tangentWS = normalize(mul(object.matrixOStoWSNormal, float4(vert.tangentOS.xyz, 0.0)).xyz); let normalWS = normalize(mul(object.matrixOStoWS, float4(vert.normalOS, 0.0)));
let tangentVS = normalize(mul(_Global.matrixWStoVS, float4(tangentWS, 0.0)).xyz); let normalVS = normalize(mul(_Global.matrixWStoVS, float4(normalWS, 0.0)));
let tangentWS = normalize(mul(object.matrixOStoWS, float4(vert.tangentOS.xyz, 0.0)));
let tangentVS = normalize(mul(_Global.matrixWStoVS, float4(tangentWS, 0.0)));
let bitangentVS = vert.tangentOS.w * normalize(cross(normalVS, tangentVS)); let bitangentVS = vert.tangentOS.w * normalize(cross(normalVS, tangentVS));
out.positionCS = positionCS; out.positionCS = positionCS;
out.instance = instanceID; out.instance = instanceID;
out.positionVS = positionVS; out.positionVS = positionVS;
out.texCoord = vert.texCoord; out.texCoord = texCoord;
out.normalVS = normalVS; out.normalVS = normalVS;
out.tangentVS = tangentVS; out.tangentVS = tangentVS;
out.bitangentVS = bitangentVS; out.bitangentVS = bitangentVS;

View File

@@ -18,7 +18,7 @@ struct Varyings {
Varyings vertexMain(Vertex vert) { Varyings vertexMain(Vertex vert) {
Varyings out; Varyings out;
let directionVS = mul(_Global.matrixWStoVS, float4(vert.directionWS, 0.0)).xyz; let directionVS = mul(_Global.matrixWStoVS, float4(vert.directionWS, 0.0));
let directionCS = mul(_Global.matrixVStoCS, float4(directionVS, 0.0)); let directionCS = mul(_Global.matrixVStoCS, float4(directionVS, 0.0));
out.positionCS = float4(directionCS.xy, 0.0, directionCS.w); out.positionCS = float4(directionCS.xy, 0.0, directionCS.w);

View File

@@ -119,13 +119,18 @@ pub fn refresh(self: *Chunk, blocks: *const Blocks, neighbors: Neighbors) !void
inline for (@typeInfo(voxels.Orientation).@"enum".fields) |field| { inline for (@typeInfo(voxels.Orientation).@"enum".fields) |field| {
const side = @field(voxels.Orientation, field.name); const side = @field(voxels.Orientation, field.name);
const material = @field(block.walls, field.name).material; const wall: Blocks.Definition.Wall = @field(block.walls, field.name);
if (material != .empty and self.getOpposite(side, ix, iy, iz, blocks, neighbors) == .empty) { if (wall.material != .empty and self.getOpposite(side, ix, iy, iz, blocks, neighbors) == .empty) {
const matrix = getMatrix(side, origin); const matrix = getMatrix(side, origin);
try uniforms.append(allocator_frame, .{ try uniforms.append(allocator_frame, .{
.matrixOStoWS = matrix, .matrixOStoWS_i = matrix.getIVersor().dropW(),
.matrixOStoWSNormal = matrix, .matrixOStoWS_j = matrix.getJVersor().dropW(),
.material = material, .matrixOStoWS_k = matrix.getKVersor().dropW(),
.matrixOStoWS_t = matrix.getTranslationVector().dropW(),
.texCoordTransform = wall.transform.getMatrix(),
.material = wall.material,
}); });
} }
} }

View File

@@ -909,7 +909,11 @@ fn render(self: *Game) !void {
const ambient_light = vm.Vector3.init(0.01, 0.01, 0.01); const ambient_light = vm.Vector3.init(0.01, 0.01, 0.01);
self.global_uniforms_staging_ptr.* = .{ self.global_uniforms_staging_ptr.* = .{
.matrixWStoVS = matrix_ws_to_vs, .matrixWStoVS_i = matrix_ws_to_vs.getIVersor().dropW(),
.matrixWStoVS_j = matrix_ws_to_vs.getJVersor().dropW(),
.matrixWStoVS_k = matrix_ws_to_vs.getKVersor().dropW(),
.matrixWStoVS_t = matrix_ws_to_vs.getTranslationVector().dropW(),
.matrixVStoCS = matrix_vs_to_cs, .matrixVStoCS = matrix_vs_to_cs,
.matrixSSPXtoCS = matrix_sspx_to_cs, .matrixSSPXtoCS = matrix_sspx_to_cs,

View File

@@ -184,9 +184,9 @@ pub const blocks = [9][:0]const u8{
"Bricks.json", "Bricks.json",
"ChiseledStoneBricks.json", "ChiseledStoneBricks.json",
"Cobblestone.json", "Cobblestone.json",
"DiamondBlock.json", "OakLogX.json",
"GoldBlock.json", "OakLogY.json",
"IronBlock.json", "OakLogZ.json",
"OakPlanks.json", "OakPlanks.json",
"SmoothStone.json", "SmoothStone.json",
"StoneBricks.json", "StoneBricks.json",

View File

@@ -44,7 +44,11 @@ pub const Vertex = extern struct {
pub const Index = u16; pub const Index = u16;
pub const GlobalUniforms = extern struct { pub const GlobalUniforms = extern struct {
matrixWStoVS: vm.Matrix4x4, matrixWStoVS_i: vm.Vector3,
matrixWStoVS_j: vm.Vector3,
matrixWStoVS_k: vm.Vector3,
matrixWStoVS_t: vm.Vector3,
matrixVStoCS: vm.Matrix4x4, matrixVStoCS: vm.Matrix4x4,
matrixSSPXtoCS: vm.Matrix3x2, matrixSSPXtoCS: vm.Matrix3x2,
@@ -93,8 +97,13 @@ pub const Material = extern struct {
}; };
pub const ObjectUniforms = extern struct { pub const ObjectUniforms = extern struct {
matrixOStoWS: vm.Matrix4x4, // Limited to a composition of translation and rotation
matrixOStoWSNormal: vm.Matrix4x4, matrixOStoWS_i: vm.Vector3,
matrixOStoWS_j: vm.Vector3,
matrixOStoWS_k: vm.Vector3,
matrixOStoWS_t: vm.Vector3,
texCoordTransform: vm.Matrix3x2,
material: Materials.Id, material: Materials.Id,
}; };

View File

@@ -59,6 +59,49 @@ pub const Transform = enum(u3) {
mirror_rotate_cw = 5, mirror_rotate_cw = 5,
mirror_rotate_ccw = 6, mirror_rotate_ccw = 6,
mirror_rotate_180 = 7, mirror_rotate_180 = 7,
pub fn getMatrix(self: Transform) vm.Matrix3x2 {
return switch (self) {
// zig fmt: off
.identity => .identity,
.rotate_cw => .init(
0, -1,
1, 0,
0, 1,
),
.rotate_ccw => .init(
0, 1,
-1, 0,
1, 0,
),
.rotate_180 => .init(
-1, 0,
0, -1,
1, 1,
),
.mirror => .init(
-1, 0,
0, 1,
1, 0,
),
.mirror_rotate_cw => .init(
0, -1,
-1, 0,
1, 1,
),
.mirror_rotate_ccw => .init(
0, 1,
1, 0,
0, 0,
),
.mirror_rotate_180 => .init(
0, 1,
-1, 0,
1, 0,
),
// zig fmt: on
};
}
}; };
// ┌──────────────────── x // ┌──────────────────── x