简体   繁体   English

Cocos2dx OpenGl Shader二贴图问题

[英]Cocos2dx OpenGl Shader two textures problem

My fragment shader for a mobile game in cocos2dx works on iOS, but does not work as expected on Android and I'm out of ideas.我的 cocos2dx 手机游戏片段着色器可在 iOS 上运行,但在 Android 上无法正常运行,我没有想法。

The idea: u_texture is the input texture of a sprite whose appearance I want to modify.想法:u_texture 是我要修改其外观的精灵的输入纹理。 u_texture1 is a "mask" texture that has some transparency and depending on its alpha channel, I want to render the sprite's appearance with some changes or without. u_texture1 是一个具有一定透明度的“蒙版”纹理,根据其 alpha 通道,我想渲染精灵的外观有一些变化或没有变化。

The very minimal example of the problem I'm experiencing:我遇到的问题的最小示例:

  1. This is working "as expected", namely, colours the sprite red where the mask is transparent and yellow where the mask is opaque.这是“按预期方式”工作的,即在遮罩透明的地方将精灵着色为红色,在遮罩不透明的地方将其着色为黄色。
    uniform sampler2D u_texture;
    uniform sampler2D u_texture1;
    
    varying vec2 v_texCoord;
    
    varying vec2 v_resolution;
    
    void main()
    {
        vec4 maskCol=texture2D(u_texture1, v_texCoord);
        vec4 currentCol = texture2D(u_texture,v_texCoord);
        vec4 newCol = currentCol;
    
        if(maskCol.a == 0.0){
            newCol.r = 1.0;
            newCol.g = 0.0;
            newCol.b = 0.0;
            newCol.a = 1.0;
        } else {
            newCol.r = 1.0;
            newCol.g = 1.0;
            newCol.b = 0.0;
            newCol.a = 1.0;
        }
        gl_FragColor = newCol;
    }
  1. This code I would expect to colour the sprite red where the mask is transparent and leave the rest alone.我希望这段代码将蒙版透明的精灵着色为红色,并单独保留 rest。 However, it does not do that.但是,它不会那样做。 Instead, it colours the sprite red where the INPUT TEXTURE is transparent, and leaves the rest alone.相反,它将 INPUT TEXTURE 透明的精灵着色为红色,并单独保留 rest。
    uniform sampler2D u_texture;
    uniform sampler2D u_texture1;

    varying vec2 v_texCoord;

    varying vec2 v_resolution;

    void main()
    {
      vec4 maskCol=texture2D(u_texture1, v_texCoord);
      vec4 currentCol = texture2D(u_texture,v_texCoord);
      vec4 newCol = currentCol;

      if(maskCol.a == 0.0){
          newCol.r = 1.0;
          newCol.g = 0.0;
          newCol.b = 0.0;
          newCol.a = 1.0;
      } 
     gl_FragColor = newCol;
 }

Can anyone see anything wrong with this code?任何人都可以看到这段代码有什么问题吗? Apart from equality comparing the alpha value - which is not the problem here, I have checked, but I'm leaving it for simplicity.除了比较 alpha 值的相等 - 这不是这里的问题,我已经检查过,但为了简单起见,我将其保留。

Does v_resolution do anything? v_resolution有什么作用吗?

glsl is fine. glsl 很好。

Two ways to check:两种检查方式:

1.Make sure that u_texture1 is a mask texture. 1.确保 u_texture1 是遮罩纹理。 Is the texture passing reversed?纹理传递是否颠倒?

2.The first glsl is correct because each newcol has a color. 2.第一个glsl是正确的,因为每个newcol都有一个颜色。 But the second glsl is not, so you can try to output newcol directly and see if the result is expected?但是第二个glsl没有,所以你可以直接尝试output newcol,看看结果是否符合预期?

2022-12-29 2022-12-29

I copied this glsl.我复制了这个glsl。

right is:右边是:

unsigned int texture1, texture2;
glGenTextures(1, &texture1);
glBindTexture(GL_TEXTURE_2D, texture1); 
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

glGenTextures(1, &texture2);
glBindTexture(GL_TEXTURE_2D, texture2);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

shader is:着色器是:

  vec4 maskCol=texture2D(texture2, TexCoord);
  vec4 currentCol = texture2D(texture1,TexCoord);
  vec4 newCol = currentCol;

  if(maskCol.a == 0.0){
      newCol.r = 1.0;
      newCol.g = 0.0;
      newCol.b = 0.0;
      newCol.a = 1.0;
  } 
 FragColor = newCol;

texture1 is:纹理 1 是:

在此处输入图像描述

texture2 is纹理 2 是

在此处输入图像描述

在此处输入图像描述

error is:错误是:

glGenTextures(1, &texture1);
glBindTexture(GL_TEXTURE_2D, texture1);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); 
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

maybe try to change texture glTexParameteri or other Settings for textures!也许尝试更改纹理 glTexParameteri 或其他纹理设置!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM