Skip to content

armgit / raea

Public Imported from GitHub
0

Render Anything Engine: tell your AI what to make. Videos, motion graphics, 3D and games as React and TypeScript, rendered on your own GPU. Rust + Bevy, macOS, Linux and Windows.

Solari and the path tracer trace vertex colours

shinyobjectzd58fade59c1f
diff --git a/engine/render/raytrace/src/solari.rs b/engine/render/raytrace/src/solari.rs
index d08dd9ef..8821ef9d 100644
--- a/engine/render/raytrace/src/solari.rs
+++ b/engine/render/raytrace/src/solari.rs
@@ -326,7 +326,7 @@ fn traceable(src: &Mesh, mapped: bool) -> Option<Traceable> {
// draws into the G-buffer, lit by Solari; it just isn't in the traced scene
let ids: Vec<_> = src.try_attributes().ok()?.map(|(a, _)| a.id).collect();
let indices = src.try_indices_option().ok()?;
- let layout = [Mesh::ATTRIBUTE_POSITION.id, Mesh::ATTRIBUTE_NORMAL.id, Mesh::ATTRIBUTE_UV_0.id, Mesh::ATTRIBUTE_TANGENT.id];
+ let layout = [Mesh::ATTRIBUTE_POSITION.id, Mesh::ATTRIBUTE_NORMAL.id, Mesh::ATTRIBUTE_UV_0.id, Mesh::ATTRIBUTE_TANGENT.id, Mesh::ATTRIBUTE_COLOR.id];
if ids == layout && matches!(indices, Some(Indices::U32(_))) {
return Some(Traceable::Same);
}
@@ -338,6 +338,13 @@ fn traceable(src: &Mesh, mapped: bool) -> Option<Traceable> {
out.insert_attribute(Mesh::ATTRIBUTE_POSITION, pos);
out.insert_attribute(Mesh::ATTRIBUTE_NORMAL, normal);
out.insert_attribute(Mesh::ATTRIBUTE_UV_0, uv);
+ // its vertex colours, which the base colour is multiplied by traced as in raster (vendored Solari's PackedVertex),
+ // or white. Without them a forest's vertex-coloured bark and moss traced grey
+ let color = match src.attribute(Mesh::ATTRIBUTE_COLOR) {
+ Some(VertexAttributeValues::Float32x4(c)) if c.len() == n => c.clone(),
+ _ => vec![[1.0f32; 4]; n],
+ };
+ out.insert_attribute(Mesh::ATTRIBUTE_COLOR, color);
let idx: Vec<u32> = match indices {
Some(Indices::U32(v)) => v.clone(),
Some(Indices::U16(v)) => v.iter().map(|&i| i as u32).collect(),
@@ -387,6 +394,23 @@ mod tests {
assert_eq!(tangents(true).len(), 3);
}
+ /// A mesh's vertex colours go into its traced copy, which the tracer multiplies the base colour by; a mesh with none
+ /// is traced white.
+ #[test]
+ fn vertex_colours_are_traced() {
+ let colour = |m: &Mesh| match traceable(m, false) {
+ Some(Traceable::Copy(c)) => match c.attribute(Mesh::ATTRIBUTE_COLOR) {
+ Some(VertexAttributeValues::Float32x4(v)) => v.clone(),
+ _ => panic!("no colour in the copy"),
+ },
+ _ => panic!("not a copy"),
+ };
+ assert_eq!(colour(&quad(RenderAssetUsages::default())), vec![[1.0f32; 4]; 3]);
+ let mut green = quad(RenderAssetUsages::default());
+ green.insert_attribute(Mesh::ATTRIBUTE_COLOR, vec![[0.1f32, 0.3, 0.05, 1.0]; 3]);
+ assert_eq!(colour(&green), vec![[0.1f32, 0.3, 0.05, 1.0]; 3]);
+ }
+
#[test]
fn an_additive_or_multiplying_surface_is_not_traced() {
// additive dust cards traced as opaque shaded a sunlit wall; opaque, masked and blended surfaces stay traced
diff --git a/vendor/bevy_solari/src/scene/bindings.wesl b/vendor/bevy_solari/src/scene/bindings.wesl
index 82646266..53b0f8ca 100644
--- a/vendor/bevy_solari/src/scene/bindings.wesl
+++ b/vendor/bevy_solari/src/scene/bindings.wesl
@@ -16,10 +16,13 @@ struct VertexBuffer { vertices: array<PackedVertex> }
struct IndexBuffer { indices: array<u32> }
+// raea: the vertex colour after the tangent (Bevy lays a mesh's attributes out by id: position, normal, uv, tangent,
+// colour). A mesh without one is traced white (raea-render-raytrace solari.rs traceable)
struct PackedVertex {
a: vec4<f32>,
b: vec4<f32>,
tangent: vec4<f32>,
+ color: vec4<f32>,
}
struct Vertex {
@@ -27,6 +30,7 @@ struct Vertex {
normal: vec3<f32>,
uv: vec2<f32>,
tangent: vec4<f32>,
+ color: vec4<f32>,
}
fn unpack_vertex(packed: PackedVertex) -> Vertex {
@@ -35,6 +39,7 @@ fn unpack_vertex(packed: PackedVertex) -> Vertex {
vertex.normal = vec3(packed.a.w, packed.b.xy);
vertex.uv = packed.b.zw;
vertex.tangent = packed.tangent;
+ vertex.color = packed.color;
return vertex;
}
@@ -355,7 +360,9 @@ fn resolve_triangle_data_full(instance_id: u32, triangle_id: u32, barycentrics:
let triangle_edge1 = world_vertices[0] - world_vertices[2];
let triangle_area = length(cross(triangle_edge0, triangle_edge1)) / 2.0;
- let resolved_material = resolve_material(material, uv);
+ var resolved_material = resolve_material(material, uv);
+ // raea: the vertex colour multiplies the base colour, as Bevy's StandardMaterial does in raster
+ resolved_material.base_color *= (mat3x4(vertices[0].color, vertices[1].color, vertices[2].color) * barycentrics).rgb;
return ResolvedRayHitFull(
world_position,
diff --git a/vendor/bevy_solari/src/scene/blas.rs b/vendor/bevy_solari/src/scene/blas.rs
index aba22a05..ae3a2f02 100644
--- a/vendor/bevy_solari/src/scene/blas.rs
+++ b/vendor/bevy_solari/src/scene/blas.rs
@@ -139,7 +139,8 @@ pub fn prepare_raytracing_blas(
size: blas_size,
vertex_buffer: vertex_slice.buffer,
first_vertex: vertex_slice.range.start,
- vertex_stride: 48,
+ // raea: position, normal, uv, tangent and the vertex colour (scene/bindings.wesl PackedVertex)
+ vertex_stride: 64,
index_buffer: Some(index_slice.buffer),
first_index: Some(index_slice.range.start),
transform_buffer: None,
@@ -263,6 +264,7 @@ fn is_mesh_raytracing_compatible(mesh: &Mesh) -> bool {
(Mesh::ATTRIBUTE_NORMAL.id, Mesh::ATTRIBUTE_NORMAL.format),
(Mesh::ATTRIBUTE_UV_0.id, Mesh::ATTRIBUTE_UV_0.format),
(Mesh::ATTRIBUTE_TANGENT.id, Mesh::ATTRIBUTE_TANGENT.format),
+ (Mesh::ATTRIBUTE_COLOR.id, Mesh::ATTRIBUTE_COLOR.format),
]);
let indexed_32 = matches!(mesh.indices(), Some(Indices::U32(..)));
mesh.enable_raytracing && triangle_list && vertex_attributes && indexed_32