Added primitive renderer, world with tree hierarchy supporting lights and objects with GLTF loading. Builds and runs with broken rendering and tree.

This commit is contained in:
paladin 2026-09-03 12:08:00 +01:00
parent f1d4534063
commit efe98454d9
17 changed files with 1968 additions and 1221 deletions

View file

@ -0,0 +1,97 @@
struct Environment {
ambient: vec4<f32>,
light: vec4<f32>,
dir: vec4<f32>,
}
struct View {
view: mat4x4<f32>,
frame: mat4x4<f32>,
}
// Vertex shader
@group(0) @binding(0)
var<uniform> view: View;
@group(0) @binding(1)
var<uniform> environment: Environment;
struct VertexInput {
@location(0) position: vec3<f32>,
@location(1) normal: vec3<f32>,
@location(2) tex_coords: vec2<f32>,
}
struct InstanceInput {
@location(4) model_matrix_0: vec4<f32>,
@location(5) model_matrix_1: vec4<f32>,
@location(6) model_matrix_2: vec4<f32>,
@location(7) model_matrix_3: vec4<f32>,
@location(8) model_color: vec4<f32>,
}
struct VertexOutput {
@builtin(position) clip_position: vec4<f32>,
@location(0) tex_coords: vec2<f32>,
@location(1) color: vec4<f32>,
@location(2) world_normal: vec3<f32>,
@location(3) world_position: vec3<f32>,
}
@vertex
fn vs_main(
model: VertexInput,
instance: InstanceInput,
) -> VertexOutput {
var out: VertexOutput;
let model_matrix = mat4x4<f32>(
instance.model_matrix_0,
instance.model_matrix_1,
instance.model_matrix_2,
instance.model_matrix_3,
);
let model_rot_matrix = mat3x3<f32>(
instance.model_matrix_0.xyz,
instance.model_matrix_1.xyz,
instance.model_matrix_2.xyz,
);
out.tex_coords = model.tex_coords;
out.color = instance.model_color;
out.world_normal = model_rot_matrix * model.normal;
var world_position: vec4<f32> = model_matrix * vec4<f32>(model.position, 1.0);
out.world_position = world_position.xyz;
out.clip_position = view.view * world_position;
return out;
}
// Fragment shader
@group(1) @binding(0)
var s_diffuse: sampler;
@group(1) @binding(1)
var t_diffuse: texture_2d<f32>;
@group(1) @binding(2)
var t_normal: texture_2d<f32>;
@group(1) @binding(3)
var t_rough: texture_2d<f32>;
@fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
let object_color: vec4<f32> = textureSample(t_diffuse, s_diffuse, in.tex_coords);
let object_normal: vec4<f32> = textureSample(t_normal, s_diffuse, in.tex_coords);
let tangent_normal = object_normal.xyz * 2.0 - 1.0;
let light_dir = normalize(environment.dir.xyz);
let view_dir = normalize(view.frame[3].xyz - in.world_position);
let half_dir = normalize(view_dir + light_dir);
let diffuse_strength = max(dot(tangent_normal, light_dir), 0.0);
let diffuse_color = environment.light.xyz * diffuse_strength;
let specular_strength = pow(max(dot(tangent_normal, half_dir), 0.0), 32.0);
let specular_color = specular_strength * environment.light.xyz;
let result = (environment.ambient.xyz + diffuse_color.xyz + specular_color.xyz) * object_color.xyz;
return vec4<f32>(result.xyz,object_color.a);
}

BIN
src/assets/plank/color.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.5 MiB

BIN
src/assets/plank/normal.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 4.7 MiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 499 KiB

View file

@ -1,35 +0,0 @@
// Vertex shader
@group(1) @binding(0)
var<uniform> view: mat4x4<f32>;
struct VertexInput {
@location(0) position: vec3<f32>,
@location(1) tex_coords: vec2<f32>,
}
struct VertexOutput {
@builtin(position) clip_position: vec4<f32>,
@location(0) tex_coords: vec2<f32>,
}
@vertex
fn vs_main(
model: VertexInput,
) -> VertexOutput {
var out: VertexOutput;
out.tex_coords = model.tex_coords;
out.clip_position = view * vec4<f32>(model.position, 1.0);
return out;
}
// Fragment shader
@group(0) @binding(0)
var t_diffuse: texture_2d<f32>;
@group(0) @binding(1)
var s_diffuse: sampler;
@fragment
fn fs_main(in: VertexOutput) -> @location(0) vec4<f32> {
return textureSample(t_diffuse, s_diffuse, in.tex_coords);
}

View file

@ -1,21 +0,0 @@
struct VertexOutput {
@builtin(position) clip_position: vec4<f32>,
@location(0) color: vec4<f32>
};
@vertex
fn vs_main(
@builtin(vertex_index) in_vertex_index: u32,
) -> VertexOutput {
var out: VertexOutput;
let x = f32(1 - i32(in_vertex_index)) * 0.5;
let y = f32(i32(in_vertex_index & 1u) * 2 - 1) * 0.5;
out.clip_position = vec4<f32>(x, y, 0.0, 1.0);
out.color = vec4<f32>(0.3,x,y,1.0);
return out;
}
@fragment
fn fs_main(@location(0) col: vec4<f32>) -> @location(0) vec4<f32> {
return col;
}