ios - 创建一个高效的纹理重新着色片段着色器

标签 ios cocos2d-iphone opengl-es-2.0 glsl

我正在尝试创建一个片段着色器来为 2D 灰度 Sprite 重新着色,但完整保留白色和近白色片段(即:不要为纯白色片段重新着色,而只为近白色片段稍微重新着色)。我不确定如何在不使用条件分支的情况下执行此操作,这会导致某些硬件性能不佳。

游戏引擎中现有的着色器只是执行一个简单的乘法:

#ifdef GL_ES                                
precision lowp float;                       
#endif                                      

varying vec4 v_fragmentColor;               
varying vec2 v_texCoord;                    
uniform sampler2D CC_Texture0;              

void main()                                 
{                                           
    vec4 texColor = texture2D(CC_Texture0, v_texCoord);     
    gl_FragColor = texColor * v_fragmentColor; 
}

我认为为了避免条件,我需要某种连续的数学函数来重新着色 RGB 值大于的片段,比如说,小于 (0.9, 0.9, 0.9) 的片段小于小于(0.9、0.9、0.9)。

任何帮助都会很棒!

最佳答案

我会这样做:计算完全重新着色的像素,然后根据函数与原始像素混合。这是一个想法:

vec4 texColor = texture2D(CC_Texture0, v_texCoord);

const vec4 kLumWeights = vec4(.2126, .7152, .0722, 0.0); // Rec. 709 luminance weights
float luminance = dot (texColor, kLumWeights);

vec4 recolored = texColor * v_fragmentColor;

const float kThreshold = 0.8;
float mixAmount = (luminance - kThreshold) / (1.0 - kThreshold); // Everything below kThreshold becomes 0, and from kThreshold to 1.0 becomes 0 to 1.0
mixAmount = clamp (mixAmount, 0.0, 1.0);
gl_FragColor = mix (recolored, texColor, mixAmount);

如果可行,请告诉我。

关于ios - 创建一个高效的纹理重新着色片段着色器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20926816/

相关文章:

java - libgdx 着色器 - 尝试设置颜色

opengl-es - 仅更新 OpenGL ES 2.0 中纹理的水平子区域

ios - UIView 的动画退出

ios - 从 cocos2d 场景到 Uiviewcontroller 的页面导航

ios - 是否可以在 iTunes connect 上创建最小的测试应用程序条目?

ios - 如何使用 CoreGraphics iOS Cocos2d 制作 Sprite

android - Cocos2dx替换场景报错

c++ - 使用 VBO 和 VAO 是否会提高具有共享内存的集成芯片的性能

ios - 如何更改 map 框注释 View ?

ios - UIWebView 和 Swift : Detect when a video starts playing