2016-06-11 65 views
1

我正在嘗試爲Android中的opengles 2.0視圖編寫着色器。 我的着色器:GLSL 2.0 Shader在不同的設備中給出不同的光

頂點着色器:

uniform mat4 u_MVPMatrix;  // A constant representing the combined model/view/projection matrix. 
uniform mat4 u_MVMatrix;  // A constant representing the combined model/view matrix.    

attribute vec4 a_Position;  // Per-vertex position information we will pass in.        
attribute vec3 a_Normal;  // Per-vertex normal information we will pass in.  
attribute vec2 a_TexCoordinate; // Per-vertex texture coordinate information we will pass in.  

varying vec3 v_Position;  // This will be passed into the fragment shader.        
varying vec3 v_Normal;   // This will be passed into the fragment shader. 
varying vec2 v_TexCoordinate; // This will be passed into the fragment shader.    

// The entry point for our vertex shader. 
void main()              
{               
    // Transform the vertex into eye space.  
    v_Position = vec3(u_MVMatrix * a_Position); 

    // Pass through the texture coordinate. 
    v_TexCoordinate = a_TexCoordinate;          

    // Transform the normal's orientation into eye space. 
    v_Normal = normalize(vec3(u_MVMatrix * vec4(a_Normal, 0.0))); 

    // gl_Position is a special variable used to store the final position. 
    // Multiply the vertex by the matrix to get the final point in normalized screen coordinates. 
    gl_Position = (u_MVPMatrix * a_Position); 
} 

片段着色器:

precision highp float;   // Set the default precision to medium. We don't need as high of a 
           // precision in the fragment shader. 
uniform vec3 u_LightPos1;   // The position of the light in eye space. 
uniform vec3 u_LightDir1;   // The position of the light in eye space. 
float l_spotCutOff=45.0; 
uniform sampler2D u_Texture; // The input texture. 

varying vec3 v_Position;  // Interpolated position for this fragment. 
varying vec3 v_Normal;   // Interpolated normal for this fragment. 
varying vec2 v_TexCoordinate; // Interpolated texture coordinate per fragment. 
float cutoff = 0.1; 
// The entry point for our fragment shader. 
void main()       
{        

    // Get a lighting direction vector from the light to the vertex. 
    vec3 lightVector1 = normalize(u_LightPos1 - v_Position); 

     // Will be used for attenuation. 
     float distance1 = length(u_LightPos1 - v_Position); 

    float diffuse=0.0; 

     // Calculate the dot product of the light vector and vertex normal. If the normal and light vector are 
     // pointing in the same direction then it will get max illumination. 
    float diffuse1 = max(dot(v_Normal, lightVector1), 0.1); 
     // Add attenuation. 
    diffuse1 = diffuse1 * (1.0/(1.0+(0.25*distance1))); 

    // Add ambient lighting 
    diffuse = diffuse1+0.2; 
    // Multiply the color by the diffuse illumination level and texture value to get final output color. 
    vec4 color = (texture2D(u_Texture, v_TexCoordinate)); 
    color.rgb *= (diffuse); 
    if(color.a < cutoff) 
     discard; 

    gl_FragColor = color; 


    }                   

現在着色器完美地工作,但它在不同的設備表現不同:

設備1: (moto x play)

1

設備2:(三星S7) 2

誰能幫助?

+0

'highp float'不適用於所有設備,它可能是差異的來源。 –

+0

我用中性代替highp,它改變了光的強度,但效果仍然一樣 – sabby

回答

0

問題可能出現在您使用的紋理格式/類型中。所有設備不支持所有紋理格式。 例如:如果您的輸出顏色可能具有負值並且設備的紋理格式不支持它們,則它將被鉗位到0並可能產生不同的結果。 更好地檢查兩個設備的使用能力

GLES20.glGetString(GLES20.GL_EXTENSIONS)); 
相關問題