added lighting to terrain

pull/379/head
Ben Hansen 2 years ago
parent f6c701633f
commit d2bc2134dc

@ -503,6 +503,7 @@ impl State {
chunk_size,
min_max_height,
&camera_bind_group_layout,
&light_bind_group_layout,
config.format,
Some(texture::Texture::DEPTH_FORMAT),
);
@ -662,7 +663,7 @@ impl State {
// &self.light_bind_group,
// );
self.terrain_pipeline.render(&mut render_pass, &self.terrain, &self.camera_bind_group);
self.terrain_pipeline.render(&mut render_pass, &self.terrain, &self.camera_bind_group, &self.light_bind_group);
}
self.queue.submit(iter::once(encoder.finish()));
output.present();

@ -68,6 +68,7 @@ impl TerrainPipeline {
chunk_size: cgmath::Vector2<u32>,
min_max_height: cgmath::Vector2<f32>,
camera_layout: &wgpu::BindGroupLayout,
light_layout: &wgpu::BindGroupLayout,
color_format: wgpu::TextureFormat,
depth_format: Option<wgpu::TextureFormat>,
) -> Self {
@ -124,7 +125,7 @@ impl TerrainPipeline {
let render_pipeline_layout =
device.create_pipeline_layout(&wgpu::PipelineLayoutDescriptor {
label: Some("TerrainPipeline::Render::PipelineLayout"),
bind_group_layouts: &[camera_layout],
bind_group_layouts: &[camera_layout, light_layout],
push_constant_ranges: &[],
});
let render_pipeline = create_render_pipeline(
@ -267,6 +268,7 @@ impl TerrainPipeline {
render_pass: &'b mut wgpu::RenderPass<'a>,
terrain: &'a Terrain,
camera_bind_group: &'a wgpu::BindGroup,
light_bind_group: &'a wgpu::BindGroup,
) {
render_pass.set_pipeline(&self.render_pipeline);
render_pass.set_bind_group(0, camera_bind_group, &[]);

@ -143,12 +143,12 @@ struct Camera {
[[group(0), binding(0)]]
var<uniform> camera: Camera;
// struct Light {
// position: vec3<f32>;
// color: vec3<f32>;
// };
// [[group(1), binding(0)]]
// var<uniform> light: Light;
struct Light {
position: vec3<f32>;
color: vec3<f32>;
};
[[group(1), binding(0)]]
var<uniform> light: Light;
struct VertexOutput {
[[builtin(position)]] clip_position: vec4<f32>;
@ -165,14 +165,14 @@ fn vs_main(
return VertexOutput(clip_position, normal, vertex.position);
}
// [[group(2), binding(0)]]
// var t_diffuse: texture_2d<f32>;
// [[group(2), binding(1)]]
// var s_diffuse: sampler;
// [[group(2), binding(2)]]
// var t_normal: texture_2d<f32>;
// [[group(2), binding(3)]]
// var s_normal: sampler;
[[group(2), binding(0)]]
var t_diffuse: texture_2d<f32>;
[[group(2), binding(1)]]
var s_diffuse: sampler;
[[group(2), binding(2)]]
var t_normal: texture_2d<f32>;
[[group(2), binding(3)]]
var s_normal: sampler;
fn color23(p: vec2<f32>) -> vec3<f32> {
return vec3<f32>(
@ -187,16 +187,20 @@ fn fs_main(in: VertexOutput) -> [[location(0)]] vec4<f32> {
var color = smoothStep(vec3<f32>(0.0), vec3<f32>(0.1), fract(in.world_pos));
color = mix(vec3<f32>(0.5, 0.1, 0.7), vec3<f32>(0.2, 0.2, 0.2), vec3<f32>(color.x * color.y * color.z));
let uv = in.world_pos.xz;
let i = floor(uv / 256.);
let ambient_strength = 0.1;
let ambient_color = light.color * ambient_strength;
let light_dir = normalize(light.position - in.world_pos);
let view_dir = normalize(camera.view_pos.xyz - in.world_pos);
let half_dir = normalize(view_dir + light_dir);
let diffuse_strength = max(dot(in.normal, light_dir), 0.0);
let diffuse_color = diffuse_strength * light.color;
let specular_strength = pow(max(dot(in.normal, half_dir), 0.0), 32.0);
let specular_color = specular_strength * light.color;
color = color23(i);
let result = (ambient_color + diffuse_color + specular_color) * color;
let f = fbm(uv) * 0.5 + 0.5;
// let f = (in.world_pos.y + 10.) * 0.1;
color = color * f;
// color = vec3<f32>(fract(uv), 0.5);
// let v = terrain_point(uv);
// color = vec3<f32>(in.clip_position.z);
return vec4<f32>(color, 1.0);
return vec4<f32>(result, 1.0);
}
Loading…
Cancel
Save