asi*_*mes 4 glsl webgl c-preprocessor
对于相对昂贵但恒定的东西,例如pow()
运行前用户指定的常量,可以define
使用 a 来减少运行时计算吗?或者每个出现的内容define
都会被它所定义的内容所取代?
例如,这样做有什么好处吗:
#define MENGER_ITER 3
#define MENGER_ITER_POW pow(3.0, -float(MENGER_ITER))
// ...other code
return (length(max(abs(vec3(x, y, z))-1.0, 0.0))-0.25)*MENGER_ITER_POW;
// ...other code
Run Code Online (Sandbox Code Playgroud)
与此相反:
// ...other code
return (length(max(abs(vec3(x, y, z))-1.0, 0.0))-0.25)*pow(3.0, -float(MENGER_ITER));
// ...other code
Run Code Online (Sandbox Code Playgroud)
#define
只是进行文本替换
对于你的情况来说,这与
shaderSource = shaderSource.replace(/MENGER_ITER_POW/g, "pow(3.0, -float(MENGER_ITER))");
shaderSource = shaderSource.replace(/MENGER_ITER/g, "3");
gl.shaderSource(someShader, shaderSource);
Run Code Online (Sandbox Code Playgroud)
好处是它使您的代码更具可读性?