diff --git a/src/graphics/render/BlockWrapsRenderer.cpp b/src/graphics/render/BlockWrapsRenderer.cpp index edf2625bf..24a7955b8 100644 --- a/src/graphics/render/BlockWrapsRenderer.cpp +++ b/src/graphics/render/BlockWrapsRenderer.cpp @@ -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 { position, {texture, texture, texture, texture, texture, texture}, + {tint, tint, tint, tint, tint, tint}, emission}); return id; } diff --git a/src/graphics/render/BlockWrapsRenderer.hpp b/src/graphics/render/BlockWrapsRenderer.hpp index 84f7f5b91..f19bf6b66 100644 --- a/src/graphics/render/BlockWrapsRenderer.hpp +++ b/src/graphics/render/BlockWrapsRenderer.hpp @@ -1,15 +1,16 @@ #pragma once +#include "assets/assets_util.hpp" +#include "voxels/Block.hpp" +#include "MainBatch.hpp" +#include "typedefs.hpp" + #include #include #include #include #include - -#include "assets/assets_util.hpp" -#include "voxels/Block.hpp" -#include "MainBatch.hpp" -#include "typedefs.hpp" +#include class Assets; class Player; @@ -22,6 +23,7 @@ class DrawContext; struct BlockWrapper { glm::ivec3 position; std::array textureFaces {}; + std::array 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; diff --git a/src/graphics/render/MainBatch.cpp b/src/graphics/render/MainBatch.cpp index 1498e1537..5b08ffe04 100644 --- a/src/graphics/render/MainBatch.cpp +++ b/src/graphics/render/MainBatch.cpp @@ -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] ); } diff --git a/src/graphics/render/MainBatch.hpp b/src/graphics/render/MainBatch.hpp index da711b075..8ad119199 100644 --- a/src/graphics/render/MainBatch.hpp +++ b/src/graphics/render/MainBatch.hpp @@ -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 ); diff --git a/src/logic/scripting/lua/libs/libblockwraps.cpp b/src/logic/scripting/lua/libs/libblockwraps.cpp index 1868aca53..5b28f7dbb 100644 --- a/src/logic/scripting/lua/libs/libblockwraps.cpp +++ b/src/logic/scripting/lua/libs/libblockwraps.cpp @@ -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}, {"unwrap", lua::wrap}, {"set_pos", lua::wrap}, {"set_texture", lua::wrap}, {"set_faces", lua::wrap}, + {"set_tints", lua::wrap}, {nullptr, nullptr} };