1
0
mirror of https://github.com/luanti-org/luanti.git synced 2025-11-16 14:45:30 +01:00

More minor shader cleanups

This commit is contained in:
sfan5
2025-11-12 17:48:36 +01:00
parent 4cbe7b2597
commit 00eea43131
7 changed files with 36 additions and 38 deletions

View File

@@ -4,8 +4,6 @@
uniform sampler2D baseTexture; uniform sampler2D baseTexture;
#endif #endif
VARYING_ vec3 vNormal;
VARYING_ vec3 vPosition;
CENTROID_ VARYING_ lowp vec4 varColor; CENTROID_ VARYING_ lowp vec4 varColor;
CENTROID_ VARYING_ mediump vec2 varTexCoord; CENTROID_ VARYING_ mediump vec2 varTexCoord;
CENTROID_ VARYING_ float varTexLayer; // actually int CENTROID_ VARYING_ float varTexLayer; // actually int

View File

@@ -1,5 +1,3 @@
VARYING_ vec3 vNormal;
VARYING_ vec3 vPosition;
CENTROID_ VARYING_ lowp vec4 varColor; CENTROID_ VARYING_ lowp vec4 varColor;
CENTROID_ VARYING_ mediump vec2 varTexCoord; CENTROID_ VARYING_ mediump vec2 varTexCoord;
CENTROID_ VARYING_ float varTexLayer; // actually int CENTROID_ VARYING_ float varTexLayer; // actually int
@@ -13,8 +11,6 @@ void main(void)
vec4 pos = inVertexPosition; vec4 pos = inVertexPosition;
gl_Position = mWorldViewProj * pos; gl_Position = mWorldViewProj * pos;
vPosition = gl_Position.xyz;
vNormal = inVertexNormal;
vec4 color = inVertexColor; vec4 color = inVertexColor;
varColor = clamp(color, 0.0, 1.0); varColor = clamp(color, 0.0, 1.0);

View File

