From 23685cd868ff941da4f0974035c8f4aa23594e03 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Fri, 22 May 2020 19:59:34 +0800 Subject: [PATCH] add tonemapping shaders --- shader/Functions/gamma.glsl | 21 +++ shader/ToneMap/ACES.glsl | 11 ++ shader/ToneMap/HejlRichard.glsl | 7 + shader/ToneMap/Linear.glsl | 6 + shader/ToneMap/LumaBasedReinhard.glsl | 8 + shader/ToneMap/RomBinDaHouse.glsl | 6 + shader/ToneMap/SimpleReinhard.glsl | 6 + shader/ToneMap/Uncharted2.glsl | 20 +++ .../WhitePreservingLumaBasedReinhard.glsl | 9 + shader/ToneMapping.glsl | 93 ++++------ shader/brdf.glsl | 159 ++++++++++++++++++ shader/cnmr.lightmodel | 17 ++ shader/full_screen_rectangle.vert.glsl | 12 ++ shader/gbuffer intro.txt | 45 +++++ shader/primitive.vert | 129 ++++++++++++++ shader/r_composition.frag | 55 ++++++ shader/r_composition.vert | 17 ++ shader/r_gbuffer.frag | 44 +++++ 18 files changed, 606 insertions(+), 59 deletions(-) create mode 100644 shader/Functions/gamma.glsl create mode 100644 shader/ToneMap/ACES.glsl create mode 100644 shader/ToneMap/HejlRichard.glsl create mode 100644 shader/ToneMap/Linear.glsl create mode 100644 shader/ToneMap/LumaBasedReinhard.glsl create mode 100644 shader/ToneMap/RomBinDaHouse.glsl create mode 100644 shader/ToneMap/SimpleReinhard.glsl create mode 100644 shader/ToneMap/Uncharted2.glsl create mode 100644 shader/ToneMap/WhitePreservingLumaBasedReinhard.glsl create mode 100644 shader/brdf.glsl create mode 100644 shader/cnmr.lightmodel create mode 100644 shader/full_screen_rectangle.vert.glsl create mode 100644 shader/gbuffer intro.txt create mode 100644 shader/primitive.vert create mode 100644 shader/r_composition.frag create mode 100644 shader/r_composition.vert create mode 100644 shader/r_gbuffer.frag diff --git a/shader/Functions/gamma.glsl b/shader/Functions/gamma.glsl new file mode 100644 index 0000000..9292904 --- /dev/null +++ b/shader/Functions/gamma.glsl @@ -0,0 +1,21 @@ +const float GAMMA = 2.2; +const float INV_GAMMA = 1.0 / GAMMA; + +// linear to sRGB approximation +// see http://chilliant.blogspot.com/2012/08/srgb-approximations-for-hlsl.html +vec3 linearTosRGB(vec3 color) +{ + return pow(color, vec3(INV_GAMMA)); +} + +// sRGB to linear approximation +// see http://chilliant.blogspot.com/2012/08/srgb-approximations-for-hlsl.html +vec3 sRGBToLinear(vec3 srgbIn) +{ + return vec3(pow(srgbIn.xyz, vec3(GAMMA))); +} + +vec4 sRGBToLinear(vec4 srgbIn) +{ + return vec4(sRGBToLinear(srgbIn.xyz), srgbIn.w); +} diff --git a/shader/ToneMap/ACES.glsl b/shader/ToneMap/ACES.glsl new file mode 100644 index 0000000..22cb9e6 --- /dev/null +++ b/shader/ToneMap/ACES.glsl @@ -0,0 +1,11 @@ +// ACES tone map +// see: https://knarkowicz.wordpress.com/2016/01/06/aces-filmic-tone-mapping-curve/ +vec3 ToneMapping(vec3 color) +{ + const float A = 2.51; + const float B = 0.03; + const float C = 2.43; + const float D = 0.59; + const float E = 0.14; + return linearTosRGB(clamp((color * (A * color + B)) / (color * (C * color + D) + E), 0.0, 1.0)); +} diff --git a/shader/ToneMap/HejlRichard.glsl b/shader/ToneMap/HejlRichard.glsl new file mode 100644 index 0000000..6b66028 --- /dev/null +++ b/shader/ToneMap/HejlRichard.glsl @@ -0,0 +1,7 @@ +// Hejl Richard tone map +// see: http://filmicworlds.com/blog/filmic-tonemapping-operators/ +vec3 ToneMapping(vec3 color) +{ + color = max(vec3(0.0), color - vec3(0.004)); + return (color*(6.2*color+.5))/(color*(6.2*color+1.7)+0.06); +} diff --git a/shader/ToneMap/Linear.glsl b/shader/ToneMap/Linear.glsl new file mode 100644 index 0000000..317085a --- /dev/null +++ b/shader/ToneMap/Linear.glsl @@ -0,0 +1,6 @@ +vec3 ToneMapping(vec3 color) +{ + color = clamp(u_Exposure * color, 0., 1.); + + return linearTosRGB(color); +} diff --git a/shader/ToneMap/LumaBasedReinhard.glsl b/shader/ToneMap/LumaBasedReinhard.glsl new file mode 100644 index 0000000..1f7fe73 --- /dev/null +++ b/shader/ToneMap/LumaBasedReinhard.glsl @@ -0,0 +1,8 @@ +vec3 ToneMapping(vec3 color) +{ + float luma = dot(color, vec3(0.2126, 0.7152, 0.0722)); + float toneMappedLuma = luma / (1. + luma); + color *= toneMappedLuma / luma; + + return linearTosRGB(color); +} diff --git a/shader/ToneMap/RomBinDaHouse.glsl b/shader/ToneMap/RomBinDaHouse.glsl new file mode 100644 index 0000000..8c80910 --- /dev/null +++ b/shader/ToneMap/RomBinDaHouse.glsl @@ -0,0 +1,6 @@ +vec3 ToneMapping(vec3 color) +{ + color = exp( -1.0 / ( 2.72*color + 0.15 ) ); + + return linearTosRGB(color); +} diff --git a/shader/ToneMap/SimpleReinhard.glsl b/shader/ToneMap/SimpleReinhard.glsl new file mode 100644 index 0000000..a7e5e24 --- /dev/null +++ b/shader/ToneMap/SimpleReinhard.glsl @@ -0,0 +1,6 @@ +vec3 ToneMapping(vec3 color) +{ + color *= u_Exposure/(1. + color / u_Exposure); + + return linearTosRGB(color); +} diff --git a/shader/ToneMap/Uncharted2.glsl b/shader/ToneMap/Uncharted2.glsl new file mode 100644 index 0000000..d21542e --- /dev/null +++ b/shader/ToneMap/Uncharted2.glsl @@ -0,0 +1,20 @@ +// Uncharted 2 tone map +// see: http://filmicworlds.com/blog/filmic-tonemapping-operators/ +vec3 toneMapUncharted2Impl(vec3 color) +{ + const float A = 0.15; + const float B = 0.50; + const float C = 0.10; + const float D = 0.20; + const float E = 0.02; + const float F = 0.30; + return ((color*(A*color+C*B)+D*E)/(color*(A*color+B)+D*F))-E/F; +} + +vec3 ToneMapping(vec3 color) +{ + const float W = 11.2; + color = toneMapUncharted2Impl(color * 2.0); + vec3 whiteScale = 1.0 / toneMapUncharted2Impl(vec3(W)); + return linearTosRGB(color * whiteScale); +} diff --git a/shader/ToneMap/WhitePreservingLumaBasedReinhard.glsl b/shader/ToneMap/WhitePreservingLumaBasedReinhard.glsl new file mode 100644 index 0000000..0c3db17 --- /dev/null +++ b/shader/ToneMap/WhitePreservingLumaBasedReinhard.glsl @@ -0,0 +1,9 @@ +vec3 ToneMapping(vec3 color) +{ + float white = 2.; + float luma = dot(color, vec3(0.2126, 0.7152, 0.0722)); + float toneMappedLuma = luma * (1. + luma / (white*white)) / (1. + luma); + color *= toneMappedLuma / luma; + + return linearTosRGB(color); +} diff --git a/shader/ToneMapping.glsl b/shader/ToneMapping.glsl index a714b54..50a4220 100644 --- a/shader/ToneMapping.glsl +++ b/shader/ToneMapping.glsl @@ -1,4 +1,4 @@ -/* +/* This shader experiments the effect of different tone mapping operators. This is still a work in progress. @@ -17,69 +17,44 @@ Zavie */ -vec3 linearToneMapping(vec3 color) -{ - float exposure = 1.; - color = clamp(exposure * color, 0., 1.); - color = pow(color, vec3(1. / gamma)); - return color; -} +uniform float u_Exposure; -vec3 simpleReinhardToneMapping(vec3 color) -{ - float exposure = 1.5; - color *= exposure/(1. + color / exposure); - color = pow(color, vec3(1. / gamma)); - return color; -} -vec3 lumaBasedReinhardToneMapping(vec3 color) +vec3 toneMap(vec3 color) { - float luma = dot(color, vec3(0.2126, 0.7152, 0.0722)); - float toneMappedLuma = luma / (1. + luma); - color *= toneMappedLuma / luma; - color = pow(color, vec3(1. / gamma)); - return color; -} + color *= u_Exposure; -vec3 whitePreservingLumaBasedReinhardToneMapping(vec3 color) -{ - float white = 2.; - float luma = dot(color, vec3(0.2126, 0.7152, 0.0722)); - float toneMappedLuma = luma * (1. + luma / (white*white)) / (1. + luma); - color *= toneMappedLuma / luma; - color = pow(color, vec3(1. / gamma)); - return color; -} +#ifdef TONEMAP_UNCHARTED + return toneMapUncharted(color); +#endif -vec3 RomBinDaHouseToneMapping(vec3 color) -{ - color = exp( -1.0 / ( 2.72*color + 0.15 ) ); - color = pow(color, vec3(1. / gamma)); - return color; -} +#ifdef TONEMAP_HEJLRICHARD + return toneMapHejlRichard(color); +#endif -vec3 filmicToneMapping(vec3 color) -{ - color = max(vec3(0.), color - vec3(0.004)); - color = (color * (6.2 * color + .5)) / (color * (6.2 * color + 1.7) + 0.06); - return color; -} +#ifdef TONEMAP_ACES + return toneMapACES(color); +#endif -vec3 Uncharted2ToneMapping(vec3 color) -{ - float A = 0.15; - float B = 0.50; - float C = 0.10; - float D = 0.20; - float E = 0.02; - float F = 0.30; - float W = 11.2; - float exposure = 2.; - color *= exposure; - color = ((color * (A * color + C * B) + D * E) / (color * (A * color + B) + D * F)) - E / F; - float white = ((W * (A * W + C * B) + D * E) / (W * (A * W + B) + D * F)) - E / F; - color /= white; - color = pow(color, vec3(1. / gamma)); - return color; +#ifdef TONEMAP_LINEAR + return linearToneMapping(color); +#endif//TONEMAP_LINEAR + +#ifdef TONEMAP_SIMPLE_REINHARD + return simpleReinhardToneMapping(color) +#endif//TONEMAP_SIMPLE_REINHARD + +#ifdef TONEMAP_LUMA_BASED_REINHARD + return lumaBasedReinhardToneMapping(color) +#endif//TONE_MAP_LUMA_BASED_REINHARD + +#ifdef TONEMAP_WHITE_PRESERVING_LUMA_BASED_REINHARD + return whitePreservingLumaBasedReinhardToneMapping(color) +#endif//TONEMAP_WHITE_PRESERVING_LUMA_BASED_REINHARD + +#ifdef TONEMAP_ROM_BIN_DA_HOUSE + return RomBinDaHouseToneMapping(color) +#endif//TONEMAP_ROM_BIN_DA_HOUSE + + return linearTosRGB(color); } diff --git a/shader/brdf.glsl b/shader/brdf.glsl new file mode 100644 index 0000000..2f2a758 --- /dev/null +++ b/shader/brdf.glsl @@ -0,0 +1,159 @@ +// +// Fresnel +// +// http://graphicrants.blogspot.com/2013/08/specular-brdf-reference.html +// https://github.com/wdas/brdf/tree/master/src/brdfs +// https://google.github.io/filament/Filament.md.html +// + +vec3 F_None(vec3 f0, vec3 f90, float VdotH) +{ + return f0; +} + +// The following equation models the Fresnel reflectance term of the spec equation (aka F()) +// Implementation of fresnel from [4], Equation 15 +vec3 F_Schlick(vec3 f0, vec3 f90, float VdotH) +{ + return f0 + (f90 - f0) * pow(clamp(1.0 - VdotH, 0.0, 1.0), 5.0); +} + +vec3 F_CookTorrance(vec3 f0, vec3 f90, float VdotH) +{ + vec3 f0_sqrt = sqrt(f0); + vec3 ior = (1.0 + f0_sqrt) / (1.0 - f0_sqrt); + vec3 c = vec3(VdotH); + vec3 g = sqrt(sq(ior) + c*c - 1.0); + return 0.5 * pow(g-c, vec3(2.0)) / pow(g+c, vec3(2.0)) * (1.0 + pow(c*(g+c) - 1.0, vec3(2.0)) / pow(c*(g-c) + 1.0, vec3(2.0))); +} + +// Smith Joint GGX +// Note: Vis = G / (4 * NdotL * NdotV) +// see Eric Heitz. 2014. Understanding the Masking-Shadowing Function in Microfacet-Based BRDFs. Journal of Computer Graphics Techniques, 3 +// see Real-Time Rendering. Page 331 to 336. +// see https://google.github.io/filament/Filament.md.html#materialsystem/specularbrdf/geometricshadowing(specularg) +float V_GGX(float NdotL, float NdotV, float alphaRoughness) +{ + float alphaRoughnessSq = alphaRoughness * alphaRoughness; + + float GGXV = NdotL * sqrt(NdotV * NdotV * (1.0 - alphaRoughnessSq) + alphaRoughnessSq); + float GGXL = NdotV * sqrt(NdotL * NdotL * (1.0 - alphaRoughnessSq) + alphaRoughnessSq); + + float GGX = GGXV + GGXL; + if (GGX > 0.0) + { + return 0.5 / GGX; + } + return 0.0; +} + +// Anisotropic GGX visibility function, with height correlation. +// T: Tanget, B: Bi-tanget +float V_GGX_anisotropic(float NdotL, float NdotV, float BdotV, float TdotV, float TdotL, float BdotL, float anisotropy, float at, float ab) +{ + float GGXV = NdotL * length(vec3(at * TdotV, ab * BdotV, NdotV)); + float GGXL = NdotV * length(vec3(at * TdotL, ab * BdotL, NdotL)); + float v = 0.5 / (GGXV + GGXL); + return clamp(v, 0.0, 1.0); +} + +// https://github.com/google/filament/blob/master/shaders/src/brdf.fs#L136 +// https://github.com/google/filament/blob/master/libs/ibl/src/CubemapIBL.cpp#L179 +// Note: Google call it V_Ashikhmin and V_Neubelt +float V_Ashikhmin(float NdotL, float NdotV) +{ + return clamp(1.0 / (4.0 * (NdotL + NdotV - NdotL * NdotV)),0.0,1.0); +} + +// https://github.com/google/filament/blob/master/shaders/src/brdf.fs#L131 +float V_Kelemen(float LdotH) +{ + // Kelemen 2001, "A Microfacet Based Coupled Specular-Matte BRDF Model with Importance Sampling" + return 0.25 / (LdotH * LdotH); +} + +// The following equation(s) model the distribution of microfacet normals across the area being drawn (aka D()) +// Implementation from "Average Irregularity Representation of a Roughened Surface for Ray Reflection" by T. S. Trowbridge, and K. P. Reitz +// Follows the distribution function recommended in the SIGGRAPH 2013 course notes from EPIC Games [1], Equation 3. +float D_GGX(float NdotH, float alphaRoughness) +{ + float alphaRoughnessSq = alphaRoughness * alphaRoughness; + float f = (NdotH * NdotH) * (alphaRoughnessSq - 1.0) + 1.0; + return alphaRoughnessSq / (M_PI * f * f); +} + +// Anisotropic GGX NDF with a single anisotropy parameter controlling the normal orientation. +// See https://google.github.io/filament/Filament.html#materialsystem/anisotropicmodel +// T: Tanget, B: Bi-tanget +float D_GGX_anisotropic(float NdotH, float TdotH, float BdotH, float anisotropy, float at, float ab) +{ + float a2 = at * ab; + vec3 f = vec3(ab * TdotH, at * BdotH, a2 * NdotH); + float w2 = a2 / dot(f, f); + return a2 * w2 * w2 / M_PI; +} + +float D_Ashikhmin(float NdotH, float alphaRoughness) +{ + // Ashikhmin 2007, "Distribution-based BRDFs" + float a2 = alphaRoughness * alphaRoughness; + float cos2h = NdotH * NdotH; + float sin2h = 1.0 - cos2h; + float sin4h = sin2h * sin2h; + float cot2 = -cos2h / (a2 * sin2h); + return 1.0 / (M_PI * (4.0 * a2 + 1.0) * sin4h) * (4.0 * exp(cot2) + sin4h); +} + +//Sheen implementation------------------------------------------------------------------------------------- +// See https://github.com/sebavan/glTF/tree/KHR_materials_sheen/extensions/2.0/Khronos/KHR_materials_sheen + +// Estevez and Kulla http://www.aconty.com/pdf/s2017_pbs_imageworks_sheen.pdf +float D_Charlie(float sheenRoughness, float NdotH) +{ + sheenRoughness = max(sheenRoughness, 0.000001); //clamp (0,1] + float alphaG = sheenRoughness * sheenRoughness; + float invR = 1.0 / alphaG; + float cos2h = NdotH * NdotH; + float sin2h = 1.0 - cos2h; + return (2.0 + invR) * pow(sin2h, invR * 0.5) / (2.0 * M_PI); +} + +//https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#acknowledgments AppendixB +vec3 BRDF_lambertian(vec3 f0, vec3 f90, vec3 diffuseColor, float VdotH) +{ + // see https://seblagarde.wordpress.com/2012/01/08/pi-or-not-to-pi-in-game-lighting-equation/ + return (1.0 - F_Schlick(f0, f90, VdotH)) * (diffuseColor / M_PI); +} + +// https://github.com/KhronosGroup/glTF/tree/master/specification/2.0#acknowledgments AppendixB +vec3 BRDF_specularGGX(vec3 f0, vec3 f90, float alphaRoughness, float VdotH, float NdotL, float NdotV, float NdotH) +{ + vec3 F = F_Schlick(f0, f90, VdotH); + float Vis = V_GGX(NdotL, NdotV, alphaRoughness); + float D = D_GGX(NdotH, alphaRoughness); + + return F * Vis * D; +} + +vec3 BRDF_specularAnisotropicGGX(vec3 f0, vec3 f90, float alphaRoughness, float VdotH, float NdotL, float NdotV, float NdotH, + float BdotV, float TdotV, float TdotL, float BdotL, float TdotH, float BdotH, float anisotropy) +{ + // Roughness along tangent and bitangent. + // Christopher Kulla and Alejandro Conty. 2017. Revisiting Physically Based Shading at Imageworks + float at = max(alphaRoughness * (1.0 + anisotropy), 0.00001); + float ab = max(alphaRoughness * (1.0 - anisotropy), 0.00001); + + vec3 F = F_Schlick(f0, f90, VdotH); + float V = V_GGX_anisotropic(NdotL, NdotV, BdotV, TdotV, TdotL, BdotL, anisotropy, at, ab); + float D = D_GGX_anisotropic(NdotH, TdotH, BdotH, anisotropy, at, ab); + + return F * V * D; +} + +// f_sheen +vec3 BRDF_specularSheen(vec3 sheenColor, float sheenIntensity, float sheenRoughness, float NdotL, float NdotV, float NdotH) +{ + float sheenDistribution = D_Charlie(sheenRoughness, NdotH); + float sheenVisibility = V_Ashikhmin(NdotL, NdotV); + return sheenColor * sheenIntensity * sheenDistribution * sheenVisibility; +} diff --git a/shader/cnmr.lightmodel b/shader/cnmr.lightmodel new file mode 100644 index 0000000..6869116 --- /dev/null +++ b/shader/cnmr.lightmodel @@ -0,0 +1,17 @@ +//该文件用于描述光照模型 +[intro] +author= +infomation= + +[attr] //该段用于描述该着色最终会需要那些数据 + //会根据最终需求,数据可能来自纹理,可能来自顶点,或是全局固定 + + vec3 BaseColor; + vec3 Normal; + float Metallic; + float Roughness; + +[compute] //计算函数体 +{ + +} diff --git a/shader/full_screen_rectangle.vert.glsl b/shader/full_screen_rectangle.vert.glsl new file mode 100644 index 0000000..2edde84 --- /dev/null +++ b/shader/full_screen_rectangle.vert.glsl @@ -0,0 +1,12 @@ +#version 450 core + +layout(location = 0) in vec2 Vertex; + +layout(location = 0) out vec2 FragmentPosition; + +void main() +{ + gl_Position=vec4(Vertex,0.0,1.0); + + FragmentPosition=(Vertex+1.0)/2.0; +} diff --git a/shader/gbuffer intro.txt b/shader/gbuffer intro.txt new file mode 100644 index 0000000..05d6979 --- /dev/null +++ b/shader/gbuffer intro.txt @@ -0,0 +1,45 @@ +[attr] //该段用于描述该材质最终会需要那些数据 + + vec3 BaseColor; + vec3 Normal; + float Metallic; + float Roughness; + +[framebuffer] //该段用于描述当前shader会输出到几个Framebuffer上 + + //如果是前向渲染,一般只有一个Framebuffer + + vec4 FinalColor; + + //如果是延迟渲染,则代表GBuffer,可能存在多个framebuffer + + vec4 gb_color_metallic; + vec4 gb_normal_roughness; + +[attr_to_fb] //该段用于描述attr如何转换到framebuffer上 + + //如果是UI之类直接上色的,那会比较颜色 + + FinalColor=vec4(BaseColor,1.0); + + //如果是前向渲染 + + FinalColor=BRDF(BaseColor,Normal,Metallic,Roughness); + + //如果是延迟渲染 + + gb_color_metallic =vec4(BaseColor,Metallic); + gb_normal_roughness =vec4(Normal, Roughness); + +[gb_to_attr] //该段为延迟渲染独有,用于表示如何从gbuffer分解成独立数据 + + vec4 gb_cm=texture(gb_color_metallic,FragmentPosition); + vec4 gb_nr=texture(gb_normal_roughness,FragmentPosition); + + BaseColor =gb_cm.rgb; + Metallic =gb_cm.a; + + Normal =gb_nr.rgb; + Roughness =gb_nr.a; + + gl_FragColor=BRDF(BaseColor,Normal,Metallic,Roughness); diff --git a/shader/primitive.vert b/shader/primitive.vert new file mode 100644 index 0000000..55c1e42 --- /dev/null +++ b/shader/primitive.vert @@ -0,0 +1,129 @@ +#include + +in vec3 a_Position; +out vec3 v_Position; + +#ifdef HAS_NORMALS +in vec3 a_Normal; +#endif + +#ifdef HAS_TANGENTS +in vec4 a_Tangent; +#endif + +#ifdef HAS_NORMALS +#ifdef HAS_TANGENTS +out mat3 v_TBN; +#else +out vec3 v_Normal; +#endif +#endif + +#ifdef HAS_UV_SET1 +in vec2 a_UV1; +#endif + +#ifdef HAS_UV_SET2 +in vec2 a_UV2; +#endif + +out vec2 v_UVCoord1; +out vec2 v_UVCoord2; + +#ifdef HAS_VERTEX_COLOR_VEC3 +in vec3 a_Color; +out vec3 v_Color; +#endif + +#ifdef HAS_VERTEX_COLOR_VEC4 +in vec4 a_Color; +out vec4 v_Color; +#endif + +uniform mat4 u_ViewProjectionMatrix; +uniform mat4 u_ModelMatrix; +uniform mat4 u_NormalMatrix; + +vec4 getPosition() +{ + vec4 pos = vec4(a_Position, 1.0); + +#ifdef USE_MORPHING + pos += getTargetPosition(); +#endif + +#ifdef USE_SKINNING + pos = getSkinningMatrix() * pos; +#endif + + return pos; +} + +#ifdef HAS_NORMALS +vec3 getNormal() +{ + vec3 normal = a_Normal; + +#ifdef USE_MORPHING + normal += getTargetNormal(); +#endif + +#ifdef USE_SKINNING + normal = mat3(getSkinningNormalMatrix()) * normal; +#endif + + return normalize(normal); +} +#endif + +#ifdef HAS_TANGENTS +vec3 getTangent() +{ + vec3 tangent = a_Tangent.xyz; + +#ifdef USE_MORPHING + tangent += getTargetTangent(); +#endif + +#ifdef USE_SKINNING + tangent = mat3(getSkinningMatrix()) * tangent; +#endif + + return normalize(tangent); +} +#endif + +void main() +{ + vec4 pos = u_ModelMatrix * getPosition(); + v_Position = vec3(pos.xyz) / pos.w; + + #ifdef HAS_NORMALS + #ifdef HAS_TANGENTS + vec3 tangent = getTangent(); + vec3 normalW = normalize(vec3(u_NormalMatrix * vec4(getNormal(), 0.0))); + vec3 tangentW = normalize(vec3(u_ModelMatrix * vec4(tangent, 0.0))); + vec3 bitangentW = cross(normalW, tangentW) * a_Tangent.w; + v_TBN = mat3(tangentW, bitangentW, normalW); + #else // !HAS_TANGENTS + v_Normal = normalize(vec3(u_NormalMatrix * vec4(getNormal(), 0.0))); + #endif + #endif // !HAS_NORMALS + + v_UVCoord1 = vec2(0.0, 0.0); + v_UVCoord2 = vec2(0.0, 0.0); + + #ifdef HAS_UV_SET1 + v_UVCoord1 = a_UV1; + #endif + + #ifdef HAS_UV_SET2 + v_UVCoord2 = a_UV2; + #endif + + #if defined(HAS_VERTEX_COLOR_VEC3) || defined(HAS_VERTEX_COLOR_VEC4) + v_Color = a_Color; + #endif + + gl_Position = u_ViewProjectionMatrix * pos; +} diff --git a/shader/r_composition.frag b/shader/r_composition.frag new file mode 100644 index 0000000..6c2130b --- /dev/null +++ b/shader/r_composition.frag @@ -0,0 +1,55 @@ +#version 460 core +/** + * Copyright (c) 2018-2020 www.hyzgame.com + * + * Create by ShaderMaker + */ +layout(push_constant) uniform Consts +{ + mat4 local_to_world; + mat3 normal; + vec3 object_position; + vec3 object_size; +} pc; +layout(binding = 0) uniform WorldMatrix // hgl/math/Math.h +{ + mat4 ortho; + + mat4 projection; + mat4 inverse_projection; + + mat4 modelview; + mat4 inverse_modelview; + + mat4 mvp; + mat4 inverse_mvp; + + vec4 view_pos; +} world; + +layout(binding=0) uniform sampler2D rb_depth; +layout(binding=1) uniform sampler2D gb_color_metallic; +layout(binding=2) uniform sampler2D gb_normal_roughness; + +layout(location=0) in vec2 vs_out_position; +layout(location=0) out vec4 FragColor; + +void main() +{ + vec3 BaseColor; + vec3 Normal; + float Metallic; + float Roughness; + + vec4 gb_cm=texture(gb_color_metallic,FragmentPosition); + vec4 gb_cr=texture(gb_normal_roughness,FragmentPosition); + BaseColor =gb_cm.rgb; + Metallic =gb_cm.a; + Normal =gb_cr.rgb; + Roughness =gb_cr.a; + +//[Begin] Your code------------------------------------ + +//[End] Your code-------------------------------------- + FragColor= +} diff --git a/shader/r_composition.vert b/shader/r_composition.vert new file mode 100644 index 0000000..c566fd2 --- /dev/null +++ b/shader/r_composition.vert @@ -0,0 +1,17 @@ +#version 460 core +/** + * Copyright (c) 2018-2020 www.hyzgame.com + * + * Create by ShaderMaker + */ + +layout(location = 0) in vec2 Vertex; + +layout(location = 0) out vec2 vs_out_position; + +void main() +{ + gl_Position=vec4(Vertex,0.0,1.0); + + vs_out_position=(Vertex+1.0)/2.0; +} diff --git a/shader/r_gbuffer.frag b/shader/r_gbuffer.frag new file mode 100644 index 0000000..4808948 --- /dev/null +++ b/shader/r_gbuffer.frag @@ -0,0 +1,44 @@ +#version 460 core +/** + * Copyright (c) 2018-2020 www.hyzgame.com + * + * Create by ShaderMaker + */ +layout(push_constant) uniform Consts +{ + mat4 local_to_world; + mat3 normal; + vec3 object_position; + vec3 object_size; +} pc; +layout(binding = 0) uniform WorldMatrix // hgl/math/Math.h +{ + mat4 ortho; + + mat4 projection; + mat4 inverse_projection; + + mat4 modelview; + mat4 inverse_modelview; + + mat4 mvp; + mat4 inverse_mvp; + + vec4 view_pos; +} world; + +layout(location=0) out vec4 gb_color_metallic; +layout(location=1) out vec4 gb_normal_roughness; + +void main() +{ + vec3 BaseColor; + vec3 Normal; + float Metallic; + float Roughness; +//[Begin] Your code------------------------------------ + +//[End] Your code-------------------------------------- + gb_color_metallic =vec4(BaseColor,Metallic); + gb_normal_roughness =vec4(Normal, Roughness); +}