From 76fe38420cd8a4e31b9536b529f9d6cdfc359dc4 Mon Sep 17 00:00:00 2001 From: Paul Tolstoi Date: Fri, 30 Jan 2015 20:39:44 +0100 Subject: [PATCH] improved shader for team colored textures --- assets/shaders/teamcolors.frag.glsl | 76 +++++++---------------------- copying.md | 1 + cpp/game_main.cpp | 2 - cpp/main.cpp | 2 +- cpp/texture.cpp | 2 +- cpp/texture.h | 2 +- py/openage/convert/slp.py | 6 +-- 7 files changed, 25 insertions(+), 66 deletions(-) diff --git a/assets/shaders/teamcolors.frag.glsl b/assets/shaders/teamcolors.frag.glsl index fe411ebb22..d949802d78 100644 --- a/assets/shaders/teamcolors.frag.glsl +++ b/assets/shaders/teamcolors.frag.glsl @@ -1,79 +1,39 @@ #version 120 -//team color replacement shader +// team color replacement shader // -//looks for an alpha value specified by 'alpha_marker' -//and then replaces this pixel with the desired color, -//tinted for player_number, and using the given color as base. +// replaces alpha hinted colors by team colors based on the player_number -//the unmodified texture itself +// the unmodified texture itself uniform sampler2D texture; -//the desired player number the final resulting colors +// the desired player number the final resulting colors uniform int player_number; -//the alpha value which marks colors to be replaced -uniform float alpha_marker; - -//color entries for all players and their subcolors +// color entries for all players and their subcolors uniform vec4 player_color[64]; -//interpolated texture coordinates sent from vertex shader +// interpolated texture coordinates sent from vertex shader varying vec2 tex_position; -//create epsilon environment for float comparison +// create epsilon environment for float comparison const float epsilon = 0.001; -//do the lookup in the player color table -//for a playernumber (red, blue, etc) -//get the subcolor (brightness variations) -vec4 get_color(int playernum, int subcolor) { - return player_color[((playernum-1) * 8) + subcolor]; -} - -//compare color c1 to a reference color -bool is_color(vec4 c1, vec4 reference) { - if (all(greaterThanEqual(c1, reference - epsilon)) && all(lessThanEqual(c1, reference + epsilon))) { - return true; - } - else { - return false; - } -} - - void main() { - //get the texel from the uniform texture. + // get the texel from the uniform texture. vec4 pixel = texture2D(texture, tex_position); - //check if this texel has an alpha marker, so we can replace it's rgb values. - if (pixel[3] >= alpha_marker - epsilon && pixel[3] <= alpha_marker + epsilon) { - - //set alpha to 1 for the comparison - pixel[3] = 1.0; + // recalcuate shade of the player color from alpha + int subcolor = int(floor((pixel.a * 255.0 - 35.0) / 25)); + // get player color based on player and subcolor (shade) + vec4 player_color = player_color[((player_number - 1) * 8) + subcolor]; - //don't replace the colors if it's already player 1 (blue) - //as the media convert scripts generates blue-player sprites - if (player_number != 1) { - bool found = false; + // check if it's a unit color + float is_unit_color = floor(pixel.a + epsilon); - //try to find the base color, there are 8 of them. - for(int i = 0; i <= 7; i++) { - if (is_color(pixel, player_color[i])) { - //base color found, now replace it with the same color - //but player_number tinted. - pixel = get_color(player_number, i); - found = true; - break; - } - } - if (!found) { - //unknown base color gets pink muhahaha - pixel = vec4(255.0/255.0, 20.0/255.0, 147.0/255.0, 1.0); - } - } - } - //else the texel had no marker so we can just draw it without player coloring + // background is either transparent or the outline (which is nearly transparent) + float is_unit_visible = float(pixel.a > 5.0 / 255.0 + epsilon); - gl_FragColor = pixel; + // assign correct color + gl_FragColor = (pixel * is_unit_color + player_color * (1 - is_unit_color)) * is_unit_visible; } diff --git a/copying.md b/copying.md index dd8f4a8b28..333b65384f 100644 --- a/copying.md +++ b/copying.md @@ -44,6 +44,7 @@ _the openage authors_ are: | Michał Janiszewski | janisozaur | janisozaur+openage@gmail.com | | Lautaro Nahuel De León | lndl | laudleon@gmail.com | | Robin Kreis | rkreis | r.kreis@uni-bremen.de | +| Paul Tolstoi | ptolstoi, antodias | paul.tolstoi@gmail.com | If you're a first-time commiter, add yourself to the above list. This is not just for legal reasons, but also to keep an overview of all those nicknames. diff --git a/cpp/game_main.cpp b/cpp/game_main.cpp index 3b40f52431..a4d1c8408d 100644 --- a/cpp/game_main.cpp +++ b/cpp/game_main.cpp @@ -192,11 +192,9 @@ GameMain::GameMain(Engine *engine) teamcolor_shader::texture = teamcolor_shader::program->get_uniform_id("texture"); teamcolor_shader::tex_coord = teamcolor_shader::program->get_attribute_id("tex_coordinates"); teamcolor_shader::player_id_var = teamcolor_shader::program->get_uniform_id("player_number"); - teamcolor_shader::alpha_marker_var = teamcolor_shader::program->get_uniform_id("alpha_marker"); teamcolor_shader::player_color_var = teamcolor_shader::program->get_uniform_id("player_color"); teamcolor_shader::program->use(); glUniform1i(teamcolor_shader::texture, 0); - glUniform1f(teamcolor_shader::alpha_marker_var, 254.0/255.0); // fill the teamcolor shader's player color table: glUniform4fv(teamcolor_shader::player_color_var, 64, playercolors); teamcolor_shader::program->stopusing(); diff --git a/cpp/main.cpp b/cpp/main.cpp index cf896612bf..7353c04406 100644 --- a/cpp/main.cpp +++ b/cpp/main.cpp @@ -1,4 +1,4 @@ -// Copyright 2013-2014 the openage authors. See copying.md for legal info. +// Copyright 2013-2015 the openage authors. See copying.md for legal info. #include "main.h" diff --git a/cpp/texture.cpp b/cpp/texture.cpp index be73550fd1..d5a5163a96 100644 --- a/cpp/texture.cpp +++ b/cpp/texture.cpp @@ -26,7 +26,7 @@ GLint texture, tex_coord; namespace teamcolor_shader { shader::Program *program; GLint texture, tex_coord; -GLint player_id_var, alpha_marker_var, player_color_var; +GLint player_id_var, player_color_var; } namespace alphamask_shader { diff --git a/cpp/texture.h b/cpp/texture.h index 22d8b8585f..76ab3f00e7 100644 --- a/cpp/texture.h +++ b/cpp/texture.h @@ -26,7 +26,7 @@ extern GLint texture, tex_coord; namespace teamcolor_shader { extern shader::Program *program; extern GLint texture, tex_coord; -extern GLint player_id_var, alpha_marker_var, player_color_var; +extern GLint player_id_var, player_color_var; } // namespace teamcolor_shader namespace alphamask_shader { diff --git a/py/openage/convert/slp.py b/py/openage/convert/slp.py index ccc05ff141..77fefe50df 100644 --- a/py/openage/convert/slp.py +++ b/py/openage/convert/slp.py @@ -1,4 +1,4 @@ -# Copyright 2013-2014 the openage authors. See copying.md for legal info. +# Copyright 2013-2015 the openage authors. See copying.md for legal info. import os from struct import Struct, unpack_from @@ -539,9 +539,9 @@ def determine_rgba_matrix(image_matrix, palette, player_number=0): elif isinstance(pixel, SpecialColor): base_pcolor, is_outline = pixel.get_pcolor() if is_outline: - alpha = 253 # mark this pixel as outline + alpha = 5 # mark this pixel as outline else: - alpha = 254 # mark this pixel as player color + alpha = 35 + base_pcolor * 25 # mark this pixel as player color # get rgb base color from the color table # store it the preview player color