GLSL 2.0 Shader在不同的设备中给出不同的光
问题描述:
我正在尝试为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)
设备2:(三星S7) 2
谁能帮助?
答
问题可能出现在您使用的纹理格式/类型中。所有设备不支持所有纹理格式。 例如:如果您的输出颜色可能具有负值并且设备的纹理格式不支持它们,则它将被钳位到0并可能产生不同的结果。 更好地检查两个设备的使用能力
GLES20.glGetString(GLES20.GL_EXTENSIONS));
'highp float'不适用于所有设备,它可能是差异的来源。 –
我用中性代替highp,它改变了光的强度,但效果仍然一样 – sabby