w3_terrain_from_math

This bundle is marked as pending. It has not been reviewed by a staff member yet.
  • Like
Reactions: deepstrasz
w3_terrain_from_math is a tool for generating terrain (z height values) using mathematical formulas. The formulas are written in a C-like expression language. Formulas are evaluated to compute the z height of each vertex of a plane mesh made of W x H squares. The plane mesh can then be viewed from different angles by moving the camera.

Expressions

Literals

There are integer and floating point literals.

Integer literals

Integer literals use base 10 only. Hexadecimal, octal, binary or other bases are not supported. The type of the integer literals is int64_t (64-bit signed integer).

Example integer literals

JASS:
-9223372036854775808, -1, 0, 1, 9223372036854775807

Floating point literals

Floating point literals use decimal notation. Scientific notation (2.99792458e+8) is not supported. The type of the floating point literals is double (64-bit floating point).

Example floating point literals

JASS:
-1.0, -0.5, 0.0, 0.5, 1.0, 3.14

Operators

Unary operators

The following unary operators are supported:
  • - negate
  • ~ bitwise negate, converts its argument to integer
  • ! logical negate, converts its argument to integer

Binary operators

The following binary operators are supported:
  • + addition
  • - subtraction
  • * multiplication
  • / division
  • % modulo/remainder, rounds towards 0
  • ** power, 4**3**2 = 4**9 = 262144
  • | bitwise or, converts its arguments to integers
  • & bitwise and, converts its arguments to integers
  • ^ bitwise exclusive or, converts its arguments to integers
  • << shift left, converts its arguments to integers
  • >> logical shift right, converts its arguments to integers
  • >>> arithmetic shift right, converts its arguments to integers
  • < less than
  • <= less than or equal to
  • >= greater than or equal to
  • > greater than
  • == equal to
  • != not equal to
  • || logical or
  • && logical and

Operator precedence​

The following table lists the precedence and associativity of operators. Operators are listed in descending precedence.

precedenceoperatorsassociativity
1() (function call)left to right
2- ~ !right to left
3**right to left
4* / %left to right
5+ -left to right
6<< >>>>>left to right
7< <= >=>left to right
8== !=left to right
9&left to right
10^left to right
11|left to right
12&&left to right
13||left to right

Variables

The following variables and constants can be used by the formulas:
  • xx : integer - the number of squares of the plane in the x direction
  • yy : integer - the number of squares of the plane in the y direction
  • x : integer - the current vertex's x value; range: [-xx/2,+xx/2]
  • y : integer - the current vertex's y value; range: [-yy/2,+yy/2]
  • xt : float - the current vertex's xt value; range: [-1.0, 1.0]
  • yt : float - the current vertex's yt value; range: [-1.0, 1.0]
  • xn : integer - similar to x; range: [0, xx]
  • yn : integer - similar to y; range: [0, yy]
  • xtn : float - similar to xt value; range: [0.0, 1.0]
  • ytn : float - similar to yt value; range: [0.0, 1.0]
  • tau : float
  • pi : float
  • e : float
  • phi : float
  • sqrt2 : float

Functions

The argument type I is an alias for int64_t, the argument type F is an alias for double and the argument type IF means that the function works for both integer and floating point arguments. When a function works for both integer and floating point arguments and one of the arguments has type F, then all the other arguments are converted to floating point. If the result of a function is NaN (not a number), 0.0 is returned instead.

The following functions can be used by the formulas:

  • if(cond: IF, exprIfTrue:IF, exprIfFalse: IF) - evaluates the cond expression and returns exprIfTrue if cond is not equal to 0, otherwise returns exprIfFalse
  • abs(x: IF) - absolute value
  • sign(x: IF) - returns the sign of x; -1 when x < 0; 1 when 0 <= x
  • sign0(x: IF) - returns the sign of x; -1 when x < 0; 0 when x == 0; 1 when0 < x
  • sqrt(x: F) - returns the square root of x
  • ln(x: F) - logarithm base e
  • logbase(x: F, base: F) - logarithm base base; logbase(8.0,2.0) == 3.0, 8.0 == 2.0**3.0
  • cos(radians: F)
  • sin(radians: F)
  • tan(x: F)
  • acos(x: F) - pi ==acos(cos(pi)), x must be in the range [-1.0, 1.0]
  • asin(x: F) - pi/2 ==asin(sin(pi/2)), x must be in the range [-1.0, 1.0]
  • atan2xy(x: F, y: F)
  • min(a: IF, b: IF)- returns the minimum value
  • max(a: IF, b: IF)- returns the maximum value
  • clamp(v: IF, a: IF, b: IF) - if v < a returns a; if b < v returns b; otherwise returns v
  • clamp01(v: F) - same as clamp(v, 0.0, 1.0)
  • clampn11(v: F) - same as clamp(v, -1.0, 1.0)
  • lerp(a: F, b: F, t: F) - linear interpolation
  • invlerp(a: F, b: F, v: F) - t == invlerp(a, b,lerp(a, b, t))
  • remap(imin: F, imax: F, omin: F, omax: F, v: F) - remaps v from the range [imin, imax] to the range [omin,omax]
  • repeat(v: F, len: F) - like operator % but rounds towards negative infinity
  • smooth01(v: F) - smoothstep, v is clamped by clamp01
  • quadratic(a: F, b: F, c: F, whichRoot: I) - computes the roots of the ax**2 + bx + c = 0equation; if there are no real roots, then 0.0 is returned; if there is1 root, then it is returned; if there are 2 roots, the whichRoot argument selects which root to return (0 - the first root, 1 - the second root)

Camera movement and keyboard shortcuts

Camera movement

The camera is moved using the w, a,s, d keys (holding shift moves the camera slightly slower). The camera is rotated with the mouse, while holding the rmb (right mouse button). The position of the camera can also be changed by clicking with the lmb on the plane mesh. Pressing ctrl+shift+c resets the camera.

Keyboard shortcuts

  • esc - program exit
  • return/enter - evaluate formula (the formula input field must be focused)
  • [ / ] - plane mesh resize for W
  • shift+[ / ] -plane mesh resize for H
  • f11 - toggle fullscreen
  • ctrl+shift+c - camera reset
  • g - toggle grid lines display
  • l - toggle lighting
  • shift+l - reset light direction
  • arrow left/right - change light direction's yaw angle
  • arrow down/up - change light direction's pitch angle

Using the z heights in a map

Warcraft 3 stores the heights of the terrain in a file called war3map.w3e inside a map file. w3_terrain_from_math cannot directly edit a map file, it can only update/patch w3e files. You need to export the war3map.w3e file from an existing map using an mpq archive program like MPQEditor. After that, you drag and drop the war3map.w3e file over the w3_terrain_from_math's window to start the z heights updating/patching. If something doesn't work, then at least you still have the mathematical formula.

Example formulas

formuladescriptionimage
(xn & yn) >> 1Sierpiński's hills
screenshot01-png.578290
(0.5*xx * xt**2) + (0.5*yy * yt**2)Noether's blanket
screenshot02-png.578291
(16*cos(4.0 * xt)) * (16.0*cos(4.0 * yt)) / 16Lobachevsky's spirit
screenshot03-png.578292
max(0, 16 + -32.0*sqrt(xt**2 + yt**2))Euclid's cone
screenshot04-png.578293
sqrt(x * y) + sqrt(-x * y)Gaussian flower
screenshot05-png.578294
(25.0*cos(xt)) | (25.0*sin(yt))Leibniz' organ
screenshot06-png.578295
repeat(x * y, 8)Hypatia's roses
screenshot07-png.578296
(0.75*max(xx, yy))*smooth01(xtn * ytn)Bernoulli's hankie
screenshot08-png.578297
quadratic(0.5*y**2, -0.5*x**2, 0, 0) / (1.0/8 * max(xx, yy))Newton's brachistochrone
screenshot09-png.578298
abs(x**2)/(xx/2) & abs(y**2)/(yy/2)Stepanov's razor
screenshot10-png.578299
(x**3 + y**3) / (xx*yy/4)Cantor's mask
screenshot11-png.578300
max( (x**4 + y**2)/(1 << 14), (x**2 + y**4)/(1 << 14))Bourbaki's basket
screenshot12-png.578301
( ((xx/2 + x)(xx/4 + x) & (yy/2 + y)(yy/4 + y)) ) / (2 *max(xx, yy))Minkowski's summer
screenshot13-png.578302

PS: if you know/discover and interesting formula you can share it in this thread
Contents

w3_terrain_from_math (Binary)

Back
Top