Skip to content

Commit

Permalink
Merge pull request #418 from MihailRis/culling-property
Browse files Browse the repository at this point in the history
New blocks rendering properties
  • Loading branch information
MihailRis authored Dec 21, 2024
2 parents cda34e3 + 6f6421d commit 9594e02
Show file tree
Hide file tree
Showing 22 changed files with 190 additions and 63 deletions.
7 changes: 7 additions & 0 deletions doc/en/block-properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,13 @@ Turns off block model shading

Determines the presence of the vertex AO effect. Turned-on by default.

### *culling*

Face culling mode:
- **default** - normal face culling
- **optional** - face culling among blocks of the same rendering group can be disabled via the `graphics.dense-render` setting.
- **disabled** - face culling among blocks of the same rendering group disabled.

## Physics

### *obstacle*
Expand Down
8 changes: 7 additions & 1 deletion doc/ru/block-properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,6 @@

При значении `true` блок не препятствует прохождению вертикального луча солнечного света.


### Без освещения - *shadeless*

Выключает освещение на модели блока.
Expand All @@ -91,6 +90,13 @@

Определяет наличие эффекта вершинного AO. Включен по-умолчанию.

### Отсечение - *culling*

Режим отсечения граней:
- **default** - обычное отсечение граней
- **optional** - отсечение граней среди блоков одной группы отрисовки можно отключить через настройку `graphics.dense-render` (Плотный рендер блоков).
- **disabled** - отсечение граней среди блоков одной группы отрисовки отключено.

## Физика

### Препятствие - *obstacle*
Expand Down
2 changes: 2 additions & 0 deletions res/content/base/blocks/leaves.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
{
"texture": "leaves",
"material": "base:grass",
"draw-group": 5,
"culling": "optional",
"base:durability": 0.7
}
Binary file modified res/content/base/textures/blocks/leaves.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions res/layouts/pages/settings_graphics.xml.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,4 +41,5 @@ function on_open()
create_setting("graphics.fog-curve", "Fog Curve", 0.1)
create_setting("graphics.gamma", "Gamma", 0.05, "", "graphics.gamma.tooltip")
create_checkbox("graphics.backlight", "Backlight", "graphics.backlight.tooltip")
create_checkbox("graphics.dense-render", "Dense blocks render", "graphics.dense-render.tooltip")
end
1 change: 1 addition & 0 deletions res/texts/en_US.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ devtools.traceback=Traceback (most recent call first)
# Tooltips
graphics.gamma.tooltip=Lighting brightness curve
graphics.backlight.tooltip=Backlight to prevent total darkness
graphics.dense-render.tooltip=Enables transparency in blocks like leaves

# settings
settings.Controls Search Mode=Search by attached button name
Expand Down
2 changes: 2 additions & 0 deletions res/texts/ru_RU.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ pack.remove-confirm=Удалить весь поставляемый паком/
# Подсказки
graphics.gamma.tooltip=Кривая яркости освещения
graphics.backlight.tooltip=Подсветка, предотвращающая полную темноту
graphics.dense-render.tooltip=Включает прозрачность блоков, таких как листья.

# Меню
menu.Apply=Применить
Expand Down Expand Up @@ -67,6 +68,7 @@ world.delete-confirm=Удалить мир безвозвратно?
# Настройки
settings.Ambient=Фон
settings.Backlight=Подсветка
settings.Dense blocks render=Плотный рендер блоков
settings.Camera Shaking=Тряска Камеры
settings.Camera Inertia=Инерция Камеры
settings.Camera FOV Effects=Эффекты поля зрения
Expand Down
10 changes: 9 additions & 1 deletion src/content/ContentLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -239,10 +239,18 @@ void ContentLoader::loadBlock(
}
def.model = *model;
} else if (!modelTypeName.empty()) {
logger.error() << "unknown model " << modelTypeName;
logger.error() << "unknown model: " << modelTypeName;
def.model = BlockModel::none;
}

std::string cullingModeName = to_string(def.culling);
root.at("culling").get(cullingModeName);
if (auto mode = CullingMode_from(cullingModeName)) {
def.culling = *mode;
} else {
logger.error() << "unknown culling mode: " << cullingModeName;
}

root.at("material").get(def.material);

