Switch computers
ci/woodpecker/push/build Pipeline failed Details

This commit is contained in:
SeanOMik 2023-11-14 23:04:32 -05:00
parent e23d4dc731
commit b2bbbbbfad
Signed by: SeanOMik
GPG Key ID: 568F326C7EB33ACB
4 changed files with 53 additions and 19 deletions

View File

@ -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<Resource<Texture>>,
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,
}
}
}

View File

@ -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,
}

View File

@ -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<RenderTexture>,
//#[allow(dead_code)]
//render_texture: Option<RenderTexture>,
material: Option<Material>,
texture_bindgroup: Option<BindGroup>,
// 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,
}
}

View File

@ -64,46 +64,56 @@ fn vs_main(
// Fragment shader
struct Material {
ambient: vec3<f32>,
diffuse: vec3<f32>,
specular: vec3<f32>,
shininess: f32,
}
@group(0) @binding(0)
var t_diffuse: texture_2d<f32>;
@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<f32> {
let object_color: vec4<f32> = textureSample(t_diffuse, s_diffuse, in.tex_coords);
var light_res = vec3<f32>(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<f32>(light_object_res, object_color.a);
}
fn blinn_phong_point_light(world_pos: vec3<f32>, world_norm: vec3<f32>, point_light: PointLight) -> vec3<f32> {
fn blinn_phong_point_light(world_pos: vec3<f32>, world_norm: vec3<f32>, point_light: PointLight, material: Material) -> vec3<f32> {
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 ////