# 3d bars makie, colors and meshes

**URL:** <https://discourse.julialang.org/t/3d-bars-makie-colors-and-meshes/70283>\
**Category:** General Usage\
**Tags:** question, plotting, makie\
**Created:** [October 24, 2021, 9:01am UTC](https://discourse.julialang.org/t/3d-bars-makie-colors-and-meshes/70283 "2021-10-24T09:01:27Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![lazarusA](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lazarusa/32/6571_2.png) [@lazarusA](https://discourse.julialang.org/u/lazarusA)\
**Post date:** [October 24, 2021, 9:01am UTC](https://discourse.julialang.org/t/3d-bars-makie-colors-and-meshes/70283/1 "2021-10-24T09:01:27Z")

</div>

Probably, the following could be done without to much effort, but I don’t seem to find the right way to do it 😃

1. A way to merge all Rect3 meshes? Plotting one by one becomes really slow after a while.
2. Add a color gradient to each rectangle as a function of `z`. Namely, the colormap maps each rectangle.

This is the start:

```julia
using GeometryBasics, GLMakie, ColorSchemes
function test3dBars()
    x = y= 1:10
    z = rand(10,10)
    δx = (x[2] - x[1]) / 2
    δy = (y[2] - y[1]) / 2
    cbarPal = :Spectral_11
    ztmp = (z .- minimum(z)) ./ (maximum(z .- minimum(z)))
    cmap = get(colorschemes[cbarPal], ztmp)
    cmap2 = reshape(cmap, size(z))
    fig = Figure(resolution=(1200, 800), fontsize=26)
    ax = Axis3(fig[1, 1]; aspect=(1, 1, 1), elevation=π / 6, perspectiveness=0.5)
    for (idx, i) in enumerate(x), (idy, j) in enumerate(y)
        rectMesh = FRect3D(Vec3f0(i - δx, j - δy, 0), Vec3f0(2δx, 2δy, z[idx, idy]))
        recmesh = GeometryBasics.mesh(rectMesh)
        mesh!(ax, recmesh; color=cmap2[idx, idy], shading=false)
    end
    fig
end
test3dBars()

```

 ![Screenshot 2021-10-24 at 11.00.16](https://global.discourse-cdn.com/julialang/original/3X/e/d/ed2f1251c4324761e9a685471fe51d686867585a.png)

---

<div class="post-metadata">

**Author:** ![ffreyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffreyer/32/21569_2.png) [@ffreyer](https://discourse.julialang.org/u/ffreyer)\
**Post date:** [October 24, 2021, 9:10am UTC](https://discourse.julialang.org/t/3d-bars-makie-colors-and-meshes/70283/2 "2021-10-24T09:10:41Z")

</div>

You should use meshscatter for this. See [https://github.com/JuliaPlots/Makie.jl/issues/624](https://github.com/JuliaPlots/Makie.jl/issues/624).

---

<div class="post-metadata">

**Author:** ![lazarusA](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lazarusa/32/6571_2.png) [@lazarusA](https://discourse.julialang.org/u/lazarusA)\
**Post date:** [October 24, 2021, 9:39am UTC](https://discourse.julialang.org/t/3d-bars-makie-colors-and-meshes/70283/3 "2021-10-24T09:39:36Z")

</div>

this kinda fixes the speed issue with meshes. But, I was thinking about meshes because a want each Rect3 or marker in this case to have the colormap gradient (and not just one color per marker).

---

<div class="post-metadata">

**Author:** ![ffreyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffreyer/32/21569_2.png) [@ffreyer](https://discourse.julialang.org/u/ffreyer)\
**Post date:** [October 24, 2021, 9:43am UTC](https://discourse.julialang.org/t/3d-bars-makie-colors-and-meshes/70283/4 "2021-10-24T09:43:24Z")

</div>

You should be able to do `color = rand(100), colormap = :viridis` or `color = rand(RGBf, 100)`

---

<div class="post-metadata">

**Author:** ![lazarusA](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lazarusa/32/6571_2.png) [@lazarusA](https://discourse.julialang.org/u/lazarusA)\
**Post date:** [October 24, 2021, 10:05am UTC](https://discourse.julialang.org/t/3d-bars-makie-colors-and-meshes/70283/5 "2021-10-24T10:05:05Z")

</div>

Again. That just maps one color per marker. And what i want is a colormap gradient for each marker.

---

<div class="post-metadata">

**Author:** ![ffreyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffreyer/32/21569_2.png) [@ffreyer](https://discourse.julialang.org/u/ffreyer)\
**Post date:** [October 24, 2021, 11:43am UTC](https://discourse.julialang.org/t/3d-bars-makie-colors-and-meshes/70283/6 "2021-10-24T11:43:47Z")

</div>

Oh sorry, I read over that part. I don’t think you can get that with `meshscatter` right now.

With `mesh` you could use `mesh!(ax, recmesh; color=last.(coordinates(recmesh)), colormap = :Spectral_11, colorrange = (0, 1), shading=false)` but that just intepolates between two colors:

 ![Screenshot from 2021-10-24 13-24-27](https://global.discourse-cdn.com/julialang/original/3X/d/5/d5dfb448e93e315598ae56ffe521579645d10bf7.png)

Alternatively you could work with a texture similar to [Issues · MakieOrg/Makie.jl · GitHub](https://github.com/JuliaPlots/Makie.jl/issues/1368:)

```julia
function test3dBars()
    x = y= 1:10
    z = rand(10,10)
    δx = (x[2] - x[1]) / 2
    δy = (y[2] - y[1]) / 2
    cbarPal = :Spectral_11
    texture = reshape(get(colorschemes[cbarPal], 0:0.01:1), 1, 101)
    fig = Figure(resolution=(1200, 800), fontsize=26)
    ax = Axis3(fig[1, 1]; aspect=(1, 1, 1), elevation=π / 6, perspectiveness=0.5)
    for (idx, i) in enumerate(x), (idy, j) in enumerate(y)
        rectMesh = FRect3D(Vec3f0(i - δx, j - δy, 0), Vec3f0(2δx, 2δy, z[idx, idy]))
        recmesh = GeometryBasics.normal_mesh(rectMesh)
        uvs = [Point2f(p[3], 0) for p in coordinates(recmesh)] # normalize this so zmax = 1
        recmesh = GeometryBasics.Mesh(
            meta(coordinates(recmesh); normals=normals(recmesh), uv = uvs), 
            faces(recmesh)
        )
        mesh!(ax, recmesh; color=texture, shading=false)
    end
    fig
end
test3dBars()

```

 ![Screenshot from 2021-10-24 13-38-44](https://global.discourse-cdn.com/julialang/original/3X/d/0/d043799f82e55f0f8252a3781e430f34f9516f13.jpeg)

> A way to merge all Rect3 meshes?

GeometryBasics implements `merge(meshes)`

---

<div class="post-metadata">

**Author:** ![lazarusA](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/lazarusa/32/6571_2.png) [@lazarusA](https://discourse.julialang.org/u/lazarusA)\
**Post date:** [October 24, 2021, 12:02pm UTC](https://discourse.julialang.org/t/3d-bars-makie-colors-and-meshes/70283/7 "2021-10-24T12:02:36Z")

</div>

Nice! This is the one! Thanks!. Although, as it is, is still slow if we add more meshes in that way.

Edit: nice merge(meshes) works like a charm and simply passing the texture works nicely. Thanks again!

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://discourse.julialang.org/u/jules)\
**Post date:** [October 24, 2021, 1:39pm UTC](https://discourse.julialang.org/t/3d-bars-makie-colors-and-meshes/70283/8 "2021-10-24T13:39:42Z")

</div>

Is this UV stuff documented anywhere?

---

<div class="post-metadata">

**Author:** ![ffreyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffreyer/32/21569_2.png) [@ffreyer](https://discourse.julialang.org/u/ffreyer)\
**Post date:** [October 24, 2021, 2:45pm UTC](https://discourse.julialang.org/t/3d-bars-makie-colors-and-meshes/70283/9 "2021-10-24T14:45:25Z")

</div>

I don’t think so? I would say it should be documented in GeometryBasics but there isn’t much there…

A mesh with uv coordinates will have one per vertex. During rendering, the uv coordinates are interpolated from the 3 corners of a triangle face for each fragment (pixel) the fragment shader renders. That coordinate is then used index a texture (image) to get a color for that fragment.

> **[UV mapping](https://en.wikipedia.org/wiki/UV_mapping)**
>
> UV mapping is the 3D modeling process of projecting a 2D image to a 3D model's surface for texture mapping. The letters "U" and "V" denote the axes of the 2D texture because "X", "Y", and "Z" are already used to denote the axes of the 3D object in model space, while "W" (in addition to XYZ) is used in calculating quaternion rotations, a common operation in computer graphics.
> UV texturing permits polygons that make up a 3D object to be painted with color (and other surface attributes) from an ord...

---

<div class="post-metadata">

**Author:** ![ffreyer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/ffreyer/32/21569_2.png) [@ffreyer](https://discourse.julialang.org/u/ffreyer)\
**Post date:** [October 24, 2021, 3:33pm UTC](https://discourse.julialang.org/t/3d-bars-makie-colors-and-meshes/70283/10 "2021-10-24T15:33:09Z")

</div>

I opened a pr that would allow solution 2 to work with meshscatter:  
[https://github.com/JuliaPlots/Makie.jl/pull/1406](https://github.com/JuliaPlots/Makie.jl/pull/1406)
