diff --git a/src/main.rs b/src/main.rs index b8d66d4..bd20b34 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,7 @@ const IMAGE_SIZE: u32 = 128; #[repr(C)] struct Vertex { pos: [f32; 3], + texture: [f32; 2], normal: [f32; 3], } @@ -182,6 +183,7 @@ fn add_obj(device:&wgpu::Device,modeldatas:& mut Vec,source:&[u8]){ let i=vertices.len() as u16; vertices.push(Vertex { pos: data.position[vert.0], + texture: data.texture[vert.1.unwrap()], normal: data.normal[vert.2.unwrap()], }); vertex_index.insert(vert,i); @@ -353,7 +355,7 @@ impl strafe_client::framework::Example for Skybox { buffers: &[wgpu::VertexBufferLayout { array_stride: std::mem::size_of::() as wgpu::BufferAddress, step_mode: wgpu::VertexStepMode::Vertex, - attributes: &wgpu::vertex_attr_array![0 => Float32x3, 1 => Float32x3], + attributes: &wgpu::vertex_attr_array![0 => Float32x3, 1 => Float32x2, 2 => Float32x3], }], }, fragment: Some(wgpu::FragmentState { diff --git a/src/shader.wgsl b/src/shader.wgsl index fe4ee93..c3e98ad 100644 --- a/src/shader.wgsl +++ b/src/shader.wgsl @@ -63,7 +63,8 @@ fn vs_ground(@builtin(vertex_index) vertex_index: u32) -> GroundOutput { struct EntityOutput { @builtin(position) position: vec4, - @location(1) normal: vec3, + @location(1) texture: vec2, + @location(2) normal: vec3, @location(3) view: vec3, }; @@ -74,11 +75,13 @@ var r_EntityTransform: mat4x4; @vertex fn vs_entity( @location(0) pos: vec3, - @location(1) normal: vec3, + @location(1) texture: vec2, + @location(2) normal: vec3, ) -> EntityOutput { var position: vec4 = r_EntityTransform * vec4(pos, 1.0); var result: EntityOutput; result.normal = (r_EntityTransform * vec4(normal, 0.0)).xyz; + result.texture=texture; result.view = position.xyz - r_data.cam_pos.xyz; result.position = r_data.proj * r_data.view * position; return result; @@ -100,10 +103,13 @@ fn fs_sky(vertex: SkyOutput) -> @location(0) vec4 { fn fs_entity(vertex: EntityOutput) -> @location(0) vec4 { let incident = normalize(vertex.view); let normal = normalize(vertex.normal); - let reflected = incident - 2.0 * dot(normal, incident) * normal; + let d = dot(normal, incident); + let reflected = incident - 2.0 * d * normal; + let dir = vec3(-1.0)+2.0*vec3(vertex.texture.x,0.0,vertex.texture.y); + let texture_color = textureSample(r_texture, r_sampler, dir).rgb; let reflected_color = textureSample(r_texture, r_sampler, reflected).rgb; - return vec4(vec3(0.1) + 0.5 * reflected_color, 1.0); + return vec4(mix(vec3(0.1) + 0.5 * reflected_color,texture_color,abs(d)), 1.0); } fn modulo_euclidean (a: f32, b: f32) -> f32 {