From d16485ae6d9a099e23cda55850087cd7e82f2d3f Mon Sep 17 00:00:00 2001 From: Quaternions Date: Wed, 20 Sep 2023 13:28:00 -0700 Subject: [PATCH] replace visual ground with custom ground model! --- src/main.rs | 65 ++++++++++++++++++++----------------------------- src/shader.wgsl | 40 ------------------------------ 2 files changed, 26 insertions(+), 79 deletions(-) diff --git a/src/main.rs b/src/main.rs index 91ebc1e..a3349dd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -119,7 +119,6 @@ pub struct Skybox { physics: strafe_client::body::PhysicsState, sky_pipeline: wgpu::RenderPipeline, entity_pipeline: wgpu::RenderPipeline, - ground_pipeline: wgpu::RenderPipeline, main_bind_group: wgpu::BindGroup, camera_buf: wgpu::Buffer, models: Vec, @@ -159,8 +158,7 @@ fn get_transform_uniform_data(transform:&glam::Mat4) -> [f32; 4*4] { raw } -fn add_obj(device:&wgpu::Device,modeldatas:& mut Vec,source:&[u8]){ - let data = obj::ObjData::load_buf(&source[..]).unwrap(); +fn add_obj(device:&wgpu::Device,modeldatas:& mut Vec,data:obj::ObjData){ let mut vertices = Vec::new(); let mut vertex_index = std::collections::HashMap::::new(); for object in data.objects { @@ -223,12 +221,34 @@ impl strafe_client::framework::Example for Skybox { queue: &wgpu::Queue, ) -> Self { let mut modeldatas = Vec::::new(); - add_obj(device,& mut modeldatas,include_bytes!("../models/teslacyberv3.0.obj")); - add_obj(device,& mut modeldatas,include_bytes!("../models/suzanne.obj")); - add_obj(device,& mut modeldatas,include_bytes!("../models/teapot.obj")); + let ground=obj::ObjData{ + position: vec![[-1.0,0.0,-1.0],[1.0,0.0,-1.0],[1.0,0.0,1.0],[-1.0,0.0,1.0]], + texture: vec![[-1.0,-1.0],[1.0,-1.0],[1.0,1.0],[-1.0,1.0]], + normal: vec![[0.0,1.0,0.0]], + objects: vec![obj::Object{ + name: "Ground Object".to_owned(), + groups: vec![obj::Group{ + name: "Ground Group".to_owned(), + index: 0, + material: None, + polys: vec![obj::SimplePolygon(vec![ + obj::IndexTuple(0,Some(0),Some(0)), + obj::IndexTuple(1,Some(1),Some(0)), + obj::IndexTuple(2,Some(2),Some(0)), + obj::IndexTuple(3,Some(3),Some(0)), + ])] + }] + }], + material_libs: Vec::new(), + }; + add_obj(device,& mut modeldatas,obj::ObjData::load_buf(&include_bytes!("../models/teslacyberv3.0.obj")[..]).unwrap()); + add_obj(device,& mut modeldatas,obj::ObjData::load_buf(&include_bytes!("../models/suzanne.obj")[..]).unwrap()); + add_obj(device,& mut modeldatas,obj::ObjData::load_buf(&include_bytes!("../models/teapot.obj")[..]).unwrap()); + add_obj(device,& mut modeldatas,ground); println!("models.len = {:?}", modeldatas.len()); modeldatas[1].transform=glam::Mat4::from_translation(glam::vec3(10.,5.,10.)); modeldatas[2].transform=glam::Mat4::from_translation(glam::vec3(-10.,5.,10.)); + modeldatas[3].transform=glam::Mat4::from_translation(glam::vec3(0.,-10.,0.))*glam::Mat4::from_scale(glam::vec3(160.0, 1.0, 160.0)); let main_bind_group_layout = device.create_bind_group_layout(&wgpu::BindGroupLayoutDescriptor { label: None, @@ -412,33 +432,6 @@ impl strafe_client::framework::Example for Skybox { multisample: wgpu::MultisampleState::default(), multiview: None, }); - let ground_pipeline = device.create_render_pipeline(&wgpu::RenderPipelineDescriptor { - label: Some("Ground"), - layout: Some(&pipeline_layout), - vertex: wgpu::VertexState { - module: &shader, - entry_point: "vs_ground", - buffers: &[], - }, - fragment: Some(wgpu::FragmentState { - module: &shader, - entry_point: "fs_ground", - targets: &[Some(config.view_formats[0].into())], - }), - primitive: wgpu::PrimitiveState { - front_face: wgpu::FrontFace::Cw, - ..Default::default() - }, - depth_stencil: Some(wgpu::DepthStencilState { - format: Self::DEPTH_FORMAT, - depth_write_enabled: true, - depth_compare: wgpu::CompareFunction::LessEqual, - stencil: wgpu::StencilState::default(), - bias: wgpu::DepthBiasState::default(), - }), - multisample: wgpu::MultisampleState::default(), - multiview: None, - }); let sampler = device.create_sampler(&wgpu::SamplerDescriptor { label: None, @@ -550,7 +543,6 @@ impl strafe_client::framework::Example for Skybox { physics, sky_pipeline, entity_pipeline, - ground_pipeline, main_bind_group, camera_buf, models, @@ -735,11 +727,6 @@ impl strafe_client::framework::Example for Skybox { } } - rpass.set_pipeline(&self.ground_pipeline); - //rpass.set_index_buffer(&[0u16,1,2,1,2,3][..] as wgpu::BufferSlice, wgpu::IndexFormat::Uint16); - //rpass.draw_indexed(0..4, 0, 0..1); - rpass.draw(0..6, 0..1); - rpass.set_pipeline(&self.sky_pipeline); rpass.draw(0..3, 0..1); } diff --git a/src/shader.wgsl b/src/shader.wgsl index a60fc5c..e3fce46 100644 --- a/src/shader.wgsl +++ b/src/shader.wgsl @@ -39,28 +39,6 @@ fn vs_sky(@builtin(vertex_index) vertex_index: u32) -> SkyOutput { return result; } -struct GroundOutput { - @builtin(position) position: vec4, - @location(4) pos: vec3, -}; - -@vertex -fn vs_ground(@builtin(vertex_index) vertex_index: u32) -> GroundOutput { - // hacky way to draw two triangles that make a square - let tmp1 = i32(vertex_index)/2-i32(vertex_index)/3; - let tmp2 = i32(vertex_index)&1; - let pos = vec3( - f32(tmp1) * 2.0 - 1.0, - 0.0, - f32(tmp2) * 2.0 - 1.0 - ) * 160.0; - - var result: GroundOutput; - result.pos = pos; - result.position = r_data.proj * r_data.view * vec4(pos, 1.0); - return result; -} - struct EntityOutput { @builtin(position) position: vec4, @location(1) texture: vec2, @@ -111,21 +89,3 @@ fn fs_entity(vertex: EntityOutput) -> @location(0) vec4 { let reflected_color = textureSample(r_texture, r_sampler, reflected).rgb; return vec4(mix(vec3(0.1) + 0.5 * reflected_color,texture_color,1.0-pow(1.0-abs(d),2.0)), 1.0); } - -fn modulo_euclidean (a: f32, b: f32) -> f32 { - var m = a % b; - if (m < 0.0) { - if (b < 0.0) { - m -= b; - } else { - m += b; - } - } - return m; -} - -@fragment -fn fs_ground(vertex: GroundOutput) -> @location(0) vec4 { - let dir = vec3(-1.0)+vec3(modulo_euclidean(vertex.pos.x/16.,1.0),0.0,modulo_euclidean(vertex.pos.z/16.,1.0))*2.0; - return vec4(textureSample(r_texture, r_sampler, dir).rgb, 1.0); -}