Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexDavies8 committed Sep 26, 2024
2 parents 862c982 + 629d76a commit ca0c4e8
Show file tree
Hide file tree
Showing 44 changed files with 166 additions and 140 deletions.
2 changes: 1 addition & 1 deletion assets/cubyz/blocks/glacite.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"class" : "stone",
"hardness" : 25,
"hardness" : 20,
"breakingPower" : 1,
"drops" : [
"auto"
Expand Down
2 changes: 1 addition & 1 deletion assets/cubyz/blocks/glacite_bricks.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"class" : "stone",
"hardness" : 25,
"hardness" : 20,
"breakingPower" : 1,
"drops" : [
"auto"
Expand Down
11 changes: 11 additions & 0 deletions assets/cubyz/blocks/glacite_tile.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"class" : "stone",
"hardness" : 20,
"breakingPower" : 1,
"drops" : [
"auto"
],
"rotation" : "stairs",
"model" : "cubyz:cube",
"texture" : "cubyz:glacite_tile"
}
10 changes: 10 additions & 0 deletions assets/cubyz/blocks/limestone_tiles.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"class": "stone",
"hardness": 12,
"drops": [
"auto"
],
"rotation": "stairs",
"model": "cubyz:cube",
"texture": "cubyz:limestone_tiles"
}
10 changes: 10 additions & 0 deletions assets/cubyz/blocks/marble_tile.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"class": "stone",
"hardness": 12,
"drops": [
"auto"
],
"rotation": "stairs",
"model": "cubyz:cube",
"texture": "cubyz:marble_tile"
}
10 changes: 10 additions & 0 deletions assets/cubyz/blocks/sandstone_tile.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"class": "stone",
"hardness": 12,
"drops": [
"auto"
],
"rotation": "stairs",
"model": "cubyz:cube",
"texture": "cubyz:sandstone_tile"
}
10 changes: 10 additions & 0 deletions assets/cubyz/blocks/stone_tile.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"class": "stone",
"hardness": 12,
"drops": [
"auto"
],
"rotation": "stairs",
"model": "cubyz:cube",
"texture": "cubyz:stone_tile"
}
Binary file modified assets/cubyz/blocks/textures/chalk/dark_grey.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/cubyz/blocks/textures/chalk/grey.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/cubyz/blocks/textures/glacite_bricks.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/cubyz/blocks/textures/glacite_tile.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/cubyz/blocks/textures/limestone_bricks.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/cubyz/blocks/textures/limestone_tiles.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/cubyz/blocks/textures/marble_bricks.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/cubyz/blocks/textures/marble_tile.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/cubyz/blocks/textures/sandstone_bricks.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/cubyz/blocks/textures/sandstone_tile.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/cubyz/blocks/textures/stone_bricks.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/cubyz/blocks/textures/stone_tile.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/cubyz/blocks/textures/void_stone.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/cubyz/blocks/textures/void_stone_bricks.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/cubyz/blocks/textures/water.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified assets/cubyz/blocks/textures/water.png_reflectivity.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 2 additions & 0 deletions assets/cubyz/blocks/textures/water.png_textureInfo.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
{
"fogDensity" : 0.02,
"fogColor" : 0x203860,
"frames" : 3,
"time" : 500,
}
Binary file removed assets/cubyz/blocks/textures/water_1.png
Binary file not shown.
Binary file removed assets/cubyz/blocks/textures/water_1.png_absorption.png
Binary file not shown.
Binary file not shown.
4 changes: 0 additions & 4 deletions assets/cubyz/blocks/textures/water_1.png_textureInfo.json

This file was deleted.

Binary file removed assets/cubyz/blocks/textures/water_2.png
Binary file not shown.
Binary file removed assets/cubyz/blocks/textures/water_2.png_absorption.png
Binary file not shown.
Binary file not shown.
4 changes: 0 additions & 4 deletions assets/cubyz/blocks/textures/water_2.png_textureInfo.json

