diff options
Diffstat (limited to 'client')
-rw-r--r-- | client/src/camera.rs | 7 | ||||
-rw-r--r-- | client/src/render/mod.rs | 11 | ||||
-rw-r--r-- | client/src/render/scene/draw.rs | 33 | ||||
-rw-r--r-- | client/src/render/scene/pipelines.rs | 20 | ||||
-rw-r--r-- | client/src/shaders/fragment_pbr.wgsl | 19 | ||||
-rw-r--r-- | client/src/shaders/vertex_world.wgsl | 16 | ||||
-rw-r--r-- | client/src/shaders/vertex_world_skin.wgsl | 14 |
7 files changed, 55 insertions, 65 deletions
diff --git a/client/src/camera.rs b/client/src/camera.rs index 4264e40..bd86e79 100644 --- a/client/src/camera.rs +++ b/client/src/camera.rs @@ -46,10 +46,11 @@ impl Camera { pub fn rotation_mat(&self) -> Mat3 { Mat3::from_euler(EulerRot::YXZ, self.rot.x, self.rot.y, self.rot.z) } - pub fn to_matrix(&self) -> Mat4 { + pub fn view_matrix(&self) -> Mat4 { + Mat4::from_mat3(self.rotation_mat().inverse()) * Mat4::from_translation(-self.pos) + } + pub fn project_matrix(&self) -> Mat4 { Mat4::perspective_rh(self.fov, self.aspect, 0.01, 300.) - * Mat4::from_mat3(self.rotation_mat().inverse()) - * Mat4::from_translation(-self.pos) } pub fn new_ui_affine(&self) -> Affine3A { Affine3A::from_mat3_translation( diff --git a/client/src/render/mod.rs b/client/src/render/mod.rs index da0d7a8..c3ee7da 100644 --- a/client/src/render/mod.rs +++ b/client/src/render/mod.rs @@ -76,7 +76,7 @@ impl<'a> Renderer<'a> { &DeviceDescriptor { required_features: Features::PUSH_CONSTANTS, required_limits: Limits { - max_push_constant_size: 64 + 48 + 16, + max_push_constant_size: 128, max_vertex_buffers: 16, ..Limits::default() }, @@ -202,7 +202,8 @@ impl<'a> Renderer<'a> { .device .create_command_encoder(&CommandEncoderDescriptor { label: None }); - let projection = camera.to_matrix(); + let view = camera.view_matrix(); + let project = camera.project_matrix(); self.timing.checkpoint("draw scene"); self.scene_pipeline.draw( @@ -211,8 +212,8 @@ impl<'a> Renderer<'a> { &self.depth, scene, &self.scene_prepare.prefabs, - projection, - camera.position(), + view, + project, ); self.timing.checkpoint("draw ui"); @@ -220,7 +221,7 @@ impl<'a> Renderer<'a> { &mut commands, &target_view, &self.depth, - projection, + project * view, input_state, &self.surface_configuration, ); diff --git a/client/src/render/scene/draw.rs b/client/src/render/scene/draw.rs index 672d7ef..bdbcb50 100644 --- a/client/src/render/scene/draw.rs +++ b/client/src/render/scene/draw.rs @@ -15,7 +15,7 @@ along with this program. If not, see <https://www.gnu.org/licenses/>. */ use super::{DemandMap, RPrefab}; -use glam::{EulerRot, Mat3, Mat4, Vec3}; +use glam::{EulerRot, Mat3, Mat4}; use std::sync::Arc; use weareshared::{packets::Resource, resources::Prefab, tree::SceneTree}; use wgpu::{ @@ -33,8 +33,8 @@ impl ScenePipeline { depth: &TextureView, scene: &SceneTree, prefabs: &DemandMap<Resource<Prefab>, Arc<RPrefab>>, - projection: Mat4, - camera_position: Vec3, + view: Mat4, + project: Mat4, ) { let mut rpass = commands.begin_render_pass(&RenderPassDescriptor { label: None, @@ -63,7 +63,7 @@ impl ScenePipeline { }); for ob in scene.objects.values() { - let prefab_projection = projection + let prefab_modelview = view * Mat4::from_translation(ob.pos.into()) * Mat4::from_mat3(Mat3::from_euler( EulerRot::YXZ, @@ -73,28 +73,23 @@ impl ScenePipeline { )); if let Some(prefab) = prefabs.try_get(ob.res.clone()) { for (affine, part) in &prefab.0 { - let part_projection = prefab_projection + let modelview = prefab_modelview * Mat4::from_translation(affine.translation.into()) * Mat4::from_mat3a(affine.matrix3); - let projection = part_projection.to_cols_array().map(|v| v.to_le_bytes()); - let mb = affine.matrix3.to_cols_array(); // TODO apply object rotation - // add padding for gpu mat3x3 repr - let model_basis = [ - mb[0], mb[1], mb[2], 0., // - mb[3], mb[4], mb[5], 0., // - mb[6], mb[7], mb[8], 0., // - ]; - let model_basis = bytemuck::cast_slice(&model_basis); - let cp = &[camera_position.x, camera_position.y, camera_position.z, 0.]; - let camera_position = bytemuck::cast_slice(cp); + let modelviewp = project * modelview; + // let light = modelview * vec4(5., 5., 5., 1.); + + // let light = light.to_array().map(|v| v.to_le_bytes()); + let modelview = modelview.to_cols_array().map(|v| v.to_le_bytes()); + let modelviewp = modelviewp.to_cols_array().map(|v| v.to_le_bytes()); rpass.set_pipeline(&part.pipeline); rpass.set_bind_group(0, &*part.tex_albedo, &[]); rpass.set_bind_group(1, &*part.tex_normal, &[]); rpass.set_bind_group(2, &*part.material, &[]); - rpass.set_push_constants(ShaderStages::VERTEX, 0, projection.as_flattened()); - rpass.set_push_constants(ShaderStages::VERTEX, 64, model_basis); - rpass.set_push_constants(ShaderStages::VERTEX, 112, camera_position); + rpass.set_push_constants(ShaderStages::VERTEX, 0, modelviewp.as_flattened()); + rpass.set_push_constants(ShaderStages::VERTEX, 64, modelview.as_flattened()); + // rpass.set_push_constants(ShaderStages::FRAGMENT, 128, light.as_flattened()); rpass.set_index_buffer(part.index.slice(..), IndexFormat::Uint32); rpass.set_vertex_buffer(0, part.va_position.slice(..)); rpass.set_vertex_buffer(1, part.va_normal.slice(..)); diff --git a/client/src/render/scene/pipelines.rs b/client/src/render/scene/pipelines.rs index 675cfae..dfc5d19 100644 --- a/client/src/render/scene/pipelines.rs +++ b/client/src/render/scene/pipelines.rs @@ -99,20 +99,12 @@ impl PipelineSpec { let pipeline_layout = device.create_pipeline_layout(&PipelineLayoutDescriptor { label: None, bind_group_layouts: &[&layouts.texture, &layouts.texture, &layouts.material], - push_constant_ranges: &[ - PushConstantRange { - // 4x4 view projections - // 3x3(+1 pad) model basis - // 3(+1 pad) camera position - range: 0..((4 * 4 + 3 * 4) * size_of::<f32>() as u32), - stages: ShaderStages::VERTEX, - }, - PushConstantRange { - range: ((4 * 4 + 3 * 4) * size_of::<f32>() as u32) - ..(4 * 4 + 3 * 4 + 4) * size_of::<f32>() as u32, - stages: ShaderStages::FRAGMENT, - }, - ], + push_constant_ranges: &[PushConstantRange { + // 4x4 model * view * project + // 4x4 model * view + range: 0..((4 * 4 + 4 * 4) * size_of::<f32>() as u32), + stages: ShaderStages::VERTEX, + }], }); device.create_render_pipeline(&RenderPipelineDescriptor { label: None, diff --git a/client/src/shaders/fragment_pbr.wgsl b/client/src/shaders/fragment_pbr.wgsl index f3387a1..60d7737 100644 --- a/client/src/shaders/fragment_pbr.wgsl +++ b/client/src/shaders/fragment_pbr.wgsl @@ -17,8 +17,8 @@ struct VertexOut { @builtin(position) clip: vec4<f32>, @location(0) normal: vec3<f32>, @location(1) tangent: vec3<f32>, - @location(2) view: vec3<f32>, - @location(3) texcoord: vec2<f32>, + @location(2) texcoord: vec2<f32>, + @location(3) position: vec3<f32>, } struct Material { @@ -34,8 +34,6 @@ struct Material { @group(1) @binding(1) var tex_normal_sampler: sampler; @group(2) @binding(0) var<uniform> material: Material; -const LIGHT: vec3<f32> = vec3(0.64, 0.64, 0.64); - @fragment fn main(vo: VertexOut) -> @location(0) vec4<f32> { let t_albedo = textureSample(tex_albedo, tex_albedo_sampler, vo.texcoord); @@ -44,16 +42,19 @@ fn main(vo: VertexOut) -> @location(0) vec4<f32> { let tangent_basis = mat3x3(vo.tangent, cross(vo.tangent, vo.normal), vo.normal); let normal = tangent_basis * (t_normal.rgb * 2. - 1.); - let alpha = t_albedo.a; + let light = vec3(0., 0., 0.); + let view = normalize(-vo.position); let ambient = 0.1; - let diffuse = saturate(dot(LIGHT, normal)); - let specular = pow(dot(reflect(-LIGHT, normal), vo.view), material.roughness); + let diffuse = saturate(dot(light, normal)) * 0.7; + let specular = pow(saturate(dot(reflect(-light, normal), view)), 2.); - let lighting = ambient + diffuse; // + specular; + let lighting = ambient + diffuse + specular; let color = t_albedo.rgb * lighting; - // let color = vec3(dot(normalize(vo.normal), normalize(vo.view)) * 0.5 + 0.5) ; + // 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 { diff --git a/client/src/shaders/vertex_world.wgsl b/client/src/shaders/vertex_world.wgsl index d7bf445..e185289 100644 --- a/client/src/shaders/vertex_world.wgsl +++ b/client/src/shaders/vertex_world.wgsl @@ -23,26 +23,26 @@ struct VertexOut { @builtin(position) clip: vec4<f32>, @location(0) normal: vec3<f32>, @location(1) tangent: vec3<f32>, - @location(2) world: vec3<f32>, - @location(3) texcoord: vec2<f32>, + @location(2) texcoord: vec2<f32>, + @location(3) position: vec3<f32>, } struct PushConst { + modelviewproject: mat4x4<f32>, modelview: mat4x4<f32>, - model_basis: mat3x3<f32>, } var<push_constant> pc: PushConst; @vertex fn main(vi: VertexIn) -> VertexOut { - let clip = pc.modelview * vec4(vi.position, 1.); + let clip = pc.modelviewproject * vec4(vi.position, 1.); let vo = VertexOut( clip, - normalize(pc.model_basis * vi.normal), - normalize(pc.model_basis * vi.tangent), - vi.position - vi.texcoord + normalize((pc.modelview * vec4(vi.normal, 0.)).xyz), + normalize((pc.modelview * vec4(vi.tangent, 0.)).xyz), + vi.texcoord, + (pc.modelview * vec4(vi.position, 1.)).xyz, ); return vo; } diff --git a/client/src/shaders/vertex_world_skin.wgsl b/client/src/shaders/vertex_world_skin.wgsl index 4b45a6f..633f06b 100644 --- a/client/src/shaders/vertex_world_skin.wgsl +++ b/client/src/shaders/vertex_world_skin.wgsl @@ -25,13 +25,13 @@ struct VertexOut { @builtin(position) clip: vec4<f32>, @location(0) normal: vec3<f32>, @location(1) tangent: vec3<f32>, - @location(2) world: vec3<f32>, - @location(3) texcoord: vec2<f32>, + @location(2) texcoord: vec2<f32>, + @location(3) position: vec3<f32>, } struct PushConst { + modelviewproject: mat4x4<f32>, modelview: mat4x4<f32>, - model_basis: mat3x3<f32>, } @group(3) @binding(0) var<uniform> joints: array<mat4x4<f32>, 128>; @@ -42,10 +42,10 @@ fn main(vi: VertexIn) -> VertexOut { let clip = pc.modelview * vec4(vi.position, 1.); let vo = VertexOut( clip, - normalize(pc.model_basis * vi.normal), - normalize(pc.model_basis * vi.tangent), - vi.position, - vi.texcoord + normalize((pc.modelview * vec4(vi.normal, 0.)).xyz), + normalize((pc.modelview * vec4(vi.tangent, 0.)).xyz), + vi.texcoord, + (pc.modelview * vec4(vi.position, 1.)).xyz, ); return vo; } |