Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improved shader for team colored textures #214

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
76 changes: 18 additions & 58 deletions assets/shaders/teamcolors.frag.glsl
Original file line number Diff line number Diff line change
@@ -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;
}
1 change: 1 addition & 0 deletions copying.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 0 additions & 2 deletions cpp/game_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
2 changes: 1 addition & 1 deletion cpp/main.cpp
Original file line number Diff line number Diff line change
@@ -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"

Expand Down
2 changes: 1 addition & 1 deletion cpp/texture.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion cpp/texture.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions py/openage/convert/slp.py
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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
Expand Down