This file was deleted.

9 changes: 1 addition & 8 deletions assets/cubyz/blocks/water.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,5 @@
"collide": false,
"absorbedLight" : 0x090501,
"model" : "cubyz:cube",
"texture" : {
"time" : 500,
"textures": [
"cubyz:water",
"cubyz:water_1",
"cubyz:water_2"
]
}
"texture" : "cubyz:water",
}
3 changes: 2 additions & 1 deletion assets/cubyz/shaders/animation_pre_processing.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
layout (local_size_x = 64, local_size_y = 1, local_size_z = 1) in;

struct AnimationData {
uint startFrame;
uint frames;
uint time;
};
Expand All @@ -22,5 +23,5 @@ uniform uint size;
void main() {
uint textureIndex = gl_GlobalInvocationID.x;
if(textureIndex >= size) return;
animatedTexture[textureIndex] = textureIndex + time / animation[textureIndex].time % animation[textureIndex].frames;
animatedTexture[textureIndex] = animation[textureIndex].startFrame + time / animation[textureIndex].time % animation[textureIndex].frames;
}
149 changes: 68 additions & 81 deletions src/blocks.zig
Original file line number Diff line number Diff line change
Expand Up @@ -331,8 +331,9 @@ pub const Block = packed struct { // MARK: Block

pub const meshes = struct { // MARK: meshes
const AnimationData = extern struct {
frames: i32,
time: i32,
startFrame: u32,
frames: u32,
time: u32,
};

const TextureData = extern struct {
Expand Down Expand Up @@ -474,104 +475,90 @@ pub const meshes = struct { // MARK: meshes
return pathBuffer[0..path.len+ending.len];
}

fn readAuxillaryTexture(_path: []const u8, pathBuffer: []u8, ending: []const u8, list: *main.List(Image), default: Image) void {
fn readAuxillaryTexture(_path: []const u8, pathBuffer: []u8, ending: []const u8, default: Image) Image {
const path = extendedPath(_path, pathBuffer, ending);
const texture = Image.readFromFile(arenaForWorld.allocator(), path) catch default;
list.append(texture);
return texture;
}

fn extractAnimationSlice(image: Image, frame: usize, frames: usize) Image {
if(image.height < frames) return image;
var startHeight = image.height/frames*frame;
if(image.height%frames > frame) startHeight += frame
else startHeight += image.height%frames;
var endHeight = image.height/frames*(frame + 1);
if(image.height%frames > frame + 1) endHeight += frame + 1
else endHeight += image.height%frames;
var result = image;
result.height = @intCast(endHeight - startHeight);
result.imageData = result.imageData[startHeight*image.width..endHeight*image.width];
return result;
}

fn readTextureData(_path: []const u8) void {
var buffer: [1024]u8 = undefined;
@memcpy(buffer[0.._path.len], _path);
const path = buffer[0.._path.len];
blockTextures.append(Image.readFromFile(arenaForWorld.allocator(), path) catch Image.defaultImage);
readAuxillaryTexture(path, &buffer, "_emission.png", &emissionTextures, Image.emptyImage);
readAuxillaryTexture(path, &buffer, "_reflectivity.png", &reflectivityTextures, Image.emptyImage);
readAuxillaryTexture(path, &buffer, "_absorption.png", &absorptionTextures, Image.whiteEmptyImage);
const textureInfoPath = extendedPath(path, &buffer, "_textureInfo.json");
const textureInfoJson = main.files.readToJson(main.stackAllocator, textureInfoPath) catch .JsonNull;
defer textureInfoJson.free(main.stackAllocator);
textureFogData.append(.{
.fogDensity = textureInfoJson.get(f32, "fogDensity", 0.0),
.fogColor = textureInfoJson.get(u32, "fogColor", 0xffffff),
});
const animationFrames = textureInfoJson.get(u32, "frames", 1);
const animationTime = textureInfoJson.get(u32, "time", 1);
animation.append(.{.startFrame = @intCast(blockTextures.items.len), .frames = animationFrames, .time = animationTime});
const base = Image.readFromFile(arenaForWorld.allocator(), path) catch Image.defaultImage;
const emission = readAuxillaryTexture(path, &buffer, "_emission.png", Image.emptyImage);
const reflectivity = readAuxillaryTexture(path, &buffer, "_reflectivity.png", Image.emptyImage);
const absorption = readAuxillaryTexture(path, &buffer, "_absorption.png", Image.whiteEmptyImage);
for(0..animationFrames) |i| {
blockTextures.append(extractAnimationSlice(base, i, animationFrames));
emissionTextures.append(extractAnimationSlice(emission, i, animationFrames));
reflectivityTextures.append(extractAnimationSlice(reflectivity, i, animationFrames));
absorptionTextures.append(extractAnimationSlice(absorption, i, animationFrames));
textureFogData.append(.{
.fogDensity = textureInfoJson.get(f32, "fogDensity", 0.0),
.fogColor = textureInfoJson.get(u32, "fogColor", 0xffffff),
});
}
}

pub fn readTexture(textureInfo: JsonElement, assetFolder: []const u8) !u16 {
pub fn readTexture(textureId: []const u8, assetFolder: []const u8) !u16 {
var result: u16 = undefined;
if(textureInfo == .JsonString or textureInfo == .JsonStringOwned) {
const resource = textureInfo.as([]const u8, "");
var splitter = std.mem.splitScalar(u8, resource, ':');
const mod = splitter.first();
const id = splitter.rest();
var buffer: [1024]u8 = undefined;
var path = try std.fmt.bufPrint(&buffer, "{s}/{s}/blocks/textures/{s}.png", .{assetFolder, mod, id});
// Test if it's already in the list:
for(textureIDs.items, 0..) |other, j| {
if(std.mem.eql(u8, other, path)) {
result = @intCast(j);
return result;
}
var splitter = std.mem.splitScalar(u8, textureId, ':');
const mod = splitter.first();
const id = splitter.rest();
var buffer: [1024]u8 = undefined;
var path = try std.fmt.bufPrint(&buffer, "{s}/{s}/blocks/textures/{s}.png", .{assetFolder, mod, id});
// Test if it's already in the list:
for(textureIDs.items, 0..) |other, j| {
if(std.mem.eql(u8, other, path)) {
result = @intCast(j);
return result;
}
const file = std.fs.cwd().openFile(path, .{}) catch |err| blk: {
if(err != error.FileNotFound) {
std.log.err("Could not open file {s}: {s}", .{path, @errorName(err)});
}
path = try std.fmt.bufPrint(&buffer, "assets/{s}/blocks/textures/{s}.png", .{mod, id}); // Default to global assets.
break :blk std.fs.cwd().openFile(path, .{}) catch |err2| {
std.log.err("File not found. Searched in \"{s}\" and also in the assetFolder \"{s}\"", .{path, assetFolder});
return err2;
};
}
const file = std.fs.cwd().openFile(path, .{}) catch |err| blk: {
if(err != error.FileNotFound) {
std.log.err("Could not open file {s}: {s}", .{path, @errorName(err)});
}
path = try std.fmt.bufPrint(&buffer, "assets/{s}/blocks/textures/{s}.png", .{mod, id}); // Default to global assets.
break :blk std.fs.cwd().openFile(path, .{}) catch |err2| {
std.log.err("File not found. Searched in \"{s}\" and also in the assetFolder \"{s}\"", .{path, assetFolder});
return err2;
};
file.close(); // It was only openend to check if it exists.
// Otherwise read it into the list:
result = @intCast(blockTextures.items.len);
};
file.close(); // It was only openend to check if it exists.
// Otherwise read it into the list:
result = @intCast(textureIDs.items.len);

textureIDs.append(arenaForWorld.allocator().dupe(u8, path));
animation.append(.{.frames = 1, .time = 1});
readTextureData(path);
} else if(textureInfo == .JsonObject) {
const animationTime = textureInfo.get(i32, "time", 500);
const textures = textureInfo.getChild("textures").toSlice();
// Add the new textures into the list. Since this is an animation all textures that weren't found need to be replaced with undefined.
result = @intCast(blockTextures.items.len);
for(textures, 0..) |item, i| {
if(i == 0) {
animation.append(.{.frames = @intCast(textures.len), .time = animationTime});
} else {
animation.append(.{.frames = 1, .time = 1});
}
var splitter = std.mem.splitScalar(u8, item.as([]const u8, "cubyz:undefined"), ':');
const mod = splitter.first();
const id = splitter.rest();
var buffer: [1024]u8 = undefined;
var path = try std.fmt.bufPrint(&buffer, "{s}/{s}/blocks/textures/{s}.png", .{assetFolder, mod, id});
const file = std.fs.cwd().openFile(path, .{}) catch |err| blk: {
if(err != error.FileNotFound) {
std.log.err("Could not open file {s}: {s}", .{path, @errorName(err)});
}
path = try std.fmt.bufPrint(&buffer, "assets/{s}/blocks/textures/{s}.png", .{mod, id}); // Default to global assets.
break :blk std.fs.cwd().openFile(path, .{}) catch |err2| {
std.log.err("File not found. Searched in \"{s}\" and also in the assetFolder \"{s}\"", .{path, assetFolder});
return err2;
};
};
file.close(); // It was only openend to check if it exists.

textureIDs.append(arenaForWorld.allocator().dupe(u8, path));
readTextureData(path);
}
} else {
return error.NotSpecified;
}
textureIDs.append(arenaForWorld.allocator().dupe(u8, path));
readTextureData(path);
return result;
}

pub fn getTextureIndices(json: JsonElement, assetFolder: []const u8, textureIndicesRef: []u16) void {
const defaultIndex = readTexture(json.getChild("texture"), assetFolder) catch 0;
const defaultIndex = readTexture(json.get([]const u8, "texture", ""), assetFolder) catch 0;
for(textureIndicesRef, sideNames) |*ref, name| {
const textureInfo = json.getChild(name);
ref.* = readTexture(textureInfo, assetFolder) catch defaultIndex;
const textureId = json.get([]const u8, name, "");
ref.* = readTexture(textureId, assetFolder) catch defaultIndex;
}
}

Expand All @@ -591,8 +578,8 @@ pub const meshes = struct { // MARK: meshes
pub fn preProcessAnimationData(time: u32) void {
animationShader.bind();
graphics.c.glUniform1ui(animationUniforms.time, time);
graphics.c.glUniform1ui(animationUniforms.size, @intCast(blockTextures.items.len));
graphics.c.glDispatchCompute(@intCast(@divFloor(blockTextures.items.len + 63, 64)), 1, 1); // TODO: Replace with @divCeil once available
graphics.c.glUniform1ui(animationUniforms.size, @intCast(animation.items.len));
graphics.c.glDispatchCompute(@intCast(@divFloor(animation.items.len + 63, 64)), 1, 1); // TODO: Replace with @divCeil once available
graphics.c.glMemoryBarrier(graphics.c.GL_SHADER_STORAGE_BARRIER_BIT);
}

Expand Down Expand Up @@ -647,7 +634,7 @@ pub const meshes = struct { // MARK: meshes
animationSSBO = SSBO.initStatic(AnimationData, animation.items);
animationSSBO.?.bind(0);

animatedTextureSSBO = SSBO.initStaticSize(u32, blockTextures.items.len);
animatedTextureSSBO = SSBO.initStaticSize(u32, animation.items.len);
animatedTextureSSBO.?.bind(1);
fogSSBO = SSBO.initStatic(FogData, textureFogData.items);
fogSSBO.?.bind(7);
Expand Down
Loading

0 comments on commit ca0c4e8

Please sign in to comment.