Working on fog shader

This commit is contained in:
Vladislav Khorev committed 2026-01-02 21:16:44 +03:00
1 parent 2b40c13c9d
commit c9178cff3a
7 files changed
+232 -99

No files matched your search

+65 -45
View File
@@ -1,57 +1,77 @@
// ---Фрагментный шейдер (Fragment Shader)
// --- Улучшенный Фрагментный шейдер
varying vec2 TexCoord;
uniform sampler2D Texture;
uniform float uDistanceToPlanetSurface;
const vec4 FOG_COLOR = vec4(0.0, 0.5, 1.0, 1.0); // Синий туман
varying vec3 worldPosition;
uniform sampler2D Texture;
uniform float uDistanceToPlanetSurface;
uniform vec3 uViewPos;
const vec4 FOG_COLOR = vec4(0.0, 0.5, 1.0, 1.0);
void main()
{
vec4 textureColor = texture2D(Texture, TexCoord);
float realDist = distance(worldPosition, uViewPos);
float h = uDistanceToPlanetSurface;
vec4 textureColor = texture2D(Texture, TexCoord);
vec3 finalColor = textureColor.rgb;
float realDist = distance(worldPosition, uViewPos);
float alphaFactor = clamp((2000 - realDist) / 500.0, 0.0, 1.0);
float fogStart;
float fogEnd;
float fogFactor;
if (uDistanceToPlanetSurface > 1000)
{
gl_FragColor = vec4(finalColor.rgb, 1.0);
// --- Логика AlphaFactor (строго 2000-2500) ---
// Объект полностью исчезает на 2500.
// Если fogStart на высоте 1000 равен 2500, то туман и объект исчезнут одновременно.
float alphaFactor = clamp((2000.0 - realDist) / 500.0, 0.0, 1.0);
// --- Логика расчета границ тумана ---
if (h >= 1000.0) {
gl_FragColor = vec4(textureColor.rgb, alphaFactor);
}
else
{
if (h > 100.0) {
// Нормализуем высоту от 100 до 1000 (t: 0 -> 1)
float t = (h - 100.0) / 900.0;
// На высоте 100 туман начинается со 100.
// К высоте 1000 он плавно "убегает" к границе 2500, где объект исчезает.
fogStart = mix(1500.0, 15000.0, t);
// Держим ширину зоны тумана в пределах 400-600 единиц
float fogRange = mix(1000.0, 6000.0, t);
fogEnd = fogStart + fogRange;
}
else if (h > 40.0) {
float t = (h - 40.0) / 60.0;
// На высоте 100 туман начинается со 100.
// К высоте 1000 он плавно "убегает" к границе 2500, где объект исчезает.
fogStart = mix(800.0, 1500.0, t);
fogEnd = mix(1000.0, 2500.0, t);
}
else if (h > 20.0) {
float t = (h - 20.0) / 20.0;
fogStart = mix(200.0, 800.0, t);
fogEnd = mix(500.0, 1000.0, t);
}
else {
// Минимумы при h = 0: start 25, end 125
float t = clamp(h / 20.0, 0.0, 1.0);
fogStart = mix(100.0, 200.0, t);
fogEnd = mix(250.0, 500.0, t);
}
// Расчет фактора тумана
float fogRange = max(fogEnd - fogStart, 1.0);
float fogFactor = clamp((realDist - fogStart) / fogRange, 0.0, 1.0);
// --- Смешивание ---
vec3 mixedColor = mix(textureColor.rgb, FOG_COLOR.rgb, fogFactor);
gl_FragColor = vec4(mixedColor, alphaFactor);
}
else if (uDistanceToPlanetSurface > 100)
{
float t = clamp((uDistanceToPlanetSurface - 100) / 900.0, 0.0, 1.0); // 1 upstairs, 0 downstairs
fogFactor = clamp((realDist - 2400) / (300.0*(1 + 10*t)), 0.0, 1.0);
gl_FragColor = mix(vec4(finalColor.rgb, alphaFactor), FOG_COLOR, fogFactor*(1.0 - t));
}
else if (uDistanceToPlanetSurface > 40)
{
//From 100 to 40:
//(1000 < realDist < 1800)
float t = clamp((uDistanceToPlanetSurface - 40) / 60.0, 0.0, 1.0); // 1 upstairs, 0 downstairs
fogFactor = clamp((realDist - 2400) / 300.0, 0.0, 1.0); // old fog factor
float fogFactor2 = clamp((realDist - 1000) / 800.0, 0.0, 1.0);
gl_FragColor = mix(vec4(finalColor.rgb, alphaFactor), FOG_COLOR, max(fogFactor, fogFactor2*(1.0 - t)));
}
else
{
fogFactor = clamp((realDist - 1000) / (800.0), 0.0, 1.0);
gl_FragColor = mix(vec4(finalColor.rgb, alphaFactor), FOG_COLOR, fogFactor);
}
//gl_FragColor = vec4(finalColor.rgb, 1.0);
}