shadows somewhat working

This commit is contained in:
Tuomas Katajisto 2025-10-12 14:39:35 +03:00
parent fff5507bbd
commit deb8208c84
9 changed files with 834 additions and 717 deletions

View File

@ -15,6 +15,6 @@ log_content :: (format_string: string, args: .. Any, loc := #caller_location, fl
} @PrintLike
logger :: (message: string, data: *void, info: Log_Info) {
wasm_write_string :: (count: s64, data: *void, log_flag: Log_Flags) #foreign; // You will need to provide this function as JS code in your WASM environment. :JaiWasm:
wasm_write_string(message.count, message.data, info.common_flags);
// wasm_write_string :: (count: s64, data: *void, log_flag: Log_Flags) #foreign; // You will need to provide this function as JS code in your WASM environment. :JaiWasm:
// wasm_write_string(message.count, message.data, info.common_flags);
}

View File

@ -20,6 +20,7 @@ Render_Command_Type :: enum {
DRAW_TRILE_POSITIONS;
UPDATE_TRIXELS;
DRAW_TRIXELS;
SET_LIGHT;
}
Render_Command :: struct {
@ -32,6 +33,12 @@ Render_Command_Sky :: struct {
worldConfig : *World_Config;
}
Render_Command_Set_Light :: struct {
#as using c : Render_Command;
c.type = .SET_LIGHT;
worldConfig : *World_Config;
}
Render_Command_Add_Trile_Positions :: struct {
#as using c : Render_Command;
c.type = .ADD_TRILE_POSITIONS;

View File

@ -5,6 +5,9 @@ trixel_count : s32 = 0;
trile_offsets : [..]s32;
current_trile_offset_index : s32 = 0;
current_world_config : *World_Config = null;
in_shadowmap_pass : bool = false;
backend_handle_command :: (cmd: *Render_Command) {
if cmd.type == {
case .ADD_TRILE_POSITIONS;
@ -30,6 +33,9 @@ backend_handle_command :: (cmd: *Render_Command) {
backend_update_trixels(trixel_update_command);
case .DRAW_TRIXELS;
backend_draw_trixels();
case .SET_LIGHT;
set_light_command := cast(*Render_Command_Set_Light)cmd;
current_world_config = set_light_command.worldConfig;
}
}
@ -90,7 +96,12 @@ backend_add_trile_positions :: (positions : []Vector4) {
}
backend_draw_trile_positions :: (trile : string, amount : s32, worldConf: *World_Config) {
mvp := create_viewproj(*camera);
mvp : Matrix4;
if !in_shadowmap_pass {
mvp = create_viewproj(*camera);
} else {
mvp = create_shadow_viewproj(*camera, worldConf);
}
vs_params : Trile_Vs_Params;
vs_params.mvp = mvp.floats;
vs_params.camera = camera.position.component;
@ -136,6 +147,8 @@ backend_draw_sky :: (wc: *World_Config) {
backend_draw_ground :: (wc: *World_Config) {
mvp := create_viewproj(*camera);
mvp_shadow : Matrix4;
if current_world_config != null then mvp_shadow = create_shadow_viewproj(*camera, current_world_config);
vs_params : Plane_Vs_Params;
world_conf : Plane_World_Config;
plane_data : Plane_Data;
@ -145,6 +158,7 @@ backend_draw_ground :: (wc: *World_Config) {
world_config_to_shader_type(wc, *world_conf);
vs_params.mvp_shadow = mvp_shadow.floats;
vs_params.mvp = mvp.floats;
sg_apply_pipeline(gPipelines.plane.pipeline);
gPipelines.plane.bind.samplers[2] = g_shadowmap_sampler;
@ -163,11 +177,15 @@ backend_process_command_buckets :: () {
}
// 2. Shadow pass
if current_world_config != null {
in_shadowmap_pass = true;
sg_begin_pass(*(sg_pass.{ action = state.pass_action_clear, attachments = g_shadowmap_attachments}));
for render_command_buckets.shadow {
backend_handle_command(it);
}
sg_end_pass();
in_shadowmap_pass = false;
}
current_trile_offset_index = 0; // This is not optimal, but it is nice and simple.
// 2. Reflection pass
@ -215,4 +233,5 @@ backend_process_command_buckets :: () {
array_reset_keeping_memory(*render_command_buckets.main);
array_reset_keeping_memory(*render_command_buckets.ui);
array_reset_keeping_memory(*trile_offsets);
current_world_config = null;
}

View File

@ -14,6 +14,7 @@ fill_uniform_with_engine_data :: (uniform: *$A, enginedata: *$B) {
create_world_rendering_tasks :: (world: *World) {
create_sky_rendering_task(*world.conf);
create_set_light_rendering_task(*world.conf);
create_ground_rendering_task(world);
for world.positions {
if it.positions.count < 1 then continue;
@ -30,6 +31,11 @@ create_sky_rendering_task :: (conf: *World_Config) {
add_rendering_task(skytask);
}
create_set_light_rendering_task :: (conf: *World_Config) {
lighttask := Rendering_Task_Set_Light.{type = .SET_LIGHT, worldConfig = conf};
add_rendering_task(lighttask);
}
create_trixel_rendering_task :: (trile: *Trile, muls: *[16][16][16]Vector3) {
trixeltask := Rendering_Task_Trixels.{type = .TRIXELS, trile = trile, colMultipliers = muls};
add_rendering_task(trixeltask);

View File

@ -16,7 +16,7 @@ draw_sky :: (cam: *Camera, worldConfig: *World_Config = null) {
sg_draw(0, 36, 1);
}
sun_shadowmap_viewproj :: (cam: *Camera, conf: *World_Config) -> Matrix4 {
create_shadow_viewproj :: (cam: *Camera, conf: *World_Config) -> Matrix4 {
up: Vector3 = .{0, 1, 0};
targetToPos := conf.sunPosition;
A := normalize(targetToPos);

View File

@ -5,6 +5,7 @@ Rendering_Task_Type :: enum {
GROUND; // We need to add an ability to invalidate buffer here too.
SKY;
SET_CAMERA;
SET_LIGHT;
TRILE; // We need to add an ability to invalidate buffer instead of updating it constantly. Also probably have a buffer for static world triles and one for moving ones.
TRIXELS;
SPRITE;
@ -26,6 +27,12 @@ Rendering_Task_Sky :: struct {
worldConfig : *World_Config;
}
Rendering_Task_Set_Light :: struct {
#as using t : Rendering_Task;
t.type = .SET_LIGHT;
worldConfig : *World_Config;
}
Rendering_Task_Ground :: struct {
#as using t : Rendering_Task;
t.type = .GROUND;
@ -67,6 +74,11 @@ rendering_tasklist : [..]*Rendering_Task;
tasks_to_commands :: () {
for rendering_tasklist {
if it.type == {
case .SET_LIGHT;
lighttask := (cast(*Rendering_Task_Set_Light)it);
lightcmd := New(Render_Command_Set_Light,, temp);
lightcmd.worldConfig = lighttask.worldConfig;
array_add(*render_command_buckets.setup, lightcmd);
case .TRIXELS;
trixelsTask := (cast(*Rendering_Task_Trixels)it);
updateCommand := New(Render_Command_Update_Trixels,, temp);

File diff suppressed because it is too large Load Diff

View File

@ -1,19 +1,24 @@
@vs vs_plane
// @glsl_options fixup_clipspace
in vec4 position;
layout(binding=0) uniform plane_vs_params {
mat4 mvp;
mat4 mvp_shadow;
};
out vec4 pos;
out flat int idx;
out vec4 light_proj_pos;
void main() {
vec3 multisize = vec3(position.xyz * 1000.0);
gl_Position = mvp * vec4(multisize, 1.0);
light_proj_pos = mvp_shadow * vec4(multisize, 1.0);
// #if !SOKOL_GLSL
// light_proj_pos.y = -light_proj_pos.y;
// #endif
pos = vec4(multisize, 1.0);
idx = gl_InstanceIndex;
}
@ -23,6 +28,7 @@ void main() {
in vec4 pos;
in flat int idx;
in vec4 light_proj_pos;
out vec4 frag_color;
uint murmurHash12(uvec2 src) {
@ -141,15 +147,18 @@ void main() {
vec3 c1 = get_ground_sample(npos, sign2(toCenter.x), 0.0);
vec3 c2 = get_ground_sample(npos, 0.0, sign2(toCenter.y));
vec3 c3 = get_ground_sample(npos, sign2(toCenter.x), sign2(toCenter.y));
float shadowp = texture(sampler2DShadow(shadow, shadowsmp), vec3(0,0,0));
// @ToDo: Consider using cool Inigo Quilez trick here to make it even smoother.
vec3 b01 = mix(c0, c1, u);
vec3 b23 = mix(c2, c3, u);
vec3 bf = mix(b01, b23, v);
vec3 light_pos = light_proj_pos.xyz / light_proj_pos.w;
light_pos = light_pos * 0.5 + 0.5;
float shadowp = texture(sampler2DShadow(shadow, shadowsmp), light_pos);
if(planeType == 1) {
frag_color = vec4(bf, 1.0);
frag_color = vec4(bf * shadowp, 1.0);
} else {
frag_color = vec4(vec3(shadowp), 1.0);
}

View File

@ -117,7 +117,7 @@ autoedit :: (rect: GR.Rect, value: *$T, theme: *GR.Overall_Theme, identifier: s3
r.h = ui_h(4,0);
if !cur_edit_color {
#insert #run generate_autoedit_code();
#insert #run,stallable generate_autoedit_code();
color_edit_already_active = false;
} else {
r.h = ui_h(50,0);