如何在GLSL(WebGL)中将int转换为float?

Ite*_*tor 13 opengl-es glsl webgl

我的代码是(在void main中):

float res;

for(int i=0; i<15; i++) {

    res = float(i)/15.0;

    //...

}
Run Code Online (Sandbox Code Playgroud)

不幸的是我收到了语法错误 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)
Run Code Online (Sandbox Code Playgroud)

如果我只是尝试i/15然后结果是一个整数,但我想得到一个浮点数.

这怎么可能投intfloat

mad*_*uri 18

似乎你不允许使用GLSL.因此,"你必须使用构造函数 ".

试试这个:

// 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;
Run Code Online (Sandbox Code Playgroud)

PS:如果您查看文档,它会说"......整数类型都可以转换为浮点数,整数和浮点数可以转换为双精度数." ...我觉得很奇怪,你的代码不被GLSL编译器接受.(参见Reto Koradi的评论)