Compare commits

...

6 Commits

Author SHA1 Message Date
samuncle
bba591c2fc Add better shader for dirt 2018-12-05 23:45:06 +01:00
samuncle
0d56de5957 Add the final touch for the dirt shader 2018-09-09 23:18:32 +02:00
Benau
7478ed6ad4 Fix crash 2018-09-09 01:03:40 +08:00
Benau
33a983a701 Merge remote-tracking branch 'origin/master' into add-dirt-to-karts 2018-09-09 00:58:05 +08:00
samuncle
1efb8b1dd1 Add simple dirt factor 2018-09-08 18:41:36 +02:00
samuncle
56bda4d4ac Ground work for kart dirtyness 2018-09-08 13:26:00 +02:00
11 changed files with 270 additions and 3 deletions

View File

@ -0,0 +1,59 @@
in vec3 bitangent;
in vec4 color;
in float hue_change;
in vec3 normal;
in vec3 tangent;
in vec2 uv;
in float camdist;
in vec4 world_position;
in vec3 world_normal;
layout(location = 0) out vec4 o_diffuse_color;
layout(location = 1) out vec4 o_normal_color;
#stk_include "utils/encode_normal.frag"
#stk_include "utils/rgb_conversion.frag"
#stk_include "utils/sp_texture_sampling.frag"
float overlay(float bg, float fg)
{
return bg < 0.5 ? (2.0 * bg * fg) : (1.0 - 2.0 * (1.0 - bg) * (1.0 - fg));
}
void main()
{
vec4 col = multi_sampleTextureLayer0(uv, camdist);
vec4 rock = multi_sampleTextureLayer4(uv, camdist);
vec2 uuv = vec2(world_position.x, world_position.z);
float mask_2 = multi_sampleTextureLayer5(uv, camdist).r;
mask_2 = pow(mask_2, 1.5);
float up_mask = dot(vec3(0., 1., 0.), world_normal);
up_mask = pow(up_mask + (up_mask * 0.1), 2);
up_mask = clamp(up_mask, 0.0, 1.0);
//vec3 final_color = mix(col.rgb, rock.rgb, overlay(color.r, mask_2));
vec3 final_color = mix(rock.rgb, col.rgb, overlay(up_mask, mask_2));
//vec3 final_color = mix(vec3(1.0, 0.0, 0.0), vec3(0.0, 1.0, 0.0), overlay(color.r, mask_2));
#if defined(Advanced_Lighting_Enabled)
vec4 layer_2 = multi_sampleTextureLayer2(uv, camdist);
vec4 layer_3 = multi_sampleTextureLayer3(uv, camdist);
o_diffuse_color = vec4(final_color, layer_2.z);
vec3 tangent_space_normal = 2.0 * layer_3.xyz - 1.0;
vec3 frag_tangent = normalize(tangent);
vec3 frag_bitangent = normalize(bitangent);
vec3 frag_normal = normalize(normal);
mat3 t_b_n = mat3(frag_tangent, frag_bitangent, frag_normal);
vec3 world_normal = t_b_n * tangent_space_normal;
o_normal_color.xy = 0.5 * EncodeNormal(normalize(world_normal)) + 0.5;
o_normal_color.zw = layer_2.xy;
#else
o_diffuse_color = vec4(final_color, 1.0);
#endif
}

View File

