diff options
-rw-r--r-- | client/src/scene_prepare.rs | 44 | ||||
-rw-r--r-- | client/src/scene_render.rs | 11 | ||||
-rw-r--r-- | client/src/shader.wgsl | 16 |
3 files changed, 50 insertions, 21 deletions
diff --git a/client/src/scene_prepare.rs b/client/src/scene_prepare.rs index b5ac2cf..20c6e85 100644 --- a/client/src/scene_prepare.rs +++ b/client/src/scene_prepare.rs @@ -91,10 +91,11 @@ pub struct RPrefab(pub Vec<(Affine3A, Arc<RMeshPart>)>); pub struct RMeshPart { pub index_count: u32, pub index: Arc<Buffer>, - pub position: Arc<Buffer>, - pub normal: Arc<Buffer>, - pub texcoord: Arc<Buffer>, - pub texture: Arc<BindGroup>, + pub va_position: Arc<Buffer>, + pub va_normal: Arc<Buffer>, + pub va_texcoord: Arc<Buffer>, + pub tex_albedo: Arc<BindGroup>, + pub tex_normal: Arc<BindGroup>, } impl ScenePreparer { @@ -220,23 +221,35 @@ impl ScenePreparer { .flatten() }; - let mut texture = None; + let mut tex_albedo = None; if let Some(albedores) = part.tex_albedo { if let Some((_tex, bg)) = self.textures.try_get(albedores) { - texture = Some(bg) + tex_albedo = Some(bg) } } else { if let Some((_tex, bg)) = self.placeholder_textures.try_get(()) { - texture = Some(bg) + tex_albedo = Some(bg) + } + } + let mut tex_normal = None; + if let Some(albedores) = part.tex_normal { + if let Some((_tex, bg)) = self.textures.try_get(albedores) { + tex_normal = Some(bg) + } + } else { + if let Some((_tex, bg)) = self.placeholder_textures.try_get(()) { + tex_normal = Some(bg) } } if let ( - Some(normal), + Some(va_normal), Some((index, index_count)), - Some(texcoord), - Some((position, _)), - ) = (normal, index, texcoord, position) + Some(va_texcoord), + Some((va_position, _)), + Some(tex_normal), + Some(tex_albedo), + ) = (normal, index, texcoord, position, tex_normal, tex_albedo) { debug!("part created ({pres})"); self.mesh_parts.insert( @@ -244,10 +257,11 @@ impl ScenePreparer { Arc::new(RMeshPart { index_count, index, - texcoord, - normal, - position, - texture: texture.unwrap(), + va_normal, + va_position, + va_texcoord, + tex_albedo, + tex_normal, }), ); } diff --git a/client/src/scene_render.rs b/client/src/scene_render.rs index bc9fc52..7b2f653 100644 --- a/client/src/scene_render.rs +++ b/client/src/scene_render.rs @@ -62,7 +62,7 @@ impl ScenePipeline { }); let pipeline_layout = device.create_pipeline_layout(&PipelineLayoutDescriptor { label: None, - bind_group_layouts: &[&bind_group_layout], + bind_group_layouts: &[&bind_group_layout, &bind_group_layout], push_constant_ranges: &[PushConstantRange { range: 0..(4 * 4 * size_of::<f32>() as u32), stages: ShaderStages::VERTEX, @@ -187,13 +187,14 @@ impl ScenePipeline { * Mat4::from_mat3a(affine.matrix3); let projection = part_projection.to_cols_array().map(|v| v.to_le_bytes()); - rpass.set_bind_group(0, &*part.texture, &[]); + rpass.set_bind_group(0, &*part.tex_albedo, &[]); + rpass.set_bind_group(1, &*part.tex_normal, &[]); rpass.set_pipeline(&self.pipeline); rpass.set_push_constants(ShaderStages::VERTEX, 0, projection.as_flattened()); rpass.set_index_buffer(part.index.slice(..), IndexFormat::Uint16); - rpass.set_vertex_buffer(0, part.position.slice(..)); - rpass.set_vertex_buffer(1, part.normal.slice(..)); - rpass.set_vertex_buffer(2, part.texcoord.slice(..)); + rpass.set_vertex_buffer(0, part.va_position.slice(..)); + rpass.set_vertex_buffer(1, part.va_normal.slice(..)); + rpass.set_vertex_buffer(2, part.va_texcoord.slice(..)); rpass.draw_indexed(0..part.index_count, 0, 0..1); } } diff --git a/client/src/shader.wgsl b/client/src/shader.wgsl index 3f93223..7ed6c0e 100644 --- a/client/src/shader.wgsl +++ b/client/src/shader.wgsl @@ -26,8 +26,12 @@ struct VertexOut { @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; var<push_constant> project: mat4x4<f32>; +const LIGHT: vec3<f32> = vec3(0.64, 0.64, 0.64); + @vertex fn vs_main(vi: VertexIn) -> VertexOut { var clip = project * vec4(vi.position, 1.); @@ -36,5 +40,15 @@ fn vs_main(vi: VertexIn) -> VertexOut { } @fragment fn fs_main(vo: VertexOut) -> @location(0) vec4<f32> { - return textureSample(tex_albedo, tex_albedo_sampler, vo.texcoord); + let t_albedo = textureSample(tex_albedo, tex_albedo_sampler, vo.texcoord); + let t_normal = textureSample(tex_normal, tex_normal_sampler, vo.texcoord); + + + let lighting = mix(1., saturate(dot(LIGHT, vo.normal)), 0.9); + + let alpha = t_albedo.a; + let color = t_albedo.rgb * lighting; + // let color = vo.normal + t_normal.rgb; + + return vec4(color, alpha); } |