Files
space_game/shaders/fs_cubes.sc
2026-01-29 17:06:34 +01:00

79 lines
2.1 KiB
Scala

$input tex_coord, world_pos
#include "bgfx_shader.sh"
#include "shaderlib.sh"
//layout(early_fragment_tests) in;
SAMPLER2D(texture_atlas_sampler, 0);
uniform vec4 u_cubes_compute_params;
// Ref: https://en.wikipedia.org/wiki/Blinn%E2%80%93Phong_reflection_model
const float lightPower = 40.0;
void main()
{
// vec4 col = texture2D(texture_atlas_sampler, tex_coord);
// // if(col.r + col.g + col.b == 0.0) discard; // playing with transparency
// vec3 normal = normalize(cross(dFdy(world_pos), dFdx(world_pos))); // screen space surface normal
// //col = vec4(normal, 1.0f);
// const float ambient = 0.2;
// const vec3 light_pos = vec3(-5.0, 5.0, -5.0);
// const vec3 light_col = vec3(1.0, 1.0, 1.0);
// vec3 light_dir = light_pos - world_pos;
// float squared_distance = light_dir.x*light_dir.x + light_dir.y*light_dir.y + light_dir.z*light_dir.z;
// light_dir = normalize(light_dir);
// vec4 diffuse = vec4(max(dot(light_dir, normal), 0) * light_col, 1.0);
// col = col * ambient + col * diffuse;
// // Note: if gl_FrontFacing is making problems
// // => https://stackoverflow.com/questions/24375171/is-there-a-reliable-alternative-to-gl-frontfacing-in-a-fragment-shader
// //if (gl_FrontFacing) {
// // col = vec4(1.0,0.0,0.0,1.0);
// //}else{
// // col = vec4(0.0,1.0,0.0,1.0);
// //}
// Calc id_coords (in id_atlas, for id sampling)
// in [0;ID_SIZE], is basically int
// id_coord = floor(id_coord);
// id_coord = id_coord +
// TODO use modf and combine floor/fract
// ! flooring may not be needed anyway
// just sample id_atlas with tex_coord
// Texturing rework V2
const uint IA_WIDTH = 2*8; // id's per row
const uint IA_HEIGHT = 2*8; // id's per column
const float ID_STRIDE_X = 1.0f / IA_WIDTH;
const float ID_STRIDE_Y = 1.0f / IA_HEIGHT;
// Calc tex_coords (in tex_atlas, for texture sampling)
// in [0;1] in local texture space
tex_coord = tex_coord * vec2(IA_WIDTH, IA_HEIGHT);
tex_coord = fract(tex_coord);
// vec4 col;
// if (tex_coord.x < 0.5)
// {
// col = vec4(1.0, 0.0, 0.0, 1.0);
// }
// else
// {
// col = vec4(0.0, 1.0, 0.0, 1.0);
// }
vec4 col = vec4(tex_coord, 0.0, 1.0);
gl_FragColor = col;
}