add gfx.blockwraps.set_tints

This commit is contained in:
MihailRis 2025-12-21 16:44:12 +03:00
parent e423c74628
commit c47432c979
5 changed files with 54 additions and 22 deletions

View file

@ -68,6 +68,7 @@ void BlockWrapsRenderer::draw(BlockWrapper& wrapper, const Texture* texture) {
glm::vec3(1.01f),
wrapper.uvRegions,
light,
wrapper.tints.data(),
wrapper.emission,
cullingBits
);
@ -82,6 +83,7 @@ void BlockWrapsRenderer::draw(BlockWrapper& wrapper, const Texture* texture) {
size * glm::vec3(1.01f),
wrapper.uvRegions,
light,
wrapper.tints.data(),
wrapper.emission,
cullingBits
);
@ -152,12 +154,16 @@ void BlockWrapsRenderer::draw(const DrawContext& pctx, const Player& player) {
}
u64id_t BlockWrapsRenderer::add(
const glm::ivec3& position, const std::string& texture, float emission
const glm::ivec3& position,
const std::string& texture,
const glm::vec3& tint,
float emission
) {
u64id_t id = nextWrapper++;
wrappers[id] = std::make_unique<BlockWrapper>(BlockWrapper {
position,
{texture, texture, texture, texture, texture, texture},
{tint, tint, tint, tint, tint, tint},
emission});
return id;
}

View file

@ -1,15 +1,16 @@
#pragma once
#include "assets/assets_util.hpp"
#include "voxels/Block.hpp"
#include "MainBatch.hpp"
#include "typedefs.hpp"
#include <array>
#include <string>
#include <memory>
#include <unordered_map>
#include <map>
#include "assets/assets_util.hpp"
#include "voxels/Block.hpp"
#include "MainBatch.hpp"
#include "typedefs.hpp"
#include <glm/vec4.hpp>
class Assets;
class Player;
@ -22,6 +23,7 @@ class DrawContext;
struct BlockWrapper {
glm::ivec3 position;
std::array<std::string, 6> textureFaces {};
std::array<glm::vec3, 6> tints {};
float emission = 0.0f;
// --- render cache ---
@ -54,7 +56,10 @@ public:
void draw(const DrawContext& ctx, const Player& player);
u64id_t add(
const glm::ivec3& position, const std::string& texture, float emission
const glm::ivec3& position,
const std::string& texture,
const glm::vec3& tint,
float emission
);
BlockWrapper* get(u64id_t id) const;

View file

@ -86,7 +86,8 @@ void MainBatch::cube(
const glm::vec3& coord,
const glm::vec3& size,
const UVRegion(&texfaces)[6],
const glm::vec4& tint,
const glm::vec4& lights,
const glm::vec3 tints[],
float emission,
uint8_t cullingBits
) {
@ -98,42 +99,42 @@ void MainBatch::cube(
quad(
coord + Z * size.z * 0.5f,
X, Y, Z, glm::vec2(size.x, size.y),
do_tint((1.0f - emission) * 0.8 + emission) * tint,
glm::vec3(1.0f), texfaces[5]
do_tint((1.0f - emission) * 0.8 + emission) * lights,
tints[5], texfaces[5]
);
if (cullingBits & 0x10)
quad(
coord - Z * size.z * 0.5f,
-X, Y, -Z, glm::vec2(size.x, size.y),
do_tint((1.0f - emission) * 0.9 + emission) * tint,
glm::vec3(1.0f), texfaces[4]
do_tint((1.0f - emission) * 0.9 + emission) * lights,
tints[4], texfaces[4]
);
if (cullingBits & 0x8)
quad(
coord + Y * size.y * 0.5f,
-X, Z, Y, glm::vec2(size.x, size.z),
do_tint((1.0f - emission) * 1.0 + emission) * tint,
glm::vec3(1.0f), texfaces[3]
do_tint((1.0f - emission) * 1.0 + emission) * lights,
tints[3], texfaces[3]
);
if (cullingBits & 0x4)
quad(
coord - Y * size.y * 0.5f,
X, Z, -Y, glm::vec2(size.x, size.z),
do_tint((1.0f - emission) * 0.7 + emission) * tint,
glm::vec3(1.0f), texfaces[2]
do_tint((1.0f - emission) * 0.7 + emission) * lights,
tints[2], texfaces[2]
);
if (cullingBits & 0x2)
quad(
coord + X * size.x * 0.5f,
-Z, Y, X, glm::vec2(size.z, size.y),
do_tint((1.0f - emission) * 0.8 + emission) * tint,
glm::vec3(1.0f), texfaces[1]
do_tint((1.0f - emission) * 0.8 + emission) * lights,
tints[1], texfaces[1]
);
if (cullingBits & 0x1)
quad(
coord - X * size.x * 0.5f,
Z, Y, -X, glm::vec2(size.z, size.y),
do_tint((1.0f - emission) * 0.9 + emission) * tint,
glm::vec3(1.0f), texfaces[0]
do_tint((1.0f - emission) * 0.9 + emission) * lights,
tints[0], texfaces[0]
);
}

View file

@ -151,7 +151,8 @@ public:
const glm::vec3& coord,
const glm::vec3& size,
const UVRegion(&texfaces)[6],
const glm::vec4& tint,
const glm::vec4& lights,
const glm::vec3 tints[],
float emission,
uint8_t cullingBits = 0xFF
);

View file

@ -10,9 +10,13 @@ static int l_wrap(lua::State* L) {
auto position = lua::tovec3(L, 1);
std::string texture = lua::require_string(L, 2);
float emission = lua::isnumber(L, 3) ? lua::tonumber(L, 3) : 1.0f;
glm::vec3 tint = lua::istable(L, 4) ? lua::tovec3(L, 4) : glm::vec3(1.0f);
return lua::pushinteger(
L, renderer->blockWraps->add(position, std::move(texture), emission)
L,
renderer->blockWraps->add(
position, std::move(texture), std::move(tint), emission
)
);
}
@ -60,11 +64,26 @@ static int l_set_faces(lua::State* L) {
return 0;
}
static int l_set_tints(lua::State* L) {
if (auto wrapper = renderer->blockWraps->get(lua::tointeger(L, 1))) {
for (int i = 0; i < wrapper->textureFaces.size(); i++) {
if (lua::isnil(L, 2 + i)) {
wrapper->tints[i] = glm::vec3(1.0f);
} else {
wrapper->tints[i] = lua::tovec3(L, 2 + i);
}
}
wrapper->dirtySides = 0xFF;
}
return 0;
}
const luaL_Reg blockwrapslib[] = {
{"wrap", lua::wrap<l_wrap>},
{"unwrap", lua::wrap<l_unwrap>},
{"set_pos", lua::wrap<l_set_pos>},
{"set_texture", lua::wrap<l_set_texture>},
{"set_faces", lua::wrap<l_set_faces>},
{"set_tints", lua::wrap<l_set_tints>},
{nullptr, nullptr}
};