From 5a98bf1ef08fb97394b6e21c85e632fb492f3734 Mon Sep 17 00:00:00 2001 From: Marco Cawthorne Date: Wed, 20 Nov 2024 14:15:27 -0800 Subject: [PATCH] HL2: Quick support for $envmaptint and $envmapsaturation --- plugins/hl2/glsl/vmt/lightmapped.glsl | 21 ++++++- plugins/hl2/glsl/vmt/vertexlit.glsl | 24 +++++++- plugins/hl2/mat_vmt.c | 81 ++++++++++++++++++++++----- plugins/hl2/mat_vmt_progs.h | 45 ++++++++++++++- 4 files changed, 151 insertions(+), 20 deletions(-) diff --git a/plugins/hl2/glsl/vmt/lightmapped.glsl b/plugins/hl2/glsl/vmt/lightmapped.glsl index fa46ab131..f8f5077f2 100644 --- a/plugins/hl2/glsl/vmt/lightmapped.glsl +++ b/plugins/hl2/glsl/vmt/lightmapped.glsl @@ -20,6 +20,14 @@ !!cvardf r_glsl_pcf !!samps =FAKESHADOWS shadowmap +#ifndef ENVTINT +#define ENVTINT 1.0,1.0,1.0 +#endif + +#ifndef ENVSAT +#define ENVSAT 1.0,1.0,1.0 +#endif + #include "sys/defs.h" varying vec2 tex_c; @@ -104,6 +112,11 @@ varying vec2 lm1, lm2, lm3; return lightmaps; } + vec3 env_saturation(vec3 rgb, float adjustment) { + vec3 intensity = vec3(dot(rgb, vec3(0.2126,0.7152,0.0722))); + return mix(intensity, rgb, adjustment); + } + void main (void) { vec4 diffuse_f; @@ -155,9 +168,15 @@ varying vec2 lm1, lm2, lm3; vec3 cube_c = reflect(-eyevector, normal_f.rgb); + vec3 cube_tint = vec3(ENVTINT); + vec3 cube_sat = vec3(ENVSAT); cube_c = cube_c.x * invsurface[0] + cube_c.y * invsurface[1] + cube_c.z * invsurface[2]; cube_c = (m_model * vec4(cube_c.xyz, 0.0)).xyz; - diffuse_f.rgb += (textureCube(s_reflectcube, cube_c).rgb * vec3(refl,refl,refl)); + vec3 cube_t = env_saturation(textureCube(s_reflectcube, cube_c).rgb, cube_sat.r); + cube_t.r *= cube_tint.r; + cube_t.g *= cube_tint.g; + cube_t.b *= cube_tint.b; + diffuse_f.rgb += (cube_t * vec3(refl,refl,refl)); #endif #ifdef FULLBRIGHT diff --git a/plugins/hl2/glsl/vmt/vertexlit.glsl b/plugins/hl2/glsl/vmt/vertexlit.glsl index 027a57e24..b89ad6d14 100644 --- a/plugins/hl2/glsl/vmt/vertexlit.glsl +++ b/plugins/hl2/glsl/vmt/vertexlit.glsl @@ -16,7 +16,6 @@ // envmaps only !!samps =REFLECTCUBEMASK reflectmask reflectcube - !!cvardf r_skipDiffuse #include "sys/defs.h" @@ -29,8 +28,18 @@ varying vec4 light; #ifdef REFLECTCUBEMASK varying vec3 eyevector; varying mat3 invsurface; + #endif +#ifndef ENVTINT +#define ENVTINT 1.0,1.0,1.0 +#endif + +#ifndef ENVSAT +#define ENVSAT 1.0 +#endif + + #ifdef FAKESHADOWS varying vec4 vtexprojcoord; #endif @@ -93,6 +102,11 @@ varying vec4 light; #include "sys/fog.h" #include "sys/pcf.h" + vec3 env_saturation(vec3 rgb, float adjustment) { + vec3 intensity = vec3(dot(rgb, vec3(0.2126,0.7152,0.0722))); + return mix(intensity, rgb, adjustment); + } + void main (void) { vec4 diffuse_f = texture2D(s_diffuse, tex_c); @@ -133,9 +147,15 @@ varying vec4 light; #endif vec3 cube_c = reflect(-eyevector, normal_f.rgb); + vec3 cube_tint = vec3(ENVTINT); + vec3 cube_sat = vec3(ENVSAT); cube_c = cube_c.x * invsurface[0] + cube_c.y * invsurface[1] + cube_c.z * invsurface[2]; cube_c = (m_model * vec4(cube_c.xyz, 0.0)).xyz; - diffuse_f.rgb += (textureCube(s_reflectcube, cube_c).rgb * vec3(refl,refl,refl)); + vec3 cube_t = env_saturation(textureCube(s_reflectcube, cube_c).rgb, cube_sat.r); + cube_t.r *= cube_tint.r; + cube_t.g *= cube_tint.g; + cube_t.b *= cube_tint.b; + diffuse_f.rgb += (cube_t * vec3(refl,refl,refl)); #endif diffuse_f.rgb *= light.rgb * e_colourident.rgb; diff --git a/plugins/hl2/mat_vmt.c b/plugins/hl2/mat_vmt.c index b89b27870..8d94ced6b 100644 --- a/plugins/hl2/mat_vmt.c +++ b/plugins/hl2/mat_vmt.c @@ -24,6 +24,12 @@ typedef struct char envfromnorm; char halflambert; + float envmaptint_r; + float envmaptint_g; + float envmaptint_b; + float envmapsat_r; + float envmapsat_g; + float envmapsat_b; float alphatestref; char *blendfunc; qboolean alphatest; @@ -166,10 +172,7 @@ static char *VMT_ParseBlock(const char *fname, vmtstate_t *st, char *line) else if (!Q_strcasecmp(key, "$basetexturetransform")) ; else if (!Q_strcasecmp(key, "$bumpmap")) // same as normalmap ~eukara - { Q_strlcpy(st->normalmap, value, sizeof(st->normalmap)); - Con_DPrintf("%s: %s \"%s\"\n", fname, key, value); - } else if (!Q_strcasecmp(key, "$dudvmap")) // refractions only { Q_strlcpy(st->dudvmap, value, sizeof(st->dudvmap)); @@ -237,9 +240,39 @@ static char *VMT_ParseBlock(const char *fname, vmtstate_t *st, char *line) else if (!Q_strcasecmp(key, "$envmapcontrast")) Con_DPrintf("%s: %s \"%s\"\n", fname, key, value); else if (!Q_strcasecmp(key, "$envmaptint")) - Con_DPrintf("%s: %s \"%s\"\n", fname, key, value); + { + char tok[64]; + char *tintline; + tintline = cmdfuncs->ParsePunctuation(value, "[", tok, sizeof(tok), 0); + + if (!strcmp(tok, "[")) { + tintline = cmdfuncs->ParseToken(tintline, tok, sizeof(tok), 0); + st->envmaptint_r = strtof(tok, NULL); + tintline = cmdfuncs->ParseToken(tintline, tok, sizeof(tok), 0); + st->envmaptint_g = strtof(tok, NULL); + tintline = cmdfuncs->ParsePunctuation(tintline, "]", tok, sizeof(tok), 0); + st->envmaptint_b = strtof(tok, NULL); + } else { + st->envmaptint_r = st->envmaptint_g = st->envmaptint_b = atof(value); + } + } else if (!Q_strcasecmp(key, "$envmapsaturation")) - Con_DPrintf("%s: %s \"%s\"\n", fname, key, value); + { + char tok[64]; + char *tintline; + tintline = cmdfuncs->ParsePunctuation(value, "[", tok, sizeof(tok), 0); + + if (!strcmp(tok, "[")) { + tintline = cmdfuncs->ParseToken(tintline, tok, sizeof(tok), 0); + st->envmapsat_r = strtof(tok, NULL); + tintline = cmdfuncs->ParseToken(tintline, tok, sizeof(tok), 0); + st->envmapsat_g = strtof(tok, NULL); + tintline = cmdfuncs->ParsePunctuation(tintline, "]", tok, sizeof(tok), 0); + st->envmapsat_b = strtof(tok, NULL); + } else { + st->envmapsat_r = st->envmapsat_g = st->envmapsat_b = atof(value); + } + } else if (!Q_strcasecmp(key, "$basealphaenvmapmask")) st->envfrombase=1; else if (!Q_strcasecmp(key, "$normalmapalphaenvmapmask")) @@ -346,6 +379,8 @@ static void Shader_GenerateFromVMT(parsestate_t *ps, vmtstate_t *st, const char { size_t offset = 0; char script[8192]; + char envmaptint[128]; + char envmapsat[128]; char *progargs = ""; if (!*st->tex[0].name) //fill in a default... @@ -357,6 +392,20 @@ static void Shader_GenerateFromVMT(parsestate_t *ps, vmtstate_t *st, const char if (st->nofog) progargs = "#NOFOG"; + /* FIXME: check proper */ + if (st->envmaptint_b > 0.0f) { + Q_snprintfz(envmaptint, sizeof(envmaptint), "#ENVTINT=%f,%f,%f", st->envmaptint_r, st->envmaptint_g, st->envmaptint_b); + } else { + Q_snprintfz(envmaptint, sizeof(envmaptint), ""); + } + + /* FIXME: check proper */ + if (st->envmapsat_r > 0.0f) { + Q_snprintfz(envmapsat, sizeof(envmapsat), "#ENVSAT=%f,%f,%f", st->envmapsat_r, st->envmapsat_g, st->envmapsat_b); + } else { + Q_snprintfz(envmapsat, sizeof(envmapsat), ""); + } + Q_strlcatfz(script, &offset, sizeof(script), "\n"); if (st->nodraw) @@ -433,12 +482,16 @@ static void Shader_GenerateFromVMT(parsestate_t *ps, vmtstate_t *st, const char } else if (!Q_strcasecmp(st->type, "DecalModulate")) { - Q_strlcatfz(script, &offset, sizeof(script), - "\t{\n" - "\t\tprogram \"vmt/vertexlit%s\"\n" - "\t\tblendFunc gl_dst_color gl_one_minus_src_alpha\n" - "\t}\n", progargs); - Q_strlcatfz(script, &offset, sizeof(script), "\tdiffusemap \"%s%s.vtf\"\n", strcmp(st->tex[0].name, "materials/")?"materials/":"", st->tex[0].name); + Q_strlcatfz(script, &offset, sizeof(script), "\t{\n"); + Q_strlcatfz(script, &offset, sizeof(script), "\t\tmap \"%s%s.vtf\"\n", strcmp(st->tex[0].name, "materials/")?"materials/":"", st->tex[0].name); + + if (!st->mod2x) { + Q_strlcatfz(script, &offset, sizeof(script),"\t\tblendFunc gl_dst_color gl_src_color\n"); + } else { + Q_strlcatfz(script, &offset, sizeof(script),"\t\tblendFunc gl_dst_color gl_one_minus_src_alpha\n"); + } + + Q_strlcatfz(script, &offset, sizeof(script), "\t}\n"); Q_strlcatfz(script, &offset, sizeof(script), "\tpolygonOffset 1\n"); } else if (!Q_strcasecmp(st->type, "Modulate")) @@ -447,7 +500,7 @@ static void Shader_GenerateFromVMT(parsestate_t *ps, vmtstate_t *st, const char Q_strlcatfz(script, &offset, sizeof(script), "\t{\n"); Q_strlcatfz(script, &offset, sizeof(script), "\t\tmap \"%s%s.vtf\"\n", strcmp(st->tex[0].name, "materials/")?"materials/":"", st->tex[0].name); - if (st->mod2x) { + if (!st->mod2x) { Q_strlcatfz(script, &offset, sizeof(script),"\t\tblendFunc gl_dst_color gl_src_color\n"); } else { Q_strlcatfz(script, &offset, sizeof(script),"\t\tblendFunc gl_dst_color gl_one_minus_src_alpha\n"); @@ -519,7 +572,7 @@ static void Shader_GenerateFromVMT(parsestate_t *ps, vmtstate_t *st, const char Q_strlcpy(st->type, "vmt/vertexlit", sizeof(st->type)); } - Q_strlcatfz(script, &offset, sizeof(script), "\tprogram \"%s%s\"\n", st->type, progargs); + Q_strlcatfz(script, &offset, sizeof(script), "\tprogram \"%s%s%s%s\"\n", st->type, progargs, envmaptint, envmapsat); } Q_strlcatfz(script, &offset, sizeof(script), "\tdiffusemap \"%s%s.vtf\"\n", strcmp(st->tex[0].name, "materials/")?"materials/":"", st->tex[0].name); @@ -556,7 +609,7 @@ static void Shader_GenerateFromVMT(parsestate_t *ps, vmtstate_t *st, const char else /* take from normalmap */ Q_strlcpy(st->type, "vmt/lightmapped", sizeof(st->type)); - Q_strlcatfz(script, &offset, sizeof(script), "\tprogram \"%s%s\"\n", st->type, progargs); + Q_strlcatfz(script, &offset, sizeof(script), "\tprogram \"%s%s%s%s\"\n", st->type, progargs, envmaptint, envmapsat); } Q_strlcatfz(script, &offset, sizeof(script), "\tdiffusemap \"%s%s.vtf\"\n", strcmp(st->tex[0].name, "materials/")?"materials/":"", st->tex[0].name); diff --git a/plugins/hl2/mat_vmt_progs.h b/plugins/hl2/mat_vmt_progs.h index 0f4f54709..c179045f4 100644 --- a/plugins/hl2/mat_vmt_progs.h +++ b/plugins/hl2/mat_vmt_progs.h @@ -27,6 +27,14 @@ YOU SHOULD NOT EDIT THIS FILE BY HAND "!!cvardf r_glsl_pcf\n" "!!samps =FAKESHADOWS shadowmap\n" +"#ifndef ENVTINT\n" +"#define ENVTINT 1.0,1.0,1.0\n" +"#endif\n" + +"#ifndef ENVSAT\n" +"#define ENVSAT 1.0,1.0,1.0\n" +"#endif\n" + "#include \"sys/defs.h\"\n" "varying vec2 tex_c;\n" @@ -111,6 +119,11 @@ YOU SHOULD NOT EDIT THIS FILE BY HAND "return lightmaps;\n" "}\n" +"vec3 env_saturation(vec3 rgb, float adjustment) {\n" +"vec3 intensity = vec3(dot(rgb, vec3(0.2126,0.7152,0.0722)));\n" +"return mix(intensity, rgb, adjustment);\n" +"}\n" + "void main (void)\n" "{\n" "vec4 diffuse_f;\n" @@ -162,9 +175,15 @@ YOU SHOULD NOT EDIT THIS FILE BY HAND "vec3 cube_c = reflect(-eyevector, normal_f.rgb);\n" +"vec3 cube_tint = vec3(ENVTINT);\n" +"vec3 cube_sat = vec3(ENVSAT);\n" "cube_c = cube_c.x * invsurface[0] + cube_c.y * invsurface[1] + cube_c.z * invsurface[2];\n" "cube_c = (m_model * vec4(cube_c.xyz, 0.0)).xyz;\n" -"diffuse_f.rgb += (textureCube(s_reflectcube, cube_c).rgb * vec3(refl,refl,refl));\n" +"vec3 cube_t = env_saturation(textureCube(s_reflectcube, cube_c).rgb, cube_sat.r);\n" +"cube_t.r *= cube_tint.r;\n" +"cube_t.g *= cube_tint.g;\n" +"cube_t.b *= cube_tint.b;\n" +"diffuse_f.rgb += (cube_t * vec3(refl,refl,refl));\n" "#endif\n" "#ifdef FULLBRIGHT\n" @@ -460,7 +479,6 @@ YOU SHOULD NOT EDIT THIS FILE BY HAND // envmaps only "!!samps =REFLECTCUBEMASK reflectmask reflectcube\n" - "!!cvardf r_skipDiffuse\n" "#include \"sys/defs.h\"\n" @@ -473,8 +491,18 @@ YOU SHOULD NOT EDIT THIS FILE BY HAND "#ifdef REFLECTCUBEMASK\n" "varying vec3 eyevector;\n" "varying mat3 invsurface;\n" + "#endif\n" +"#ifndef ENVTINT\n" +"#define ENVTINT 1.0,1.0,1.0\n" +"#endif\n" + +"#ifndef ENVSAT\n" +"#define ENVSAT 1.0\n" +"#endif\n" + + "#ifdef FAKESHADOWS\n" "varying vec4 vtexprojcoord;\n" "#endif\n" @@ -537,6 +565,11 @@ YOU SHOULD NOT EDIT THIS FILE BY HAND "#include \"sys/fog.h\"\n" "#include \"sys/pcf.h\"\n" +"vec3 env_saturation(vec3 rgb, float adjustment) {\n" +"vec3 intensity = vec3(dot(rgb, vec3(0.2126,0.7152,0.0722)));\n" +"return mix(intensity, rgb, adjustment);\n" +"}\n" + "void main (void)\n" "{\n" "vec4 diffuse_f = texture2D(s_diffuse, tex_c);\n" @@ -577,9 +610,15 @@ YOU SHOULD NOT EDIT THIS FILE BY HAND "#endif\n" "vec3 cube_c = reflect(-eyevector, normal_f.rgb);\n" +"vec3 cube_tint = vec3(ENVTINT);\n" +"vec3 cube_sat = vec3(ENVSAT);\n" "cube_c = cube_c.x * invsurface[0] + cube_c.y * invsurface[1] + cube_c.z * invsurface[2];\n" "cube_c = (m_model * vec4(cube_c.xyz, 0.0)).xyz;\n" -"diffuse_f.rgb += (textureCube(s_reflectcube, cube_c).rgb * vec3(refl,refl,refl));\n" +"vec3 cube_t = env_saturation(textureCube(s_reflectcube, cube_c).rgb, cube_sat.r);\n" +"cube_t.r *= cube_tint.r;\n" +"cube_t.g *= cube_tint.g;\n" +"cube_t.b *= cube_tint.b;\n" +"diffuse_f.rgb += (cube_t * vec3(refl,refl,refl));\n" "#endif\n" "diffuse_f.rgb *= light.rgb * e_colourident.rgb;\n"