2015-05-08 44 views
0

我已经看到很多关于小插曲着色器的教程,就像these,但他们都没有说如何改变小插图的颜色,他们只是谈论应用棕褐色或灰色着色器整个复合图像。彩色小插图着色器(外部部分) - LIBGDX

例如上面的视频给出了阴影着色器的下面的代码。如何更改小插曲的颜色?所以它不是黑色,而是红色或橙色,图案内部的图像部分保持不变。

const float outerRadius = .65, innerRadius = .4, intensity = .6; 

void main(){ 
    vec4 color = texture2D(u_sampler2D, v_texCoord0) * v_color; 
    vec2 relativePosition = gl_FragCoord.xy/u_resolution - .5; 
    relativePosition.y *= u_resolution.x/u_resolution.y; 
    float len = length(relativePosition); 
    float vignette = smoothstep(outerRadius, innerRadius, len); 
    color.rgb = mix(color.rgb, color.rgb * vignette, intensity); 

    gl_FragColor = color; 
} 

回答

1

在您发布的着色器,它看起来像vignette值基本上是各种混合在图像的暗度值,所以在与mix功能的线,它只是由纹理颜色相乘。

所以要修改它以使用任意颜色,您需要将其更改为不透明度值(反转)。现在它是不透明的,你可以乘以强度来简化下一次计算。最后,你可以融合你选择的小插曲颜色。

所以首先在main函数之前声明你想要的颜色。

const vec3 vignetteColor = vec3(1.0, 0.0, 0.0); //red 
//this could be a uniform if you want to dynamically change it. 

然后你的第二个到最后两行更改到以下。

float vignetteOpacity = smoothstep(innerRadius, outerRadius, len) * intensity; //note inner and outer swapped to switch darkness to opacity 
color.rgb = mix(color.rgb, vignetteColor, vignetteOpacity); 

顺便说一句,“intensity = .6f”将导致着色器不编译移动,所以删除f。 (除非你的目标是OpenGL ES 3.0,但是libgdx还没有完全支持。)

+0

谢谢你,它像一个魅力一样工作! – gogonapel