add block material shaders

- 'shader' property of block materials: shaders/materials/<name>.glslv, .glslf with material_vertex / material_fragment hooks and #param uniforms
- material shaders code is included into main, translucent and shadows programs through generated headers, no extra draw calls
- chunk vertex normal.w byte replaced with 'flags': bit 7 - emission flag, bits 0-6 - material shader index (vertex format size is not changed)
- doc/*/block-materials.md
This commit is contained in:
lookibed 2026-10-02 06:33:01 +03:00
parent f90750c066
commit 81489a13cf
33 changed files with 761 additions and 53 deletions

View file

@ -41,7 +41,7 @@ void main() {
a_fog = calc_fog(length(viewmodel * vec4(pos3d * FOG_POS_SCALE, 0.0)) / 256.0);
#endif
a_emission = v_normal.w;
a_emission = unpack_emission(v_normal.w);
vec4 viewmodelpos = u_view * a_modelpos;
a_position = viewmodelpos.xyz;

View file

@ -8,4 +8,13 @@ vec3 apply_planet_curvature(vec3 modelPos, vec3 pos3d) {
return modelPos;
}
// chunk vertex flags (normal.w): bit 7 - emission flag, bits 0-6 - material
float unpack_emission(float w) {
return float(int(w * 255.0 + 0.5) >> 7);
}
int unpack_material(float w) {
return int(w * 255.0 + 0.5) & 127;
}
#endif // COMMONS_GLSL_

View file

@ -11,6 +11,7 @@ in vec3 a_realnormal;
in vec3 a_skyLight;
in vec4 a_modelpos;
in float a_emission;
flat in int a_material;
#include <world_uniforms>

View file

@ -11,6 +11,7 @@ out vec3 a_realnormal;
out vec3 a_skyLight;
out vec4 a_modelpos;
out float a_emission;
flat out int a_material;
#include <world_uniforms>

View file

@ -15,8 +15,12 @@ uniform bool u_alphaClip;
uniform bool u_debugLights;
uniform bool u_debugNormals;
#include <__materials_fragment__>
void main() {
vec4 texColor = texture(u_texture0, a_texCoord);
vec4 texColor = apply_material_fragment(
a_material, texture(u_texture0, a_texCoord)
);
float alpha = texColor.a;
if (u_alphaClip) {
if (alpha < 0.2f)

View file

@ -14,11 +14,16 @@ uniform float u_dayTime;
out vec4 a_torchLight;
#include <__materials_vertex__>
void main() {
a_material = unpack_material(v_normal.w);
a_realnormal = v_normal.xyz * 2.0 - 1.0;
a_modelpos = u_model * vec4(v_position, 1.0f);
// material shader may also modify a_realnormal
a_modelpos.xyz = apply_material_vertex(a_material, a_modelpos.xyz);
vec3 pos3d = a_modelpos.xyz - u_cameraPos;
a_realnormal = v_normal.xyz * 2.0 - 1.0;
a_normal = calc_screen_normal(a_realnormal);
a_torchLight = vec4(calc_torch_light(
@ -37,7 +42,7 @@ void main() {
a_fog = calc_fog(length(viewmodel * vec4(pos3d * FOG_POS_SCALE, 0.0)) / 256.0);
#endif
a_emission = v_normal.w;
a_emission = unpack_emission(v_normal.w);
vec4 viewmodelpos = u_view * a_modelpos;
a_position = viewmodelpos.xyz;

View file

@ -1,9 +1,13 @@
in vec2 a_texCoord;
#include <world_fragment_header>
uniform sampler2D u_texture0;
#include <__materials_fragment__>
void main() {
vec4 tex_color = texture(u_texture0, a_texCoord);
vec4 tex_color = apply_material_fragment(
a_material, texture(u_texture0, a_texCoord)
);
if (tex_color.a < 0.5) {
discard;
}

View file

@ -5,13 +5,18 @@ layout (location = 1) in vec2 v_texCoord;
layout (location = 2) in vec4 v_light;
layout (location = 3) in vec4 v_normal;
out vec2 a_texCoord;
// same vertex stage interface as world shaders for material shaders
#include <world_vertex_header>
uniform mat4 u_model;
uniform mat4 u_proj;
uniform mat4 u_view;
uniform float u_dayTime;
#include <__materials_vertex__>
void main() {
a_texCoord = v_texCoord;
gl_Position = u_proj * u_view * u_model * vec4(v_position, 1.0f);
a_material = unpack_material(v_normal.w);
a_realnormal = v_normal.xyz * 2.0 - 1.0;
a_modelpos = u_model * vec4(v_position, 1.0f);
a_modelpos.xyz = apply_material_vertex(a_material, a_modelpos.xyz);
gl_Position = u_proj * u_view * a_modelpos;
}

View file

@ -14,8 +14,12 @@ uniform bool u_debugNormals;
#include <shadows>
#include <__materials_fragment__>
void main() {
vec4 texColor = texture(u_texture0, a_texCoord);
vec4 texColor = apply_material_fragment(
a_material, texture(u_texture0, a_texCoord)
);
float alpha = texColor.a;
if (u_alphaClip) {
if (alpha < 0.2f)

View file

@ -14,11 +14,15 @@ out vec4 a_torchLight;
uniform float u_dayTime;
#include <__materials_vertex__>
void main() {
a_material = unpack_material(v_normal.w);
a_realnormal = v_normal.xyz * 2.0 - 1.0;
a_modelpos = u_model * vec4(v_position, 1.0f);
a_modelpos.xyz = apply_material_vertex(a_material, a_modelpos.xyz);
vec3 pos3d = a_modelpos.xyz - u_cameraPos;
a_realnormal = v_normal.xyz * 2.0 - 1.0;
a_normal = calc_screen_normal(a_realnormal);
a_torchLight = vec4(calc_torch_light(
@ -33,7 +37,7 @@ void main() {
mat4 viewmodel = u_view * u_model;
a_distance = length(viewmodel * vec4(pos3d, 0.0));
a_fog = calc_fog(length(viewmodel * vec4(pos3d * FOG_POS_SCALE, 0.0)) / 256.0);
a_emission = v_normal.w;
a_emission = unpack_emission(v_normal.w);
vec4 viewmodelpos = u_view * a_modelpos;
a_position = viewmodelpos.xyz;