mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-11-21 01:49:44 +00:00
feat: entity translucent pass & test layered transparency pipeline
This commit is contained in:
parent
e7f030f226
commit
7522016e84
19 changed files with 484 additions and 111 deletions
|
|
@ -143,8 +143,10 @@ void BlockWrapsRenderer::draw(const DrawContext& pctx) {
|
|||
|
||||
auto& shader = assets.require<Shader>("entity");
|
||||
shader.use();
|
||||
shader.uniform1i("u_alphaClip", false);
|
||||
shader.uniform1i("u_dithering", 0);
|
||||
shader.uniform1f("u_alphaThreshold", 0.15f);
|
||||
shader.uniform1i("u_opaque", false);
|
||||
shader.uniform1i("u_forward", false);
|
||||
shader.uniform1i("u_forwardLight", false);
|
||||
|
||||
for (const auto& [_, wrapper] : wrappers) {
|
||||
if (wrapper->dirtySides) {
|
||||
|
|
|
|||
|
|
@ -102,10 +102,13 @@ void ModelBatch::draw(
|
|||
const glm::vec4& tint,
|
||||
const glm::vec3& lightSampleOffset,
|
||||
const model::Model* model,
|
||||
const TextureNamesMap* varTextures
|
||||
const TextureNamesMap* varTextures,
|
||||
bool translucent
|
||||
) {
|
||||
auto& queue =
|
||||
translucent || tint.a < 1.0f ? translucentEntries : entries;
|
||||
for (const auto& mesh : model->meshes) {
|
||||
entries.push_back(DrawEntry {
|
||||
queue.push_back(DrawEntry {
|
||||
matrix,
|
||||
extract_rotation(matrix),
|
||||
tint,
|
||||
|
|
@ -115,14 +118,9 @@ void ModelBatch::draw(
|
|||
}
|
||||
}
|
||||
|
||||
void ModelBatch::render() {
|
||||
std::sort(entries.begin(), entries.end(),
|
||||
[](const DrawEntry& a, const DrawEntry& b) {
|
||||
return a.mesh->texture < b.mesh->texture;
|
||||
}
|
||||
);
|
||||
void ModelBatch::render(std::vector<DrawEntry>& queue) {
|
||||
bool backlight = settings.graphics.backlight.get();
|
||||
for (auto& entry : entries) {
|
||||
for (auto& entry : queue) {
|
||||
localLightsOffset = entry.lightSampleOffset;
|
||||
draw(
|
||||
*entry.mesh,
|
||||
|
|
@ -135,7 +133,36 @@ void ModelBatch::render() {
|
|||
localLightsOffset = {};
|
||||
}
|
||||
batch->flush();
|
||||
entries.clear();
|
||||
queue.clear();
|
||||
}
|
||||
|
||||
void ModelBatch::render() {
|
||||
renderOpaque();
|
||||
render(translucentEntries);
|
||||
}
|
||||
|
||||
void ModelBatch::renderOpaque() {
|
||||
std::sort(entries.begin(), entries.end(),
|
||||
[](const DrawEntry& a, const DrawEntry& b) {
|
||||
return a.mesh->texture < b.mesh->texture;
|
||||
}
|
||||
);
|
||||
render(entries);
|
||||
}
|
||||
|
||||
void ModelBatch::renderTranslucent(const glm::vec3& cameraPosition) {
|
||||
auto distance2 = [&cameraPosition](const DrawEntry& entry) {
|
||||
glm::vec3 delta = glm::vec3(entry.matrix[3]) - cameraPosition;
|
||||
return glm::dot(delta, delta);
|
||||
};
|
||||
std::stable_sort(
|
||||
translucentEntries.begin(),
|
||||
translucentEntries.end(),
|
||||
[&distance2](const DrawEntry& a, const DrawEntry& b) {
|
||||
return distance2(a) > distance2(b);
|
||||
}
|
||||
);
|
||||
render(translucentEntries);
|
||||
}
|
||||
|
||||
void ModelBatch::setLightsOffset(const glm::vec3& offset) {
|
||||
|
|
|
|||
|
|
@ -35,10 +35,16 @@ public:
|
|||
const glm::vec4& tint,
|
||||
const glm::vec3& lightSampleOffset,
|
||||
const model::Model* model,
|
||||
const TextureNamesMap* varTextures
|
||||
const TextureNamesMap* varTextures,
|
||||
bool translucent = false
|
||||
);
|
||||
|
||||
void render();
|
||||
|
||||
void renderOpaque();
|
||||
|
||||
void renderTranslucent(const glm::vec3& cameraPosition);
|
||||
|
||||
void setLightsOffset(const glm::vec3& offset);
|
||||
private:
|
||||
const Assets& assets;
|
||||
|
|
@ -73,4 +79,7 @@ private:
|
|||
const TextureNamesMap* varTextures;
|
||||
};
|
||||
std::vector<DrawEntry> entries;
|
||||
std::vector<DrawEntry> translucentEntries;
|
||||
|
||||
void render(std::vector<DrawEntry>& queue);
|
||||
};
|
||||
|
|
|
|||
|
|
@ -35,6 +35,7 @@
|
|||
#include "graphics/core/Shadows.hpp"
|
||||
#include "graphics/core/Cubemap.hpp"
|
||||
#include "graphics/core/Texture.hpp"
|
||||
#include "graphics/core/TranslucentLayers.hpp"
|
||||
#include "items/Inventory.hpp"
|
||||
#include "items/ItemDef.hpp"
|
||||
#include "items/ItemStack.hpp"
|
||||
|
|
@ -66,6 +67,31 @@ inline constexpr size_t MODEL_BATCH_CAPACITY = 20'000;
|
|||
|
||||
static debug::Logger logger("world-renderer");
|
||||
|
||||
namespace translucent {
|
||||
enum Layer : size_t {
|
||||
BLOCKS,
|
||||
ENTITIES,
|
||||
WEATHER,
|
||||
COUNT
|
||||
};
|
||||
}
|
||||
|
||||
struct EntityShaderPass {
|
||||
float alphaThreshold;
|
||||
bool opaque;
|
||||
bool forward;
|
||||
bool forwardLight;
|
||||
float opacity;
|
||||
};
|
||||
|
||||
static void setup_entity_pass(Shader& shader, const EntityShaderPass& pass) {
|
||||
shader.uniform1f("u_alphaThreshold", pass.alphaThreshold);
|
||||
shader.uniform1i("u_opaque", pass.opaque);
|
||||
shader.uniform1i("u_forward", pass.forward);
|
||||
shader.uniform1i("u_forwardLight", pass.forwardLight);
|
||||
shader.uniform1f("u_opacity", pass.opacity);
|
||||
}
|
||||
|
||||
bool WorldRenderer::showChunkBorders = false;
|
||||
bool WorldRenderer::showEntitiesDebug = false;
|
||||
|
||||
|
|
@ -246,9 +272,7 @@ void WorldRenderer::renderOpaque(
|
|||
frustumCulling->update(camera.getProjView());
|
||||
}
|
||||
|
||||
entityShader.uniform1i("u_alphaClip", true);
|
||||
entityShader.uniform1i("u_dithering", 1);
|
||||
entityShader.uniform1f("u_opacity", 1.0f);
|
||||
setup_entity_pass(entityShader, {0.5f, true, false, false, 1.0f});
|
||||
level.entities->render(
|
||||
assets,
|
||||
*modelBatch,
|
||||
|
|
@ -256,7 +280,7 @@ void WorldRenderer::renderOpaque(
|
|||
player.currentCamera.get() == player.fpCamera.get() ? player.getEntity()
|
||||
: 0
|
||||
);
|
||||
modelBatch->render();
|
||||
modelBatch->renderOpaque();
|
||||
particles->render(camera);
|
||||
|
||||
auto& mainShader = assets.require<Shader>("main");
|
||||
|
|
@ -373,21 +397,7 @@ void WorldRenderer::renderFrameClassic(
|
|||
renderDebugLines(ctx, camera);
|
||||
}
|
||||
|
||||
DrawContext wctx = ctx.sub();
|
||||
wctx.useTexture(advanced_pipeline::TARGET_SKYBOX, skybox->getCubemap());
|
||||
wctx.useTexture(advanced_pipeline::TARGET_COLOR, nullptr);
|
||||
// Translucent blocks
|
||||
{
|
||||
const auto& settings = engine.getSettings();
|
||||
auto& translucentShader = assets.require<Shader>("translucent");
|
||||
auto sctx = wctx.sub();
|
||||
sctx.setCullFace(true);
|
||||
translucentShader.use();
|
||||
setupWorldShader(translucentShader, camera, settings, calcFogFactor());
|
||||
chunksRenderer->drawSortedMeshes(camera, translucentShader);
|
||||
}
|
||||
|
||||
renderWeatherEffects(camera);
|
||||
renderTranslucency(ctx, camera, postProcessing);
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
}
|
||||
|
||||
|
|
@ -418,6 +428,7 @@ void WorldRenderer::renderFrameAdvanced(
|
|||
ctx.useTexture(advanced_pipeline::TARGET_COLOR, nullptr);
|
||||
|
||||
postProcessing.bindDepthBuffer();
|
||||
ctx.setFramebuffer(postProcessing.getFramebuffer());
|
||||
skybox->draw(
|
||||
level.environment, ctx, camera, worldInfo.daytime, weather.clouds()
|
||||
);
|
||||
|
|
@ -426,21 +437,7 @@ void WorldRenderer::renderFrameAdvanced(
|
|||
renderDebugLines(ctx, camera);
|
||||
}
|
||||
|
||||
DrawContext wctx = ctx.sub();
|
||||
wctx.useTexture(advanced_pipeline::TARGET_SKYBOX, skybox->getCubemap());
|
||||
wctx.useTexture(advanced_pipeline::TARGET_COLOR, nullptr);
|
||||
// Translucent blocks
|
||||
{
|
||||
const auto& settings = engine.getSettings();
|
||||
auto& translucentShader = assets.require<Shader>("translucent");
|
||||
auto sctx = wctx.sub();
|
||||
sctx.setCullFace(true);
|
||||
translucentShader.use();
|
||||
setupWorldShader(translucentShader, camera, settings, fogFactor);
|
||||
chunksRenderer->drawSortedMeshes(camera, translucentShader);
|
||||
}
|
||||
|
||||
renderWeatherEffects(camera);
|
||||
renderTranslucency(ctx, camera, postProcessing);
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
}
|
||||
|
||||
|
|
@ -528,10 +525,10 @@ void WorldRenderer::renderWeatherEffects(Camera& camera) {
|
|||
float zero = weather->fall.minOpacity;
|
||||
float one = weather->fall.maxOpacity;
|
||||
float t = (weather->intensity * (one - zero)) * maxIntensity + zero;
|
||||
entityShader.uniform1i("u_alphaClip", weather->fall.opaque);
|
||||
entityShader.uniform1i("u_dithering", 0);
|
||||
entityShader.uniform1f(
|
||||
"u_opacity", weather->fall.opaque ? t * t : t
|
||||
bool opaque = weather->fall.opaque;
|
||||
setup_entity_pass(
|
||||
entityShader,
|
||||
{opaque ? 0.5f : 0.15f, false, true, false, opaque ? t * t : t}
|
||||
);
|
||||
if (weather->intensity > 1.e-3f && !weather->fall.texture.empty()) {
|
||||
precipitation->render(camera, *weather);
|
||||
|
|
@ -539,6 +536,67 @@ void WorldRenderer::renderWeatherEffects(Camera& camera) {
|
|||
}
|
||||
}
|
||||
|
||||
void WorldRenderer::renderTranslucency(
|
||||
const DrawContext& pctx,
|
||||
Camera& camera,
|
||||
PostProcessing& postProcessing
|
||||
) {
|
||||
const auto& settings = engine.getSettings();
|
||||
float fogFactor = calcFogFactor();
|
||||
auto& frame = *postProcessing.getFramebuffer();
|
||||
const auto& vp = pctx.getViewport();
|
||||
|
||||
if (translucentLayers == nullptr) {
|
||||
translucentLayers = std::make_unique<TranslucentLayers>(
|
||||
translucent::COUNT, vp.x, vp.y
|
||||
);
|
||||
} else {
|
||||
translucentLayers->resize(vp.x, vp.y);
|
||||
}
|
||||
translucentLayers->prepare(frame.getFBO());
|
||||
|
||||
DrawContext ctx = pctx.sub();
|
||||
ctx.setFramebuffer(&frame);
|
||||
ctx.setDepthTest(true);
|
||||
ctx.setDepthMask(true);
|
||||
ctx.setBlendMode(BlendMode::coverage);
|
||||
ctx.useTexture(advanced_pipeline::TARGET_SKYBOX, skybox->getCubemap());
|
||||
ctx.useTexture(advanced_pipeline::TARGET_COLOR, nullptr);
|
||||
|
||||
{
|
||||
auto lctx = ctx.sub();
|
||||
lctx.setFramebuffer(&translucentLayers->getLayer(translucent::BLOCKS));
|
||||
lctx.setCullFace(true);
|
||||
auto& shader = assets.require<Shader>("translucent");
|
||||
shader.use();
|
||||
setupWorldShader(shader, camera, settings, fogFactor);
|
||||
chunksRenderer->drawSortedMeshes(camera, shader);
|
||||
}
|
||||
{
|
||||
auto lctx = ctx.sub();
|
||||
lctx.setFramebuffer(&translucentLayers->getLayer(translucent::ENTITIES));
|
||||
lctx.setCullFace(true);
|
||||
auto& shader = assets.require<Shader>("entity");
|
||||
setupWorldShader(shader, camera, settings, fogFactor);
|
||||
setup_entity_pass(shader, {0.002f, false, true, true, 1.0f});
|
||||
modelBatch->renderTranslucent(camera.position);
|
||||
}
|
||||
{
|
||||
auto lctx = ctx.sub();
|
||||
lctx.setFramebuffer(&translucentLayers->getLayer(translucent::WEATHER));
|
||||
renderWeatherEffects(camera);
|
||||
}
|
||||
{
|
||||
auto cctx = ctx.sub();
|
||||
cctx.setDepthTest(false);
|
||||
cctx.setDepthMask(false);
|
||||
cctx.setBlendMode(BlendMode::premultiplied);
|
||||
translucentLayers->compose(
|
||||
assets.require<Shader>("translucent_composite")
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
void WorldRenderer::renderDebugLines(const DrawContext& ctx, Camera& camera) {
|
||||
auto& linesShader = assets.require<Shader>("lines");
|
||||
linesShader.use();
|
||||
|
|
@ -566,6 +624,7 @@ void WorldRenderer::renderHandsPass(const DrawContext& pctx, Camera& camera) {
|
|||
hudcam.position = {};
|
||||
|
||||
setupWorldShader(entityShader, hudcam, engine.getSettings(), 0.0f);
|
||||
setup_entity_pass(entityShader, {0.15f, false, false, false, 1.0f});
|
||||
hands->render(camera);
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -33,6 +33,7 @@ class Shader;
|
|||
class Shadows;
|
||||
class Skybox;
|
||||
class TextsRenderer;
|
||||
class TranslucentLayers;
|
||||
class CloudsRenderer;
|
||||
struct EngineSettings;
|
||||
struct Weather;
|
||||
|
|
@ -79,6 +80,7 @@ private:
|
|||
std::unique_ptr<DebugLinesRenderer> debugLines;
|
||||
std::unique_ptr<PrecipitationRenderer> precipitation;
|
||||
std::unique_ptr<CloudsRenderer> cloudsRenderer;
|
||||
std::unique_ptr<TranslucentLayers> translucentLayers;
|
||||
|
||||
float timer = 0.0f;
|
||||
bool debug = false;
|
||||
|
|
@ -125,6 +127,12 @@ private:
|
|||
|
||||
void renderWeatherEffects(Camera& camera);
|
||||
|
||||
void renderTranslucency(
|
||||
const DrawContext& context,
|
||||
Camera& camera,
|
||||
PostProcessing& postProcessing
|
||||
);
|
||||
|
||||
void renderHandsPass(const DrawContext& pctx, Camera& camera);
|
||||
|
||||
void renderDebugLines(const DrawContext& context, Camera& camera);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue