我的代码是(在 void main 内):
float res;
for(int i=0; i<15; i++) {
res = float(i)/15.0;
//...
}
不幸的是,我在
float(i)/15.0
收到语法错误。如果我只写
i/15.0
,那么错误是:wrong operand types no operation '/' exists that takes a left-hand operand of type 'mediump int' and a right operand of type 'const float' (or there is no acceptable conversion)
如果我只是尝试
i/15
那么结果是一个整数,但我想得到一个浮点数。怎么可能投
int
至float
?
最佳答案
It seems不允许您在 GLSL 中进行转换。因此,“你必须使用 a constructor”。
试试这个:
// http://www.shaderific.com/glsl-types/
// "Implicit type conversions are not supported.
// Type conversions can be done using constructors..."
float i_float = float(i);
res = i_float / 15.0;
PS:如果你看看at the documentation ,它说“......任何整数类型都可以转换为浮点数,整数和浮点数都可以转换为 double 数。” ...我觉得奇怪的是您的代码不被 GLSL 编译器接受。 (参见 Reto Koradi 的评论)
关于opengl-es - 如何将 int 转换为在 GLSL(WebGL)中 float ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33270823/