// rotation profile
Expand Down
1 change: 1 addition & 0 deletions src/files/settings_io.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ SettingsHandler::SettingsHandler(EngineSettings& settings) {
builder.section("graphics");
builder.add("fog-curve", &settings.graphics.fogCurve);
builder.add("backlight", &settings.graphics.backlight);
builder.add("dense-render", &settings.graphics.denseRender);
builder.add("gamma", &settings.graphics.gamma);
builder.add("frustum-culling", &settings.graphics.frustumCulling);
builder.add("skybox-resolution", &settings.graphics.skyboxResolution);
Expand Down
80 changes: 49 additions & 31 deletions src/frontend/ContentGfxCache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,53 +6,71 @@
#include "assets/Assets.hpp"
#include "content/Content.hpp"
#include "content/ContentPack.hpp"
#include "core_defs.hpp"
#include "graphics/core/Atlas.hpp"
#include "maths/UVRegion.hpp"
#include "voxels/Block.hpp"
#include "core_defs.hpp"
#include "settings.hpp"

ContentGfxCache::ContentGfxCache(const Content* content, const Assets& assets)
: content(content) {
auto indices = content->getIndices();
sideregions = std::make_unique<UVRegion[]>(indices->blocks.count() * 6);
const auto& atlas = assets.require<Atlas>("blocks");

const auto& blocks = indices->blocks.getIterable();
for (blockid_t i = 0; i < blocks.size(); i++) {
auto def = blocks[i];
for (uint side = 0; side < 6; side++) {
const std::string& tex = def->textureFaces[side];
if (atlas.has(tex)) {
sideregions[i * 6 + side] = atlas.get(tex);
} else if (atlas.has(TEXTURE_NOTFOUND)) {
sideregions[i * 6 + side] = atlas.get(TEXTURE_NOTFOUND);
}
ContentGfxCache::ContentGfxCache(
const Content& content,
const Assets& assets,
const GraphicsSettings& settings
)
: content(content), assets(assets), settings(settings) {
refresh();
}

void ContentGfxCache::refresh(const Block& def, const Atlas& atlas) {
for (uint side = 0; side < 6; side++) {
std::string tex = def.textureFaces[side];
if (def.culling == CullingMode::OPTIONAL &&
!settings.denseRender.get() && atlas.has(tex + "_opaque")) {
tex = tex + "_opaque";
}
if (def->model == BlockModel::custom) {
auto model = assets.require<model::Model>(def->modelName);
// temporary dirty fix tbh
if (def->modelName.find(':') == std::string::npos) {
for (auto& mesh : model.meshes) {
size_t pos = mesh.texture.find(':');
if (pos == std::string::npos) {
continue;
}
if (auto region = atlas.getIf(mesh.texture.substr(pos+1))) {
for (auto& vertex : mesh.vertices) {
vertex.uv = region->apply(vertex.uv);
}
if (atlas.has(tex)) {
sideregions[def.rt.id * 6 + side] = atlas.get(tex);
} else if (atlas.has(TEXTURE_NOTFOUND)) {
sideregions[def.rt.id * 6 + side] = atlas.get(TEXTURE_NOTFOUND);
}
}
if (def.model == BlockModel::custom) {
auto model = assets.require<model::Model>(def.modelName);
// temporary dirty fix tbh
if (def.modelName.find(':') == std::string::npos) {
for (auto& mesh : model.meshes) {
size_t pos = mesh.texture.find(':');
if (pos == std::string::npos) {
continue;
}
if (auto region = atlas.getIf(mesh.texture.substr(pos+1))) {
for (auto& vertex : mesh.vertices) {
vertex.uv = region->apply(vertex.uv);
}
}
}
models[def->rt.id] = std::move(model);
}
models[def.rt.id] = std::move(model);
}
}

void ContentGfxCache::refresh() {
auto indices = content.getIndices();
sideregions = std::make_unique<UVRegion[]>(indices->blocks.count() * 6);
const auto& atlas = assets.require<Atlas>("blocks");

const auto& blocks = indices->blocks.getIterable();
for (blockid_t i = 0; i < blocks.size(); i++) {
auto def = blocks[i];
refresh(*def, atlas);
}
}

ContentGfxCache::~ContentGfxCache() = default;

const Content* ContentGfxCache::getContent() const {
return content;
return &content;
}

const model::Model& ContentGfxCache::getModel(blockid_t id) const {
Expand Down
18 changes: 16 additions & 2 deletions src/frontend/ContentGfxCache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,29 @@

class Content;
class Assets;
class Atlas;
class Block;
struct UVRegion;
struct GraphicsSettings;

namespace model {
struct Model;
}

class ContentGfxCache {
const Content* content;
const Content& content;
const Assets& assets;
const GraphicsSettings& settings;

// array of block sides uv regions (6 per block)
std::unique_ptr<UVRegion[]> sideregions;
std::unordered_map<blockid_t, model::Model> models;
public:
ContentGfxCache(const Content* content, const Assets& assets);
ContentGfxCache(
const Content& content,
const Assets& assets,
const GraphicsSettings& settings
);
~ContentGfxCache();

inline const UVRegion& getRegion(blockid_t id, int side) const {
Expand All @@ -32,4 +42,8 @@ class ContentGfxCache {
const model::Model& getModel(blockid_t id) const;

const Content* getContent() const;

void refresh(const Block& block, const Atlas& atlas);

void refresh();
};
21 changes: 15 additions & 6 deletions src/frontend/LevelFrontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,17 @@
#include "world/Level.hpp"

LevelFrontend::LevelFrontend(
Player* currentPlayer, LevelController* controller, Assets& assets
) : level(*controller->getLevel()),
controller(controller),
assets(assets),
contentCache(std::make_unique<ContentGfxCache>(level.content, assets))
{
Player* currentPlayer,
LevelController* controller,
Assets& assets,
const EngineSettings& settings
)
: level(*controller->getLevel()),
controller(controller),
assets(assets),
contentCache(std::make_unique<ContentGfxCache>(
*level.content, assets, settings.graphics
)) {
assets.store(
BlocksPreview::build(
*contentCache, assets, *level.content->getIndices()
Expand Down Expand Up @@ -98,6 +103,10 @@ const Assets& LevelFrontend::getAssets() const {
return assets;
}

ContentGfxCache& LevelFrontend::getContentGfxCache() {
return *contentCache;
}

const ContentGfxCache& LevelFrontend::getContentGfxCache() const {
return *contentCache;
}
Expand Down
9 changes: 8 additions & 1 deletion src/frontend/LevelFrontend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,26 @@ class Assets;
class Player;
class ContentGfxCache;
class LevelController;
struct EngineSettings;

class LevelFrontend {
Level& level;
LevelController* controller;
const Assets& assets;
std::unique_ptr<ContentGfxCache> contentCache;
public:
LevelFrontend(Player* currentPlayer, LevelController* controller, Assets& assets);
LevelFrontend(
Player* currentPlayer,
LevelController* controller,
Assets& assets,
const EngineSettings& settings
);
~LevelFrontend();

Level& getLevel();
const Level& getLevel() const;
const Assets& getAssets() const;
const ContentGfxCache& getContentGfxCache() const;
ContentGfxCache& getContentGfxCache();
LevelController* getController() const;
};
8 changes: 7 additions & 1 deletion src/frontend/screens/LevelScreen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "graphics/render/Decorator.hpp"
#include "graphics/ui/elements/Menu.hpp"
#include "graphics/ui/GUI.hpp"
#include "frontend/ContentGfxCache.hpp"
#include "logic/LevelController.hpp"
#include "logic/scripting/scripting_hud.hpp"
#include "util/stringutil.hpp"
Expand All @@ -42,7 +43,7 @@ LevelScreen::LevelScreen(Engine* engine, std::unique_ptr<Level> levelPtr)

controller = std::make_unique<LevelController>(engine, std::move(levelPtr));
frontend = std::make_unique<LevelFrontend>(
controller->getPlayer(), controller.get(), assets
controller->getPlayer(), controller.get(), assets, settings
);
worldRenderer = std::make_unique<WorldRenderer>(
engine, *frontend, controller->getPlayer()
Expand All @@ -57,6 +58,11 @@ LevelScreen::LevelScreen(Engine* engine, std::unique_ptr<Level> levelPtr)
controller->getLevel()->chunks->saveAndClear();
worldRenderer->clear();
}));
keepAlive(settings.graphics.denseRender.observe([=](bool) {
controller->getLevel()->chunks->saveAndClear();
worldRenderer->clear();
frontend->getContentGfxCache().refresh();
}));
keepAlive(settings.camera.fov.observe([=](double value) {
controller->getPlayer()->fpCamera->setFov(glm::radians(value));
}));
Expand Down
Loading

0 comments on commit 9594e02

Please sign in to comment.