Faking marbles

Perlin noise embedded on a sphere

Still on the matter of Perlin noise, I finally managed to figure out how to wrap Perlin noise on a sphere (that is, as a Texture[]). It was a bit inconvenient that it seems (at least, to me) that not one of the usual references on Perlin noise mentioned the explicit formulae needed for a proper spherical embedding of Perlin noise. Making use of the functions already defined in the doc page for Compile[], here are two Mathematica functions you can use for generating textures that can be wrapped on a sphere:

sphericalPerlin1 = With[{octaves = 4},
Compile[{{theta, _Real}, {phi, _Real}, {amplitude, _Real},
{frequency, _Real}, {gain, _Real}, {lacunarity, _Real}},
Module[{noiseVal = 0.0, x, y, z, sf, freq = frequency,
amp = amplitude},
sf = Sin[phi];
x = sf*Cos[theta] + 1; y = sf*Sin[theta] + 1; z = Cos[phi] + 1;
Do[
noiseVal += amp*signedNoise[x*freq, y*freq, z*freq];
freq *= lacunarity; amp *= gain,
{octaves}
];
noiseVal
], CompilationOptions -> {"InlineExternalDefinitions" -> True}
]]

sphericalPerlin2 = With[{octaves = 4},
Compile[{{theta, _Real}, {phi, _Real}, {amplitude, _Real},
{frequency, _Real}, {gain, _Real}, {lacunarity, _Real}},
Module[{noiseVal = 0.0, x, y, z, sf, freq = frequency,
amp = amplitude},
sf = Sin[phi];
x = sf*Cos[theta] + 1; y = sf*Sin[theta] + 1; z = Cos[phi] + 1;
Do[
noiseVal += amp*Abs[signedNoise[x*freq, y*freq, z*freq]];
freq *= lacunarity; amp *= gain,
{octaves}
];
noiseVal
], CompilationOptions -> {"InlineExternalDefinitions" -> True}
]]

As an example, here is how to use sphericalPerlin2[] along with DensityPlot[] to generate a texture:

DensityPlot[sphericalPerlin2[th, ph, 0.5, 2.0, 0.5, 2.0],
{th, 0, 2 Pi}, {ph, 0, Pi},
AspectRatio -> Automatic, Frame -> None, PerformanceGoal -> "Quality", PlotPoints -> 200, PlotRange -> All]

and here is the result, after adding appropriate coloring (with thanks to Rob Collyer for the color gradient):

fiery marble map

In fact, this is the texture I used for upper leftmost “marble” in the first image.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: