本文已参与掘金创作者训练营第三期「高产更文」赛道,详情查看:掘力计划|创作者训练营第三期正在进行,「写」出个人影响力。
程序语言定义
脚本程序是由分号分隔的一系列语句定义的。编译器从上到下读取和评估语句。这些语句可以由变量赋值、函数调用和数学表达式组成。所有变量都存储为 32 位浮点值。
有一组预定义的输入变量可以读取,还有一组输出变量可以写入。
这些变量如下表所示:
预定义的输入变量
| 名字 | 描述 | 单位/范围 |
|---|---|---|
| x | Current pen x coordinate | Screen pixels |
| y | Current pen y coordinate | Screen pixels |
| rx | If ruler active, x coordinate input to ruler stage | Screen pixels |
| ry | If ruler active, y coordinate input to ruler stage | Screen pixels |
| sx | Start of stroke x coordinate | Screen pixels |
| sy | Start of stroke y coordinate | Screen pixels |
| ex | Last stroke end x coordinate | Screen pixels |
| ey | Last stroke end y coordinate | Screen pixels |
| psx | Last stroke start x coordinate | Screen pixels |
| psy | Last stroke start y coordinate | Screen pixels |
| p | Pen pressure | Normalized [0..1] |
| d | Distance from start of current stroke | Screen pixels |
| dn | For rulers with a center or connection point: normalized distance | Radial/Perspective/Connected: 1.0 at start, 0.0 at center/VP/connection point Ellipse: 0.0 at start, plus or minus 1.0 for each time around. |
| ds | Rulers: Radial, Perspective, Connected Distance from start to center/VP/connection point | Screen pixels |
| s | Pen speed | Screen pixels between updates |
| ew | Current ellipse ruler width (width >= height) | Screen pixels |
| eh | Current ellipse ruler height | Screen pixels |
| er | Ellipse ruler rotation | Degrees |
| eca | Ellipse ruler current angle of pen position | Degrees |
| nx | Line normal x coordinate | Normalized [0..1] |
| ny | Line normal y coordinate | Normalized [0..1] |
| dx | Change in x since last pen position | Screen pixels |
| dy | Change in y since last pen position | Screen pixels |
| cx | Custom center position x (set with shortcut) | Screen pixels |
| cy | Custom center position y (set with shortcut) | Screen pixels |
| rcx | Ruler center position x | Screen pixels |
| rcy | Ruler center position y | Screen pixels |
| r | Pen rotation | Normalized [0..1] |
| te | Pen tilt elevation | [0..1024] |
| ta | Pen tilt angle | Normalized [0..1] |
| res0 | Result 0 of special functions | Function dependant |
| res1 | Result 1 of special functions | Function dependant |
预定义的输出变量
| 名字 | 描述 | 单位/范围 |
|---|---|---|
| ox | Pen x coordinate | Screen pixels |
| oy | Pen y coordinate | Screen pixels |
| op | Pen pressure | Normalized [0..1] |
| or | Pen rotation | Normalized [0..1] |
| ote | Pen tilt elevation | [0..1024] |
| ota | Pen tilt angle | Normalized [0..1] |
当您开始启用脚本程序进行绘图时,所有输出变量(预定义的,以及您自己的)都初始化为 0 。
当你作画时,预定义的输入变量值将被更新,程序将被评估,预定义的输出变量将被读取并用于修改传递给你的美术应用程序的数据。
请注意,如果您不想修改所有的输出变量,则不必修改。如果一个程序只影响输出压力(op),或者只影响位置(ox 和 oy),这是完全可以接受的。
但是,为了使程序成功编译,您至少需要修改其中一个变量。
如果程序使用中心位置变量( cx 和 cy ),您可以通过将鼠标悬停在您想要的屏幕位置上,并点击设置脚本中心位置(Set Scripting Center Pos)快捷方式来设置该位置,该快捷方式可以在设置->编辑快捷方式菜单中设置。
如果您启用了覆盖,您还可以看到屏幕上的圆心位置,并与您的鼠标或笔进行交互。
程序中使用的数学运算符
| 操作符 | 释义 | 优先级 | ||
|---|---|---|---|---|
| = | assignement | -1 (can only be applied to variables) | ||
| condition ? x : y | if condition expression is true, x, otherwise y | 0 | ||
| && | boolean and | 1 | ||
| boolean or | 2 | |||
| <= | less or equal | 4 | ||
| >= | greater or equal | 4 | ||
| != | not equal | 4 | ||
| == | equal | 4 | ||
| greater than | 4 | |||
| < | less than | 4 | ||
| + | addition | 5 | ||
| - | subtraction | 5 | ||
| * | multiplication | 6 | ||
| / | division | 6 | ||
| raise x to the power of y | 7 | |||
程序中使用的内置函数
| 函数名 | 描述 |
|---|---|
| sin(x) | sine function (all trigonometric functions use radians) |
| cos(x) | cosine function |
| tan(x) | tangent function |
| asin(x) | inverse sine function |
| acos(x) | inverse cosine function |
| atan(x) | inverse tangent function |
| atan2(y, x) | inverse tangent of y/x, use if correct angle quadrant is needed |
| sinh(x) | hyperbolic sine function |
| cosh(x) | hyperbolic cosine function |
| tanh(x) | hyperbolic tangent function |
| asinh(x) | inverse hyperbolic sine function |
| acosh(x) | inverse hyperbolic cosine function |
| atanh(x) | inverse hyperbolic tangent function |
| log2(x) | logarithm to the base 2 |
| log10(x) | logarithm to the base 10 |
| log(x) | logarithm to the base 10 |
| ln(x) | logarithm to base e (2.71828...) |
| exp(x) | e raised to the power of x |
| fsqrt(x) | square root |
| frsqrt(x) | reciprocal square root |
| sign(x) | sign function -1 if x<0; 1 if x>0 |
| rint(x) | round to nearest integer |
| fabs(x) | absolute value |
| min(...) | min of all arguments |
| max(...) | max of all arguments |
| sum(...) | sum of all arguments |
| avg(...) | mean value of all arguments |
| rand() | random value between 0 and 1 |
| srand() | random value between -1 and 1 |
| randGauss(mean, stddev) | random value using gaussian distribution using supplied mean and standard deviation |
| fmod(x, d) | floating point remainder of x/d |
| ceil(x) | smallest integral value that is not less than x (round up) |
| floor(x) | largest integral value that is not greater than x (round down) |
| frac(x) | fractional part of x, equivalent to x - floor(x) |
| mix(a, b, w) | linear interpolation: a + (b - a) * w |
| smoothStep(e0, e1, x) | 0 when x <= e0, 1 when x >= e1, 3rd order interpolation in between |
| smootherStep(e0, e1, x) | 0 when x <= e0, 1 when x >= e1, 5th order interpolation in between |
| clamp(min, max, x) | clamps x between min and max |
| rotDeg(x, y, angle) | rotates the (x,y) vector angle degrees stores the rotated vector in (res0,res1), and returns the rotated x component |
| rotRad(x, y, angle) | same as rotDeg, but angle is in radians |
| normalize(x, y) | normalizes vector (x, y), stores it in (res0, res1), and returns the length of (x, y) |
| length(x, y) | length of vector (x, y) |
| rlength(x, y) | 1 / length(x, y) |
| lengthSq(x, y) | square length of vector (x, y) |
| debugOut(string, value) | printf style function that prints to the log file. Example: debugOut(“value: %f”, value); |