Skip to content

Instantly share code, notes, and snippets.

@unixpickle
Created May 16, 2020 23:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save unixpickle/28227db33597e96bfabcda769c95e851 to your computer and use it in GitHub Desktop.
Save unixpickle/28227db33597e96bfabcda769c95e851 to your computer and use it in GitHub Desktop.
func CreateMesh() *model3d.Mesh {
box := model3d.NewMesh()
stepSize := 0.05
addQuad := func(p model3d.Coord3D, normalAxis int) {
ax1 := model3d.Coord3D{X: stepSize}
ax2 := model3d.Coord3D{Y: stepSize}
if normalAxis == 0 {
ax1 = model3d.Coord3D{Z: stepSize}
} else if normalAxis == 1 {
ax2 = model3d.Coord3D{Z: stepSize}
}
box.Add(&model3d.Triangle{p, p.Add(ax1), p.Add(ax2)})
box.Add(&model3d.Triangle{p.Add(ax1), p.Add(ax2), p.Add(ax1).Add(ax2)})
}
// All but top two faces.
for x := -20; x < 20; x += 5 {
for z := -50; z < 50; z += 5 {
fx := float64(x) / 100
fz := float64(z) / 100
for _, fy := range []float64{-0.2, 0.2} {
addQuad(model3d.Coord3D{X: fx, Y: fy, Z: fz}, 1)
addQuad(model3d.Coord3D{X: fy, Y: fx, Z: fz}, 0)
}
}
}
// Top two faces.
for x := -20; x < 20; x += 5 {
for y := -20; y < 20; y += 5 {
fx := float64(x) / 100
fy := float64(y) / 100
addQuad(model3d.Coord3D{X: fx, Y: fy, Z: -0.5}, 2)
addQuad(model3d.Coord3D{X: fx, Y: fy, Z: 0.5}, 2)
}
}
// I got really lazy here, so now let's make the
// mesh manifold and oriented.
box = box.Repair(1e-8)
box, _ = box.RepairNormals(1e-8)
// Don't let the box face the camera head on,
// allowing us to see more detail.
rotate := model3d.NewMatrix3Rotation(model3d.Coord3D{Z: 1}, 0.4)
rotate[8] = 2
return box.MapCoords(rotate.MulColumn).FlipDelaunay()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment