mirror of https://github.com/bkaradzic/bgfx
fix bump example tangent lighting wrong
This commit is contained in:
parent
9296a44f5e
commit
c4aa82e2fa
|
@ -1,4 +1,4 @@
|
|||
$input v_wpos, v_view, v_normal, v_tangent, v_bitangent, v_texcoord0 // in...
|
||||
$input v_wpos, v_view, v_normal, v_tangent, v_bitangent, v_texcoord0// in...
|
||||
|
||||
/*
|
||||
* Copyright 2011-2018 Branimir Karadzic. All rights reserved.
|
||||
|
@ -15,7 +15,8 @@ uniform vec4 u_lightRgbInnerR[4];
|
|||
vec2 blinn(vec3 _lightDir, vec3 _normal, vec3 _viewDir)
|
||||
{
|
||||
float ndotl = dot(_normal, _lightDir);
|
||||
vec3 reflected = _lightDir - 2.0*ndotl*_normal; // reflect(_lightDir, _normal);
|
||||
//vec3 reflected = _lightDir - 2.0*ndotl*_normal; // reflect(_lightDir, _normal);
|
||||
vec3 reflected = 2.0*ndotl*_normal - _lightDir;
|
||||
float rdotv = dot(reflected, _viewDir);
|
||||
return vec2(ndotl, rdotv);
|
||||
}
|
||||
|
@ -45,25 +46,31 @@ vec3 calcLight(int _idx, mat3 _tbn, vec3 _wpos, vec3 _normal, vec3 _view)
|
|||
{
|
||||
vec3 lp = u_lightPosRadius[_idx].xyz - _wpos;
|
||||
float attn = 1.0 - smoothstep(u_lightRgbInnerR[_idx].w, 1.0, length(lp) / u_lightPosRadius[_idx].w);
|
||||
vec3 lightDir = mul(_tbn, normalize(lp) );
|
||||
vec3 lightDir = mul( normalize(lp), _tbn );
|
||||
vec2 bln = blinn(lightDir, _normal, _view);
|
||||
vec4 lc = lit(bln.x, bln.y, 1.0);
|
||||
vec3 rgb = u_lightRgbInnerR[_idx].xyz * saturate(lc.y) * attn;
|
||||
return rgb;
|
||||
}
|
||||
|
||||
mat3 mtx3FromCols(vec3 c0, vec3 c1, vec3 c2)
|
||||
{
|
||||
#ifdef BGFX_SHADER_LANGUAGE_GLSL
|
||||
return mat3(c0, c1, c2);
|
||||
#else
|
||||
return transpose(mat3(c0, c1, c2));
|
||||
#endif
|
||||
}
|
||||
|
||||
|
||||
void main()
|
||||
{
|
||||
mat3 tbn = mat3(
|
||||
normalize(v_tangent),
|
||||
normalize(v_bitangent),
|
||||
normalize(v_normal)
|
||||
);
|
||||
mat3 tbn = mtx3FromCols(v_tangent, v_bitangent, v_normal);
|
||||
|
||||
vec3 normal;
|
||||
normal.xy = texture2D(s_texNormal, v_texcoord0).xy * 2.0 - 1.0;
|
||||
normal.z = sqrt(1.0 - dot(normal.xy, normal.xy) );
|
||||
vec3 view = -normalize(v_view);
|
||||
vec3 view = normalize(v_view);
|
||||
|
||||
vec3 lightColor;
|
||||
lightColor = calcLight(0, tbn, v_wpos, normal, view);
|
||||
|
|
|
@ -8,30 +8,37 @@ $output v_wpos, v_view, v_normal, v_tangent, v_bitangent, v_texcoord0
|
|||
|
||||
#include "../common/common.sh"
|
||||
|
||||
mat3 mtx3FromCols(vec3 c0, vec3 c1, vec3 c2)
|
||||
{
|
||||
#ifdef BGFX_SHADER_LANGUAGE_GLSL
|
||||
return mat3(c0, c1, c2);
|
||||
#else
|
||||
return transpose(mat3(c0, c1, c2));
|
||||
#endif
|
||||
}
|
||||
|
||||
void main()
|
||||
{
|
||||
vec3 wpos = mul(u_model[0], vec4(a_position, 1.0) ).xyz;
|
||||
v_wpos = wpos;
|
||||
|
||||
gl_Position = mul(u_viewProj, vec4(wpos, 1.0) );
|
||||
|
||||
vec4 normal = a_normal * 2.0 - 1.0;
|
||||
vec3 wnormal = mul(u_model[0], vec4(normal.xyz, 0.0) ).xyz;
|
||||
|
||||
vec4 tangent = a_tangent * 2.0 - 1.0;
|
||||
|
||||
vec3 wnormal = mul(u_model[0], vec4(normal.xyz, 0.0) ).xyz;
|
||||
vec3 wtangent = mul(u_model[0], vec4(tangent.xyz, 0.0) ).xyz;
|
||||
|
||||
vec3 viewNormal = normalize(mul(u_view, vec4(wnormal, 0.0) ).xyz);
|
||||
vec3 viewTangent = normalize(mul(u_view, vec4(wtangent, 0.0) ).xyz);
|
||||
vec3 viewBitangent = cross(viewNormal, viewTangent) * tangent.w;
|
||||
mat3 tbn = mat3(viewTangent, viewBitangent, viewNormal);
|
||||
v_normal = normalize(wnormal);
|
||||
v_tangent = normalize(wtangent);
|
||||
v_bitangent = cross(v_normal, v_tangent) * tangent.w;
|
||||
|
||||
v_wpos = wpos;
|
||||
|
||||
vec3 view = mul(u_view, vec4(wpos, 0.0) ).xyz;
|
||||
v_view = mul(view, tbn);
|
||||
|
||||
v_normal = viewNormal;
|
||||
v_tangent = viewTangent;
|
||||
v_bitangent = viewBitangent;
|
||||
mat3 tbn = mtx3FromCols(v_tangent, v_bitangent, v_normal);
|
||||
|
||||
// eye position in world space
|
||||
vec3 weyepos = mul(vec4(0.0, 0.0, 0.0, 1.0), u_view).xyz;
|
||||
// tangent space view dir
|
||||
v_view = mul(weyepos - wpos, tbn);
|
||||
v_texcoord0 = a_texcoord0;
|
||||
}
|
||||
|
|
|
@ -25,19 +25,16 @@ void main()
|
|||
vec4 tangent = a_tangent * 2.0 - 1.0;
|
||||
vec3 wtangent = instMul(model, vec4(tangent.xyz, 0.0) ).xyz;
|
||||
|
||||
vec3 viewNormal = normalize(mul(u_view, vec4(wnormal, 0.0) ).xyz);
|
||||
vec3 viewTangent = normalize(mul(u_view, vec4(wtangent, 0.0) ).xyz);
|
||||
vec3 viewBitangent = cross(viewNormal, viewTangent) * tangent.w;
|
||||
mat3 tbn = mat3(viewTangent, viewBitangent, viewNormal);
|
||||
v_normal = wnormal;
|
||||
v_tangent = wtangent;
|
||||
v_bitangent = cross(v_normal, v_tangent) * tangent.w;
|
||||
|
||||
mat3 tbn = mat3(v_tangent, v_bitangent, v_normal);
|
||||
|
||||
v_wpos = wpos;
|
||||
|
||||
vec3 view = mul(u_view, vec4(wpos, 0.0) ).xyz;
|
||||
v_view = instMul(view, tbn);
|
||||
|
||||
v_normal = viewNormal;
|
||||
v_tangent = viewTangent;
|
||||
v_bitangent = viewBitangent;
|
||||
vec3 weyepos = mul(vec4(0.0, 0.0, 0.0, 1.0), u_view).xyz;
|
||||
v_view = instMul(weyepos - wpos, tbn);
|
||||
|
||||
v_texcoord0 = a_texcoord0;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue