A tool for generating textures from text files.
C++ CMake Shell
Switch branches/tags
Nothing to show
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.


Texture Generator Build Status

This is an incomplete command line tool that generates textures based on a JSON spec. Parts of the project are inspired by mrdoob's texgen.js.

Output samples

Generator Functions:

  • constant color
  • random non-coherent noise
  • perlin noise
  • simplex noise
  • fractal Brownian motion (fBm)
  • gradient / gradient mapping
  • sin
  • or / xor
  • pow ("clouds")
  • rectangle
  • circle
  • custom mathematical expression

Tool Features

  • TGA output
  • Watch files for changes and automatically regenerate textures
  • Files can contain multiple output textures
  • Outputs how long each texture took to generate

Usage Example

$ cat << EOF > test.json
	"size": [ 256, 256 ],
	"out": "test.tga",
	"ops": [
		{ "add": "xor", "tint": [ 1.0, 0.5, 0.7 ] },
		{ "add": "sinx", "freq": 0.004, "tint": [ 0.25, 0.0, 0.0 ] },
		{ "sub": "siny", "freq": 0.004, "tint": [ 0.25, 0.0, 0.0 ] },
		{ "add": "sinx", "freq": 0.0065, "tint": [ 0.1, 0.5, 0.2 ] },
		{ "add": "siny", "freq": 0.0065, "tint": [ 0.0, 0.4, 0.5 ] },
		{ "add": "noise", "tint": [ 0.1, 0.1, 0.2 ] }

$ gentex test.json

See more examples in the tests-directory.


You need CMake and a C++11 compiler such as g++ 4.9 or clang++ 3.5. A couple of needed third-party libraries are included in the repository so there are no additional dependencies.

mkdir build
cd build
cmake ..
make -j4

JSON Texture Spec

The gentex tool takes a valid JSON file as input and writes one or more image files as output.

Each texture is specified as a JSON object. Multiple textures can be produced from a single input file by wrapping the texture specifications inside a JSON array.

Each individual texture spec contains the following keys:

  • size: 2d array specifying the dimensions of the generated image, e.g. "size": [ 256, 256 ]
  • out: output filename, e.g. "out": "test.tga"
    • format is determined from file extension, supported:
      • .png (recommened, losslessly compressed)
      • .tga (fastest to write, defaults to uncompressed, large file size)
      • .jpg (lossy compression, smallest file size on complex images)
  • ops: array of operations (each is a JSON object) that produce the desired image when applied sequentially (see below)


Each operation in the ops array must have a key that determines how the result from the operation is applied to the current state of the generation. The key's value determines what kind of pixels are generated. Each operation then takes a variable list of additional parameters.

Available keys (composition operators):

  • set: overwrite the existing state
  • add: add new pixel values to the old ones
  • sub: subtract new pixel values from the old ones
  • mul: multiply old pixel values with the new ones
  • div: divide old pixel values by the new ones
  • min: pick smallest of the values, in each color channel
  • max: pick largest of the values, in each color channel
  • save: special, store the current image, named with the given string value for later use

Available values (generator functions):

Almost all generators take an optional tint parameter which is a color that is multiplied with the result of the function. Tint is omitted from the list below.

  • const: plain, constant color
  • rect: rectangle
    • pos: position in pixels
    • size: size in pixels
  • circle: circle
    • pos: position in pixels
    • radius: radius in pixels
  • pixelate: pixelate the image
    • size: how big the new "pixels" are
  • boxblur: blur using a box filter
    • radius: radius(es) of the box kernel to use (can specify separately for x/y axes)
  • blend: blend between the current state and another saved image state
    • other: string naming the image to blend with, must be used previously in save command
    • alpha: blend factor between 0-1, near 0 is mostly current state, near 1 means mostly the other state
  • noise: random non-coherent white noise
  • simplex: coherent simplex noise
  • perlin: coherent perlin noise
  • fbm: fractal Brownian motion, i.e. multiple octaves of perlin noise
  • gradientx: horizontal linear gradient
    • colors: array of at least two colors that form the gradient
    • stops: optional array of numbers from 0 to 1 specifying the fraction of the full length the corresponding color takes
  • gradienty: same as gradientx but vertical
  • gradientr: radial gradient, like circle and gradientx/y combined
  • gradientmap: similar to other gradients, but uses the value of each existing pixel as the position for looking up the gradient color
  • sinx: sine wave in the form of sin((x + offset) * freq * pi)
    • freq: frequency value (will be multiplied by pi)
    • offset: offset value
  • siny: same as sinx but in the y direction
  • sin: same as sinx followed by siny, with ability to set the parameters individually through a 2d array
  • calc: arbitrary per-pixel math expression
    • expr: the expression, available variables: x, y, w, h
  • TODO: incomplete list (see tests and source code for more info)

Number parameters can also be strings, in which case they are evaluated as math expressions. Array parameters can also contain math expressions in their components inside strings. Furthermore, 2d array can be a single number / expression in which case both elements assume the same value.

When a parameter is color (such as tint), the following formats are supported:

  • Hex string: "#8800ff"
  • Abbreviated hex string: "#80f"
  • RGB component array: [0.5, 0.0, "sin(pi/4)"]
  • Lone number or expression is copied to all elements: 0.4 --> [0.4, 0.4, 0.4]

Math expressions

Math expressions must always be inside quotes (in order to keep the JSON valid).

Following constants are available: pi, tau, e

Following functions are available: abs, sqrt, ln, lb, lg, cos, sin, tan, exp, rnd

Following operators are available: ! ^ + - * / % < > ( )