打开 GLES 法线贴图着色器光照不正确
Open GLES Normal Map Shader Lighting Incorrect
我试图创建一个 Open GL ES 法线贴图着色器。我使用的网格在 Blender 中工作正常,但我的着色器没有。我也尝试在另一个示例程序中使用网格。似乎它只对某些面孔有效。
这是着色器问题还是 mesh/tangent 问题?
副总裁
uniform mat4 worldViewProjMatrix;
uniform mat4 worldViewMatrix;
attribute vec4 vertex;
attribute vec3 normal;
attribute vec3 tangent;
attribute vec3 binormal;
attribute vec4 uv0;
varying vec3 v_Position;
varying vec3 v_Normal;
varying vec3 v_Tangent;
varying vec3 v_Binormal;
varying vec2 v_UV;
void main()
{
v_Position = (vec4(vertex.xyz, 1.0) * worldViewMatrix).xyz;
v_Normal = ((worldViewMatrix * vec4(normalize(normal), 0.0)).xyz);
v_Tangent = ((worldViewMatrix * vec4(normalize(tangent), 0.0)).xyz);
v_Binormal = ((worldViewMatrix * vec4(normalize(binormal), 0.0)).xyz);//normalize(cross(v_Normal, v_Tangent));
v_UV = uv0.st;
gl_Position = worldViewProjMatrix * vec4(vertex.xyz, 1.0);
}
FP
uniform vec4 lightPositionViewSpace;
uniform vec4 surfaceDiffuseColour;
uniform vec4 surfaceAmbientColour;
uniform sampler2D tex0;
varying vec3 v_Position;
varying vec3 v_Normal;
varying vec3 v_Tangent;
varying vec3 v_Binormal;
varying vec2 v_UV;
void main()
{
mat3 TBN = transpose(mat3(
v_Tangent,
v_Binormal,
v_Normal
));
vec3 lightVector = normalize(lightPositionViewSpace.xyz);//normalize(lightPositionViewSpace.xyz - v_Position);
lightVector = normalize(lightVector*TBN);
vec3 normal = normalize(texture2D(tex0, v_UV).rgb*2.0 - 1.0);
float diffuse = max(dot(normal, lightVector), 0.0);
gl_FragColor = surfaceDiffuseColour*diffuse+surfaceAmbientColour;
}
我不知道上面的代码有什么问题。
但是我写了一个新的,这似乎有效。
也许这会对想要简单的 Open GLES 法线贴图着色器的人有所帮助:
副总裁
uniform mat4 worldViewProjMatrix;
uniform vec4 lightDirObjSpace;
attribute vec4 vertex;
attribute vec3 normal;
attribute vec3 tangent;
attribute vec3 binormal;
attribute vec4 uv0;
varying vec2 vTexCoord;
varying vec3 vLightVector;
void main( void )
{
vTexCoord = uv0.st;
vLightVector.x = dot( lightDirObjSpace.xyz, tangent );
vLightVector.y = dot( lightDirObjSpace.xyz, binormal );
vLightVector.z = dot( lightDirObjSpace.xyz, normal );
gl_Position = worldViewProjMatrix * vec4(vertex.xyz, 1.0);
}
FP
uniform vec3 surfaceDiffuseColour;
uniform vec3 surfaceAmbientColour;
uniform sampler2D tex0;
varying vec2 vTexCoord;
varying vec3 vLightVector;
void main(void)
{
vec3 normal = normalize(((texture2D( tex0, vTexCoord ).xyz) * 2.0) - 1.0);
float diffuse = max(0.0, dot(normalize(normal), normalize(vLightVector)));
gl_FragColor = vec4(surfaceDiffuseColour * diffuse + surfaceAmbientColour, 1.0);
}
我试图创建一个 Open GL ES 法线贴图着色器。我使用的网格在 Blender 中工作正常,但我的着色器没有。我也尝试在另一个示例程序中使用网格。似乎它只对某些面孔有效。 这是着色器问题还是 mesh/tangent 问题?
副总裁
uniform mat4 worldViewProjMatrix;
uniform mat4 worldViewMatrix;
attribute vec4 vertex;
attribute vec3 normal;
attribute vec3 tangent;
attribute vec3 binormal;
attribute vec4 uv0;
varying vec3 v_Position;
varying vec3 v_Normal;
varying vec3 v_Tangent;
varying vec3 v_Binormal;
varying vec2 v_UV;
void main()
{
v_Position = (vec4(vertex.xyz, 1.0) * worldViewMatrix).xyz;
v_Normal = ((worldViewMatrix * vec4(normalize(normal), 0.0)).xyz);
v_Tangent = ((worldViewMatrix * vec4(normalize(tangent), 0.0)).xyz);
v_Binormal = ((worldViewMatrix * vec4(normalize(binormal), 0.0)).xyz);//normalize(cross(v_Normal, v_Tangent));
v_UV = uv0.st;
gl_Position = worldViewProjMatrix * vec4(vertex.xyz, 1.0);
}
FP
uniform vec4 lightPositionViewSpace;
uniform vec4 surfaceDiffuseColour;
uniform vec4 surfaceAmbientColour;
uniform sampler2D tex0;
varying vec3 v_Position;
varying vec3 v_Normal;
varying vec3 v_Tangent;
varying vec3 v_Binormal;
varying vec2 v_UV;
void main()
{
mat3 TBN = transpose(mat3(
v_Tangent,
v_Binormal,
v_Normal
));
vec3 lightVector = normalize(lightPositionViewSpace.xyz);//normalize(lightPositionViewSpace.xyz - v_Position);
lightVector = normalize(lightVector*TBN);
vec3 normal = normalize(texture2D(tex0, v_UV).rgb*2.0 - 1.0);
float diffuse = max(dot(normal, lightVector), 0.0);
gl_FragColor = surfaceDiffuseColour*diffuse+surfaceAmbientColour;
}
我不知道上面的代码有什么问题。 但是我写了一个新的,这似乎有效。 也许这会对想要简单的 Open GLES 法线贴图着色器的人有所帮助:
副总裁
uniform mat4 worldViewProjMatrix;
uniform vec4 lightDirObjSpace;
attribute vec4 vertex;
attribute vec3 normal;
attribute vec3 tangent;
attribute vec3 binormal;
attribute vec4 uv0;
varying vec2 vTexCoord;
varying vec3 vLightVector;
void main( void )
{
vTexCoord = uv0.st;
vLightVector.x = dot( lightDirObjSpace.xyz, tangent );
vLightVector.y = dot( lightDirObjSpace.xyz, binormal );
vLightVector.z = dot( lightDirObjSpace.xyz, normal );
gl_Position = worldViewProjMatrix * vec4(vertex.xyz, 1.0);
}
FP
uniform vec3 surfaceDiffuseColour;
uniform vec3 surfaceAmbientColour;
uniform sampler2D tex0;
varying vec2 vTexCoord;
varying vec3 vLightVector;
void main(void)
{
vec3 normal = normalize(((texture2D( tex0, vTexCoord ).xyz) * 2.0) - 1.0);
float diffuse = max(0.0, dot(normalize(normal), normalize(vLightVector)));
gl_FragColor = vec4(surfaceDiffuseColour * diffuse + surfaceAmbientColour, 1.0);
}