world serialization complete, post processing saving

This commit is contained in:
Tuomas Katajisto 2025-10-11 23:39:25 +03:00
parent 4b349195d6
commit fff5507bbd
13 changed files with 9126 additions and 40 deletions

View File

@ -1,13 +1,13 @@
#scope_file
char_pos : Vector3 = .{0.0, 1.5, 0.0};
char_pos : Vector3 = .{0.0, 0.0, 0.0};
rotation : float = 0.0;
cam : Camera = .{
far = 2000.0,
near = 1.0,
target = .{0.0, 4.0, 0.0},
position = .{3.0, 5.0, 3.0}
target = .{0.0, 0.0, 0.0},
position = .{5.0, 5.0, 0.0}
};
#scope_export
@ -19,7 +19,12 @@ game_ui :: () {
}
game_tick :: () {
if input_button_states[#char "W"] & .START then char_pos += Vector3.{1.0, 0.0, 0.0};
if input_button_states[#char "S"] & .START then char_pos += Vector3.{-1.0, 0.0, 0.0};
if input_button_states[#char "A"] & .START then char_pos += Vector3.{0.0, 0.0, 1.0};
if input_button_states[#char "D"] & .START then char_pos += Vector3.{0.0, 0.0, -1.0};
cam.target = char_pos;
cam.position = char_pos + .{10,5,0};
}
game_draw :: () {

View File

@ -0,0 +1,7 @@
{
"exposure": -0.252874,
"contrast": 1.604926,
"saturation": 0,
"gamma": 2.162069,
"tonemap": 1
}

File diff suppressed because it is too large Load Diff

186
game/resources/worlds.json Normal file

File diff suppressed because one or more lines are too long

View File

@ -262,11 +262,15 @@ draw_tools_tab :: (theme: *GR.Overall_Theme, total_r: GR.Rect) {
}
}
handle_tool_click :: (x: int, y: int, z: int) {
handle_tool_click :: (x: int, y: int, z: int, delete: bool = false) {
curworld := get_current_world();
if editMode == {
case .TRILES;
if editor_current_trile != null then add_trile(editor_current_trile.name, cast(float)x, cast(float)y, cast(float)z);
if delete {
remove_trile(cast(float)x, cast(float)y, cast(float)z);
} else {
if editor_current_trile != null then add_trile(editor_current_trile.name, cast(float)x, cast(float)y, cast(float)z);
}
case .GROUND;
ray := get_mouse_ray(*get_level_editor_camera());
hit, point := ray_plane_collision_point(ray, 0, 100);
@ -312,6 +316,25 @@ add_trile :: (name: string, x: float, y: float, z: float) {
array_add(*curworld.world.positions, poses);
} @Command
remove_trile :: (x: float, y: float, z: float) {
curworld := get_current_world();
loose_float_comp :: (a: float, b: float) -> bool {
return abs(a-b) < 0.001;
}
for curworld.world.positions {
for v, idx: it.positions {
if loose_float_comp(v.x, x)
&& loose_float_comp(v.y, y)
&& loose_float_comp(v.z, z) {
array_unordered_remove_by_index(*it.positions, idx);
break;
}
}
}
} @Command
tick_level_editor :: () {
@ -329,6 +352,9 @@ tick_level_editor :: () {
if get_mouse_state(Key_Code.MOUSE_BUTTON_LEFT) & .START {
handle_tool_click(xx floor(point.x), xx editY, xx floor(point.y));
}
if get_mouse_state(Key_Code.MOUSE_BUTTON_RIGHT) & .START {
handle_tool_click(xx floor(point.x), xx editY, xx floor(point.y), true);
}
}
}

View File

@ -27,5 +27,18 @@ draw_post_process_editor_ui :: (theme: *GR.Overall_Theme) {
r := GR.get_rect(0, ui_h(5,0), ui_w(20, 20), ui_h(95, 0));
ui_add_mouse_occluder(r);
draw_bg_rectangle(r, theme);
r2 := r;
r2.h = ui_h(5,0);
r.h -= r2.h;
r.y += r2.h;
if GR.button(r2, "Save postprocessing", *theme.button_theme) {
#if OS != .WASM {
file :: #import "File";
json := Jaison.json_write_string(current_post_process, " ");
file.write_entire_file("./game/resources/postprocess.json", json);
}
}
autoedit(r, *current_post_process, theme);
}

View File

@ -50,6 +50,15 @@ ntrile :: (name: string) {
editor_current_trile = get_trile(newt.name);
} @Command
cpytrile :: (trile: string, destination: string) {
set_trile_gfx(editor_current_trile.name, generate_trile_gfx_matias(editor_current_trile));
newt : Trile;
newt.name = sprint("%", destination);
newt.trixels = get_trile(trile).trixels;
set_trile(newt.name, newt);
editor_current_trile = get_trile(newt.name);
} @Command
ltrile :: (name: string) {
set_trile_gfx(editor_current_trile.name, generate_trile_gfx_matias(editor_current_trile));
nt := get_trile(name);

View File

@ -10,7 +10,9 @@ MEM_DEBUG :: false;
#import "Hash_Table";
#import "Hash";
#import "Simple_Package_Reader";
stbi :: #import "stb_image";
Jaison :: #import "Jaison";
stbi :: #import "stb_image";
#load "trile.jai";
#load "rendering/rendering.jai";
@ -110,6 +112,8 @@ init_after_asset_pack :: () {
}
init_editor();
game_init();
lworlds();
load_post_process_from_pack();
}
is_in_reflection_pass : bool = false;

View File

@ -16,6 +16,7 @@ create_world_rendering_tasks :: (world: *World) {
create_sky_rendering_task(*world.conf);
create_ground_rendering_task(world);
for world.positions {
if it.positions.count < 1 then continue;
triletask := Rendering_Task_Trile.{};
triletask.trile = it.trileName;
triletask.positions = it.positions;

View File

@ -1,10 +1,21 @@
Post_Process :: struct {
exposure : float;
contrast : float;
exposure : float = 0.0; @Slider,-1,1,0.1;
contrast : float = 1.0; @Slider,0.1,4.0,0.1;
saturation : float;
gamma : float = 1.0; @Slider,0.3,3.0,0.1;
tonemap : float = 1.0; @Slider,0,1,1;
}
current_post_process : Post_Process;
load_post_process_from_pack :: () {
s := load_string_from_pack("./game/resources/postprocess.json");
success, pp:= Jaison.json_parse_string(s, Post_Process,, temp);
if success {
current_post_process = pp;
}
}
reset_post_process :: () {
load_post_process_from_pack();
} @Command

View File

@ -100,7 +100,7 @@ vs_pp_source_glsl430 := u8.[
{
tonemapped = _61;
}
frag_color = vec4(pow(tonemapped, vec3(1.0 / post_process_config[0].w)), 1.0);
frag_color = vec4((((pow(tonemapped, vec3(1.0 / post_process_config[0].w)) - vec3(0.5)) * max(post_process_config[0].y, 0.0)) + vec3(0.5)) + vec3(post_process_config[0].x), 1.0);
}
*/
@ -153,11 +153,17 @@ fs_pp_source_glsl430 := u8.[
0x20,0x20,0x20,0x20,0x20,0x74,0x6f,0x6e,0x65,0x6d,0x61,0x70,0x70,0x65,0x64,0x20,
0x3d,0x20,0x5f,0x36,0x31,0x3b,0x0a,0x20,0x20,0x20,0x20,0x7d,0x0a,0x20,0x20,0x20,
0x20,0x66,0x72,0x61,0x67,0x5f,0x63,0x6f,0x6c,0x6f,0x72,0x20,0x3d,0x20,0x76,0x65,
0x63,0x34,0x28,0x70,0x6f,0x77,0x28,0x74,0x6f,0x6e,0x65,0x6d,0x61,0x70,0x70,0x65,
0x64,0x2c,0x20,0x76,0x65,0x63,0x33,0x28,0x31,0x2e,0x30,0x20,0x2f,0x20,0x70,0x6f,
0x73,0x74,0x5f,0x70,0x72,0x6f,0x63,0x65,0x73,0x73,0x5f,0x63,0x6f,0x6e,0x66,0x69,
0x67,0x5b,0x30,0x5d,0x2e,0x77,0x29,0x29,0x2c,0x20,0x31,0x2e,0x30,0x29,0x3b,0x0a,
0x7d,0x0a,0x0a,0x00,
0x63,0x34,0x28,0x28,0x28,0x28,0x70,0x6f,0x77,0x28,0x74,0x6f,0x6e,0x65,0x6d,0x61,
0x70,0x70,0x65,0x64,0x2c,0x20,0x76,0x65,0x63,0x33,0x28,0x31,0x2e,0x30,0x20,0x2f,
0x20,0x70,0x6f,0x73,0x74,0x5f,0x70,0x72,0x6f,0x63,0x65,0x73,0x73,0x5f,0x63,0x6f,
0x6e,0x66,0x69,0x67,0x5b,0x30,0x5d,0x2e,0x77,0x29,0x29,0x20,0x2d,0x20,0x76,0x65,
0x63,0x33,0x28,0x30,0x2e,0x35,0x29,0x29,0x20,0x2a,0x20,0x6d,0x61,0x78,0x28,0x70,
0x6f,0x73,0x74,0x5f,0x70,0x72,0x6f,0x63,0x65,0x73,0x73,0x5f,0x63,0x6f,0x6e,0x66,
0x69,0x67,0x5b,0x30,0x5d,0x2e,0x79,0x2c,0x20,0x30,0x2e,0x30,0x29,0x29,0x20,0x2b,
0x20,0x76,0x65,0x63,0x33,0x28,0x30,0x2e,0x35,0x29,0x29,0x20,0x2b,0x20,0x76,0x65,
0x63,0x33,0x28,0x70,0x6f,0x73,0x74,0x5f,0x70,0x72,0x6f,0x63,0x65,0x73,0x73,0x5f,
0x63,0x6f,0x6e,0x66,0x69,0x67,0x5b,0x30,0x5d,0x2e,0x78,0x29,0x2c,0x20,0x31,0x2e,
0x30,0x29,0x3b,0x0a,0x7d,0x0a,0x0a,0x00,
];
/*
#version 300 es
@ -218,7 +224,7 @@ vs_pp_source_glsl300es := u8.[
{
tonemapped = _61;
}
frag_color = vec4(pow(tonemapped, vec3(1.0 / post_process_config[0].w)), 1.0);
frag_color = vec4((((pow(tonemapped, vec3(1.0 / post_process_config[0].w)) - vec3(0.5)) * max(post_process_config[0].y, 0.0)) + vec3(0.5)) + vec3(post_process_config[0].x), 1.0);
}
*/
@ -275,11 +281,18 @@ fs_pp_source_glsl300es := u8.[
0x20,0x20,0x20,0x20,0x7b,0x0a,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x74,0x6f,
0x6e,0x65,0x6d,0x61,0x70,0x70,0x65,0x64,0x20,0x3d,0x20,0x5f,0x36,0x31,0x3b,0x0a,
0x20,0x20,0x20,0x20,0x7d,0x0a,0x20,0x20,0x20,0x20,0x66,0x72,0x61,0x67,0x5f,0x63,
0x6f,0x6c,0x6f,0x72,0x20,0x3d,0x20,0x76,0x65,0x63,0x34,0x28,0x70,0x6f,0x77,0x28,
0x74,0x6f,0x6e,0x65,0x6d,0x61,0x70,0x70,0x65,0x64,0x2c,0x20,0x76,0x65,0x63,0x33,
0x28,0x31,0x2e,0x30,0x20,0x2f,0x20,0x70,0x6f,0x73,0x74,0x5f,0x70,0x72,0x6f,0x63,
0x65,0x73,0x73,0x5f,0x63,0x6f,0x6e,0x66,0x69,0x67,0x5b,0x30,0x5d,0x2e,0x77,0x29,
0x29,0x2c,0x20,0x31,0x2e,0x30,0x29,0x3b,0x0a,0x7d,0x0a,0x0a,0x00,
0x6f,0x6c,0x6f,0x72,0x20,0x3d,0x20,0x76,0x65,0x63,0x34,0x28,0x28,0x28,0x28,0x70,
0x6f,0x77,0x28,0x74,0x6f,0x6e,0x65,0x6d,0x61,0x70,0x70,0x65,0x64,0x2c,0x20,0x76,
0x65,0x63,0x33,0x28,0x31,0x2e,0x30,0x20,0x2f,0x20,0x70,0x6f,0x73,0x74,0x5f,0x70,
0x72,0x6f,0x63,0x65,0x73,0x73,0x5f,0x63,0x6f,0x6e,0x66,0x69,0x67,0x5b,0x30,0x5d,
0x2e,0x77,0x29,0x29,0x20,0x2d,0x20,0x76,0x65,0x63,0x33,0x28,0x30,0x2e,0x35,0x29,
0x29,0x20,0x2a,0x20,0x6d,0x61,0x78,0x28,0x70,0x6f,0x73,0x74,0x5f,0x70,0x72,0x6f,
0x63,0x65,0x73,0x73,0x5f,0x63,0x6f,0x6e,0x66,0x69,0x67,0x5b,0x30,0x5d,0x2e,0x79,
0x2c,0x20,0x30,0x2e,0x30,0x29,0x29,0x20,0x2b,0x20,0x76,0x65,0x63,0x33,0x28,0x30,
0x2e,0x35,0x29,0x29,0x20,0x2b,0x20,0x76,0x65,0x63,0x33,0x28,0x70,0x6f,0x73,0x74,
0x5f,0x70,0x72,0x6f,0x63,0x65,0x73,0x73,0x5f,0x63,0x6f,0x6e,0x66,0x69,0x67,0x5b,
0x30,0x5d,0x2e,0x78,0x29,0x2c,0x20,0x31,0x2e,0x30,0x29,0x3b,0x0a,0x7d,0x0a,0x0a,
0x00,
];
/*
#include <metal_stdlib>
@ -387,7 +400,7 @@ vs_pp_source_metal_macos := u8.[
{
tonemapped = _61;
}
out.frag_color = float4(powr(tonemapped, float3(1.0 / _65.gamma)), 1.0);
out.frag_color = float4((((powr(tonemapped, float3(1.0 / _65.gamma)) - float3(0.5)) * fast::max(_65.contrast, 0.0)) + float3(0.5)) + float3(_65.exposure), 1.0);
return out;
}
@ -469,12 +482,17 @@ fs_pp_source_metal_macos := u8.[
0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x74,0x6f,0x6e,0x65,0x6d,0x61,0x70,0x70,
0x65,0x64,0x20,0x3d,0x20,0x5f,0x36,0x31,0x3b,0x0a,0x20,0x20,0x20,0x20,0x7d,0x0a,
0x20,0x20,0x20,0x20,0x6f,0x75,0x74,0x2e,0x66,0x72,0x61,0x67,0x5f,0x63,0x6f,0x6c,
0x6f,0x72,0x20,0x3d,0x20,0x66,0x6c,0x6f,0x61,0x74,0x34,0x28,0x70,0x6f,0x77,0x72,
0x28,0x74,0x6f,0x6e,0x65,0x6d,0x61,0x70,0x70,0x65,0x64,0x2c,0x20,0x66,0x6c,0x6f,
0x61,0x74,0x33,0x28,0x31,0x2e,0x30,0x20,0x2f,0x20,0x5f,0x36,0x35,0x2e,0x67,0x61,
0x6d,0x6d,0x61,0x29,0x29,0x2c,0x20,0x31,0x2e,0x30,0x29,0x3b,0x0a,0x20,0x20,0x20,
0x20,0x72,0x65,0x74,0x75,0x72,0x6e,0x20,0x6f,0x75,0x74,0x3b,0x0a,0x7d,0x0a,0x0a,
0x00,
0x6f,0x72,0x20,0x3d,0x20,0x66,0x6c,0x6f,0x61,0x74,0x34,0x28,0x28,0x28,0x28,0x70,
0x6f,0x77,0x72,0x28,0x74,0x6f,0x6e,0x65,0x6d,0x61,0x70,0x70,0x65,0x64,0x2c,0x20,
0x66,0x6c,0x6f,0x61,0x74,0x33,0x28,0x31,0x2e,0x30,0x20,0x2f,0x20,0x5f,0x36,0x35,
0x2e,0x67,0x61,0x6d,0x6d,0x61,0x29,0x29,0x20,0x2d,0x20,0x66,0x6c,0x6f,0x61,0x74,
0x33,0x28,0x30,0x2e,0x35,0x29,0x29,0x20,0x2a,0x20,0x66,0x61,0x73,0x74,0x3a,0x3a,
0x6d,0x61,0x78,0x28,0x5f,0x36,0x35,0x2e,0x63,0x6f,0x6e,0x74,0x72,0x61,0x73,0x74,
0x2c,0x20,0x30,0x2e,0x30,0x29,0x29,0x20,0x2b,0x20,0x66,0x6c,0x6f,0x61,0x74,0x33,
0x28,0x30,0x2e,0x35,0x29,0x29,0x20,0x2b,0x20,0x66,0x6c,0x6f,0x61,0x74,0x33,0x28,
0x5f,0x36,0x35,0x2e,0x65,0x78,0x70,0x6f,0x73,0x75,0x72,0x65,0x29,0x2c,0x20,0x31,
0x2e,0x30,0x29,0x3b,0x0a,0x20,0x20,0x20,0x20,0x72,0x65,0x74,0x75,0x72,0x6e,0x20,
0x6f,0x75,0x74,0x3b,0x0a,0x7d,0x0a,0x0a,0x00,
];
postprocess_shader_desc :: (backend: sg_backend) -> sg_shader_desc {
desc: sg_shader_desc;

View File

@ -44,6 +44,8 @@ void main() {
}
// tonemapped *= pow(2.0, exposure);
vec3 gammaCorrected = pow(tonemapped, vec3(1.0/gamma));
gammaCorrected.rgb = ((gammaCorrected.rgb - 0.5f) * max(contrast, 0)) + 0.5f;
gammaCorrected.rgb += exposure;
frag_color = vec4(gammaCorrected, 1.0);
}

View File

@ -36,11 +36,10 @@ get_current_world :: () -> *Current_World {
}
sworlds :: () {
Jaison :: #import "Jaison";
worlds : [..]World;
worlds : [..]World_Serialized;
worlds.allocator = temp;
for v : world_table {
array_add(*worlds, v);
array_add(*worlds, serialize_world(*v,, temp));
}
#if OS != .WASM {
file :: #import "File";
@ -50,12 +49,13 @@ sworlds :: () {
} @Command
lworlds :: () {
Jaison :: #import "Jaison";
s := load_string_from_pack("./game/resources/worlds.json");
success, worlds := Jaison.json_parse_string(s, [..]World,, temp);
success, worlds := Jaison.json_parse_string(s, [..]World_Serialized,, temp);
for worlds {
// set_trile(sprint("%",it.name), trile_from_serialize_form(it));
// print("Loaded %\n", it.name);
w : World;
deserialize_world(*it, *w);
table_set(*world_table, w.name, w);
print("Loaded world: %\n", w.name);
}
} @Command
@ -101,10 +101,10 @@ TrilePositions :: struct {
positions: [..]Vector4;
}
Ground_Tile :: enum {
WATER;
GRASS;
SAND;
Ground_Tile :: enum u8 {
WATER :: 0;
GRASS :: 1;
SAND :: 2;
}
World :: struct {
@ -114,6 +114,69 @@ World :: struct {
ground : [1000][1000]Ground_Tile;
}
Trile_Positions_Serialized :: struct {
trileName : string;
positions : [..]float;
}
World_Serialized :: struct {
name : string;
conf : World_Config;
positions : [..]Trile_Positions_Serialized;
ground : string;
}
// You should call this function with the temp allocator as context allocator.
serialize_world :: (world: *World) -> World_Serialized {
ws : World_Serialized;
ws.name = world.name;
ws.conf = world.conf;
for world.positions {
tsp : Trile_Positions_Serialized;
tsp.trileName = it.trileName;
for v: it.positions {
array_add(*tsp.positions, v.x);
array_add(*tsp.positions, v.y);
array_add(*tsp.positions, v.z);
array_add(*tsp.positions, v.w);
}
array_add(*ws.positions, tsp);
}
groundstring : [..]u8;
for i: 0..999 {
for j: 0..999 {
array_add(*groundstring, (cast(u8) world.ground[i][j]) + #char "a");
}
}
ws.ground.data = groundstring.data;
ws.ground.count = groundstring.count;
return ws;
}
deserialize_world :: (ws: *World_Serialized, world: *World) {
world.name = sprint("%", ws.name); // It's important that we re-store everything since the serialization is going to temp and gets freed.
world.conf = ws.conf;
for ws.positions {
tp : TrilePositions;
tp.trileName = sprint("%", it.trileName);
for i : 0..it.positions.count/4-1 {
v: Vector4;
v.x = it.positions[i*4+0];
v.y = it.positions[i*4+1];
v.z = it.positions[i*4+2];
v.w = it.positions[i*4+3];
array_add(*tp.positions, v);
}
array_add(*world.positions, tp);
}
for c, ci : ws.ground {
world.ground[ci/1000][ci%1000] = cast(Ground_Tile)((cast(u8)c)-#char "a");
}
}
update_image_from_ground :: (world: *World, img: *sg_image) {
materialdata : [1000*1000*4]u8;
counter : int = 0;