【问题标题】:Fragment shader: No uniform with name in shader片段着色器:着色器中没有名称的统一
【发布时间】:2013-07-02 11:42:49
【问题描述】:

我对 libgdx 中的片段着色器有疑问。下面是我的片段着色器。

#ifdef GL_ES
precision mediump float;
#endif
uniform float u_aspectRatio;
varying vec2 v_texCoords;
uniform sampler2D u_texture;     
void main()                 
{                       
  gl_FragColor = texture2D(u_texture, v_texCoords);
}

在我做的程序中

shader.setUniformi("u_texture", 0); // work fine
shader.setUniformf("u_aspectRatio", 0.0f); //no uniform with name 'u_aspectRatio' in shader

shader.isCompiled() 返回 true 并且第一个设置工作正常,但第二个我有错误“在着色器中没有名称为 'u_aspectRatio' 的制服”。如果删除行:

uniform float u_aspectRatio;

从着色器一切正常,但是当我添加这条线(在功能中我想使用这个对象)并尝试设置一些数据时我有错误。

【问题讨论】:

  • 我找到了答案。在着色器中不能统一不使用psObject

标签: opengl-es-2.0 glsl shader libgdx


【解决方案1】:

这是因为着色器编译器优化了未使用的制服。你可以忽略它。如果不能,请使用可以的着色器程序类。

【讨论】:

    【解决方案2】:

    着色器编译器将优化掉未使用的制服,因此在运行时没有u_aspectRatio 制服。见http://ogltotd.blogspot.com/2007/12/active-shader-uniforms-and-vertex.html

    “修复”此着色器的另一种方法是在某处使用该变量(例如,将 v_texCoords 乘以它)。

    【讨论】:

      【解决方案3】:

      我遇到了和你一样的问题,但以下解决方案对我有用

      像这样使用浮动制服:

       int a = shader.getUniformLocation("u_aspectRatio");
       shader.setUniformf(a ,0.0f);
      

      【讨论】:

        猜你喜欢
        • 2021-04-21
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-11-20
        • 2013-09-17
        • 2019-01-24
        • 1970-01-01
        相关资源
        最近更新 更多