@ -0,0 +1,82 @@
in vec3 bitangent;
in vec4 color;
in float hue_change;
in vec3 normal;
in vec3 tangent;
in vec2 uv;
in vec4 world_position;
layout(location = 0) out vec4 o_diffuse_color;
layout(location = 1) out vec4 o_normal_color;
#stk_include "utils/encode_normal.frag"
#stk_include "utils/rgb_conversion.frag"
#stk_include "utils/sp_texture_sampling.frag"
uniform sampler2D g_dirt_albedo;
uniform sampler2D g_dirt_normal;
uniform float dirt_factor;
void main()
{
// Triplanar detail map
vec2 xyuv = vec2(world_position.x, world_position.y);
vec3 dirt = texture(g_dirt_albedo, uv * 4).rgb;
vec4 col = sampleTextureLayer0(uv);
if (hue_change > 0.0)
{
float mask = col.a;
vec3 old_hsv = rgbToHsv(col.rgb);
float mask_step = step(mask, 0.5);
#if !defined(Advanced_Lighting_Enabled)
// For similar color
float saturation = mask * 1.825; // 2.5 * 0.5 ^ (1. / 2.2)
#else
float saturation = mask * 2.5;
#endif
vec2 new_xy = mix(vec2(old_hsv.x, old_hsv.y), vec2(hue_change,
max(old_hsv.y, saturation)), vec2(mask_step, mask_step));
vec3 new_color = hsvToRgb(vec3(new_xy.x, new_xy.y, old_hsv.z));
col = vec4(new_color.r, new_color.g, new_color.b, 1.0);
}
float dirtMask = sampleTextureLayer4(uv).r;
float nitroMask = sampleTextureLayer4(uv).g;
//dirtMask *= dirtMask;
// Dirt factor
dirtMask = 1.0 - dirtMask;
dirtMask *= clamp(dirt_factor, 0.0, 1.0);
nitroMask *= clamp(dirt_factor, 0.0, 1.0);
vec3 final_color = col.xyz * color.xyz;
//dirtMask = step(dirtMask,dirt_factor - 0.5);
final_color = mix(final_color, dirt, dirtMask);
final_color = mix(final_color, vec3(0, 0.415, 0.639), nitroMask);
#if defined(Advanced_Lighting_Enabled)
vec4 layer_2 = sampleTextureLayer2(uv);
vec4 layer_3 = sampleTextureLayer3(uv);
// Part emitting light
layer_2.z = mix(layer_2.z, 1.0, nitroMask);
o_diffuse_color = vec4(final_color, layer_2.z);
// Dirt part should not be glossy
layer_2.r = mix(layer_2.r, 0.0, dirtMask);
// Mix the normal map for the dirt
vec4 dirt_nor = texture(g_dirt_normal, uv * 4);
layer_3 = mix(layer_3, dirt_nor, dirtMask);
vec3 tangent_space_normal = 2.0 * layer_3.xyz - 1.0;
vec3 frag_tangent = normalize(tangent);
vec3 frag_bitangent = normalize(bitangent);
vec3 frag_normal = normalize(normal);
mat3 t_b_n = mat3(frag_tangent, frag_bitangent, frag_normal);
vec3 world_normal = t_b_n * tangent_space_normal;
o_normal_color.xy = 0.5 * EncodeNormal(normalize(world_normal)) + 0.5;
o_normal_color.zw = layer_2.xy;
#else
o_diffuse_color = vec4(final_color, 1.0);
#endif
}

View File

