1
我正在尝试制作一个照明系统,程序更改纹理(块)的亮度取决于它获取的光线,程序会为玩家可见的每个块(纹理)执行此操作。GLSL渲染纹理错误
但是,照明系统的工作原理非常完美,但是在使用着色器进行渲染时,一切都会变得糟糕起来。
此代码是在渲染循环 -
float light = Lighting.checkLight(mapPosY, mapPosX, this); // Returns the light the current block gets
map[mapPos].light = light; // Applies the light to the block
Shaders.block.setUniformf("lightBlock", light); // Sets the light value to the shader's uniform, to change the brightness of the current block/texture.
batch.draw(map[mapPos].TEXTURE, (mapPosX * Block.WIDTH), (mapPosY * Block.HEIGHT), Block.WIDTH, Block.HEIGHT); // Renders the block/texture to the screen.
结果是相当随机..
正如我说的头两行很好地工作,问题可能是在三线或着色器本身。
着色器:
顶点着色器 -
attribute vec4 a_color;
attribute vec3 a_position;
attribute vec2 a_texCoord0;
uniform mat4 u_projTrans;
varying vec4 vColor;
varying vec2 vTexCoord;
void main() {
vColor = a_color;
vTexCoord = a_texCoord0;
gl_Position = u_projTrans * vec4(a_position, 1.0f);
}
片段着色器 -
varying vec4 vColor;
varying vec2 vTexCoord;
uniform vec2 screenSize;
uniform sampler2D tex;
uniform float lightBlock;
const float outerRadius = .65, innerRadius = .4, intensity = .6;
const float SOFTNESS = 0.6;
void main() {
vec4 texColor = texture2D(tex, vTexCoord) * vColor;
vec2 relativePosition = gl_FragCoord.xy/screenSize - .5;
float len = length(relativePosition);
float vignette = smoothstep(outerRadius, innerRadius, len);
texColor.rgb = mix(texColor.rgb, texColor.rgb * vignette * lightBlock, intensity);
gl_FragColor = texColor;
}
为什么downvote?问题很好.. – Israelg99
你确定问题是没有正确传递给片段着色器的制服吗?您是否尝试过使用这个变量(即:硬编码值)?我没有关于libgdx的任何信息,但是你的程序似乎很简单... –
@PedroBoechat是的,我现在玩了两天的值,而且它仍然看起来像制服没有正确传递到片段shader ..如果你说着色器很好,它可以帮助我很多,因为现在我知道我不应该搜索这个修补程序。 – Israelg99