0
我試着創建Open GL ES法線貼圖着色器。我使用的網格在Blender中工作正常,但我的着色器沒有。我也嘗試在另一個示例程序中使用網格。它似乎只適用於某些人臉。 這是着色器問題還是網格/切線問題?打開GLES法線貼圖着色器照明不正確
VP
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;
}
這裏看看:法線貼圖了可怕的錯誤(HTTP:/ /stackoverflow.com/a/28541305/2521214)提供一些提示 – Spektre
我的投注是您的光矢量未正確設置/計算/轉換,並且最終顏色組合也是可疑的。我通常通過TBN而不是光矢量來轉換法向量......如果沒有關於您的制服和座標系的更多信息很難回答...... – Spektre