VEX —— 数据类型及操作符

目录

一,数据类型

struct

类型转换

二,操作符

点操作符 Dot operator

比较操作符 Comparisons

类型交换

操作符优先级

三,注释

四,保留的关键字


一,数据类型

        VEX即可在32模式下运行,也可在64位模式下运行;在32位下,所有float/vector/integer都是32位;在64位下,它们就是64位;无double和long类型;默认VEX使用32位integers,如使用AttribCast节点转换为64位,VEX会丢弃多余的位;

可使用下划线来断开长数字;

  • int,整型,如1,-1,0x31,0b1,0212,1_000;
  • float,浮点型,如3.14,0.000_1;
  • vector2,二维向量,可表示纹理坐标,如{1,1};
  • vector,三维向量,可表示位置/方向/法线/颜色等,如{0,0,0};
  • vector4,四维向量,可表示位置(xyzw)或颜色(rgba),常用于四元数quaternion,如{0,0,0,1};
  • matrix2,二维矩阵,表示2D旋转矩阵,如{{1,0},{0,1}};
  • matrix3,三维矩阵,表示3D旋转矩阵或2D变换矩阵,如{{1,0,0},{0,1,0},{0,0,1}};
  • matrix,四维矩阵,表示3D变换矩阵,如{{1,0,0,0},{0,1,0,0},{0,0,1,0},{0,0,0,1}};
  • string,字符串,如“hello world”;
  • array,数组,如{0,1,2,3,4,5};
  • struct,结构体;
  • dict,字典;
  • bsdf,双向散射分布函数;

struct

        从H12起即可使用关键字struct定义新的结构化类型;类似C++11成员初始化,可在struct内给成员默认值;struct有两个隐式的构造函数,带参数(使用参数初始化成员)和不带参数(使用默认值);使用前必须定义;

        可在struct内定义函数,来组织代码并允许有限形式的面向对象编程;

  • 在struct函数内,可使用this来引用结构体实例;
  • 在struct函数内,可通过名字引用结构体字段,好像变量一样;
  • 可使用运算符->来调用结构体函数,如sampler->sample(),在结构体内部使用this->method();
struct randsampler {
    //Fields
    int  seed;

    //Methods
    float sample()
    {
        //Struct functions can refer to fields by name
        return random(seed++);
    }
} 

cvex shader()
{
    randsampler sampler = randsampler(11);
    for (int i = 0; i < 10; i++)
    {
        //Use -> to call methods on struct instances
        printf("%f\n", sampler->sample());
    }
}

Mantra特定类型

        Mantra有一些预定义的struct类型,用于特定的着色函数,如light,material,lpeaccumulator;

类型转换

        变量转换,类似C++/Java,将一种类型值转换为另一种类型,如int转换为float;

int a, b;
float c;
c = a / b; //编译器会进行整形除法
c = (float)a / (float)b; //编译器才会进行浮点除法

        函数转换,VEX调用函数不仅像C++/Java一样基于参数类型,还会基于返回值类型;为避免参数类型相同返回值类型不同而带来的歧义,可强制转换函数typename( ... )

float n;
n = noise(noise(P)); //即可调用float noise(vector),也可调用vector noise(vector)
n = noise(vector(noise(P)));
vector n = vector( noise(P) ); //无需转换,因为包含隐式转换
vector n = noise(P);

        由于函数转换不会产生任何类型转换(只是选择要调用的函数),因此使用它不会降低性能。 好的经验是尽可能使用函数转换,仅在需要显式类型转换时才使用变量转换;

二,操作符

        VEX具有标准的C语言操作,但也有些不同,如:

  • 两个vector或point相乘,表示各个元素间的相乘,而不是dot或cross;
  • 许多运算符是为非标量数据类型定义的,如vector*matrix会通过matrix变换vector;
  • 在不明确情况下,用一操作符将两不同类型组合后,其结果类型为右侧类型;
int + vector = vector

点操作符 Dot operator

  • 可使用点操作符,去访问vector、matrix、struct其中的各个组件;

Vector2,.x/.u访问第一个元素,.y/.v访问第二个元素;

Vector,.x/.r访问第一个元素,.y/.g访问第二个元素,.z/.b访问第三个元素;

Vector4,.x/.r访问第一个元素,.y/.g访问第二个元素,.z/.b访问第三个元素,.w/.a访问第四个元素;

Matrix,.xx访问[0][0],.zz访问[2][2],.ax访问[3][0]等;

v.xyz = set(v.x,v.y,v.z);

v1 = v.xyz; 

v4.rgba = set(v4.r,v4.g,v4.b,v4.a);

比较操作符 Comparisons

  • 关系操作符:==, !=, <, <=, >, >=
    • 左右类型应相同,仅限类型string、float、integer,返回整型;
  • 匹配操作符:~=
    • 字符串匹配操作符,相当于match函数;
  • 逻辑操作符:||, &&, !
  • 位操作符:&, |, ^,  ~

类型交换

  • float和int操作时,结果类型为左侧类型,如int*float=int;
  • 如vector和scalar(int或float)加减乘除时,相当于与每个组件加减乘除;
  • 如不同尺寸的vectors加减乘除时,相当于小尺寸vector缺失一个组件1,然后在每个组件加减乘除;可能会产生意想不到的结果,建议单独手动操作组件;

操作符优先级

Order Operator Associativity Description
15 () LtR Function call, expression grouping, structure member
13 ! LtR Logical negation
13 ~ LtR One’s complement
13 + LtR Unary plus (for example, +5)
13 - LtR Unary minus (for example, -5)
13 ++ LtR Increment (for exmaple, x++)
13 -- LtR Decrememt (for example, x--)
13 (type) LtR Type cast (for example, (int)x)
12 * LtR Multiplication
12 / LtR Division
12 % LtR Modulus
11 + LtR Addition
11 - LtR Subtraction
10 < LtR Less-than
10 > LtR Greater than
10 <= LtR Less-than or equal
10 >= LtR Greater than or equal
9 == LtR Equal
9 != LtR NOT Equal
9 ~= LtR String matches
8 & LtR Bitwise AND
7 ^ LtR Bitwise XOR
6 | LtR Bitwise OR
5 && LtR Logical AND
4 || LtR Logical OR
3 ?: LtR Ternary conditional (for example, x ? "true" : "false")
2 = += -= *= /= %= &= |= ^= RtL Variable assignment
1 , LtR Argument separator

三,注释

VEX 使用 C++ 风格的注释:

  • 单行注释前面加//;
  • 自由格式注释以 /* 开头并以 */ 结尾;

四,保留的关键字

  • char,int,float, vector2/,vector/vector4,matrix2/matrix3/matrix,string, struct,dict
  • if,else
  • do,for,while,foreach
  • break,continue
  • return
  • false,true
  • const
  • void
  • typedef
  • export
  • union
  • bsdf
  • illuminance
  • color
  • normal
  • gather
  • hpoint
  • import
  • integer
  • point
  • forpoints

你可能感兴趣的:(#,VEX,Houdini,VEX)