Skip to content

Commit 1a876e1

Browse files
committed
skinning WIP
1 parent 2d85a45 commit 1a876e1

File tree

11 files changed

+245
-668
lines changed

11 files changed

+245
-668
lines changed

Cargo.lock

Lines changed: 9 additions & 429 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ gltf = { git = 'https://github.com/schell/gltf.git', branch ="feature/channel-sa
2323
image = "0.24"
2424
log = "0.4"
2525
naga = { version = "0.19", features = ["spv-in", "wgsl-out", "wgsl-in", "msl-out"] }
26-
plotters = "0.3"
2726
pretty_assertions = "1.4.0"
2827
proc-macro2 = { version = "1.0", features = ["span-locations"] }
2928
glam = { version = "0.24.2", default-features = false }

crates/example/src/lib.rs

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use renderling::{
1111
math::{Mat4, UVec2, Vec3, Vec4},
1212
skybox::Skybox,
1313
slab::Hybrid,
14-
stage::{Animator, GltfDocument, Node, Stage},
14+
stage::{Animator, GltfDocument, Stage},
1515
transform::Transform,
1616
Context,
1717
};
@@ -68,7 +68,6 @@ pub struct App {
6868
camera: Hybrid<Camera>,
6969

7070
document: Option<GltfDocument>,
71-
nodes: Option<Vec<Node>>,
7271
animators: Option<Vec<Animator>>,
7372
animations_conflict: bool,
7473

@@ -106,7 +105,6 @@ impl App {
106105
camera,
107106

108107
document: None,
109-
nodes: None,
110108
animators: None,
111109
animations_conflict: false,
112110

@@ -162,11 +160,13 @@ impl App {
162160
self.last_cursor_position = None;
163161
self.stage.set_images(std::iter::empty()).unwrap();
164162
self.document = None;
165-
self.nodes = None;
166163
log::debug!("ticking stage to reclaim buffers");
167164
self.stage.tick();
168165

169-
let mut doc = match self.stage.load_gltf_document_from_bytes(bytes) {
166+
let doc = match self
167+
.stage
168+
.load_gltf_document_from_bytes(bytes, self.camera.id())
169+
{
170170
Err(e) => {
171171
log::error!("gltf loading error: {e}");
172172
return;
@@ -204,13 +204,7 @@ impl App {
204204
}
205205
}
206206
}
207-
let nodes = match self.stage.draw_gltf_scene(&doc, nodes, self.camera.id()) {
208-
Err(e) => {
209-
log::error!("could not draw scene: {e}");
210-
vec![]
211-
}
212-
Ok(ns) => ns,
213-
};
207+
214208
if doc.animations.is_empty() {
215209
log::trace!(" animations: none");
216210
} else {
@@ -219,8 +213,8 @@ impl App {
219213
let mut animated_nodes = HashSet::default();
220214
let mut has_conflicting_animations = false;
221215
self.animators = Some(
222-
std::mem::take(&mut doc.animations)
223-
.into_iter()
216+
doc.animations
217+
.iter()
224218
.enumerate()
225219
.map(|(i, a)| {
226220
let target_nodes = a.target_node_indices().collect::<HashSet<_>>();
@@ -236,15 +230,14 @@ impl App {
236230
tween.properties.description()
237231
);
238232
}
239-
Animator::new(&nodes, a)
233+
Animator::new(doc.nodes.iter(), a.clone())
240234
})
241235
.collect(),
242236
);
243237
if has_conflicting_animations {
244238
log::trace!(" and some animations conflict");
245239
}
246240
self.animations_conflict = has_conflicting_animations;
247-
self.nodes = Some(nodes);
248241
self.document = Some(doc);
249242

250243
let halfway_point = min + ((max - min).normalize() * ((max - min).length() / 2.0));

crates/renderling/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,11 @@ features = ["gltf", "raw-window-handle", "winit"]
119119

120120
[dev-dependencies]
121121
assert_approx_eq = {workspace = true}
122-
criterion = { version = "0.5", features = ["html_reports"] }
123122
ctor = "0.2.2"
124123
env_logger = {workspace = true}
125124
icosahedron = "0.1"
126125
img-diff = { path = "../img-diff" }
127126
naga.workspace = true
128-
plotters.workspace = true
129127
pretty_assertions.workspace = true
130128
ttf-parser = "0.20.0"
131129

crates/renderling/src/bloom/cpu.rs

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -713,21 +713,19 @@ mod test {
713713
let height = 128;
714714
let ctx = Context::headless(width, height);
715715
let mut stage = ctx.new_stage().with_bloom(false);
716-
let doc = stage
717-
.load_gltf_document_from_path("../../gltf/EmissiveStrengthTest.glb")
718-
.unwrap();
716+
719717
let projection = crate::camera::perspective(width as f32, height as f32);
720718
let view = crate::camera::look_at(Vec3::new(0.0, 2.0, 18.0), Vec3::ZERO, Vec3::Y);
721719
let camera = stage.new_value(Camera::new(projection, view));
722720
let skybox = stage
723721
.new_skybox_from_path("../../img/hdr/night.hdr", camera.id())
724722
.unwrap();
725723
stage.set_skybox(skybox);
726-
let scene_index = doc.default_scene.unwrap();
727-
let nodes = doc.scenes.get(scene_index).unwrap();
728-
let _scene = stage
729-
.draw_gltf_scene(&doc, nodes.into_iter().copied(), camera.id())
724+
725+
let _doc = stage
726+
.load_gltf_document_from_path("../../gltf/EmissiveStrengthTest.glb", camera.id())
730727
.unwrap();
728+
731729
let frame = ctx.get_next_frame().unwrap();
732730
stage.render(&frame.view());
733731
let img = frame.read_image().unwrap();
Binary file not shown.
Binary file not shown.
Binary file not shown.

crates/renderling/src/stage.rs

Lines changed: 20 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ pub use gltf_support::*;
3434
/// For more info on vertex skinning, see
3535
/// <https://github.khronos.org/glTF-Tutorials/gltfTutorial/gltfTutorial_019_SimpleSkin.html>
3636
#[derive(Clone, Copy, Default, SlabItem)]
37+
#[cfg_attr(not(target_arch = "spirv"), derive(Debug))]
3738
pub struct Skin {
3839
// Ids of the skeleton nodes' global transforms used as joints in this skin.
3940
pub joints: Array<Id<Transform>>,
@@ -45,22 +46,27 @@ pub struct Skin {
4546
}
4647

4748
impl Skin {
48-
pub fn get_transform(&self, vertex: Vertex, slab: &[u32]) -> Transform {
49+
pub fn get_transform(&self, vertex: Vertex, slab: &[u32]) -> Mat4 {
4950
let mut mat = Mat4::ZERO;
5051
for i in 0..vertex.joints.len() {
52+
let joint_weight = vertex.weights[i];
53+
if joint_weight == 0.0 {
54+
continue;
55+
}
56+
57+
let inverse_bind_matrix = slab.read(self.inverse_bind_matrices.at(i));
58+
5159
let joint_index = vertex.joints[i] as usize;
5260
let joint_id = slab.read(self.joints.at(joint_index));
53-
let inverse_bind_matrix = slab.read(self.inverse_bind_matrices.at(i));
5461
let joint_matrix = Mat4::from(slab.read(joint_id)) * inverse_bind_matrix;
55-
let joint_weight = vertex.weights[i];
56-
mat += joint_weight * joint_matrix;
62+
63+
mat += joint_matrix * joint_weight;
5764
}
58-
let mat = if mat == Mat4::ZERO {
65+
if mat == Mat4::ZERO {
5966
Mat4::IDENTITY
6067
} else {
6168
mat
62-
};
63-
Transform::from(mat)
69+
}
6470
}
6571
}
6672

@@ -245,6 +251,8 @@ pub fn renderlet_vertex(
245251
out_world_pos: &mut Vec3,
246252
#[spirv(position)] out_clip_pos: &mut Vec4,
247253
) {
254+
use crate::math::IsMatrix;
255+
248256
let renderlet = slab.read_unchecked(renderlet_id);
249257

250258
let mut vertex_log = RenderletVertexLog::new(renderlet_id, renderlet.debug_index, vertex_index);
@@ -266,18 +274,15 @@ pub fn renderlet_vertex(
266274
*out_uv0 = vertex.uv0;
267275
*out_uv1 = vertex.uv1;
268276

269-
let transform = if renderlet.skin_id.is_some() {
277+
let model_matrix = if renderlet.skin_id.is_some() {
270278
let skin = slab.read(renderlet.skin_id);
271279
skin.get_transform(vertex, slab)
272280
} else {
273-
slab.read(renderlet.transform_id)
281+
let t = slab.read(renderlet.transform_id);
282+
Mat4::from(t)
274283
};
275-
let model_matrix = Mat4::from_scale_rotation_translation(
276-
transform.scale,
277-
transform.rotation,
278-
transform.translation,
279-
);
280-
let scale2 = transform.scale * transform.scale;
284+
let (scale, _, _) = model_matrix.to_scale_rotation_translation_or_id();
285+
let scale2 = scale * scale;
281286
let normal = vertex.normal.alt_norm_or_zero();
282287
let tangent = vertex.tangent.xyz().alt_norm_or_zero();
283288
let normal_w: Vec3 = (model_matrix * (normal / scale2).extend(0.0))

0 commit comments

Comments
 (0)