Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
adrien-ben committed May 9, 2024
1 parent 8e2813d commit 185a8ca
Show file tree
Hide file tree
Showing 23 changed files with 569 additions and 396 deletions.
8 changes: 8 additions & 0 deletions crates/libs/model/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub struct Model {
animations: Option<Animations>,
skins: Vec<Skin>,
textures: Textures,
materials: Vec<Material>,
lights: Vec<Light>,
}

Expand Down Expand Up @@ -99,6 +100,8 @@ impl Model {
&images,
);

let materials = create_materials_from_gltf(&document);

let lights = create_lights_from_gltf(&document);

let model = Model {
Expand All @@ -109,6 +112,7 @@ impl Model {
animations,
skins,
textures,
materials,
lights,
};

Expand Down Expand Up @@ -220,6 +224,10 @@ impl Model {
&self.textures.textures
}

pub fn materials(&self) -> &[Material] {
&self.materials
}

pub fn lights(&self) -> &[Light] {
&self.lights
}
Expand Down
29 changes: 29 additions & 0 deletions crates/libs/model/src/material.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use gltf::{
material::{AlphaMode, Material as GltfMaterial, NormalTexture, OcclusionTexture},
texture::Info,
Document,
};

const ALPHA_MODE_OPAQUE: u32 = 0;
Expand All @@ -24,6 +25,30 @@ pub struct Material {
clearcoat: Option<Clearcoat>,
}

impl Default for Material {
fn default() -> Self {
Self {
color: [1.0, 1.0, 1.0, 1.0],
emissive: [0.0, 0.0, 0.0],
occlusion: 0.0,
color_texture: None,
emissive_texture: None,
normals_texture: None,
occlusion_texture: None,
workflow: Workflow::MetallicRoughness(MetallicRoughnessWorkflow {
metallic: 1.0,
roughness: 1.0,
metallic_roughness_texture: None,
}),
alpha_mode: ALPHA_MODE_OPAQUE,
alpha_cutoff: 0.5,
double_sided: false,
is_unlit: false,
clearcoat: None,
}
}
}

#[derive(Clone, Copy, Debug)]
pub struct TextureInfo {
index: usize,
Expand Down Expand Up @@ -213,6 +238,10 @@ impl TextureInfo {
}
}

pub(crate) fn create_materials_from_gltf(document: &Document) -> Vec<Material> {
document.materials().map(Material::from).collect()
}

impl<'a> From<GltfMaterial<'a>> for Material {
fn from(material: GltfMaterial) -> Material {
let color = match material.pbr_specular_glossiness() {
Expand Down
8 changes: 8 additions & 0 deletions crates/libs/model/src/mesh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ pub struct Primitive {
vertices: VertexBuffer,
indices: Option<IndexBuffer>,
material: Material,
material_index: Option<usize>,
aabb: Aabb<f32>,
}

Expand All @@ -62,6 +63,10 @@ impl Primitive {
self.material
}

pub fn material_index(&self) -> Option<usize> {
self.material_index
}

pub fn aabb(&self) -> Aabb<f32> {
self.aabb
}
Expand All @@ -78,6 +83,7 @@ struct PrimitiveData {
indices: Option<IndexBufferPart>,
vertices: VertexBufferPart,
material: Material,
material_index: Option<usize>,
aabb: Aabb<f32>,
}

Expand Down Expand Up @@ -172,6 +178,7 @@ pub fn create_meshes_from_gltf(
indices,
vertices: (offset, accessor.count()),
material,
material_index: primitive.material().index(),
aabb,
});
}
Expand Down Expand Up @@ -227,6 +234,7 @@ pub fn create_meshes_from_gltf(
vertices: vertex_buffer,
indices: index_buffer,
material: buffers.material,
material_index: buffers.material_index,
aabb: buffers.aabb,
}
})
Expand Down
18 changes: 8 additions & 10 deletions crates/viewer/shaders/gbuffer.vert
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
#version 450
#extension GL_GOOGLE_include_directive : require

#include "libs/camera.glsl"

layout(location = 0) in vec3 vPositions;
layout(location = 1) in vec3 vNormals;
Expand All @@ -9,14 +12,9 @@ layout(location = 5) in vec4 vWeights;
layout(location = 6) in uvec4 vJoints;
layout(location = 7) in vec4 vColors;

layout(binding = 0, set = 0) uniform CameraUBO {
mat4 view;
mat4 proj;
mat4 invertedProj;
vec4 eye;
float zNear;
float zFar;
} cameraUBO;
layout(binding = 0, set = 0) uniform Frame {
Camera camera;
};

layout(binding = 1, set = 0) uniform TransformUBO {
mat4 matrix;
Expand All @@ -40,10 +38,10 @@ void main() {
+ vWeights.w * skin.jointMatrices[vJoints.w];
}

oViewSpaceNormal = normalize((cameraUBO.view * world * vec4(vNormals, 0.0)).xyz);
oViewSpaceNormal = normalize((camera.view * world * vec4(vNormals, 0.0)).xyz);
oTexcoords0 = vTexcoords0;
oTexcoords1 = vTexcoords1;
oAlpha = vColors.a;

gl_Position = cameraUBO.proj * cameraUBO.view * world * vec4(vPositions, 1.0);
gl_Position = camera.proj * camera.view * world * vec4(vPositions, 1.0);
}
Binary file modified crates/viewer/shaders/gbuffer.vert.spv
Binary file not shown.
8 changes: 8 additions & 0 deletions crates/viewer/shaders/libs/camera.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
struct Camera {
mat4 view;
mat4 proj;
mat4 invertedProj;
vec4 eye;
float zNear;
float zFar;
};
58 changes: 58 additions & 0 deletions crates/viewer/shaders/libs/material.glsl
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
const uint NO_TEXTURE_ID = 255;

const uint ALPHA_MODE_MASK = 1;
const uint ALPHA_MODE_BLEND = 2;
const float ALPHA_CUTOFF_BIAS = 0.0000001;

const uint METALLIC_ROUGHNESS_WORKFLOW = 0;

struct Material {
vec4 color;
vec3 emissiveFactor;
// - roughness for metallic/roughness workflows
// - glossiness for specular/glossiness workflows
float roughnessGlossiness;
// Contains the metallic (or specular) factor.
// - metallic: r (for metallic/roughness workflows)
// - specular: rgb (for specular/glossiness workflows)
vec3 metallicSpecular;
float occlusion;
float alphaCutoff;
float clearcoatFactor;
float clearcoatRoughness;
uint colorTextureChannel;
uint materialTextureChannel;
uint emissiveTextureChannel;
uint normalsTextureChannel;
uint occlusionTextureChannel;
uint clearcoatFactorTextureChannel;
uint clearcoatRoughnessTextureChannel;
uint clearcoatNormalTextureChannel;
uint alphaMode;
bool isUnlit;
uint workflow;
};

struct TextureChannels {
uint color;
uint material;
uint emissive;
uint normal;
uint occlusion;
uint clearcoatFactor;
uint clearcoatRoughness;
uint clearcoatNormal;
};

TextureChannels getTextureChannels(Material m) {
return TextureChannels(
m.colorTextureChannel,
m.materialTextureChannel,
m.emissiveTextureChannel,
m.normalsTextureChannel,
m.occlusionTextureChannel,
m.clearcoatFactorTextureChannel,
m.clearcoatRoughnessTextureChannel,
m.clearcoatNormalTextureChannel
);
}
Loading

0 comments on commit 185a8ca

Please sign in to comment.