{"id":345,"date":"2010-05-24T06:46:10","date_gmt":"2010-05-24T06:46:10","guid":{"rendered":"http:\/\/the-witness.net\/news\/?p=345"},"modified":"2010-05-24T06:46:10","modified_gmt":"2010-05-24T06:46:10","slug":"kriging-is-cool","status":"publish","type":"post","link":"http:\/\/the-witness.net\/news\/2010\/05\/kriging-is-cool\/","title":{"rendered":"Kriging is cool."},"content":{"rendered":"<p>In <em>The Witness<\/em> we decided that it's a good idea to have a heightfield terrain making up the bulk of the island surface. \u00a0The advantages of this are in rendering performance and in ease of texturing (it is relatively easy, with one continuously-parameterized terrain block, to get rid of seams; if your terrain is a bunch of otherwise-unrelated meshes, then what do you do?)  <!--more--> When we made this decision it seemed clear to me that the biggest disadvantage, for this game at least, was authoring the terrain. \u00a0There are typically a few different ways that large heightfields are built:<\/p>\n<ol>\n<li>Use an external terrain editing tool and then write an importer for its file format<\/li>\n<li>Build a terrain editing tool into the game's editor (this will require a bunch of work to do a good job, and will have fewer features than an external tool but they may be more appropriate for your game; also, you can make reasonable things happen to entities placed in the world when you interactively move the terrain around)<\/li>\n<li>Paint heightmaps in an image editor like Photoshop and hotload those from the game engine (requires much less programming; doesn't provide many features, but you have very fine control over the results; unfortunately, the editing process becomes pretty cumbersome)<\/li>\n<\/ol>\n<p>These seemed to be the main approaches on offer. \u00a0However, I wasn't enthusiastic about doing any of the traditional things; I have written a number of terrain systems for games, and what bugged me, with respect to this game, was the way in which terrain handling tends to be very different from laying out objects in the world, and how this creates an extremely disjoint work process.<\/p>\n<p>In concrete terms, this means that terrain was not going to be as malleable as the other objects in the world. \u00a0I am taking a very rough-drafty approach to the game; we are building areas, then moving them across the world if we feel they will work better somewhere else, rotating them as a group, etc. \u00a0The goal is to build a very-cohesive game world in which carefully-tuned gameplay is the most important thing -- where we more-or-less have a rough draft of the whole world set up before we go in and try to get serious about how everything looks.<\/p>\n<p>This kind of approach just dies when you have elements that behave very differently, and are difficult to manipulate together, such as terrain and objects. \u00a0If I have two hills with a path winding between them, a bench by the path with a book upon the bench, and a signpost nearby, what happens when I want to move that scene across the map and then rotate it by 30 degrees? \u00a0With the entities, it is no problem, we just do that in the editor. \u00a0But with the terrain it just becomes a mess to move the hills at all, regardless of which of the above approaches are used. \u00a0And if you want to move and rotate the hills in sync with the entities so that the whole scene is intact... <strong>good luck!<\/strong><\/p>\n<p>To solve this problem, terrain needed to be built in the in-game editor, in a way that would be maximally malleable -- so that I can pick up a hill and rotate and drag it. \u00a0Because of the requirement for rotation, you can't really be manipulating samples directly; it has to be control surfaces of some kind (because if you are just blitting samples around, a few rotation operations are going to mangle whatever original shapes you made).<\/p>\n<p>As for control surfaces, the first ideas that come to mind are things like spline patches. \u00a0But the problem with spline patches is that you have to be very careful managing the control points in a patch, and they ultimately aren't that malleable -- what if I want to split a patch in half without changing the shapes of areas near the edges? \u00a0What if I want to move one small, high-detail terrain area so that it overlaps somewhat with a big, low-detail area? \u00a0How is that going to work?  And if I have spline patches, then any time I instantiate a new grid of patches, I have to manage a bunch of individual control points, which is labor-intensive. \u00a0If I am rough-drafting, I want to do the minimum possible work to lay out a given area. \u00a0If I just generally want to build a hill but don't care much about its shape, I ought to be able to do that with just 2 or 3 control points, arbitrarily-placed.<\/p>\n<p>Clearly what I wanted was a system that would just look at a set of unstructured control points -- no topology or anything -- and generate a terrain from those points in the local area. \u00a0When there is no topology or parameterization, you can merge and split groups of control points at will; you can have them overlap; you can do whatever you want.<\/p>\n<p>I started putting together an ad hoc scheme for doing this, but did not take it very far before discovering that the mathematics for doing a good job at exactly this problem had already been invented decades ago. \u00a0The method is called <a href=\"http:\/\/en.wikipedia.org\/wiki\/Kriging\" onclick=\"_gaq.push(['_trackEvent', 'outbound-article', 'http:\/\/en.wikipedia.org\/wiki\/Kriging', 'Kriging']);\" >Kriging<\/a>, and it is well known in geostatistics. \u00a0If you've got a survey of height field data (or any other field data -- the density of a mineral at certain points, for instance), and that data is missing values for some locations, Kriging will give you a reasonable guess at the values of the field at the unknown points.<\/p>\n<p>For rough-draft terrain modeling, we can turn the idea around; if we give a Kriging function only a small set of known data points, it will do a quite fantastic job of filling in the values at all the other points. \u00a0The basic math involves the inverse of an n by n matrix, where n is the number of known data points.  In theory that could become a huge computation, but for the number of points we may typically have in one local area of the map, the computation is easy for modern CPUs.  Kriging is not hard to implement; I started with some public domain code written at a university and modified it for our needs (though this basically involved rewriting it, as it was messy and had some serious bugs! \u00a0If anyone wants our version of the Kriging code, drop me a line).<\/p>\n<p>Here's how we have it integrated into our game engine. \u00a0There's a type of object that exists as a set of terrain control points, which can be moved around the world arbitrarily, just like anything else. \u00a0It looks like this:<\/p>\n<p>            \n\t\t<div class=\"ngg-gallery-singlepic-image ngg-center\" style=\"max-width: 400px\">\n\t\t\t    \t<a href=\"http:\/\/the-witness.net\/news\/wp-content\/gallery\/kriging\/untitled-1.jpg\"\n\t\t     title=\"\"\n             data-src=\"http:\/\/the-witness.net\/news\/wp-content\/gallery\/kriging\/untitled-1.jpg\"\n             data-thumbnail=\"http:\/\/the-witness.net\/news\/wp-content\/gallery\/kriging\/thumbs\/thumbs_untitled-1.jpg\"\n             data-image-id=\"75\"\n             data-title=\"untitled-1\"\n             data-description=\"\"\n             target='_self'\n             class=\"shutterset_14b67f7d8cce79794931c0b3b61d11be\">\n            <img class=\"ngg-singlepic\"\n             src=\"http:\/\/the-witness.net\/news\/wp-content\/gallery\/kriging\/dynamic\/untitled-1.jpg-nggid0275-ngg0dyn-400x300x100-00f0w010c010r110f110r010t010.jpg\"\n             alt=\"untitled-1\"\n             title=\"untitled-1\"\n              width=\"400\"               \/>\n    \t<\/a>\n\t\t      <\/div>\n    <span><\/span>    <\/p>\n<p>The only meaningful data in this object, with respect to terrain shaping, are the positions of the control points (purple boxes). \u00a0The surface is just a visualization; it tells you what surface these points would define if they were the only control points in the universe. \u00a0(Though actually, the surface defined by those points would extend infinitely in all directions; this visualization stays within the convex hull of the control points, which helps you see what you are doing when editing).<\/p>\n<p>Right now these control points haven't actually been applied to the game's terrain (green surface below); they are just hovering above it. \u00a0Here's a shot from another angle:<\/p>\n<p>            \n\t\t<div class=\"ngg-gallery-singlepic-image ngg-center\" style=\"max-width: 400px\">\n\t\t\t    \t<a href=\"http:\/\/the-witness.net\/news\/wp-content\/gallery\/kriging\/untitled-2.jpg\"\n\t\t     title=\"\"\n             data-src=\"http:\/\/the-witness.net\/news\/wp-content\/gallery\/kriging\/untitled-2.jpg\"\n             data-thumbnail=\"http:\/\/the-witness.net\/news\/wp-content\/gallery\/kriging\/thumbs\/thumbs_untitled-2.jpg\"\n             data-image-id=\"80\"\n             data-title=\"untitled-2\"\n             data-description=\"\"\n             target='_self'\n             class=\"shutterset_dc4d5ad918bd57619715f68f3c0f3eea\">\n            <img class=\"ngg-singlepic\"\n             src=\"http:\/\/the-witness.net\/news\/wp-content\/gallery\/kriging\/dynamic\/untitled-2.jpg-nggid0280-ngg0dyn-400x300x100-00f0w010c010r110f110r010t010.jpg\"\n             alt=\"untitled-2\"\n             title=\"untitled-2\"\n              width=\"400\"               \/>\n    \t<\/a>\n\t\t      <\/div>\n    <span><\/span>    <\/p>\n<p>I can press a key to add a new control point to this object:<\/p>\n<p>            \n\t\t<div class=\"ngg-gallery-singlepic-image ngg-center\" style=\"max-width: 400px\">\n\t\t\t    \t<a href=\"http:\/\/the-witness.net\/news\/wp-content\/gallery\/kriging\/untitled-3.jpg\"\n\t\t     title=\"\"\n             data-src=\"http:\/\/the-witness.net\/news\/wp-content\/gallery\/kriging\/untitled-3.jpg\"\n             data-thumbnail=\"http:\/\/the-witness.net\/news\/wp-content\/gallery\/kriging\/thumbs\/thumbs_untitled-3.jpg\"\n             data-image-id=\"81\"\n             data-title=\"untitled-3\"\n             data-description=\"\"\n             target='_self'\n             class=\"shutterset_ff93f8fb278857fb8e0b8c374f0aefce\">\n            <img class=\"ngg-singlepic\"\n             src=\"http:\/\/the-witness.net\/news\/wp-content\/gallery\/kriging\/dynamic\/untitled-3.jpg-nggid0281-ngg0dyn-400x300x100-00f0w010c010r110f110r010t010.jpg\"\n             alt=\"untitled-3\"\n             title=\"untitled-3\"\n              width=\"400\"               \/>\n    \t<\/a>\n\t\t      <\/div>\n    <span><\/span>    <\/p>\n<p>The new control point doesn't affect the shape of the old surface in any way, which is very convenient.  I can drag the new control point at will:<\/p>\n<p>            \n\t\t<div class=\"ngg-gallery-singlepic-image ngg-center\" style=\"max-width: 400px\">\n\t\t\t    \t<a href=\"http:\/\/the-witness.net\/news\/wp-content\/gallery\/kriging\/untitled-4.jpg\"\n\t\t     title=\"\"\n             data-src=\"http:\/\/the-witness.net\/news\/wp-content\/gallery\/kriging\/untitled-4.jpg\"\n             data-thumbnail=\"http:\/\/the-witness.net\/news\/wp-content\/gallery\/kriging\/thumbs\/thumbs_untitled-4.jpg\"\n             data-image-id=\"82\"\n             data-title=\"untitled-4\"\n             data-description=\"\"\n             target='_self'\n             class=\"shutterset_6bb4ebb19da728f9e0b57e7acdd5babf\">\n            <img class=\"ngg-singlepic\"\n             src=\"http:\/\/the-witness.net\/news\/wp-content\/gallery\/kriging\/dynamic\/untitled-4.jpg-nggid0282-ngg0dyn-400x300x100-00f0w010c010r110f110r010t010.jpg\"\n             alt=\"untitled-4\"\n             title=\"untitled-4\"\n              width=\"400\"               \/>\n    \t<\/a>\n\t\t      <\/div>\n    <span><\/span>    <\/p>\n<p>Or add many more control points:<\/p>\n<p>            \n\t\t<div class=\"ngg-gallery-singlepic-image ngg-center\" style=\"max-width: 400px\">\n\t\t\t    \t<a href=\"http:\/\/the-witness.net\/news\/wp-content\/gallery\/kriging\/untitled-5.jpg\"\n\t\t     title=\"\"\n             data-src=\"http:\/\/the-witness.net\/news\/wp-content\/gallery\/kriging\/untitled-5.jpg\"\n             data-thumbnail=\"http:\/\/the-witness.net\/news\/wp-content\/gallery\/kriging\/thumbs\/thumbs_untitled-5.jpg\"\n             data-image-id=\"83\"\n             data-title=\"untitled-5\"\n             data-description=\"\"\n             target='_self'\n             class=\"shutterset_ee2670ea726c811eb3a7131851be8cd8\">\n            <img class=\"ngg-singlepic\"\n             src=\"http:\/\/the-witness.net\/news\/wp-content\/gallery\/kriging\/dynamic\/untitled-5.jpg-nggid0283-ngg0dyn-400x300x100-00f0w010c010r110f110r010t010.jpg\"\n             alt=\"untitled-5\"\n             title=\"untitled-5\"\n              width=\"400\"               \/>\n    \t<\/a>\n\t\t      <\/div>\n    <span><\/span>    <\/p>\n<p>Here's that same visualization surface, with the control points and gridding turned off, so you can see it better:<\/p>\n<p>            \n\t\t<div class=\"ngg-gallery-singlepic-image ngg-center\" style=\"max-width: 400px\">\n\t\t\t    \t<a href=\"http:\/\/the-witness.net\/news\/wp-content\/gallery\/kriging\/untitled-6.jpg\"\n\t\t     title=\"\"\n             data-src=\"http:\/\/the-witness.net\/news\/wp-content\/gallery\/kriging\/untitled-6.jpg\"\n             data-thumbnail=\"http:\/\/the-witness.net\/news\/wp-content\/gallery\/kriging\/thumbs\/thumbs_untitled-6.jpg\"\n             data-image-id=\"84\"\n             data-title=\"untitled-6\"\n             data-description=\"\"\n             target='_self'\n             class=\"shutterset_e6e1f8b5de3183c73c5f0140be290b53\">\n            <img class=\"ngg-singlepic\"\n             src=\"http:\/\/the-witness.net\/news\/wp-content\/gallery\/kriging\/dynamic\/untitled-6.jpg-nggid0284-ngg0dyn-400x300x100-00f0w010c010r110f110r010t010.jpg\"\n             alt=\"untitled-6\"\n             title=\"untitled-6\"\n              width=\"400\"               \/>\n    \t<\/a>\n\t\t      <\/div>\n    <span><\/span>    <\/p>\n<p>Then, when I decide I like the shape, I can press another key to apply these control points to the game's terrain:<\/p>\n<p>            \n\t\t<div class=\"ngg-gallery-singlepic-image ngg-center\" style=\"max-width: 400px\">\n\t\t\t    \t<a href=\"http:\/\/the-witness.net\/news\/wp-content\/gallery\/kriging\/untitled-7.jpg\"\n\t\t     title=\"\"\n             data-src=\"http:\/\/the-witness.net\/news\/wp-content\/gallery\/kriging\/untitled-7.jpg\"\n             data-thumbnail=\"http:\/\/the-witness.net\/news\/wp-content\/gallery\/kriging\/thumbs\/thumbs_untitled-7.jpg\"\n             data-image-id=\"85\"\n             data-title=\"untitled-7\"\n             data-description=\"\"\n             target='_self'\n             class=\"shutterset_7fab1ee19d3e1e7c3dd7e389d51de924\">\n            <img class=\"ngg-singlepic\"\n             src=\"http:\/\/the-witness.net\/news\/wp-content\/gallery\/kriging\/dynamic\/untitled-7.jpg-nggid0285-ngg0dyn-400x300x100-00f0w010c010r110f110r010t010.jpg\"\n             alt=\"untitled-7\"\n             title=\"untitled-7\"\n              width=\"400\"               \/>\n    \t<\/a>\n\t\t      <\/div>\n    <span><\/span>    <\/p>\n<p>(Now there's a lot of z-fighting between the control surface and the terrain because they are in pretty much the same place.)<\/p>\n<p>Here is the terrain with the control surface hidden. \u00a0Note that the terrain values have been smoothly interpolated, even in areas outside the control object:<\/p>\n<p>            \n\t\t<div class=\"ngg-gallery-singlepic-image ngg-center\" style=\"max-width: 400px\">\n\t\t\t    \t<a href=\"http:\/\/the-witness.net\/news\/wp-content\/gallery\/kriging\/untitled-8.jpg\"\n\t\t     title=\"\"\n             data-src=\"http:\/\/the-witness.net\/news\/wp-content\/gallery\/kriging\/untitled-8.jpg\"\n             data-thumbnail=\"http:\/\/the-witness.net\/news\/wp-content\/gallery\/kriging\/thumbs\/thumbs_untitled-8.jpg\"\n             data-image-id=\"86\"\n             data-title=\"untitled-8\"\n             data-description=\"\"\n             target='_self'\n             class=\"shutterset_33612225a654800db158e41052bf618d\">\n            <img class=\"ngg-singlepic\"\n             src=\"http:\/\/the-witness.net\/news\/wp-content\/gallery\/kriging\/dynamic\/untitled-8.jpg-nggid0286-ngg0dyn-400x300x100-00f0w010c010r110f110r010t010.jpg\"\n             alt=\"untitled-8\"\n             title=\"untitled-8\"\n              width=\"400\"               \/>\n    \t<\/a>\n\t\t      <\/div>\n    <span><\/span>    <\/p>\n<p><em>(By the way, those seams you see in the previous two pictures are lighting seams. \u00a0These don't have anything to do with Kriging, it's just that the terrain blocks don't know how to look up their neighbors' height values when generating their surface normals. \u00a0Any block-based terrain system has to solve a similar problem. \u00a0It's easy to solve, but at a low priority on the to-do list, hence it hasn't been done yet. \u00a0The distance between seams tells you how big the terrain blocks are, so the seams are sort of useful in this image!)<\/em><\/p>\n<p><em><span style=\"font-style: normal;\">Suppose this control surface is part of a scene that I want to reorient. \u00a0I'll rotate it about 70 degrees clockwise (now you'll see the surface intercutting more-heavily with the existing terrain values):<\/span><\/em><\/p>\n<p>            \n\t\t<div class=\"ngg-gallery-singlepic-image ngg-center\" style=\"max-width: 400px\">\n\t\t\t    \t<a href=\"http:\/\/the-witness.net\/news\/wp-content\/gallery\/kriging\/untitled-9.jpg\"\n\t\t     title=\"\"\n             data-src=\"http:\/\/the-witness.net\/news\/wp-content\/gallery\/kriging\/untitled-9.jpg\"\n             data-thumbnail=\"http:\/\/the-witness.net\/news\/wp-content\/gallery\/kriging\/thumbs\/thumbs_untitled-9.jpg\"\n             data-image-id=\"87\"\n             data-title=\"untitled-9\"\n             data-description=\"\"\n             target='_self'\n             class=\"shutterset_6085372825872410e00671b76d011f8e\">\n            <img class=\"ngg-singlepic\"\n             src=\"http:\/\/the-witness.net\/news\/wp-content\/gallery\/kriging\/dynamic\/untitled-9.jpg-nggid0287-ngg0dyn-400x300x100-00f0w010c010r110f110r010t010.jpg\"\n             alt=\"untitled-9\"\n             title=\"untitled-9\"\n              width=\"400\"               \/>\n    \t<\/a>\n\t\t      <\/div>\n    <span><\/span>    <\/p>\n<p>And then once again, I apply Kriging to the terrain, generating the same hills in a rotated orientation:<\/p>\n<p>            \n\t\t<div class=\"ngg-gallery-singlepic-image ngg-center\" style=\"max-width: 400px\">\n\t\t\t    \t<a href=\"http:\/\/the-witness.net\/news\/wp-content\/gallery\/kriging\/untitled-10.jpg\"\n\t\t     title=\"\"\n             data-src=\"http:\/\/the-witness.net\/news\/wp-content\/gallery\/kriging\/untitled-10.jpg\"\n             data-thumbnail=\"http:\/\/the-witness.net\/news\/wp-content\/gallery\/kriging\/thumbs\/thumbs_untitled-10.jpg\"\n             data-image-id=\"76\"\n             data-title=\"untitled-10\"\n             data-description=\"\"\n             target='_self'\n             class=\"shutterset_4d0e11771358a99831c049424525676c\">\n            <img class=\"ngg-singlepic\"\n             src=\"http:\/\/the-witness.net\/news\/wp-content\/gallery\/kriging\/dynamic\/untitled-10.jpg-nggid0276-ngg0dyn-400x300x100-00f0w010c010r110f110r010t010.jpg\"\n             alt=\"untitled-10\"\n             title=\"untitled-10\"\n              width=\"400\"               \/>\n    \t<\/a>\n\t\t      <\/div>\n    <span><\/span>    <\/p>\n<p>To illustrate the way that interpolation is not confined to a single control object, and that in fact this is a control point soup, here I've created three control objects:<\/p>\n<p>            \n\t\t<div class=\"ngg-gallery-singlepic-image ngg-center\" style=\"max-width: 400px\">\n\t\t\t    \t<a href=\"http:\/\/the-witness.net\/news\/wp-content\/gallery\/kriging\/untitled-11.jpg\"\n\t\t     title=\"\"\n             data-src=\"http:\/\/the-witness.net\/news\/wp-content\/gallery\/kriging\/untitled-11.jpg\"\n             data-thumbnail=\"http:\/\/the-witness.net\/news\/wp-content\/gallery\/kriging\/thumbs\/thumbs_untitled-11.jpg\"\n             data-image-id=\"77\"\n             data-title=\"untitled-11\"\n             data-description=\"\"\n             target='_self'\n             class=\"shutterset_ee9e9339400e0523a1925bc8fa03e8df\">\n            <img class=\"ngg-singlepic\"\n             src=\"http:\/\/the-witness.net\/news\/wp-content\/gallery\/kriging\/dynamic\/untitled-11.jpg-nggid0277-ngg0dyn-400x300x100-00f0w010c010r110f110r010t010.jpg\"\n             alt=\"untitled-11\"\n             title=\"untitled-11\"\n              width=\"400\"               \/>\n    \t<\/a>\n\t\t      <\/div>\n    <span><\/span>    <\/p>\n<p>And here's what the terrain looks like with them applied:<\/p>\n<p>            \n\t\t<div class=\"ngg-gallery-singlepic-image ngg-center\" style=\"max-width: 400px\">\n\t\t\t    \t<a href=\"http:\/\/the-witness.net\/news\/wp-content\/gallery\/kriging\/untitled-12.jpg\"\n\t\t     title=\"\"\n             data-src=\"http:\/\/the-witness.net\/news\/wp-content\/gallery\/kriging\/untitled-12.jpg\"\n             data-thumbnail=\"http:\/\/the-witness.net\/news\/wp-content\/gallery\/kriging\/thumbs\/thumbs_untitled-12.jpg\"\n             data-image-id=\"78\"\n             data-title=\"untitled-12\"\n             data-description=\"\"\n             target='_self'\n             class=\"shutterset_88be361d595a96308c8c440c184ed572\">\n            <img class=\"ngg-singlepic\"\n             src=\"http:\/\/the-witness.net\/news\/wp-content\/gallery\/kriging\/dynamic\/untitled-12.jpg-nggid0278-ngg0dyn-400x300x100-00f0w010c010r110f110r010t010.jpg\"\n             alt=\"untitled-12\"\n             title=\"untitled-12\"\n              width=\"400\"               \/>\n    \t<\/a>\n\t\t      <\/div>\n    <span><\/span>    <\/p>\n<p>And here's the final terrain along with the control objects (lots of z-fighting again):<\/p>\n<p>            \n\t\t<div class=\"ngg-gallery-singlepic-image ngg-center\" style=\"max-width: 400px\">\n\t\t\t    \t<a href=\"http:\/\/the-witness.net\/news\/wp-content\/gallery\/kriging\/untitled-13.jpg\"\n\t\t     title=\"\"\n             data-src=\"http:\/\/the-witness.net\/news\/wp-content\/gallery\/kriging\/untitled-13.jpg\"\n             data-thumbnail=\"http:\/\/the-witness.net\/news\/wp-content\/gallery\/kriging\/thumbs\/thumbs_untitled-13.jpg\"\n             data-image-id=\"79\"\n             data-title=\"untitled-13\"\n             data-description=\"\"\n             target='_self'\n             class=\"shutterset_1e2ebfcea120e23ecfd1a2d82537e8a6\">\n            <img class=\"ngg-singlepic\"\n             src=\"http:\/\/the-witness.net\/news\/wp-content\/gallery\/kriging\/dynamic\/untitled-13.jpg-nggid0279-ngg0dyn-400x300x100-00f0w010c010r110f110r010t010.jpg\"\n             alt=\"untitled-13\"\n             title=\"untitled-13\"\n              width=\"400\"               \/>\n    \t<\/a>\n\t\t      <\/div>\n    <span><\/span>    <\/p>\n<p>Hopefully that's enough screenshots to illustrate the point. \u00a0Compared to splines, Kriging has the following highly beneficial properties (aside from the huge benefits of not requiring a topology, discussed above):<\/p>\n<ul>\n<li>Always interpolates every control point exactly<\/li>\n<li>Adding a new control point does not change the existing surface at all.<\/li>\n<li>No funny spline-like behavior (curves appearing in strange places, huge overshoots, etc)<\/li>\n<\/ul>\n<p><P>&nbsp;<P><br \/>\n<strong>Hacking In Compact Support<\/strong><\/p>\n<p>When doing Kriging in a video game, you probably don't want to compute the terrain for the entire world at once for every change, because that would be very expensive, and it also is a little bit unintuitive (since a change to a control point very far away could change some interpolated terrain nearby, even if only slightly). \u00a0However, the vanilla Kriging algorithm would require global computation and thus behave nonlocally. \u00a0For <em>The Witness<\/em> we clearly wanted our control points to affect only nearby areas, <em>i.e.<\/em> have compact support.  My first attempt at this was:<\/p>\n<ul>\n<li>Pick a radius r0 at which any control point has maximum effect, and a larger radius r1 at which the point has no effect, with smooth interpolation in-between.<\/li>\n<li>Terrain consists of contiguous blocks 33x33 samples in size.<\/li>\n<li>For each block of terrain you want to Krige, find all control points whose distance in the XY plane from any sample in the block is less than r1 <em>(these are all the control points that could possibly affect any sample in the block).<\/em><\/li>\n<li>Pass all these points to the Kriging routine, which computes every sample in the block. \u00a0It does this by building an inverse variogram matrix -- square with dimension (n+1) -- and performing one matrix-vector multiply for each terrain sample, which is pretty fast.<\/li>\n<li>For each sample, the Kriging routine gives us the set of weights we would use to blend each control point if the control points did not have compact support. \u00a0To provide compact support, just compute an additional factor that is (distance - r1) \/ (r0 - r1) clamped to [0, 1], multiply all the weights by these factors, and then re-normalize the weights (ensure they all add to 1).<\/li>\n<\/ul>\n<p><P><br \/>\nThis almost works, but not quite; we get discontinuities between terrain blocks (which should not happen if everything is correct, since two vertices at the same XY position should always get the same Z value).  At first I thought this was a programming error, but after looking more closely at the situation I convinced myself that the reason is mathematical. \u00a0My assumption had been that a control point with a weight of 0 will have the same effect as a control point that never enters into the calculation, but this seems not to be true.<\/p>\n<p>The step that I added, multiplying the compact-support factors against the weights provided by the matrix-vector multiply, is -- by the distributive property -- the same as multiplying each row of the matrix by the support factor before doing the matrix-vector multiply. \u00a0If a support factor is 0, then this makes the matrix rank-deficient, <em>i.e.<\/em> it could not have been the result of an inversion, which means that this step is not compatible with the mathematical technique being applied. \u00a0The idea of a 0 blend factor just isn't valid. \u00a0<em>(This also brings into question whether the idea of multiplying by the support factors is valid at all, but for now I will just observe that it produces very useful and convenient behavior. \u00a0However, there are some small stepping artifacts that I see in some locations. \u00a0I think these may be due to having too-low a density of control points in the world (some terrain samples having 0 control points inside r1) but there's a possibility that they aren't, and have something to do with the weighting procedure itself. \u00a0I haven't investigated yet.)<\/em><\/p>\n<p>Now, as I type this, I am not 100% convinced that it is the right reasoning, and that something more like a typical programming error might be the problem. \u00a0My math is pretty rusty these days. \u00a0However, my updated version of the routine does not have discontinuities. \u00a0<\/p>\n<p>Unfortunately, the fix for discontinuities has made the Kriging a bit slower. \u00a0What I do is, instead of building just one inverse variogram for the entire terrain block, I build a new variogram for every vertex, and include in that variogram only the control points that have nonzero support factors. \u00a0Thus the dimensions of the matrix will change from sample to sample, and I am inverting a new matrix for each output sample.<\/p>\n<p>Unoptimized, this version of the algorithm takes between 5 and 25 seconds to recompute the terrain for the entire world, depending on how large r1 is. \u00a0This is fine for now, especially since editing operations only hit about 1\/50 of the world or less at any given time. \u00a0However, it'll get slower as more control points are added to the world.<\/p>\n<p>Fortunately, this first version of the discontinuity-fixing was intended to be slow -- I programmed it in the most straightforward way possible, to maximize the likelihood that it would work. \u00a0It is easy to think of ways to make it faster by caching the matrices, but it's unclear which of these would be fastest. \u00a0For example, if you just cache one copy of the matrix and invalidate it any time a new control point comes into or out of range, then you might easily find yourself in a situation where you oscillate between recomputing two different matrices as you work your way along the sample grid. \u00a0This is a little reminiscent of the kinds of problems that 3D hardware has, which store images in a non-linear way so that they can be cache-efficient when sampled along lines in arbitrary directions... but it's not clear to me how far to take that analogy or whether any applicable wisdom can be derived from it.<\/p>\n<p>So I intend to optimize it, or come up with a faster solution to the discontinuity problem, but it's not clear yet what form that will take.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In The Witness we decided that it&#8217;s a good idea to have a heightfield terrain making up the bulk of the island surface. \u00a0The advantages of this are in rendering performance and in ease of texturing (it is relatively easy, with one continuously-parameterized terrain block, to get rid of seams; \u2026<\/p>\n<p class=\"continue-reading-button\"> <a class=\"continue-reading-link\" href=\"http:\/\/the-witness.net\/news\/2010\/05\/kriging-is-cool\/\">Continue reading<i class=\"crycon-right-dir\"><\/i><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[7],"tags":[],"_links":{"self":[{"href":"http:\/\/the-witness.net\/news\/wp-json\/wp\/v2\/posts\/345"}],"collection":[{"href":"http:\/\/the-witness.net\/news\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/the-witness.net\/news\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/the-witness.net\/news\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"http:\/\/the-witness.net\/news\/wp-json\/wp\/v2\/comments?post=345"}],"version-history":[{"count":25,"href":"http:\/\/the-witness.net\/news\/wp-json\/wp\/v2\/posts\/345\/revisions"}],"predecessor-version":[{"id":1220,"href":"http:\/\/the-witness.net\/news\/wp-json\/wp\/v2\/posts\/345\/revisions\/1220"}],"wp:attachment":[{"href":"http:\/\/the-witness.net\/news\/wp-json\/wp\/v2\/media?parent=345"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/the-witness.net\/news\/wp-json\/wp\/v2\/categories?post=345"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/the-witness.net\/news\/wp-json\/wp\/v2\/tags?post=345"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}