Fragment shader: No uniform with name in shader

匿名 (未验证) 提交于 2019-12-03 00:44:02

问题:

I have problem with fragment shader in libgdx. Below is my fragment shader.

#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); }

In program I do

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

shader.isCompiled() return true and first set work fine, but in second I have error "no uniform with name 'u_aspectRatio' in shader". If delete line:

uniform float u_aspectRatio;

from shader all work fine, but when I add this line(in feature I want work with this object) and try set some data I have error.

回答1:

The shader compiler will optimize away unused uniforms, so there is no u_aspectRatio uniform at run-time. See http://ogltotd.blogspot.com/2007/12/active-shader-uniforms-and-vertex.html

The other way to "fix" this shader would be to use the variable somewhere (e.g., multiply v_texCoords by it).



回答2:

I was having the same problem as you are getting but the following solution worked for me

Use float uniform Like this :

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


回答3:

That's because the shader compiler optimizes away unused uniforms. You can ignore it. If you can't, use a shader program class that does.



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!