【问题标题】:Why can't I set a variable equal to a function return value in GLSL (WebGL)?为什么我不能在 GLSL (WebGL) 中设置一个等于函数返回值的变量?
【发布时间】:2012-03-23 13:16:31
【问题描述】:

我刚开始使用 GLSL Sandbox (http://glsl.heroku.com),我注意到如果我声明一个函数并尝试将其返回值传递给主功能。这就是我现在拥有的:

#ifdef GL_ES
precision mediump float;
#endif

uniform float time;

void main( void ) {
    float color = test(); // code breaks here
    gl_FragColor = vec4( vec3( color, color * 0.5, sin( color + time / 3.0 ) * 0.75 ), 1.0 );
}

float test(){return 5.0;}

我不太确定为什么会这样。我猜这是我不知道的片段着色器。如果有人能解释我做错了什么,那就太好了,谢谢。

【问题讨论】:

    标签: glsl webgl shader


    【解决方案1】:

    您可能习惯于 JavaScript 或 Python 等语言,您可以在其中访问在您尝试使用它们之后声明的值(如函数)。 GLSL 是仿照 C 和 C++ 建模的,所以这是不可能的。

    如果你不想在main 之前定义test,那么你至少需要声明它:

    float test();
    
    void main( void ) {
        float color = test(); // code breaks here
        gl_FragColor = vec4( vec3( color, color * 0.5, sin( color + time / 3.0 ) * 0.75 ), 1.0 );
    }
    
    float test(){return 5.0;}
    

    【讨论】:

    • 德普。我早该知道。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2021-09-20
    • 2015-04-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-01-08
    • 1970-01-01
    • 2015-11-28
    相关资源
    最近更新 更多