Compare commits

...

2 Commits

13 changed files with 127 additions and 53 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@@ -18,7 +18,7 @@ struct Varyings {
Varyings vertexMain(Vertex vert) {
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));
out.positionCS = float4(directionCS.xy, 0.0, directionCS.w);

2
castle

Submodule castle updated: af5f035830...99b8cf1d8e

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| {
const side = @field(voxels.Orientation, field.name);
const material = @field(block.walls, field.name).material;
if (material != .empty and self.getOpposite(side, ix, iy, iz, blocks, neighbors) == .empty) {
const wall: Blocks.Definition.Wall = @field(block.walls, field.name);
if (wall.material != .empty and self.getOpposite(side, ix, iy, iz, blocks, neighbors) == .empty) {
const matrix = getMatrix(side, origin);
try uniforms.append(allocator_frame, .{
.matrixOStoWS = matrix,
.matrixOStoWSNormal = matrix,
.material = material,
.matrixOStoWS = .{
matrix.getIVersor().dropW(),
matrix.getJVersor().dropW(),
matrix.getKVersor().dropW(),
matrix.getTranslationVector().dropW(),
},
.texCoordTransform = wall.transform.getMatrix(),
.material = wall.material,
});
}
}

View File

@@ -86,9 +86,9 @@ pub fn init() !Game {
.mag_filter = .linear,
.min_filter = .linear,
.mipmap_mode = .linear,
.address_mode_u = .repeat,
.address_mode_v = .repeat,
.address_mode_w = .repeat,
.address_mode_u = .clamp_to_edge,
.address_mode_v = .clamp_to_edge,
.address_mode_w = .clamp_to_edge,
.mip_lod_bias = 0,
.anisotropy_enable = .false,
.max_anisotropy = 0,
@@ -909,7 +909,12 @@ fn render(self: *Game) !void {
const ambient_light = vm.Vector3.init(0.01, 0.01, 0.01);
self.global_uniforms_staging_ptr.* = .{
.matrixWStoVS = matrix_ws_to_vs,
.matrixWStoVS = .{
matrix_ws_to_vs.getIVersor().dropW(),
matrix_ws_to_vs.getJVersor().dropW(),
matrix_ws_to_vs.getKVersor().dropW(),
matrix_ws_to_vs.getTranslationVector().dropW(),
},
.matrixVStoCS = matrix_vs_to_cs,
.matrixSSPXtoCS = matrix_sspx_to_cs,

View File

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

View File

@@ -133,9 +133,9 @@ pub fn init() !*Gui {
.mag_filter = .linear,
.min_filter = .linear,
.mipmap_mode = .linear,
.address_mode_u = .repeat,
.address_mode_v = .repeat,
.address_mode_w = .repeat,
.address_mode_u = .clamp_to_edge,
.address_mode_v = .clamp_to_edge,
.address_mode_w = .clamp_to_edge,
.mip_lod_bias = 0,
.anisotropy_enable = .false,
.max_anisotropy = 0,

View File

@@ -44,7 +44,7 @@ pub const Vertex = extern struct {
pub const Index = u16;
pub const GlobalUniforms = extern struct {
matrixWStoVS: vm.Matrix4x4,
matrixWStoVS: [4]vm.Vector3,
matrixVStoCS: vm.Matrix4x4,
matrixSSPXtoCS: vm.Matrix3x2,
@@ -93,9 +93,9 @@ pub const Material = extern struct {
};
pub const ObjectUniforms = extern struct {
matrixOStoWS: vm.Matrix4x4,
matrixOStoWSNormal: vm.Matrix4x4,
// Limited to a composition of translation and rotation
matrixOStoWS: [4]vm.Vector3,
texCoordTransform: vm.Matrix3x2,
material: Materials.Id,
};

View File

@@ -59,6 +59,49 @@ pub const Transform = enum(u3) {
mirror_rotate_cw = 5,
mirror_rotate_ccw = 6,
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