# Can the two sides of a mesh have a different color (e.g., origami paper)?

**URL:** https://discourse.julialang.org/t/can-the-two-sides-of-a-mesh-have-a-different-color-e-g-origami-paper/97118
**Category:** General Usage
**Tags:** makie, geometric-algebra
**Created:** [April 5, 2023, 3:14pm UTC](https://discourse.julialang.org/t/can-the-two-sides-of-a-mesh-have-a-different-color-e-g-origami-paper/97118 "2023-04-05T15:14:24Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![g2g](https://avatars.discourse-cdn.com/v4/letter/g/41988e/32.png) [@g2g](https://discourse.julialang.org/u/g2g)
#### Post date: [April 5, 2023, 3:14pm UTC](https://discourse.julialang.org/t/can-the-two-sides-of-a-mesh-have-a-different-color-e-g-origami-paper/97118/1 "2023-04-05T15:14:24Z")

</div>

![fold](https://global.discourse-cdn.com/julialang/original/3X/2/b/2b7f514d70742bab9ac0658239e850eca80d7270.gif)

```julia
# fold.jl # MWE of moving mesh
#
# All the paper test sheet's vertices and faces are defined
# according to the wavefront 3D object file format by the
# following six lines of text:
#=
v -2.0 0 2.0
v -2.0 0 -2.0
v 2.0 0 -2.0
v 2.0 0 2.0
f 1 2 4
f 3 4 2
=#
using GLMakie, GLMakie.FileIO
using GeometryBasics
include("ripga3d.jl")

# convert coordinate Point{3, Float32} to/from PGA point
function point(A::Point{3, Float32})::Vector{Float32}
	return A[1]*e032 + A[2]*e013 + A[3]*e021 + e123
end
function toPoint(A::Vector{Float32})
	return Point{3, Float32}(A[14],A[13],A[12])
end

function fold()
	# initialize data
	S = load("sheet.obj")
	SC,SF = coordinates(S),faces(S)
	S_obs = Observable(S)
	
	# initialize figure
	fig = Figure(resolution = (600, 650))
	ax3d = Axis3(fig[1,1],
		elevation = pi/16,
		azimuth = -pi/4,
		limits = (-2,2, -2,2, -2,2),
		aspect = (1,1,1))
	mesh!(ax3d, S_obs)
	fig
	
	# generate video of folding
	nFrame = 200
	PL = point(SC[2]) & point(SC[4]) # Pivot Line
	SP = point(SC[1]) # Starting Point
	record(fig, "fold.mp4", 1:nFrame) do iFrame
		angle = pi*(1-abs(2*(iFrame-1)/nFrame-1))
		R = rotor(angle, PL)
		MP = R >>> SP # Moving Point
		SC[1] = toPoint(MP)
		S_obs[] = GeometryBasics.Mesh(SC, SF)
	end # for each video frame
end # origami()

# quick and dirty ffmpeg conversion from fold.mp4 to fold.gif:
# /tool/ffmpeg-2022-07/bin/ffmpeg.exe -i fold.mp4 -r 24 -s 480x520 -loop 0 fold.gif
# /tool/ffmpeg-2022-07/bin/ffmpeg.exe -i fold.mp4 -r 24 -s 600x650 -loop 0 fold.gif

```

After porting several [bivector.net](http://bivector.net) ganja.js [Projective Geometric Algebra](https://medium.com/@olarth/julia-and-projective-geometric-algebra-e2d5abb11519) application examples to Julia and GLMakie, I’d like to port the ganja.js [origami](https://enkimute.github.io/ganja.js/examples/coffeeshop.html#pga3d_origami) application example which has more complex graphics than the other application examples I’ve ported. So I have a few questions:

- Can a mesh be like origami paper with a different color on the two sides of the paper?
- What is the simplest way to plot both the wireframe and the colored faces?
- Is a GLMakie mesh the best approach to animating the many folds of origami or would a GLMakie surface (or something else) be better?

---

<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: [April 5, 2023, 5:36pm UTC](https://discourse.julialang.org/t/can-the-two-sides-of-a-mesh-have-a-different-color-e-g-origami-paper/97118/2 "2023-04-05T17:36:20Z")

</div>

I don’t think the normal GLMakie shaders support different colors for each side, but in principle it’s possible. I’m not sure how easy it is nowadays to use a custom glsl shader, though. And a mesh should be good, a surface is still a mesh, it’s just a convenient parameterization of one over a grid.

---

<div class="post-metadata">

### Author: ![rafael.guerra](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/rafael.guerra/32/216610_2.png) [@rafael.guerra](https://discourse.julialang.org/u/rafael.guerra)
#### Post date: [April 5, 2023, 10:56pm UTC](https://discourse.julialang.org/t/can-the-two-sides-of-a-mesh-have-a-different-color-e-g-origami-paper/97118/3 "2023-04-05T22:56:21Z")

</div>

Could an appropriate [light position](https://discourse.julialang.org/t/fixing-makie-light-position-after-moving-the-camera/75514) help a bit here?

---

<div class="post-metadata">

### Author: ![g2g](https://avatars.discourse-cdn.com/v4/letter/g/41988e/32.png) [@g2g](https://discourse.julialang.org/u/g2g)
#### Post date: [April 6, 2023, 12:06am UTC](https://discourse.julialang.org/t/can-the-two-sides-of-a-mesh-have-a-different-color-e-g-origami-paper/97118/4 "2023-04-06T00:06:43Z")

</div>

I haven’t experimented with lighting, but changing from mesh!() to poly!() and changing the color and alpha channel helped.

```julia
	poly!(ax3d, S_obs,
	   color = RGBA(1, 1, 0.9, 0.9),
	   strokewidth = 1)
	fig

```

![fold](https://global.discourse-cdn.com/julialang/original/3X/a/4/a41a53660f8694fbb9a7d8b836a728c1621a5f54.gif)

---

<div class="post-metadata">

### Author: ![empet](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/empet/32/221303_2.png) [@empet](https://discourse.julialang.org/u/empet)
#### Post date: [April 6, 2023, 4:12pm UTC](https://discourse.julialang.org/t/can-the-two-sides-of-a-mesh-have-a-different-color-e-g-origami-paper/97118/5 "2023-04-06T16:12:36Z")

</div>

I plotted the moving triangle as a surface defined by a convex combination of  
its vertices:

```julia
using GeometryBasics
#g(u,v);[0,1]x[0,1]->R^3 is the triangle, ABC, parameterization
g(u,v; A=Point3(0, -2.0, 2.0), B=Point3(0, 2.0, 2.0), C=Point3(0, -2.0, -2.0))=v*(1-u) *A+(1-v)*B+u*v*C
function triangle_surf(A::Point3, B::Point3, C::Point3; n=30)
    ul = range(0,1, length=n)
    vl = range(0,1, length=n)
    u = ul' .*ones(n)
    v= ones(n)'.* vl
    P = g.(u, v; A=A, B=B, C=C)
end

```

Each frame displays two triangles, with two fixed points (B, and C) and the third point  
 ![square-folding](https://global.discourse-cdn.com/julialang/original/3X/c/f/cf5f3300ec1c58ededccabf5246cb9ad04738ec0.gif)  
at a small distance (0.025) from each other, on the normal direction to one of the triangles.  
The two triangles are colored by two distinct colors.

The moving point, A’ runs over an arc of a circle, namely:  
c(t)=2\sqrt{2}\cos(t)w\_1 + 2\sqrt{2}\sin(t)w\_2, where  
w\_1=[-\sqrt{2}/2, 0.5, -0.5], w\_2 =[-\sqrt{2}/2, -0.5, 0.5], and t\in[3\pi/4, 7\pi/4].  
The triangle A’BC is filled with the burgundy color in my animation.  
Get the unit vector of the normal direction to A’BC:  
dn=cross(\vec{A'B}, \vec{A'C})  
dn=dn/norm(dn)  
The points A’‘=A’+0.025\*dn, B, C are vertices of the white/gray triangle.

---

<div class="post-metadata">

### Author: ![g2g](https://avatars.discourse-cdn.com/v4/letter/g/41988e/32.png) [@g2g](https://discourse.julialang.org/u/g2g)
#### Post date: [April 6, 2023, 5:20pm UTC](https://discourse.julialang.org/t/can-the-two-sides-of-a-mesh-have-a-different-color-e-g-origami-paper/97118/6 "2023-04-06T17:20:47Z")

</div>

> [@empet](#):
>
> I plotted the moving triangle as a surface

Nice! The origami example I am porting from JavaScript to Julia folds a kabuto (Samurai helmet) that in some places is more than 10 layers thick. Because some of those folds are toward the camera and some are away from the camera, it seems there would need to be quite a bit of bookkeeping to keep track of the different depths of the different colored faces.
