Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 22 additions & 9 deletions code/renderervk/shaders/glsl/gen_frag.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -104,19 +104,16 @@ layout (constant_id = 10) const int acff = 0; // NONE, RGB, RGBA, ALPHA
#ifdef USE_VK_PBR
#define PI 3.1415926535897932384626433832795

struct NTB {
vec3 normal, tangent, binormal;
};

layout(location = 9) in vec4 var_Normal;
layout(location = 10) in vec4 var_LightDir;
layout(location = 11) in vec4 var_ViewDir;
layout(location = 12) in NTB in_ntb;
layout(location = 12) in vec4 var_Tangent;

layout(set = 5, binding = 0) uniform sampler2D brdflut_texture;
layout(set = 6, binding = 0) uniform sampler2D normal_texture;
layout(set = 7, binding = 0) uniform sampler2D physical_texture;
layout(set = 8, binding = 0) uniform samplerCube env_texture;
layout(set = 9, binding = 0) uniform sampler2D deluxe_texture;

// prob use ubo for specularScale & normalScale ..
layout (constant_id = 11) const float specularScale_x = 1.0;
Expand All @@ -134,14 +131,18 @@ layout (constant_id = 20) const int physical_texture_set = 0;
layout (constant_id = 21) const int env_texture_set = 0;
layout (constant_id = 22) const int lightmap_texture_set = 0;

vec3 CalcNormal( in vec3 vertexNormal, in vec2 frag_tex_coord )
layout (constant_id = 23) const int deluxe_mapping = -1; // -1: deluxemapping off, 0: on, 1: on + bound
layout (constant_id = 24) const float deluxe_specular_scale = 1.0;

vec3 CalcNormal( in vec3 vertexNormal, in vec4 vertexTangent, in vec2 frag_tex_coord )
{
if ( normal_texture_set > -1 ) {
vec3 biTangent = vertexTangent.w * cross(vertexNormal, vertexTangent.xyz);
vec3 n = texture(normal_texture, frag_tex_coord).agb - vec3(0.5);

n.xy *= vec2( normalScale_x, normalScale_y );
n.z = sqrt(clamp((0.25 - n.x * n.x) - n.y * n.y, 0.0, 1.0));
n = n.x * in_ntb.tangent + n.y * in_ntb.binormal + n.z * vertexNormal;
n = n.x * vertexTangent.xyz + n.y * biTangent + n.z * vertexNormal;

return normalize(n);
}else {
Expand Down Expand Up @@ -661,6 +662,9 @@ void main() {

lightColor = lightmapColor * bias_color;
diffuse = color0;

if ( deluxe_mapping > 0 )
L = ( texture(deluxe_texture, frag_tex_coord1).rgb - vec3(0.5) );
}
else {
lightColor = bias_color;
Expand All @@ -675,7 +679,7 @@ void main() {
attenuation = 1.0;
#endif

N = CalcNormal( var_Normal.xyz, frag_tex_coord0 );
N = CalcNormal( var_Normal.xyz, var_Tangent, frag_tex_coord0 );

#if defined(USE_TX1) || defined(USE_TX2)
if ( lightmap_texture_set > -1 ) {
Expand Down Expand Up @@ -723,7 +727,16 @@ void main() {

float NH = clamp( dot( N, H ), 0.0, 1.0 );
float VH = clamp( dot( E, H ), 0.0, 1.0 );
Fs = CalcSpecular( specular.rgb, NH, NL, NE, LH, VH, roughness );


#if ( defined(USE_TX1) || defined(USE_TX2) )
if ( deluxe_specular_scale != 0.0 && deluxe_mapping > -1 )
{
Fs = CalcSpecular( specular.rgb, NH, NL, NE, LH, VH, roughness ) * deluxe_specular_scale;
}
#endif



vec3 reflectance = Fd + Fs;

Expand Down
27 changes: 6 additions & 21 deletions code/renderervk/shaders/glsl/gen_vert.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ layout(location = 3) in vec2 in_tex_coord1;
layout(location = 4) in vec2 in_tex_coord2;
#endif

#ifdef USE_ENV
#if defined(USE_ENV) || defined(USE_VK_PBR)
layout(location = 5) in vec3 in_normal;
#endif

Expand Down Expand Up @@ -91,29 +91,15 @@ layout(set = 0, binding = 1) uniform UBO_Camera {
mat4 u_ModelMatrix;
};

struct NTB {
vec3 normal, tangent, binormal;
};

layout(location = 8) in vec4 in_qtangent;
layout(location = 8) in vec4 in_tangent;
layout(location = 9) in vec4 in_lightdir;

layout(location = 9) out vec4 var_Normal;
layout(location = 10) out vec4 var_LightDir;
layout(location = 11) out vec4 var_ViewDir;
layout(location = 12) out NTB out_ntb;
layout(location = 12) out vec4 var_Tangent;

vec3 QuatTransVec( in vec4 quat, in vec3 vec ) {
vec3 tmp = 2.0 * cross( quat.xyz, vec );
return vec + quat.w * tmp + cross( quat.xyz, tmp );
}

void QTangentToNTB( in vec4 qtangent, out NTB ntb ) {
ntb.normal = QuatTransVec( qtangent, vec3( 0.0, 0.0, 1.0 ) );
ntb.tangent = QuatTransVec( qtangent, vec3( 1.0, 0.0, 0.0 ) );
ntb.tangent *= sign( qtangent.w );
ntb.binormal = QuatTransVec( qtangent, vec3( 0.0, 1.0, 0.0 ) );
}
#endif

out gl_PerVertex {
Expand All @@ -124,13 +110,11 @@ void main() {
gl_Position = mvp * vec4(in_position, 1.0);

#ifdef USE_VK_PBR
QTangentToNTB( in_qtangent, out_ntb );
vec3 tangent = in_tangent.xyz;
#endif

vec3 position = in_position;
#if defined(USE_VK_PBR)
vec3 normal = out_ntb.normal;
#elif defined(USE_ENV)
#if defined(USE_ENV) || defined(USE_VK_PBR)
vec3 normal = in_normal;
#endif

Expand Down Expand Up @@ -198,5 +182,6 @@ void main() {
var_LightDir = vec4(L, 0.0);
var_Normal = vec4(normal, 0.0);
var_ViewDir = vec4(viewDir, 0.0);
var_Tangent = vec4(tangent, in_tangent.w);
#endif
}
Loading