From 4197746c69e509839666465548b43b7c99e7ef55 Mon Sep 17 00:00:00 2001 From: MihailRis Date: Sat, 20 Jul 2024 17:13:03 +0300 Subject: [PATCH] add entity shader --- res/shaders/entity.glslf | 24 +++++++++++++++++++ res/shaders/entity.glslv | 42 ++++++++++++++++++++++++++++++++++ res/shaders/lib/commons.glsl | 8 ++++--- res/shaders/lib/constants.glsl | 3 +++ res/shaders/main.glslf | 2 +- res/shaders/main.glslv | 15 ++++++------ src/assets/AssetsLoader.cpp | 1 + src/coders/GLSLExtension.cpp | 4 ---- 8 files changed, 83 insertions(+), 16 deletions(-) create mode 100644 res/shaders/entity.glslf create mode 100644 res/shaders/entity.glslv diff --git a/res/shaders/entity.glslf b/res/shaders/entity.glslf new file mode 100644 index 00000000..a49bdfa8 --- /dev/null +++ b/res/shaders/entity.glslf @@ -0,0 +1,24 @@ +in vec4 a_color; +in vec2 a_texCoord; +in float a_distance; +in vec3 a_dir; +out vec4 f_color; + +uniform sampler2D u_texture0; +uniform samplerCube u_cubemap; +uniform vec3 u_fogColor; +uniform float u_fogFactor; +uniform float u_fogCurve; + +void main() { + vec3 fogColor = texture(u_cubemap, a_dir).rgb; + vec4 tex_color = texture(u_texture0, a_texCoord); + float depth = (a_distance/256.0); + float alpha = a_color.a * tex_color.a; + // anyway it's any alpha-test alternative required + if (alpha < 0.3f) + discard; + f_color = mix(a_color * tex_color, vec4(fogColor,1.0), + min(1.0, pow(depth*u_fogFactor, u_fogCurve))); + f_color.a = alpha; +} diff --git a/res/shaders/entity.glslv b/res/shaders/entity.glslv new file mode 100644 index 00000000..12ffe109 --- /dev/null +++ b/res/shaders/entity.glslv @@ -0,0 +1,42 @@ +#include + +layout (location = 0) in vec3 v_position; +layout (location = 1) in vec2 v_texCoord; +layout (location = 2) in vec3 v_color; +layout (location = 3) in vec3 v_normal; +layout (location = 4) in float v_light; + +out vec4 a_color; +out vec2 a_texCoord; +out float a_distance; +out vec3 a_dir; + +uniform mat4 u_model; +uniform mat4 u_proj; +uniform mat4 u_view; +uniform vec3 u_cameraPos; +uniform float u_gamma; +uniform samplerCube u_cubemap; + +uniform vec3 u_torchlightColor; +uniform float u_torchlightDistance; + +void main() { + vec4 modelpos = u_model * vec4(v_position, 1.0); + vec3 pos3d = modelpos.xyz - u_cameraPos; + modelpos.xyz = apply_planet_curvature(modelpos.xyz, pos3d); + + vec4 decomp_light = decompress_light(v_light); + vec3 light = decomp_light.rgb; + float torchlight = max(0.0, 1.0-distance(u_cameraPos, modelpos.xyz) / + u_torchlightDistance); + light += torchlight * u_torchlightColor; + a_color = vec4(pow(light, vec3(u_gamma)) * v_normal,1.0f); + a_texCoord = v_texCoord; + + a_dir = modelpos.xyz - u_cameraPos; + vec3 skyLightColor = pick_sky_color(u_cubemap); + a_color.rgb = max(a_color.rgb, skyLightColor.rgb*decomp_light.a); + a_distance = length(u_view * u_model * vec4(pos3d * FOG_POS_SCALE, 0.0)); + gl_Position = u_proj * u_view * modelpos; +} diff --git a/res/shaders/lib/commons.glsl b/res/shaders/lib/commons.glsl index 0a694422..0a9561b3 100644 --- a/res/shaders/lib/commons.glsl +++ b/res/shaders/lib/commons.glsl @@ -3,9 +3,6 @@ #include -#define PI 3.1415926535897932384626433832795 -#define PI2 (PI*2) - vec4 decompress_light(float compressed_light) { vec4 result; int compressed = floatBitsToInt(compressed_light); @@ -24,4 +21,9 @@ vec3 pick_sky_color(samplerCube cubemap) { return skyLightColor; } +vec3 apply_planet_curvature(vec3 modelPos, vec3 pos3d) { + modelPos.y -= pow(length(pos3d.xz)*CURVATURE_FACTOR, 3.0); + return modelPos; +} + #endif // COMMONS_GLSL_ diff --git a/res/shaders/lib/constants.glsl b/res/shaders/lib/constants.glsl index 161a505b..cc617506 100644 --- a/res/shaders/lib/constants.glsl +++ b/res/shaders/lib/constants.glsl @@ -1,6 +1,9 @@ #ifndef CONSTANTS_GLSL_ #define CONSTANTS_GLSL_ +#define PI 3.1415926535897932384626433832795 +#define PI2 (PI*2) + // geometry #define CURVATURE_FACTOR 0.002 diff --git a/res/shaders/main.glslf b/res/shaders/main.glslf index 071c56b3..4c1cb014 100644 --- a/res/shaders/main.glslf +++ b/res/shaders/main.glslf @@ -10,7 +10,7 @@ uniform vec3 u_fogColor; uniform float u_fogFactor; uniform float u_fogCurve; -void main(){ +void main() { vec3 fogColor = texture(u_cubemap, a_dir).rgb; vec4 tex_color = texture(u_texture0, a_texCoord); float depth = (a_distance/256.0); diff --git a/res/shaders/main.glslv b/res/shaders/main.glslv index 90b0d82f..ba3687e8 100644 --- a/res/shaders/main.glslv +++ b/res/shaders/main.glslv @@ -12,7 +12,6 @@ out vec3 a_dir; uniform mat4 u_model; uniform mat4 u_proj; uniform mat4 u_view; -uniform vec3 u_skyLightColor; uniform vec3 u_cameraPos; uniform float u_gamma; uniform samplerCube u_cubemap; @@ -20,15 +19,15 @@ uniform samplerCube u_cubemap; uniform vec3 u_torchlightColor; uniform float u_torchlightDistance; -void main(){ +void main() { vec4 modelpos = u_model * vec4(v_position, 1.0); - vec3 pos3d = modelpos.xyz-u_cameraPos.xyz; - modelpos.y -= pow(length(pos3d.xz)*CURVATURE_FACTOR, 3.0); - vec4 viewmodelpos = u_view * modelpos; + vec3 pos3d = modelpos.xyz-u_cameraPos; + modelpos.xyz = apply_planet_curvature(modelpos.xyz, pos3d); + vec4 decomp_light = decompress_light(v_light); - vec3 light = decomp_light.rgb; - float torchlight = max(0.0, 1.0-distance(u_cameraPos, modelpos.xyz)/u_torchlightDistance); + float torchlight = max(0.0, 1.0-distance(u_cameraPos, modelpos.xyz) / + u_torchlightDistance); light += torchlight * u_torchlightColor; a_color = vec4(pow(light, vec3(u_gamma)),1.0f); a_texCoord = v_texCoord; @@ -37,5 +36,5 @@ void main(){ vec3 skyLightColor = pick_sky_color(u_cubemap); a_color.rgb = max(a_color.rgb, skyLightColor.rgb*decomp_light.a); a_distance = length(u_view * u_model * vec4(pos3d * FOG_POS_SCALE, 0.0)); - gl_Position = u_proj * viewmodelpos; + gl_Position = u_proj * u_view * modelpos; } diff --git a/src/assets/AssetsLoader.cpp b/src/assets/AssetsLoader.cpp index ae39e4d8..955eb0b1 100644 --- a/src/assets/AssetsLoader.cpp +++ b/src/assets/AssetsLoader.cpp @@ -180,6 +180,7 @@ void AssetsLoader::addDefaults(AssetsLoader& loader, const Content* content) { loader.add(AssetType::FONT, FONTS_FOLDER+"/font", "normal"); loader.add(AssetType::SHADER, SHADERS_FOLDER+"/ui", "ui"); loader.add(AssetType::SHADER, SHADERS_FOLDER+"/main", "main"); + loader.add(AssetType::SHADER, SHADERS_FOLDER+"/entity", "entity"); loader.add(AssetType::SHADER, SHADERS_FOLDER+"/lines", "lines"); loader.add(AssetType::TEXTURE, TEXTURES_FOLDER+"/gui/menubg", "gui/menubg"); loader.add(AssetType::TEXTURE, TEXTURES_FOLDER+"/gui/delete_icon", "gui/delete_icon"); diff --git a/src/coders/GLSLExtension.cpp b/src/coders/GLSLExtension.cpp index cce84e8c..629b2ff6 100644 --- a/src/coders/GLSLExtension.cpp +++ b/src/coders/GLSLExtension.cpp @@ -141,9 +141,5 @@ std::string GLSLExtension::process(const fs::path& file, const std::string& sour ss << source.substr(pos, endline+1-pos); pos = endline+1; } - if (!header) { - std::cout << " ========================================================== " << file.u8string() << std::endl; - std::cout << ss.str() << std::endl; - } return ss.str(); }