Skip to content

Commit

Permalink
Merge pull request #183 from rdeits/mesh-vertex-colors
Browse files Browse the repository at this point in the history
Support vertexColors for meshes via GeometryBasics.meta
  • Loading branch information
rdeits authored Nov 26, 2020
2 parents 61e3420 + a33e509 commit 1203fb7
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 3 deletions.
3 changes: 3 additions & 0 deletions src/MeshCat.jl
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ using GeometryBasics: GeometryBasics,
GeometryPrimitive,
HyperRectangle,
HyperSphere,
MeshMeta,
NgonFace,
OffsetInteger,
Point,
Expand All @@ -18,6 +19,8 @@ using GeometryBasics: GeometryBasics,
SimplexFace,
Vec,
decompose,
meta,
metafree,
origin,
radius,
texturecoordinates,
Expand Down
13 changes: 10 additions & 3 deletions src/lowering.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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()),
Expand Down
14 changes: 14 additions & 0 deletions test/visualizer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

2 comments on commit 1203fb7

@rdeits
Copy link
Owner Author

@rdeits rdeits commented on 1203fb7 Nov 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator register()

  • MeshCat.jl now requires Julia 1.3 or above
  • Added support for GeometryBasics 0.3
  • Added support for mesh vertex colors

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/25368

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v0.13.0 -m "<description of version>" 1203fb736319f69300a3ef55a51c3fbb3a58d5c1
git push origin v0.13.0

Please sign in to comment.