summaryrefslogtreecommitdiff
path: root/client/src/render/shaders
diff options
context:
space:
mode:
Diffstat (limited to 'client/src/render/shaders')
-rw-r--r--client/src/render/shaders/fragment_pbr.wgsl64
-rw-r--r--client/src/render/shaders/fragment_ui.wgsl28
-rw-r--r--client/src/render/shaders/mod.rs37
-rw-r--r--client/src/render/shaders/texture_copy.wgsl37
-rw-r--r--client/src/render/shaders/vertex_ui.wgsl43
-rw-r--r--client/src/render/shaders/vertex_world.wgsl48
-rw-r--r--client/src/render/shaders/vertex_world_skin.wgsl58
7 files changed, 315 insertions, 0 deletions
diff --git a/client/src/render/shaders/fragment_pbr.wgsl b/client/src/render/shaders/fragment_pbr.wgsl
new file mode 100644
index 0000000..c8fb857
--- /dev/null
+++ b/client/src/render/shaders/fragment_pbr.wgsl
@@ -0,0 +1,64 @@
+// wearechat - generic multiplayer game with voip
+// Copyright (C) 2025 metamuffin
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, version 3 of the License only.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+struct VertexOut {
+ @builtin(position) clip: vec4<f32>,
+ @location(0) normal: vec3<f32>,
+ @location(1) tangent: vec3<f32>,
+ @location(2) texcoord: vec2<f32>,
+ @location(3) position: vec3<f32>,
+}
+
+struct Material {
+ roughness: f32,
+ metallic: f32,
+ albedo_alpha: vec4<f32>,
+ emission: vec3<f32>,
+}
+
+@group(0) @binding(0) var tex_albedo: texture_2d<f32>;
+@group(0) @binding(1) var tex_albedo_sampler: sampler;
+@group(1) @binding(0) var tex_normal: texture_2d<f32>;
+@group(1) @binding(1) var tex_normal_sampler: sampler;
+@group(2) @binding(0) var<uniform> material: Material;
+
+@fragment
+fn main(vo: VertexOut) -> @location(0) vec4<f32> {
+ let t_albedo = textureSample(tex_albedo, tex_albedo_sampler, vo.texcoord);
+ let t_normal = textureSample(tex_normal, tex_normal_sampler, vo.texcoord);
+
+ let tangent_basis = mat3x3(vo.tangent, cross(vo.tangent, vo.normal), vo.normal);
+ let normal = tangent_basis * (t_normal.rgb * 2. - 1.);
+
+ let light = vec3(0.64, 0.64, 0.64);
+ // let view = normalize(-vo.position);
+
+ let ambient = 0.1;
+ let diffuse = saturate(dot(light, normal));
+ // let specular = pow(saturate(dot(reflect(-light, normal), view)), 2.);
+
+ let lighting = ambient + diffuse;
+
+ let color = t_albedo.rgb * lighting;
+ // let color = vec3(dot(normal, view) * 0.5 + 0.5) ;
+ // let color = view * 0.5 + 0.5;
+ let alpha = t_albedo.a;
+
+ // TODO better (and faster?) randomness for alpha dither
+ if fract(dot(sin(vo.clip * 123.) * 1213., vec4(3., 2., 1., 4.))) > alpha {
+ discard;
+ }
+ return vec4(color, 1.);
+}
diff --git a/client/src/render/shaders/fragment_ui.wgsl b/client/src/render/shaders/fragment_ui.wgsl
new file mode 100644
index 0000000..7cb66ab
--- /dev/null
+++ b/client/src/render/shaders/fragment_ui.wgsl
@@ -0,0 +1,28 @@
+// wearechat - generic multiplayer game with voip
+// Copyright (C) 2025 metamuffin
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, version 3 of the License only.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+struct VertexOut {
+ @builtin(position) clip: vec4<f32>,
+ @location(0) uv: vec2<f32>,
+ @location(1) color: vec4<f32>,
+}
+
+@group(0) @binding(0) var texture: texture_2d<f32>;
+@group(0) @binding(1) var texture_sampler: sampler;
+
+@fragment
+fn main(vo: VertexOut) -> @location(0) vec4<f32> {
+ return pow(textureSample(texture, texture_sampler, vo.uv) * vo.color, vec4(2.2));
+}
diff --git a/client/src/render/shaders/mod.rs b/client/src/render/shaders/mod.rs
new file mode 100644
index 0000000..7e4e3be
--- /dev/null
+++ b/client/src/render/shaders/mod.rs
@@ -0,0 +1,37 @@
+/*
+ wearechat - generic multiplayer game with voip
+ Copyright (C) 2025 metamuffin
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU Affero General Public License as published by
+ the Free Software Foundation, version 3 of the License only.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU Affero General Public License for more details.
+
+ You should have received a copy of the GNU Affero General Public License
+ along with this program. If not, see <https://www.gnu.org/licenses/>.
+*/
+use log::info;
+use wgpu::{Device, ShaderModule, include_wgsl};
+
+pub struct SceneShaders {
+ pub fragment_pbr: ShaderModule,
+ pub vertex_world: ShaderModule,
+ pub vertex_world_skin: ShaderModule,
+}
+
+impl SceneShaders {
+ pub fn load(device: &Device) -> Self {
+ info!("compiling shaders...");
+ let s = Self {
+ fragment_pbr: device.create_shader_module(include_wgsl!("fragment_pbr.wgsl")),
+ vertex_world: device.create_shader_module(include_wgsl!("vertex_world.wgsl")),
+ vertex_world_skin: device.create_shader_module(include_wgsl!("vertex_world_skin.wgsl")),
+ };
+ info!("done");
+ s
+ }
+}
diff --git a/client/src/render/shaders/texture_copy.wgsl b/client/src/render/shaders/texture_copy.wgsl
new file mode 100644
index 0000000..be38a76
--- /dev/null
+++ b/client/src/render/shaders/texture_copy.wgsl
@@ -0,0 +1,37 @@
+// wearechat - generic multiplayer game with voip
+// Copyright (C) 2025 metamuffin
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, version 3 of the License only.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+struct VertexOut {
+ @builtin(position) clip: vec4<f32>,
+ @location(0) texcoord: vec2<f32>,
+};
+
+@group(0) @binding(0) var texture: texture_2d<f32>;
+@group(0) @binding(1) var texture_sampler: sampler;
+
+@vertex
+fn vs_main(@builtin(vertex_index) vertex_index: u32) -> VertexOut {
+ let x = i32(vertex_index) / 2;
+ let y = i32(vertex_index) & 1;
+ return VertexOut(
+ vec4<f32>(f32(x) * 4.0 - 1.0, 1.0 - f32(y) * 4.0, 0.0, 1.0),
+ vec2<f32>(f32(x) * 2.0, f32(y) * 2.0)
+ );
+}
+
+@fragment
+fn fs_main(vertex: VertexOut) -> @location(0) vec4<f32> {
+ return textureSample(texture, texture_sampler, vertex.texcoord);
+}
diff --git a/client/src/render/shaders/vertex_ui.wgsl b/client/src/render/shaders/vertex_ui.wgsl
new file mode 100644
index 0000000..7cbac8b
--- /dev/null
+++ b/client/src/render/shaders/vertex_ui.wgsl
@@ -0,0 +1,43 @@
+// wearechat - generic multiplayer game with voip
+// Copyright (C) 2025 metamuffin
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, version 3 of the License only.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+struct VertexIn {
+ @location(0) pos: vec2<f32>,
+ @location(1) uv: vec2<f32>,
+ @location(2) color: u32,
+}
+struct VertexOut {
+ @builtin(position) clip: vec4<f32>,
+ @location(0) uv: vec2<f32>,
+ @location(1) color: vec4<f32>,
+}
+
+var<push_constant> project: mat4x4<f32>;
+
+fn unpack_color(color: u32) -> vec4<f32> {
+ return vec4<f32>(
+ f32(color & 255u),
+ f32((color >> 8u) & 255u),
+ f32((color >> 16u) & 255u),
+ f32((color >> 24u) & 255u),
+ ) / 255.0;
+}
+
+@vertex
+fn main(@builtin(vertex_index) vindex: u32, vi: VertexIn) -> VertexOut {
+ var clip = project * vec4(vi.pos, 0., 1.);
+ let vo = VertexOut(clip, vi.uv, unpack_color(vi.color));
+ return vo;
+}
diff --git a/client/src/render/shaders/vertex_world.wgsl b/client/src/render/shaders/vertex_world.wgsl
new file mode 100644
index 0000000..4f342fd
--- /dev/null
+++ b/client/src/render/shaders/vertex_world.wgsl
@@ -0,0 +1,48 @@
+// wearechat - generic multiplayer game with voip
+// Copyright (C) 2025 metamuffin
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, version 3 of the License only.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+struct VertexIn {
+ @location(0) position: vec3<f32>,
+ @location(1) normal: vec3<f32>,
+ @location(2) tangent: vec3<f32>, // TODO maybe compress this
+ @location(3) texcoord: vec2<f32>,
+}
+struct VertexOut {
+ @builtin(position) clip: vec4<f32>,
+ @location(0) normal: vec3<f32>,
+ @location(1) tangent: vec3<f32>,
+ @location(2) texcoord: vec2<f32>,
+ @location(3) position: vec3<f32>,
+}
+
+struct PushConst {
+ modelviewproject: mat4x4<f32>,
+ model: mat4x4<f32>,
+}
+
+var<push_constant> pc: PushConst;
+
+@vertex
+fn main(vi: VertexIn) -> VertexOut {
+ let clip = pc.modelviewproject * vec4(vi.position, 1.);
+ let vo = VertexOut(
+ clip,
+ normalize((pc.model * vec4(vi.normal, 0.)).xyz),
+ normalize((pc.model * vec4(vi.tangent, 0.)).xyz),
+ vi.texcoord,
+ (pc.model * vec4(vi.position, 1.)).xyz,
+ );
+ return vo;
+}
diff --git a/client/src/render/shaders/vertex_world_skin.wgsl b/client/src/render/shaders/vertex_world_skin.wgsl
new file mode 100644
index 0000000..6e2b308
--- /dev/null
+++ b/client/src/render/shaders/vertex_world_skin.wgsl
@@ -0,0 +1,58 @@
+// wearechat - generic multiplayer game with voip
+// Copyright (C) 2025 metamuffin
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, version 3 of the License only.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see <https://www.gnu.org/licenses/>.
+
+struct VertexIn {
+ @location(0) position: vec3<f32>,
+ @location(1) normal: vec3<f32>,
+ @location(2) tangent: vec3<f32>, // TODO maybe compress this
+ @location(3) texcoord: vec2<f32>,
+ @location(4) joint_index: vec4<u32>,
+ @location(5) joint_weight: vec4<f32>,
+}
+struct VertexOut {
+ @builtin(position) clip: vec4<f32>,
+ @location(0) normal: vec3<f32>,
+ @location(1) tangent: vec3<f32>,
+ @location(2) texcoord: vec2<f32>,
+ @location(3) position: vec3<f32>,
+}
+
+struct PushConst {
+ modelviewproject: mat4x4<f32>,
+ model: mat4x4<f32>,
+}
+
+@group(3) @binding(0) var<uniform> joints: array<mat4x4<f32>, 128>;
+var<push_constant> pc: PushConst;
+
+@vertex
+fn main(vi: VertexIn) -> VertexOut {
+ let pos_in = vec4(vi.position, 1.);
+ let j0 = vi.joint_weight.x * (joints[vi.joint_index.x] * pos_in);
+ let j1 = vi.joint_weight.y * (joints[vi.joint_index.y] * pos_in);
+ let j2 = vi.joint_weight.z * (joints[vi.joint_index.z] * pos_in);
+ let j3 = vi.joint_weight.w * (joints[vi.joint_index.w] * pos_in);
+ let position = j0 + j1 + j2 + j3;
+
+ let clip = pc.modelviewproject * position;
+ let vo = VertexOut(
+ clip,
+ normalize((pc.model * vec4(vi.normal, 0.)).xyz),
+ normalize((pc.model * vec4(vi.tangent, 0.)).xyz),
+ vi.texcoord,
+ (pc.model * vec4(vi.position, 1.)).xyz,
+ );
+ return vo;
+}