17
0
mirror of https://github.com/MaSzyna-EU07/maszyna.git synced 2026-10-24 13:35:24 +02:00

feat(track): parallax track materials blended at the joints

This commit is contained in:
2026-10-21 23:25:40 +02:00
parent 674abfeeab
commit b8eb5c71a1
8 changed files with 130 additions and 14 deletions

View File

@@ -0,0 +1,22 @@
// the rails of a track of a parallax material faded into those of the tracks joined at its ends, see trackblend.glsl
#define TRACKBLEND_NORMALMAP 1
#define TRACKBLEND_PARALLAX 1
#param (height_scale, 2, 1, 1, zero)
#param (height_offset, 2, 2, 1, zero)
#texture (diffuse, 0, sRGB_A)
uniform sampler2D diffuse;
#texture (diffuse1, 1, sRGB_A)
uniform sampler2D diffuse1;
#texture (diffuse2, 2, sRGB_A)
uniform sampler2D diffuse2;
#texture (diffuse3, 3, sRGB_A)
uniform sampler2D diffuse3;
#texture (normalmap, 4, RGBA)
uniform sampler2D normalmap;
#texture (normalmap1, 5, RGBA)
uniform sampler2D normalmap1;
#texture (normalmap2, 6, RGBA)
uniform sampler2D normalmap2;
#texture (normalmap3, 7, RGBA)
uniform sampler2D normalmap3;
#include <trackblend.glsl>

View File

@@ -0,0 +1,23 @@
// the ballast of a track of a parallax material mixed in grains into that of the tracks joined at its ends, see trackblend.glsl
#define TRACKBLEND_GRAIN 1
#define TRACKBLEND_NORMALMAP 1
#define TRACKBLEND_PARALLAX 1
#param (height_scale, 2, 1, 1, zero)
#param (height_offset, 2, 2, 1, zero)
#texture (diffuse, 0, sRGB_A)
uniform sampler2D diffuse;
#texture (diffuse1, 1, sRGB_A)
uniform sampler2D diffuse1;
#texture (diffuse2, 2, sRGB_A)
uniform sampler2D diffuse2;
#texture (diffuse3, 3, sRGB_A)
uniform sampler2D diffuse3;
#texture (normalmap, 4, RGBA)
uniform sampler2D normalmap;
#texture (normalmap1, 5, RGBA)
uniform sampler2D normalmap1;
#texture (normalmap2, 6, RGBA)
uniform sampler2D normalmap2;
#texture (normalmap3, 7, RGBA)
uniform sampler2D normalmap3;
#include <trackblend.glsl>

View File

@@ -6,12 +6,16 @@
// mat_default, 1 normalmap, 2 sunlessnormalmap, 3 shadowlessnormalmap
// TRACKBLEND_SPECGLOSS the specgloss map of the track itself as well, the textures of the ends p1 and p2 alone (no room
// for a third set of them)
// TRACKBLEND_PARALLAX with the normal maps, as mat_parallax: each texture shifted by the heights in its own normal map
in vec3 f_normal;
in vec2 f_coord;
in vec4 f_pos;
#ifdef TRACKBLEND_NORMALMAP
in mat3 f_tbn;
#endif
#ifdef TRACKBLEND_PARALLAX
in vec3 TangentFragPos;
#endif
in vec4 f_clip_pos;
in vec4 f_clip_future_pos;
@@ -77,6 +81,49 @@ float grain_value(vec2 p)
}
#endif
#ifdef TRACKBLEND_PARALLAX
// the texture coordinates shifted by the heights (b) of the normal map, as ParallaxMapping() of mat_parallax.frag
vec2 parallax_coord(sampler2D heights, vec2 coord, vec3 viewdir, vec2 dcoorddx, vec2 dcoorddy)
{
float pos_len = length(f_pos.xyz);
if (pos_len > 100.0)
return coord;
float fade = 1.0 - smoothstep(80.0, 100.0, pos_len);
#if EXTRAEFFECTS_ENABLED
const float minLayers = 8.0;
const float maxLayers = 32.0;
float LayersWeight = pos_len / 20.0;
vec2 currentTexCoords = coord;
float currentDepthMapValue = textureGrad(heights, currentTexCoords, dcoorddx, dcoorddy).b;
LayersWeight = min(abs(dot(vec3(0.0, 0.0, 1.0), viewdir)), LayersWeight);
float numLayers = mix(maxLayers, minLayers, clamp(LayersWeight, 0.0, 1.0));
float layerDepth = 1.0 / numLayers;
float currentLayerDepth = 0.0;
vec2 P = viewdir.xy * param[2].y * fade;
vec2 deltaTexCoords = P / numLayers;
while (currentLayerDepth < currentDepthMapValue)
{
currentTexCoords -= deltaTexCoords;
currentDepthMapValue = textureGrad(heights, currentTexCoords, dcoorddx, dcoorddy).b;
currentLayerDepth += layerDepth;
}
vec2 prevTexCoords = currentTexCoords + deltaTexCoords;
float afterDepth = currentDepthMapValue - currentLayerDepth;
float beforeDepth = textureGrad(heights, prevTexCoords, dcoorddx, dcoorddy).b - currentLayerDepth + layerDepth;
float weight = afterDepth / (afterDepth - beforeDepth);
return prevTexCoords * weight + currentTexCoords * (1.0 - weight);
#else
float height = textureGrad(heights, coord, dcoorddx, dcoorddy).b;
vec2 p = viewdir.xy / viewdir.z * (height * (param[2].y - param[2].z) * 0.2) * fade;
return coord - p;
#endif
}
// each texture at the coordinates shifted by the heights of its own normal map
#define TRACKBLEND_COORD(heights) parallax_coord(heights, f_coord, viewdir, dx, dy)
#else
#define TRACKBLEND_COORD(heights) f_coord
#endif
void main()
{
#ifdef TRACKBLEND_SPECGLOSS
@@ -93,15 +140,19 @@ void main()
// the gradients are taken outside the branches the textures are sampled in
vec2 dx = dFdx(f_coord);
vec2 dy = dFdy(f_coord);
#ifdef TRACKBLEND_PARALLAX
vec3 viewdir = normalize(vec3(0.0) - TangentFragPos);
#endif
#ifdef TRACKBLEND_GRAIN
vec2 grainpos = f_coord * GRAIN_CELLS;
// grains smaller than a pixel would shimmer, there the textures fade into each other instead
float footprint = max(length(dx), length(dy)) * GRAIN_CELLS;
float grained = 1.0 - smoothstep(0.35, 0.9, footprint);
#endif
vec4 tex_color = textureGrad(diffuse, f_coord, dx, dy);
vec2 coord = TRACKBLEND_COORD(normalmap);
vec4 tex_color = textureGrad(diffuse, coord, dx, dy);
#ifdef TRACKBLEND_NORMALMAP
vec4 tex_normal = textureGrad(normalmap, f_coord, dx, dy);
vec4 tex_normal = textureGrad(normalmap, coord, dx, dy);
#endif
if (total > 0.0)
{
@@ -120,24 +171,27 @@ void main()
#endif
if (weights.x > 0.0)
{
blended += textureGrad(diffuse1, f_coord, dx, dy) * weights.x;
vec2 coord1 = TRACKBLEND_COORD(normalmap1);
blended += textureGrad(diffuse1, coord1, dx, dy) * weights.x;
#ifdef TRACKBLEND_NORMALMAP
blendednormal += textureGrad(normalmap1, f_coord, dx, dy) * weights.x;
blendednormal += textureGrad(normalmap1, coord1, dx, dy) * weights.x;
#endif
}
if (weights.y > 0.0)
{
blended += textureGrad(diffuse2, f_coord, dx, dy) * weights.y;
vec2 coord2 = TRACKBLEND_COORD(normalmap2);
blended += textureGrad(diffuse2, coord2, dx, dy) * weights.y;
#ifdef TRACKBLEND_NORMALMAP
blendednormal += textureGrad(normalmap2, f_coord, dx, dy) * weights.y;
blendednormal += textureGrad(normalmap2, coord2, dx, dy) * weights.y;
#endif
}
#ifndef TRACKBLEND_SPECGLOSS
if (weights.z > 0.0)
{
blended += textureGrad(diffuse3, f_coord, dx, dy) * weights.z;
vec2 coord3 = TRACKBLEND_COORD(normalmap3);
blended += textureGrad(diffuse3, coord3, dx, dy) * weights.z;
#ifdef TRACKBLEND_NORMALMAP
blendednormal += textureGrad(normalmap3, f_coord, dx, dy) * weights.z;
blendednormal += textureGrad(normalmap3, coord3, dx, dy) * weights.z;
#endif
}
#endif
@@ -159,7 +213,7 @@ void main()
vec3 fragnormal = normalize(f_tbn * normalize(normal.xyz));
float reflectivity = param[1].z * tex_normal.a;
#ifdef TRACKBLEND_SPECGLOSS
vec4 tex_specgloss = textureGrad(specgloss, f_coord, dx, dy);
vec4 tex_specgloss = textureGrad(specgloss, coord, dx, dy);
float specularity = tex_specgloss.r;
glossiness = tex_specgloss.g * abs(param[1].w);
metalic = tex_specgloss.b;