From b2bbbbbfad5fdbc6f3f92e0bc261209e231a082d Mon Sep 17 00:00:00 2001 From: SeanOMik Date: Tue, 14 Nov 2023 23:04:32 -0500 Subject: [PATCH] Switch computers --- src/render/material.rs | 28 +++++++++++++++++++++++----- src/render/render_job.rs | 3 ++- src/render/renderer.rs | 15 ++++++++++----- src/render/shaders/base.wgsl | 26 ++++++++++++++++++-------- 4 files changed, 53 insertions(+), 19 deletions(-) diff --git a/src/render/material.rs b/src/render/material.rs index fa5de81..ceccc57 100755 --- a/src/render/material.rs +++ b/src/render/material.rs @@ -1,9 +1,27 @@ -use std::sync::Arc; - -use lyra_resource::{Texture, Resource}; +use super::texture::RenderTexture; #[derive(Clone)] pub struct Material { - pub shader_id: u32, - pub texture: Arc>, + pub shader_id: u64, + pub diffuse_texture: RenderTexture, + pub ambient: glam::Vec3, + pub diffuse: glam::Vec3, + pub specular: glam::Vec3, + pub shininess: f32, +} + +impl Material { + pub fn from_resource(device: &wgpu::Device, queue: &wgpu::Queue, value: &lyra_resource::Material) -> Self { + let image = value.base_color_texture.as_ref().map(|t| &t.data.as_ref().unwrap().image).unwrap(); + let diffuse_texture = RenderTexture::from_image(device, queue, image, None).unwrap(); + + Self { + shader_id: value.shader_uuid.unwrap_or(0), + diffuse_texture, + ambient: glam::Vec3::new(1.0, 0.5, 0.31), + diffuse: glam::Vec3::new(value.base_color.x, value.base_color.y, value.base_color.z), + specular: glam::Vec3::new(0.5, 0.5, 0.5), + shininess: 32.0, + } + } } \ No newline at end of file diff --git a/src/render/render_job.rs b/src/render/render_job.rs index a15db01..91424aa 100755 --- a/src/render/render_job.rs +++ b/src/render/render_job.rs @@ -2,11 +2,12 @@ use edict::EntityId; use crate::math::Transform; +use super::material::Material; + pub struct RenderJob { pub entity: EntityId, pub shader_id: u64, pub mesh_buffer_id: uuid::Uuid, - pub transform: Transform, } diff --git a/src/render/renderer.rs b/src/render/renderer.rs index b726888..7078581 100755 --- a/src/render/renderer.rs +++ b/src/render/renderer.rs @@ -23,6 +23,7 @@ use crate::render::light::PointLightUniform; use super::camera::{RenderCamera, CameraUniform}; use super::desc_buf_lay::DescVertexBufferLayout; use super::light::{PointLight, LightUniformBuffers}; +use super::material::Material; use super::texture::RenderTexture; use super::transform_buffer_storage::{TransformBufferIndices, TransformBuffers}; use super::vertex::Vertex; @@ -43,8 +44,9 @@ struct MeshBufferStorage { buffer_vertex: BufferStorage, buffer_indices: Option<(wgpu::IndexFormat, BufferStorage)>, - #[allow(dead_code)] - render_texture: Option, + //#[allow(dead_code)] + //render_texture: Option, + material: Option, texture_bindgroup: Option, // The index of the transform for this entity. @@ -89,6 +91,9 @@ pub struct BasicRenderer { default_texture_bind_group: BindGroup, depth_buffer_texture: RenderTexture, + material_buffer: wgpu::Buffer, + material_bind_group: wgpu::BindGroup, + light_buffers: LightUniformBuffers, } @@ -380,8 +385,8 @@ impl BasicRenderer { let (vertex_buffer, buffer_indices) = self.create_vertex_index_buffers(mesh); let diffuse_bindgroup = if let Some(model_texture) = &mesh.material().base_color_texture { - let image = &model_texture.data.as_ref().unwrap().image; - let diffuse_texture = RenderTexture::from_image(&self.device, &self.queue, image, None).unwrap(); + let material = Material::from_resource(&self.device, &self.queue, &mesh.material()); + let diffuse_texture = &material.diffuse_texture; let diffuse_bind_group = self.device.create_bind_group( &wgpu::BindGroupDescriptor { @@ -408,7 +413,7 @@ impl BasicRenderer { MeshBufferStorage { buffer_vertex: vertex_buffer, buffer_indices, - render_texture: None, + material: None, texture_bindgroup: diffuse_bindgroup, } } diff --git a/src/render/shaders/base.wgsl b/src/render/shaders/base.wgsl index b9d85c5..f372fe7 100755 --- a/src/render/shaders/base.wgsl +++ b/src/render/shaders/base.wgsl @@ -64,46 +64,56 @@ fn vs_main( // Fragment shader +struct Material { + ambient: vec3, + diffuse: vec3, + specular: vec3, + shininess: f32, +} + @group(0) @binding(0) var t_diffuse: texture_2d; -@group(0)@binding(1) +@group(0) @binding(1) var s_diffuse: sampler; +@group(4) @binding(0) +var u_material: Material; + @fragment fn fs_main(in: VertexOutput) -> @location(0) vec4 { let object_color: vec4 = textureSample(t_diffuse, s_diffuse, in.tex_coords); var light_res = vec3(0.0); for (var i = 0u; i < u_lights.point_light_count; i++) { - light_res += blinn_phong_point_light(in.world_position, in.world_normal, u_lights.point_lights[i]); + light_res += blinn_phong_point_light(in.world_position, in.world_normal, u_lights.point_lights[i], u_material); } let light_object_res = light_res * object_color.xyz; return vec4(light_object_res, object_color.a); } -fn blinn_phong_point_light(world_pos: vec3, world_norm: vec3, point_light: PointLight) -> vec3 { +fn blinn_phong_point_light(world_pos: vec3, world_norm: vec3, point_light: PointLight, material: Material) -> vec3 { let light_color = point_light.color.xyz; let light_pos = point_light.position.xyz; let camera_view_pos = u_camera.view_pos.xyz; // We don't need (or want) much ambient light, so 0.1 is fine - let ambient_strength = 0.1; - var ambient_color = light_color * ambient_strength; + //let ambient_strength = 0.1; + var ambient_color = light_color * material.ambient; //// diffuse //// let light_dir = normalize(light_pos - world_pos); let diffuse_strength = max(dot(world_norm, light_dir), 0.0); - var diffuse_color = light_color * diffuse_strength; + var diffuse_color = light_color * (diffuse_strength * material.diffuse); //// end of diffuse //// //// specular //// let view_dir = normalize(camera_view_pos - world_pos); let half_dir = normalize(view_dir + light_dir); - let specular_strength = pow(max(dot(world_norm, half_dir), 0.0), 32.0); - var specular_color = specular_strength * light_color; + let specular_strength = pow(max(dot(world_norm, half_dir), 0.0), material.shininess); + var specular_color = specular_strength * (light_color * material.specular); //// end of specular //// //// point light attenuation ////