From 36a1c47bb71614852d21663c17aa375d18e9bb45 Mon Sep 17 00:00:00 2001 From: Robin Deits Date: Tue, 22 Sep 2020 23:29:10 -0400 Subject: [PATCH] Support vertexColors for meshes via GeometryBasics.meta --- src/MeshCat.jl | 3 +++ src/lowering.jl | 13 ++++++++++--- test/visualizer.jl | 14 ++++++++++++++ 3 files changed, 27 insertions(+), 3 deletions(-) diff --git a/src/MeshCat.jl b/src/MeshCat.jl index 3e09d45..3a80ad5 100644 --- a/src/MeshCat.jl +++ b/src/MeshCat.jl @@ -10,6 +10,7 @@ using GeometryBasics: GeometryBasics, GeometryPrimitive, HyperRectangle, HyperSphere, + MeshMeta, NgonFace, OffsetInteger, Point, @@ -18,6 +19,8 @@ using GeometryBasics: GeometryBasics, SimplexFace, Vec, decompose, + meta, + metafree, origin, radius, texturecoordinates, diff --git a/src/lowering.jl b/src/lowering.jl index 443202e..bf023ee 100644 --- a/src/lowering.jl +++ b/src/lowering.jl @@ -144,10 +144,17 @@ to_zero_index(f::AbstractNgonFace{N}) where {N} = SVector(raw.(convert(NgonFace{ lower(faces::Vector{<:AbstractFace}) = lower(to_zero_index.(faces)) -function lower(mesh::AbstractVector{<:Polytope}) +function lower(mesh_meta::M) where {M <: AbstractMesh} + mesh = metafree(mesh_meta) attributes = Dict{String, Any}( - "position" => lower(convert(Vector{Point3f0}, decompose(Point3f0, mesh))) + "position" => lower(convert(Vector{Point3f0}, decompose(Point3f0, mesh))), ) + if M <: MeshMeta + metadata = meta(mesh_meta) + if hasfield(typeof(metadata), :vertexColors) + attributes["color"] = lower(convert(Vector{RGB{Float32}}, metadata.vertexColors)) + end + end uv = texturecoordinates(mesh) if uv !== nothing attributes["uv"] = lower(uv) @@ -175,7 +182,7 @@ function lower(cloud::PointCloud) "position" => lower(convert(Vector{Point3f0}, cloud.position)), ) if !isempty(cloud.color) - attributes["color"] = lower(cloud.color) + attributes["color"] = lower(convert(Vector{RGB{Float32}}, cloud.color)) end Dict{String, Any}( "uuid" => string(uuid1()), diff --git a/test/visualizer.jl b/test/visualizer.jl index 575f2eb..b8ed056 100644 --- a/test/visualizer.jl +++ b/test/visualizer.jl @@ -160,6 +160,20 @@ end settransform!(v[:valkyrie, :head], Translation(0, 0.5, 0.5)) end + @testset "mesh with vertex colors" begin + # Create a simple mesh with a single triangle + geometry = GeometryBasics.Mesh( + [Point(0., 0, 0), Point(1., 0, 0), Point(1., 1, 0)], + [NgonFace(1, 2, 3)]) + # Wrap that mesh with metadata encoding the vertex colors + mesh_meta = meta(geometry, vertexColors=[RGB(1, 0, 0), RGB(0, 1, 0), RGB(0, 0, 1)]) + # Create a Gouraud-shaded material with vertex coloring enabled + material = MeshLambertMaterial(vertexColors=true) + # Add it to the scene + setobject!(v[:vertex_color_mesh], mesh_meta, material) + settransform!(v[:vertex_color_mesh], Translation(1, -1.5, 0)) + end + end @testset "points" begin