@ -23,7 +23,7 @@ void main()
detail *= 2.5;
vec4 col = sampleTextureLayer0(uv);
col = (col - detail) + 0.5;
//col = (col - detail) + 0.5;
if (hue_change > 0.0)
{
float mask = col.a;

View File

@ -0,0 +1,13 @@
<spshader>
<shader-info name="advancedTerrain" fallback-shader="solid" use-tangents="Y" srgb="Y Y N N Y N"/>
<first-pass vertex-shader="sp_pass.vert"
fragment-shader="sp_advanced_terrain.frag">
</first-pass>
<shadow-pass vertex-shader="sp_shadow.vert"
fragment-shader="white.frag">
</shadow-pass>
<uniform-assigners>
<uniform-assigner name="layer"
function="shadowCascadeUniformAssigner"/>
</uniform-assigners>
</spshader>

View File

@ -0,0 +1,23 @@
<spshader>
<shader-info name="kartDirt" fallback-shader="solid" use-tangents="Y"/>
<first-pass vertex-shader="sp_pass.vert"
fragment-shader="sp_kart_dirt.frag"
skinned-mesh-shader="sp_skinning.vert">
<prefilled-textures>
<prefilled-texture name="g_dirt_albedo" file="gfx_kartDirt_a_Albedo.png"
srgb="Y" sampler="trilinear"/>
<prefilled-texture name="g_dirt_normal" file="gfx_kartDirt_a_Normal.png"
sampler="trilinear"/>
</prefilled-textures>
</first-pass>
<shadow-pass vertex-shader="sp_shadow.vert"
fragment-shader="white.frag"
skinned-mesh-shader="sp_skinning_shadow.vert">
</shadow-pass>
<uniform-assigners>
<uniform-assigner name="layer"
function="shadowCascadeUniformAssigner"/>
<uniform-assigner name="dirt_factor"
function="dirtFactorUniformAssigner"/>
</uniform-assigners>
</spshader>

View File

@ -136,3 +136,26 @@ vec4 sampleTextureLayer5(vec2 uv)
{
return texture(tex_layer_5, uv);
}
vec4 multi_sampleTextureLayer5(vec2 uv, float distance)
{
vec4 l_col = sampleTextureLayer5(uv * LOW_SAMPLING);
vec4 m_col = sampleTextureLayer5(uv * MEDIUM_SAMPLING);
vec4 h_col = sampleTextureLayer5(uv * HIGH_SAMPLING);
// From Low to medium
float factor = distance * 0.02;
factor = pow(factor, 2.5);
factor = clamp(factor, 0.0, 1.0);
vec4 f_col = mix(m_col, l_col, factor);
// From medium to high
factor = distance * 0.1;
factor = pow(factor, 2.5);
factor = clamp(factor, 0.0, 1.0);
f_col = mix(h_col, f_col, factor);
return f_col;
}

View File

@ -46,8 +46,8 @@ SkidMarks::SkidMarks(const AbstractKart& kart, float width) : m_kart(kart)
{
m_width = width;
m_material = material_manager->getMaterialSPM("skidmarks.png", "",
"alphablend");
m_shader = SP::SPShaderManager::get()->getSPShader("alphablend");
"alphatest");
m_shader = SP::SPShaderManager::get()->getSPShader("alphatest");
assert(m_shader);
auto texture = SP::SPTextureManager::get()->getTexture(
m_material->getSamplerPath(0), m_material,

View File

@ -18,6 +18,7 @@
#include "graphics/sp/sp_shader_manager.hpp"
#include "io/file_manager.hpp"
#include "io/xml_node.hpp"
#include "graphics/camera.hpp"
#include "graphics/central_settings.hpp"
#include "graphics/sp/sp_base.hpp"
#include "graphics/sp/sp_shader.hpp"
@ -25,6 +26,7 @@
#include "graphics/sp/sp_texture_manager.hpp"
#include "graphics/sp/sp_uniform_assigner.hpp"
#include "tracks/track.hpp"
#include "karts/abstract_kart.hpp"
#include "utils/string_utils.hpp"
#include "utils/log.hpp"
@ -60,6 +62,26 @@ SPShaderManager::SPShaderManager()
ua->setValue(sp_wind_dir);
}
},
{ "dirtFactorUniformAssigner", [](SPUniformAssigner* ua)
{
AbstractKart* k = NULL;
if (Camera::getNumCameras() > 0)
{
if (Camera::getActiveCamera() != NULL)
{
k = Camera::getActiveCamera()->getKart();
}
}
if (k)
{
ua->setValue(k->getDirtFactor());
}
else
{
ua->setValue(0.0f);
}
}
},
{ "isDuringDayUniformAssigner", [](SPUniformAssigner* ua)
{
int is_during_day = Track::getCurrentTrack() ?

View File

@ -356,6 +356,9 @@ public:
/** True if the wheels are touching the ground. */
virtual bool isOnGround() const = 0;
// ------------------------------------------------------------------------
/** Returns the dirt factor (how dirty the kart is). */
virtual float getDirtFactor() const = 0;
// ------------------------------------------------------------------------
/** Returns the slipstream object of this kart. */
virtual const SlipStream* getSlipstream() const = 0;
// ------------------------------------------------------------------------

View File

@ -377,6 +377,8 @@ void Kart::reset()
m_view_blocked_by_plunger = 0;
m_has_caught_nolok_bubblegum = false;
m_is_jumping = false;
m_dirt_factor = 0.0f;
m_dirt_reset_counter = 0.0f;
for (int i=0;i<m_xyz_history_size;i++)
{
@ -1164,6 +1166,14 @@ bool Kart::isOnGround() const
&& !getKartAnimation());
} // isOnGround
//-----------------------------------------------------------------------------
/** The kart as a dirt factor (if the player skids and go in dirty places)
*/
float Kart::getDirtFactor() const
{
return m_dirt_factor;
} // getDirtFactor
//-----------------------------------------------------------------------------
/** The kart is near the ground, but not necessarily on it (small jumps). This
* is used to determine when to stop flying.
@ -1890,6 +1900,29 @@ void Kart::handleMaterialGFX(float dt)
{
const Material *material = getMaterial();
if (m_skidding->isSkidding())
{
m_dirt_factor += 0.001f;
if (m_dirt_factor > 1.0f)
{
m_dirt_factor = 1.0f;
}
m_dirt_reset_counter = 2.0f;
}
else
{
m_dirt_reset_counter -= 0.01f;
if (m_dirt_reset_counter < 0.0f)
{
m_dirt_reset_counter = 0.0f;
m_dirt_factor -= 0.002f;
if (m_dirt_factor < 0.0f)
{
m_dirt_factor = 0.0f;
}
}
}
// First test: give the terrain effect, if the kart is
// on top of a surface (i.e. not falling), actually touching
// something with the wheels, and the material has not the

View File

@ -166,6 +166,12 @@ protected:
* the kart is squashed. */
float m_squash_time;
/** How dirty the kart is, 0 = clean */
float m_dirt_factor;
/** Counter to reset the dirt */
float m_dirt_reset_counter;
/** Current leaning of the kart. */
float m_current_lean;
@ -453,6 +459,9 @@ public:
/** True if the wheels are touching the ground. */
virtual bool isOnGround() const OVERRIDE;
// ------------------------------------------------------------------------
/** Returns the dirt factor (how dirty the kart is). */
virtual float getDirtFactor() const OVERRIDE;
// ------------------------------------------------------------------------
/** Returns true if the kart is close to the ground, used to dis/enable
* the upright constraint to allow for more realistic explosions. */
bool isNearGround() const;