Techniqueadvanced
Geomorphing
Also called geomorph.
Interpolate the vertex positions between two levels of detail over several frames so that switching levels causes no visible pop.
A geomorph moves the vertices of a finer mesh, over a short time or a short range of distance, from the places they have on the coarser mesh to their own places. The level of detail switch then happens in small steps that the eye does not see as a pop. It differs from a blend of two images with alpha blending: only one mesh is drawn, the finer one, and its geometry changes. Hoppe introduced the term for progressive meshes; terrain methods such as geometry clipmaps use the same blend on heights.
In plain words
When a game switches a mountain from a rough model to a detailed one, new bumps appear in one frame. That jump is easy to see.
A geomorph hides it. Draw the detailed model, but at first put every new point exactly on the surface of the rough model, so the picture does not change. Then, over a second or over a few meters of travel, slide each new point to where it should be. At the end the detailed model is in place, and at no time did anything jump.
The same trick runs backward when the model gets simpler: slide the extra points onto the rough surface first, then switch.
When it applies
- Two levels of detail of the same surface, where every vertex of the finer level has a natural place on the coarser one: its parent vertex in a progressive mesh, or a point of the coarse surface below it in a height field.
- The finer level has the same or more vertices, and its connectivity is used during the morph. The coarse mesh need not be drawn.
- The switch is driven by a parameter that changes smoothly: time after the switch, or the distance to the viewer.
- The error to hide is in the positions. A change in the outline of a mesh with a different topology, or in the texture, needs another method, such as a cross-fade.
The template
The holes are the coarse place of each vertex of the fine mesh, the fine place , and the blend factor .
GEOMORPH(fine mesh, c, f, α) ▷ holes: c(v), f(v), α- 1
for each vertex v of the fine mesh do - 2
if v keeps its place in the coarse mesh then g(v) ← f(v) - 3
else g(v) ← (1 - α) c(v) + α f(v) - 4
return the fine mesh with the vertex places g
The steps of the method are:
- Choose the two levels and the fine mesh that is drawn during the morph.
- For each vertex of the fine mesh, compute its coarse place once, when the switch starts. In a progressive mesh it is the position of the vertex that the split came from. In a height grid it is the height of the coarse surface at the vertex, the mean of the two ends of the coarse edge that the vertex lies on.
- Each frame, compute from the time since the switch, or from the distance of each vertex to the viewer, and clamp it to .
- Run lines L1 to L4, on the CPU or in the vertex shader, and draw the fine mesh. Normals and texture coordinates can be blended in the same way.
- When reaches 1, drop and use the fine mesh as it is. For a switch to the coarse level, run from 1 to 0 and then draw the coarse mesh.
On a regular height grid, as in the code, the coarse mesh uses every other vertex and the same diagonal direction, so each coarse triangle is the union of four fine triangles. Line L2 keeps the coarse vertices, and line L3 moves each other vertex from the midpoint height of its coarse edge to its own height.
Classic examples
A height grid. The fine grid has vertices, and the coarse grid has the four corners with the heights 0, 2, 4 and 6 at , , and . The other fine heights are 3 at , 1 at , 5 at , 4 at and 7 at .
| vertex | ||||
|---|---|---|---|---|
| 1 | 2 | 3 | ||
| 2 | 3/2 | 1 | ||
| 3 | 4 | 5 | ||
| 4 | 4 | 4 | ||
| 5 | 6 | 7 |
At the domain point the drawn surface has the height at , which is the height of the coarse surface there, then 2 at , and at , the fine surface. Without the morph the vertex would jump by 2 in one frame.
Progressive meshes. Hoppe (1996) defines a geomorph between any two meshes of a progressive mesh: a vertex split makes two vertices from one, and both start at the position of the parent and move to their own positions. A sequence of splits then becomes one smooth change, so a renderer can move between any two meshes of the sequence without a pop.
Geometry clipmaps. In geometry clipmaps, each level has a transition region near its outer border. There grows from 0 to 1 with the distance from the viewer, so the heights of a level equal those of the next coarser level at the border, and the levels meet without a crack and without a jump when the viewer moves. Asirvatham and Hoppe use a region of width for a grid of vertices; this width is a tuning.
The same blend on textures. Trilinear filtering of a mipmap blends two levels by the fraction of the level of detail, so a texture also changes level without a pop. It is the same template, with texels in place of vertices.
Variations
- Time or distance. A morph over time starts at the switch and runs for a fixed number of frames. A morph over distance gives each vertex its own from its distance to the viewer, so the mesh is the same for a given view, and a still camera gives a still image.
- Vertical only. For terrain, only the height changes and the of each vertex stays fixed, which keeps the grid and its texture coordinates.
- In the vertex shader. Store as a second attribute or read it from the coarse level, and compute line L3 on the GPU.
- Selective refinement. View-dependent refinement and ROAM change the mesh in many places each frame. A morph per split or per merge needs a state per vertex; many systems use a small error threshold instead, so that each change is below a pixel.
- Morph of attributes. Normals, colors and texture coordinates are blended with the same , and normals are normalized after the blend.
Pitfalls
- A coarse place that is not on the coarse surface. If line L3 starts from a value that the coarse mesh does not show (for example the fine height of a neighbor), the first frame of the morph already differs from the coarse mesh, and the pop is still there.
- Different diagonals. On a grid, if the coarse cells and the fine cells use different diagonal directions, a coarse triangle is not a union of fine triangles. Then in line L3 does not reproduce the coarse surface, even with the right midpoints.
- Moving the coarse vertices. Line L2 must keep the places of the vertices that the coarse mesh shares. If they move too, the neighbors at a border with an unmorphed region open a crack.
- A jump in alpha. If is computed from the position of the viewer with a clamp at a level border that differs from the border of the morph region, then jumps there, and the surface jumps with it.
- Morphing in screen space only. Blending projected positions in place of world positions gives a surface that changes when the camera turns, and shading that does not match the geometry.
Code
Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/geomorphing/. The authors are listed in prov/N3KQ0C.jsonl.
"""Geomorphing between two levels of a regular height grid.
The fine mesh has vertices at the integer points (x, y) with 0 <= x, y <= m,
m even, and splits each unit cell by the diagonal from (x, y) to (x+1, y+1).
The coarse mesh uses only the even points and the same diagonal direction on
its cells of side 2, so every coarse triangle is the union of four fine
triangles. A point of the domain is (x, y); its height is a separate value.
The morph keeps the fine connectivity and moves each vertex that is not a
coarse vertex from the coarse surface (alpha = 0) to its own height
(alpha = 1). Every such vertex is the midpoint of a coarse edge.
"""
from fractions import Fraction
from math import floor
def coarse_edge(v):
"""The two ends of the coarse edge whose midpoint is the fine vertex v."""
x, y = v
if x % 2 and y % 2:
return (x - 1, y - 1), (x + 1, y + 1)
if x % 2:
return (x - 1, y), (x + 1, y)
return (x, y - 1), (x, y + 1)
def geomorph(h, m, alpha):
"""Vertex heights of the morphed mesh for alpha in [0, 1]."""
g = {}
for x in range(m + 1): # @line L1
for y in range(m + 1):
if x % 2 == 0 and y % 2 == 0:
g[(x, y)] = h[(x, y)] # @line L2
else:
a, b = coarse_edge((x, y))
g[(x, y)] = (1 - alpha) * Fraction(h[a] + h[b], 2) + alpha * h[(x, y)] # @line L3
return g # @line L4
def surface(g, step, p):
"""The height at domain point p of the grid surface with vertex heights g and cell side step."""
x, y = p
top = max(k for k, _ in g) // step - 1
i = min(max(floor(x / step), 0), top)
j = min(max(floor(y / step), 0), top)
u, v = Fraction(x) / step - i, Fraction(y) / step - j
x0, y0, x1, y1 = i * step, j * step, (i + 1) * step, (j + 1) * step
if u >= v:
# Triangle (x0, y0), (x1, y0), (x1, y1).
return g[(x0, y0)] + u * (g[(x1, y0)] - g[(x0, y0)]) + v * (g[(x1, y1)] - g[(x1, y0)])
# Triangle (x0, y0), (x1, y1), (x0, y1).
return g[(x0, y0)] + v * (g[(x0, y1)] - g[(x0, y0)]) + u * (g[(x1, y1)] - g[(x0, y1)])
test_geomorphing.py 55 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/geomorphing/. The authors are listed in prov/N3KQ0C.jsonl.
import random
from fractions import Fraction
from geomorphing import geomorph, surface
def heights(rng, m):
return {(x, y): rng.randint(-20, 20) for x in range(m + 1) for y in range(m + 1)}
def points(rng, m, k):
return [(Fraction(rng.randint(0, 64 * m), 64), Fraction(rng.randint(0, 64 * m), 64)) for _ in range(k)]
def test_alpha_zero_is_the_coarse_mesh():
rng = random.Random(1)
for m in (2, 4, 8):
h = heights(rng, m)
g = geomorph(h, m, Fraction(0))
coarse = {v: z for v, z in h.items() if v[0] % 2 == 0 and v[1] % 2 == 0}
for p in points(rng, m, 300):
assert surface(g, 1, p) == surface(coarse, 2, p)
def test_alpha_one_is_the_fine_mesh():
rng = random.Random(2)
for m in (2, 4, 8):
h = heights(rng, m)
g = geomorph(h, m, Fraction(1))
assert g == h
for p in points(rng, m, 100):
assert surface(g, 1, p) == surface(h, 1, p)
def test_every_point_moves_linearly_in_alpha():
rng = random.Random(3)
m = 8
h = heights(rng, m)
g0, g1 = geomorph(h, m, Fraction(0)), geomorph(h, m, Fraction(1))
for _ in range(20):
a = Fraction(rng.randint(0, 100), 100)
ga = geomorph(h, m, a)
for p in points(rng, m, 50):
assert surface(ga, 1, p) == (1 - a) * surface(g0, 1, p) + a * surface(g1, 1, p)
def test_coarse_vertices_never_move():
rng = random.Random(4)
h = heights(rng, 6)
for a in (Fraction(0), Fraction(1, 3), Fraction(1)):
g = geomorph(h, 6, a)
assert all(g[v] == h[v] for v in h if v[0] % 2 == 0 and v[1] % 2 == 0)
Exercises
- Show that on the grid of the code, at , the drawn surface equals the coarse surface at every point, not only at the vertices. Use the fact that each coarse triangle is the union of four fine triangles.
- Show that the height at each domain point is an affine function of . What does this say about the largest speed of a surface point during a morph over frames?
- For the example, find the domain point in the cell whose height changes the most from to , and compute that change.
- In a geometry clipmap with and the transition width , goes from 0 to 1 over 25.5 cells of the level. A viewer moves toward the border of the level by one cell of that level per frame. Over about how many frames does a vertex in the region complete its morph, and what is the largest change of per frame?
- Give a progressive mesh split in which both new vertices start at the parent's position, and show that the faces and that the split creates have zero area at .
Proof: The grid geomorph equals the coarse surface at alpha 0 and the fine surface at alpha 1, and every surface point moves linearly in alphafull
We use the setup of the page. The fine mesh has the grid points with and even, and each unit cell is cut by the diagonal from to . The coarse mesh has the even grid points, and each cell of side 2 is cut by the diagonal in the same direction. The heights are given at all fine vertices. For a mesh and vertex values , the surface is the function that is affine on each triangle and takes the value at each vertex . The arithmetic is exact.
Lemma 1. Each fine vertex that is not a coarse vertex is the midpoint of exactly the coarse edge that coarse_edge returns. Each coarse triangle is the union of four fine triangles, whose vertices are the three corners of the coarse triangle and the midpoints of its three edges.
Proof. A fine vertex with odd and even is the midpoint of the horizontal coarse edge from to ; with even and odd, of the vertical edge from to ; with both odd, of the diagonal from to , which is a coarse edge because the coarse diagonals have the direction . These are the three cases of the code. Take the coarse triangle with the corners , and , with and even. Its edge midpoints are , and . The fine triangles in it are the lower triangle of the cell at , both triangles of the cell at , and the lower triangle of the cell at , and their vertices are these six points. The other coarse triangle of the cell is the same after the exchange of and .
Theorem 2. Let be the vertex values of lines L1 to L4. On the fine mesh, equals the coarse surface (the coarse mesh with the values at the coarse vertices) at every point of the domain, equals the fine surface , and for each point , .
Proof. At , line L3 gives , and line L2 gives at the coarse vertices, so .
At , a coarse vertex keeps . A vertex of the other kind gets the mean of at the two ends of the coarse edge through it (Lemma 1). The coarse surface is affine along that edge, so this mean is . So at every fine vertex. Now take a fine triangle . By Lemma 1 it lies in one coarse triangle, on which is affine. Both and are affine on and agree at its three vertices, so they agree on .
For the last claim, the value is over the three vertices of the fine triangle that holds , with barycentric coordinates that do not depend on . Each is , so the sum is affine in in the same way.
The morph keeps the fine connectivity for all , so it never has a crack or a T-junction: it is one conforming triangulation whose vertex heights move. A coarse vertex does not move. The other vertices move only in height, along the vertical line through their domain point. This is the vertical form of the geomorph that terrain methods use. The geomorph of Hoppe moves the two vertices of a vertex split from the position of their parent, which is a different start position; the linear blend of the positions is the same.
Corollary 3. If the blend factor goes from 0 to 1 over a time or a distance range, the height of each surface point is continuous in that time or distance, and the largest jump at the start or the end of the range is 0.
Proof. is affine in by Theorem 2, so it is continuous in any parameter on which depends continuously. At it is the surface that was drawn before the change, and at the surface that is drawn after it.
References
- Hugues Hoppe, Progressive meshes, SIGGRAPH 1996, 99 to 108, the section on geomorphs (the linear blend of vertex positions between two meshes of a progressive mesh)
- Frank Losasso and Hugues Hoppe, Geometry clipmaps, terrain rendering using nested regular grids, ACM Transactions on Graphics 23(3), SIGGRAPH 2004, 769 to 776, the transition regions, in which the heights of a level are blended toward the next coarser level
- Arul Asirvatham and Hugues Hoppe, Terrain rendering using GPU-based geometry clipmaps, in Matt Pharr (editor), GPU Gems 2, Addison-Wesley, 2005, Chapter 2, the morph z' = (1 - alpha) z_f + alpha z_c in the vertex shader, with the transition width w = n/10 (general locator)
- Tomas Akenine-Möller, Eric Haines, Naty Hoffman, Angelo Pesce, Michał Iwanicki and Sébastien Hillaire, Real-Time Rendering, fourth edition, CRC Press, 2018, the part on level of detail and the switching between levels in the chapter on acceleration algorithms (general locator)
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H2 full proof |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag N3KQ0C Edit this page History Report an error Markdown