目录
一,数据类型
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是一种在32位和64位模式下运行的语言,支持多种数据类型如int、float、vector和matrix。在类型转换中,VEX允许显式和隐式转换。操作符包括点操作符、比较操作符以及类型交互,同时强调了操作符的优先级。此外,文章还介绍了struct的使用和注释的格式。
最低0.47元/天 解锁文章
3607

被折叠的 条评论
为什么被折叠?



