CesiumGS / cesium

An open-source JavaScript library for world-class 3D globes and maps :earth_americas:
https://cesium.com/cesiumjs/
Apache License 2.0
12.73k stars 3.45k forks source link

a strange problem when using KHR_techniques_webgl extension #8164

Closed hanaimin closed 4 years ago

hanaimin commented 5 years ago

Can not pick the feature when use the gltf2.0 KHR_techniques_webgl extension.

the FS like that.

precision highp float;
varying vec3 v_normal;
varying float v_batchId;
varying vec4 v_color;
uniform vec4 u_diffuse;
uniform vec4 u_ambient;
uniform bool u_hasDiffuse;
uniform bool u_hasColor;
uniform bool u_hasNormal;
uniform bool u_isLightOn;
uniform vec4 u_specular;
uniform float u_shininess;
uniform bool u_twoSideLightOn;
void main(void)
{
    vec4 ambient = vec4(0.2,0.2,0.2,1.0);
    vec4 diffuse = vec4(1., 1., 1., 1.);
    if(u_hasColor)
    {
        diffuse = diffuse * v_color;
    }
    if(u_hasDiffuse)
    {
        diffuse = diffuse * u_diffuse;
    }
     ambient *= u_ambient;
    if(u_hasNormal && u_isLightOn)
    {
        vec3 normal = (v_normal);
        if(u_twoSideLightOn)
        {
            if(gl_FrontFacing) 
                diffuse.rgb *= max(dot(normal,vec3(0.,0.,1.)), 0.0) * 0.5 + 0.5; 
            else 
                diffuse.rgb *= max(0.0, dot(normal,vec3(0.,0.,-1.))) * 0.5 + 0.5; 
        }
        else
        {
            diffuse.rgb *= max(dot(normal,vec3(0.,0.,1.)), 0.) * 0.5 + 0.5; 
        }
    }
   gl_FragColor = diffuse + vec4(ambient.rgb,0.0);
}

but if I modify the FS code like following, pick-feature is OK. note the last line code modified crazily.

precision highp float;
varying vec3 v_normal;
varying float v_batchId;
varying vec4 v_color;
uniform vec4 u_diffuse;
uniform vec4 u_ambient;
uniform bool u_hasDiffuse;
uniform bool u_hasColor;
uniform bool u_hasNormal;
uniform bool u_isLightOn;
uniform vec4 u_specular;
uniform float u_shininess;
uniform bool u_twoSideLightOn;
void main(void)
{
    vec4 ambient = vec4(0.2,0.2,0.2,1.0);
    vec4 diffuse = vec4(1., 1., 1., 1.);
    if(u_hasColor)
    {
        diffuse = diffuse * v_color;
    }
    if(u_hasDiffuse)
    {
        diffuse = diffuse * u_diffuse;
    }
     ambient *= u_ambient;
    if(u_hasNormal && u_isLightOn)
    {
        vec3 normal = (v_normal);
        if(u_twoSideLightOn)
        {
            if(gl_FrontFacing) 
                diffuse.rgb *= max(dot(normal,vec3(0.,0.,1.)), 0.0) * 0.5 + 0.5; 
            else 
                diffuse.rgb *= max(0.0, dot(normal,vec3(0.,0.,-1.))) * 0.5 + 0.5; 
        }
        else
        {
            diffuse.rgb *= max(dot(normal,vec3(0.,0.,1.)), 0.) * 0.5 + 0.5; 
        }
    }
   **gl_FragColor = diffuse + vec4(ambient.rgb,v_batchId);**
}

So strange.

OmarShehata commented 5 years ago

@hanaimin can you provide a sample glTF file that shows this issue?

hanaimin commented 5 years ago

@hanaimin can you provide a sample glTF file that shows this issue? thanks, the sample glTF(glb) file : 0.zip

hanaimin commented 5 years ago

and the tileset here: tileset.zip

hanaimin commented 5 years ago

and linked shaders: Shader.zip crazily modified

hanaimin commented 5 years ago

@OmarShehata hi, is there any solution for the issue?

ryanliang000 commented 4 years ago

in the glb file, the diffuse value alpha channel is 0.0, default it should be 1.0.
"u_diffuse": [ 0.866667, 0.721569, 0.133333, 0.0 ], or you can just change the fs shader to ignore diffuse.alpha if(u_hasDiffuse) { diffuse.rgb = diffuse.rgb * u_diffuse.rgb; }

OmarShehata commented 4 years ago

Thanks for the tip @rainliang000. @hanaimin can you confirm this by adding:

gl_FragColor = diffuse + vec4(ambient.rgb, 1.0);

As the last line of your shader?

hanaimin commented 4 years ago

@OmarShehata @rainliang000 Thank you very much. both solutions are OK.