10
\$\begingroup\$

I needed a Minecraft-like stone texture, instead of creating it using image editing software I decided to generate one using R.

The idea is to create some noise and stretch it horizontally so the texture tends to have some horizontal lines.

Example output (this example is upscaled, the original file is 32x32 pixels):

enter image description here

My questions are: What do you dislike about this code? Are the libraries 'ambient' and 'png' necessary, or does R have appropriate functionality builtin?

The package ambient is used to create the Perlin noise, and the package png is used to create a low resolution PNG image.

library(ambient)
library(png)

size <- 32

# data is in a 1:2 ratio.
data <- noise_perlin(dim=c(size, size / 2), frequency=0.9)

min <- 0.1
max <- 0.6

# data's bound is -1 to 1, transform that into a bound of min to max.
inrange <- min + ((data + 1) / 2) * (max - min)

# turn into 1:1 ratio by repeating columns.
stretched <- inrange[, rep(1:(ncol(inrange)), each=2)]

writePNG(target="stone.png", image=stretched)
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.