OpenGL ES 2.0 / 3.0中的虹彩效果。替换黑色区域

问题描述 投票:0回答:1

我正在尝试将虹彩效果应用于对象。为此,我使用了以下着色器:

顶点着色器:

#version 300 es 
uniform mat4 u_mvpMatrix; 
uniform mat4 u_mvMatrix; 
uniform mat4 u_vMatrix;

in vec4 a_position; 
in vec2 a_textureCoordinates; 
in vec3 a_normal; 

out vec2 v_textureCoordinates; 
out float v_CosViewAngle;
out float v_LightIntensity;

void main() {
    // transform normal orientation into eye space
    vec3 modelViewNormal = mat3(u_mvMatrix) * a_normal;
    vec3 modelViewVertex = vec3(u_mvMatrix * a_position);
    mediump vec3 eyeDirection = normalize(-modelViewVertex);

    vec3 lightVector = normalize(lightPosition - modelViewVertex);
    lightVector = mat3(u_vMatrix) * lightVector;

    v_LightIntensity = max(dot(lightVector, modelViewNormal), 0.0);
    v_CosViewAngle = max(dot(eyeDirection, modelViewNormal), 0.1);
    ...
}

片段着色器:

#version 300 es
precision lowp float;

in vec2 v_textureCoordinates; 
in float v_CosViewAngle;
in float v_LightIntensity;
out vec4 outColor;
uniform sampler2D s_texture;

// wave numbers for the iridescence effect: k = 2.0 * pi / wavelength (nm).
const mediump float PI = 3.141592654;
const mediump vec3 rgbK = 2.0 * PI * vec3(1.0/475.0, 1.0/510.0, 1.0/650.0);
const mediump float iridescence = 4.4;
const mediump float minThickness = 80.0;
const mediump float maxVariation = 50.0;

void main() {
    float thickness = texture(s_texture, v_textureCoordinates).r 
        * maxVariation + minThickness; // or texture2D() - in OpenGL ES 2.0
    float delta = (thickness / v_LightIntensity) + (thickness / v_CosViewAngle);
    lowp vec3 color = cos(delta * rgbK) * iridescence * v_LightIntensity;
    vec4 resultColor = vec4(color, 1.0);
    outColor = resultColor;
}

结果:

enter image description here

问题:如何用普通纹理替换黑色区域?换句话说,要具有通用的纹理而不是黑色。

注意:我的身份是:

vec4 resultColor = texture(s_texture, v_textureCoordinates) * (vec4(color, 1.0) + 0.5);

等等:

if (color == vec3(0.0, 0.0, 0.0)) { // if black color
   resultColor = texture(s_texture, v_textureCoordinates);
} else {
   resultColor = texture(s_texture, v_textureCoordinates) * (vec4(color, 1.0) + 0.5);
}

但是这不能解决问题。

提前感谢!

着色器的代码部分取自PVRShamanGUI。

android opengl-es shader opengl-es-2.0 opengl-es-3.0
1个回答
0
投票

颜色不是完全黑色,颜色通道的值不完全是0.0。因此,您必须将颜色成分的平均值(灰度)与某个阈值进行比较。mix颜色

mix

另一种可能性是根据vec4 texturColor = texture(s_texture, v_textureCoordinates); const float threshold = 0.05; float gray_scale = (color.r + color.g + color.b) / 3.0; if (gray_scale < threshold) { resultColor = texturColor } else { resultColor = mix(texturColor, color, gray_scale); } 的值来混合颜色:

delta
© www.soinside.com 2019 - 2024. All rights reserved.