@@ -42,7 +42,6 @@ uniform float crackTextureScale;
VARYING_ vec3 vNormal; VARYING_ vec3 vNormal;
VARYING_ vec3 vPosition;
// World position in the visible world (i.e. relative to the cameraOffset.) // World position in the visible world (i.e. relative to the cameraOffset.)
// This can be used for many shader effects without loss of precision. // This can be used for many shader effects without loss of precision.
// If the absolute position is required it can be calculated with // If the absolute position is required it can be calculated with
@@ -63,7 +62,8 @@ vec4 perm(vec4 x)
} }
// Corresponding gradient of snoise // Corresponding gradient of snoise
vec3 gnoise(vec3 p){ vec3 gnoise(vec3 p)
{
vec3 a = floor(p); vec3 a = floor(p);
vec3 d = p - a; vec3 d = p - a;
vec3 dd = 6.0 * d * (1.0 - d); vec3 dd = 6.0 * d * (1.0 - d);
@@ -96,7 +96,10 @@ vec3 gnoise(vec3 p){
} }
vec2 wave_noise(vec3 p, float off) { vec2 wave_noise(vec3 p, float off) {
return (gnoise(p + vec3(0.0, 0.0, off)) * 0.4 + gnoise(2.0 * p + vec3(0.0, off, off)) * 0.2 + gnoise(3.0 * p + vec3(0.0, off, off)) * 0.225 + gnoise(4.0 * p + vec3(-off, off, 0.0)) * 0.2).xz; return (gnoise(p + vec3(0.0, 0.0, off)) * 0.4 +
gnoise(2.0 * p + vec3(0.0, off, off)) * 0.2 +
gnoise(3.0 * p + vec3(0.0, off, off)) * 0.225 +
gnoise(4.0 * p + vec3(-off, off, 0.0)) * 0.2).xz;
} }
#endif #endif
@@ -110,13 +113,16 @@ vec3 getLightSpacePosition()
{ {
return shadow_position * 0.5 + 0.5; return shadow_position * 0.5 + 0.5;
} }
// custom smoothstep implementation because it's not defined in glsl1.2
// https://docs.gl/sl4/smoothstep #if __VERSION__ >= 130
#define mtsmoothstep smoothstep
#else
float mtsmoothstep(in float edge0, in float edge1, in float x) float mtsmoothstep(in float edge0, in float edge1, in float x)
{ {
float t = clamp((x - edge0) / (edge1 - edge0), 0.0, 1.0); float t = clamp((x - edge0) / (edge1 - edge0), 0.0, 1.0);
return t * t * (3.0 - 2.0 * t); return t * t * (3.0 - 2.0 * t);
} }
#endif
float shadowCutoff(float x) { float shadowCutoff(float x) {
#if defined(ENABLE_TRANSLUCENT_FOLIAGE) && MATERIAL_TYPE == TILE_MATERIAL_WAVING_LEAVES #if defined(ENABLE_TRANSLUCENT_FOLIAGE) && MATERIAL_TYPE == TILE_MATERIAL_WAVING_LEAVES

View File

@@ -7,7 +7,6 @@ uniform highp vec3 cameraOffset;
uniform float animationTimer; uniform float animationTimer;
VARYING_ vec3 vNormal; VARYING_ vec3 vNormal;
VARYING_ vec3 vPosition;
// World position in the visible world (i.e. relative to the cameraOffset.) // World position in the visible world (i.e. relative to the cameraOffset.)
// This can be used for many shader effects without loss of precision. // This can be used for many shader effects without loss of precision.
// If the absolute position is required it can be calculated with // If the absolute position is required it can be calculated with
@@ -43,14 +42,13 @@ CENTROID_ VARYING_ float nightRatio;
VARYING_ highp vec3 eyeVec; VARYING_ highp vec3 eyeVec;
// Color of the light emitted by the light sources. // Color of the light emitted by the light sources.
const vec3 artificialLight = vec3(1.04, 1.04, 1.04); const vec3 artificialLight = vec3(1.04, 1.04, 1.04);
const float e = 2.718281828459;
const float BS = 10.0; #ifdef ENABLE_DYNAMIC_SHADOWS
uniform float xyPerspectiveBias0; uniform float xyPerspectiveBias0;
uniform float xyPerspectiveBias1; uniform float xyPerspectiveBias1;
uniform float zPerspectiveBias; uniform float zPerspectiveBias;
#ifdef ENABLE_DYNAMIC_SHADOWS
vec4 getRelativePosition(in vec4 position) vec4 getRelativePosition(in vec4 position)
{ {
vec2 l = position.xy - CameraPos.xy; vec2 l = position.xy - CameraPos.xy;
@@ -77,14 +75,16 @@ vec4 applyPerspectiveDistortion(in vec4 position)
return position; return position;
} }
// custom smoothstep implementation because it's not defined in glsl1.2 #if __VERSION__ >= 130
// https://docs.gl/sl4/smoothstep #define mtsmoothstep smoothstep
#else
float mtsmoothstep(in float edge0, in float edge1, in float x) float mtsmoothstep(in float edge0, in float edge1, in float x)
{ {
float t = clamp((x - edge0) / (edge1 - edge0), 0.0, 1.0); float t = clamp((x - edge0) / (edge1 - edge0), 0.0, 1.0);
return t * t * (3.0 - 2.0 * t); return t * t * (3.0 - 2.0 * t);
} }
#endif #endif
#endif
float smoothCurve(float x) float smoothCurve(float x)
@@ -189,7 +189,6 @@ void main(void)
worldPosition = (mWorld * pos).xyz; worldPosition = (mWorld * pos).xyz;
gl_Position = mWorldViewProj * pos; gl_Position = mWorldViewProj * pos;
vPosition = gl_Position.xyz;
eyeVec = -(mWorldView * pos).xyz; eyeVec = -(mWorldView * pos).xyz;
#ifdef SECONDSTAGE #ifdef SECONDSTAGE
normalPass = normalize((inVertexNormal+1)/2); normalPass = normalize((inVertexNormal+1)/2);

View File

@@ -35,7 +35,6 @@ uniform float animationTimer;
VARYING_ vec3 vNormal; VARYING_ vec3 vNormal;
VARYING_ vec3 vPosition;
// World position in the visible world (i.e. relative to the cameraOffset.) // World position in the visible world (i.e. relative to the cameraOffset.)
// This can be used for many shader effects without loss of precision. // This can be used for many shader effects without loss of precision.
// If the absolute position is required it can be calculated with // If the absolute position is required it can be calculated with
@@ -62,13 +61,16 @@ vec3 getLightSpacePosition()
{ {
return shadow_position * 0.5 + 0.5; return shadow_position * 0.5 + 0.5;
} }
// custom smoothstep implementation because it's not defined in glsl1.2
// https://docs.gl/sl4/smoothstep #if __VERSION__ >= 130
#define mtsmoothstep smoothstep
#else
float mtsmoothstep(in float edge0, in float edge1, in float x) float mtsmoothstep(in float edge0, in float edge1, in float x)
{ {
float t = clamp((x - edge0) / (edge1 - edge0), 0.0, 1.0); float t = clamp((x - edge0) / (edge1 - edge0), 0.0, 1.0);
return t * t * (3.0 - 2.0 * t); return t * t * (3.0 - 2.0 * t);
} }
#endif
#ifdef COLORED_SHADOWS #ifdef COLORED_SHADOWS
@@ -363,7 +365,6 @@ float getShadow(sampler2D shadowsampler, vec2 smTexCoord, float realDistance)
void main(void) void main(void)
{ {
vec3 color;
vec2 uv = varTexCoord.st; vec2 uv = varTexCoord.st;
#ifdef USE_ARRAY_TEXTURE #ifdef USE_ARRAY_TEXTURE
@@ -382,8 +383,7 @@ void main(void)
discard; discard;
#endif #endif
color = base.rgb; vec4 col = vec4(base.rgb * varColor.rgb, 1.0);
vec4 col = vec4(color.rgb * varColor.rgb, 1.0);
col.rgb *= vIDiff; col.rgb *= vIDiff;
#ifdef ENABLE_DYNAMIC_SHADOWS #ifdef ENABLE_DYNAMIC_SHADOWS

View File

@@ -4,7 +4,6 @@ uniform float animationTimer;
uniform lowp vec4 materialColor; uniform lowp vec4 materialColor;
VARYING_ vec3 vNormal; VARYING_ vec3 vNormal;
VARYING_ vec3 vPosition;
VARYING_ vec3 worldPosition; VARYING_ vec3 worldPosition;
VARYING_ lowp vec4 varColor; VARYING_ lowp vec4 varColor;
CENTROID_ VARYING_ mediump vec2 varTexCoord; CENTROID_ VARYING_ mediump vec2 varTexCoord;
@@ -32,14 +31,13 @@ VARYING_ float nightRatio;
// Color of the light emitted by the light sources. // Color of the light emitted by the light sources.
const vec3 artificialLight = vec3(1.04, 1.04, 1.04); const vec3 artificialLight = vec3(1.04, 1.04, 1.04);
VARYING_ float vIDiff; VARYING_ float vIDiff;
const float e = 2.718281828459;
const float BS = 10.0; #ifdef ENABLE_DYNAMIC_SHADOWS
uniform float xyPerspectiveBias0; uniform float xyPerspectiveBias0;
uniform float xyPerspectiveBias1; uniform float xyPerspectiveBias1;
uniform float zPerspectiveBias; uniform float zPerspectiveBias;
#ifdef ENABLE_DYNAMIC_SHADOWS
vec4 getRelativePosition(in vec4 position) vec4 getRelativePosition(in vec4 position)
{ {
vec2 l = position.xy - CameraPos.xy; vec2 l = position.xy - CameraPos.xy;
@@ -66,8 +64,9 @@ vec4 applyPerspectiveDistortion(in vec4 position)
return position; return position;
} }
// custom smoothstep implementation because it's not defined in glsl1.2 #if __VERSION__ >= 130
// https://docs.gl/sl4/smoothstep #define mtsmoothstep smoothstep
#else
float mtsmoothstep(in float edge0, in float edge1, in float x) float mtsmoothstep(in float edge0, in float edge1, in float x)
{ {
float t = clamp((x - edge0) / (edge1 - edge0), 0.0, 1.0); float t = clamp((x - edge0) / (edge1 - edge0), 0.0, 1.0);
@@ -75,6 +74,8 @@ float mtsmoothstep(in float edge0, in float edge1, in float x)
} }
#endif #endif
#endif
float directional_ambient(vec3 normal) float directional_ambient(vec3 normal)
{ {
@@ -95,7 +96,6 @@ void main(void)
gl_Position = mWorldViewProj * inVertexPosition; gl_Position = mWorldViewProj * inVertexPosition;
vPosition = gl_Position.xyz;
vNormal = (mWorld * vec4(inVertexNormal, 0.0)).xyz; vNormal = (mWorld * vec4(inVertexNormal, 0.0)).xyz;
worldPosition = (mWorld * inVertexPosition).xyz; worldPosition = (mWorld * inVertexPosition).xyz;
eyeVec = -(mWorldView * inVertexPosition).xyz; eyeVec = -(mWorldView * inVertexPosition).xyz;

View File

@@ -696,6 +696,9 @@ void ShaderSource::generateShader(ShaderInfo &shaderinfo)
shaders_header << "#version 100\n" shaders_header << "#version 100\n"
<< "#define CENTROID_\n"; << "#define CENTROID_\n";
} }
// Precision is only meaningful on GLES
shaders_header << "precision mediump float;\n"
"precision mediump sampler2D;\n";
} else { } else {
assert(false); assert(false);
} }
@@ -708,8 +711,6 @@ void ShaderSource::generateShader(ShaderInfo &shaderinfo)
// cf. EVertexAttributes.h for the predefined ones // cf. EVertexAttributes.h for the predefined ones
vertex_header = R"( vertex_header = R"(
precision mediump float;
uniform highp mat4 mWorldView; uniform highp mat4 mWorldView;
uniform highp mat4 mWorldViewProj; uniform highp mat4 mWorldViewProj;
uniform mediump mat4 mTexture; uniform mediump mat4 mTexture;
@@ -732,9 +733,7 @@ void ShaderSource::generateShader(ShaderInfo &shaderinfo)
// normally expects, so we need to take that into account. // normally expects, so we need to take that into account.
vertex_header += "#define inVertexColor (inVertexColor.bgra)\n"; vertex_header += "#define inVertexColor (inVertexColor.bgra)\n";
fragment_header = R"( fragment_header = "";
precision mediump float;
)";
if (use_glsl3) { if (use_glsl3) {
fragment_header += "#define VARYING_ in\n" fragment_header += "#define VARYING_ in\n"
"#define gl_FragColor outFragColor\n" "#define gl_FragColor outFragColor\n"