mirror of
https://github.com/MihailRis/voxelcore.git
synced 2026-11-20 09:29: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
|
|
@ -8,6 +8,7 @@ local function refresh_model(id)
|
|||
itemid = id
|
||||
rig:set_model(itemIndex, item.model_name(itemid))
|
||||
rig:set_matrix(itemIndex, mat4.rotate({0, 1, 0}, -80))
|
||||
rig:set_color({1, 1, 1, 0.5})
|
||||
end
|
||||
|
||||
function on_render()
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@
|
|||
"lines",
|
||||
"entity",
|
||||
"translucent",
|
||||
"translucent_composite",
|
||||
"background",
|
||||
"skybox_gen",
|
||||
"shadows",
|
||||
|
|
|
|||
|
|
@ -1,13 +1,12 @@
|
|||
#include <shadows>
|
||||
#include <deferred_light>
|
||||
#include <fog>
|
||||
|
||||
#define CLOUDS_FOG_FACTOR_MUL 0.3f
|
||||
#define CLOUDS_FOG_CURVE_MUL 0.4f
|
||||
#define COLOR_TEMPERATURE_COMPENSATION 1.14f
|
||||
|
||||
vec4 effect() {
|
||||
vec4 pos = texture(u_position, v_uv);
|
||||
float light = 1.0;
|
||||
float ao = 1.0;
|
||||
|
||||
#ifdef ENABLE_SSAO
|
||||
vec2 texel = 2.0 / u_screenSize;
|
||||
|
|
@ -23,7 +22,7 @@ vec4 effect() {
|
|||
wsum += w;
|
||||
}
|
||||
}
|
||||
light = sum / wsum;
|
||||
ao = sum / wsum;
|
||||
#endif // ENABLE_SSAO
|
||||
|
||||
vec4 modelpos = u_inverseView * pos;
|
||||
|
|
@ -31,18 +30,7 @@ vec4 effect() {
|
|||
vec3 dir = modelpos.xyz - u_cameraPos;
|
||||
|
||||
float emission = texture(u_emission, v_uv).r;
|
||||
|
||||
float rg = 1.0;
|
||||
#ifdef ENABLE_SHADOWS
|
||||
float shadow_mul = calc_shadow(modelpos, normal, length(pos));
|
||||
light *= shadow_mul;
|
||||
rg *= min(1.0, shadow_mul + emission) * COLOR_TEMPERATURE_COMPENSATION;
|
||||
light *= 2.0;
|
||||
#endif
|
||||
|
||||
light = max(light, emission);
|
||||
|
||||
light = pow(light, u_gamma);
|
||||
vec3 light = calc_deferred_light(modelpos, normal, length(pos), emission, ao);
|
||||
|
||||
vec3 fogColor = texture(u_skybox, dir).rgb;
|
||||
float fog = calc_fog(
|
||||
|
|
@ -50,5 +38,5 @@ vec4 effect() {
|
|||
mix(1.0, CLOUDS_FOG_FACTOR_MUL, emission),
|
||||
mix(1.0, CLOUDS_FOG_CURVE_MUL, emission)
|
||||
);
|
||||
return vec4(mix(texture(u_screen, v_uv).rgb * mix(1.0, light, 1.0) * vec3(rg, 0.5 + rg * 0.5, 1.0), fogColor, fog), 1.0);
|
||||
return vec4(mix(texture(u_screen, v_uv).rgb * light, fogColor, fog), 1.0);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -3,60 +3,43 @@ layout (location = 1) out vec4 f_position;
|
|||
layout (location = 2) out vec4 f_normal;
|
||||
layout (location = 3) out vec4 f_emission;
|
||||
|
||||
uniform vec3 u_sunDir;
|
||||
|
||||
#include <world_fragment_header>
|
||||
#include <deferred_light>
|
||||
|
||||
in vec4 a_color;
|
||||
|
||||
uniform sampler2D u_texture0;
|
||||
|
||||
uniform vec3 u_fogColor;
|
||||
uniform float u_fogFactor;
|
||||
uniform float u_fogCurve;
|
||||
uniform bool u_alphaClip;
|
||||
uniform bool u_dithering;
|
||||
uniform vec3 u_sunDir;
|
||||
|
||||
const float BAYER_MATRIX[64] = float[](
|
||||
0.0, 32.0, 8.0, 40.0, 2.0, 34.0, 10.0, 42.0,
|
||||
48.0, 16.0, 56.0, 24.0, 50.0, 18.0, 58.0, 26.0,
|
||||
12.0, 44.0, 4.0, 36.0, 14.0, 46.0, 6.0, 38.0,
|
||||
60.0, 28.0, 52.0, 20.0, 62.0, 30.0, 54.0, 22.0,
|
||||
3.0, 35.0, 11.0, 43.0, 1.0, 33.0, 9.0, 41.0,
|
||||
51.0, 19.0, 59.0, 27.0, 49.0, 17.0, 57.0, 25.0,
|
||||
15.0, 47.0, 7.0, 39.0, 13.0, 45.0, 5.0, 37.0,
|
||||
63.0, 31.0, 55.0, 23.0, 61.0, 29.0, 53.0, 21.0
|
||||
);
|
||||
|
||||
float calc_bayer(vec2 pos){
|
||||
ivec2 p = ivec2(mod(pos, 8.0));
|
||||
return BAYER_MATRIX[p.y * 8 + p.x] / 64.0;
|
||||
}
|
||||
uniform float u_alphaThreshold;
|
||||
uniform bool u_opaque;
|
||||
uniform bool u_forward;
|
||||
uniform bool u_forwardLight;
|
||||
|
||||
void main() {
|
||||
vec4 texColor = texture(u_texture0, a_texCoord);
|
||||
float alpha = a_color.a * texColor.a;
|
||||
// anyway it's any alpha-test alternative required
|
||||
if (u_dithering) {
|
||||
if (u_alphaClip) {
|
||||
float bayer = calc_bayer(gl_FragCoord.xy);
|
||||
if (alpha - bayer < 0.01f) {
|
||||
discard;
|
||||
}
|
||||
alpha = 1.0;
|
||||
} else if (alpha < 0.05f) {
|
||||
discard;
|
||||
}
|
||||
} else if (alpha < (u_alphaClip ? 0.5f : 0.15f)) {
|
||||
if (alpha < u_alphaThreshold) {
|
||||
discard;
|
||||
}
|
||||
f_color = a_color * texColor;
|
||||
|
||||
#ifndef ADVANCED_RENDER
|
||||
#ifdef ADVANCED_RENDER
|
||||
if (u_forward) {
|
||||
if (u_forwardLight) {
|
||||
f_color.rgb *= calc_deferred_light(
|
||||
a_modelpos, a_realnormal, length(a_position), a_emission, 1.0
|
||||
);
|
||||
}
|
||||
vec3 fogColor = texture(u_skybox, a_dir).rgb;
|
||||
f_color.rgb = mix(f_color.rgb, fogColor, a_fog);
|
||||
}
|
||||
#else
|
||||
vec3 fogColor = texture(u_skybox, a_dir).rgb;
|
||||
f_color = mix(f_color, vec4(fogColor, 1.0), a_fog);
|
||||
f_color.rgb = mix(f_color.rgb, fogColor, a_fog);
|
||||
#endif
|
||||
|
||||
f_color.a = alpha;
|
||||
f_color.a = u_opaque ? 1.0 : alpha;
|
||||
f_position = vec4(a_position, 1.0);
|
||||
f_normal = vec4(a_normal, 1.0);
|
||||
f_emission = vec4(vec3(a_emission), 1.0);
|
||||
|
|
|
|||
24
res/shaders/lib/deferred_light.glsl
Normal file
24
res/shaders/lib/deferred_light.glsl
Normal file
|
|
@ -0,0 +1,24 @@
|
|||
#ifndef DEFERRED_LIGHT_GLSL_
|
||||
#define DEFERRED_LIGHT_GLSL_
|
||||
|
||||
#include <shadows>
|
||||
|
||||
#define COLOR_TEMPERATURE_COMPENSATION 1.14f
|
||||
|
||||
vec3 calc_deferred_light(
|
||||
vec4 modelpos, vec3 normal, float distance, float emission, float ao
|
||||
) {
|
||||
float light = ao;
|
||||
float rg = 1.0;
|
||||
#ifdef ENABLE_SHADOWS
|
||||
float shadow_mul = calc_shadow(modelpos, normal, distance);
|
||||
light *= shadow_mul;
|
||||
rg *= min(1.0, shadow_mul + emission) * COLOR_TEMPERATURE_COMPENSATION;
|
||||
light *= 2.0;
|
||||
#endif
|
||||
light = max(light, emission);
|
||||
light = pow(light, u_gamma);
|
||||
return light * vec3(rg, 0.5 + rg * 0.5, 1.0);
|
||||
}
|
||||
|
||||
#endif // DEFERRED_LIGHT_GLSL_
|
||||
55
res/shaders/translucent_composite.glslf
Normal file
55
res/shaders/translucent_composite.glslf
Normal file
|
|
@ -0,0 +1,55 @@
|
|||
in vec2 v_uv;
|
||||
out vec4 f_color;
|
||||
|
||||
#define LAYERS_COUNT 3
|
||||
|
||||
uniform sampler2D u_layerColor0;
|
||||
uniform sampler2D u_layerColor1;
|
||||
uniform sampler2D u_layerColor2;
|
||||
uniform sampler2D u_layerDepth0;
|
||||
uniform sampler2D u_layerDepth1;
|
||||
uniform sampler2D u_layerDepth2;
|
||||
|
||||
void main() {
|
||||
vec4 colors[LAYERS_COUNT];
|
||||
float depths[LAYERS_COUNT];
|
||||
colors[0] = texture(u_layerColor0, v_uv);
|
||||
colors[1] = texture(u_layerColor1, v_uv);
|
||||
colors[2] = texture(u_layerColor2, v_uv);
|
||||
depths[0] = texture(u_layerDepth0, v_uv).r;
|
||||
depths[1] = texture(u_layerDepth1, v_uv).r;
|
||||
depths[2] = texture(u_layerDepth2, v_uv).r;
|
||||
|
||||
vec4 layerColors[LAYERS_COUNT];
|
||||
float layerDepths[LAYERS_COUNT];
|
||||
int count = 0;
|
||||
for (int i = 0; i < LAYERS_COUNT; i++) {
|
||||
if (colors[i].a > 0.0) {
|
||||
layerColors[count] = colors[i];
|
||||
layerDepths[count] = depths[i];
|
||||
count++;
|
||||
}
|
||||
}
|
||||
if (count == 0) {
|
||||
discard;
|
||||
}
|
||||
|
||||
for (int i = 1; i < count; i++) {
|
||||
vec4 color = layerColors[i];
|
||||
float depth = layerDepths[i];
|
||||
int j = i - 1;
|
||||
while (j >= 0 && layerDepths[j] < depth) {
|
||||
layerColors[j + 1] = layerColors[j];
|
||||
layerDepths[j + 1] = layerDepths[j];
|
||||
j--;
|
||||
}
|
||||
layerColors[j + 1] = color;
|
||||
layerDepths[j + 1] = depth;
|
||||
}
|
||||
|
||||
vec4 result = vec4(0.0);
|
||||
for (int i = 0; i < count; i++) {
|
||||
result = result * (1.0 - layerColors[i].a) + layerColors[i];
|
||||
}
|
||||
f_color = result;
|
||||
}
|
||||
8
res/shaders/translucent_composite.glslv
Normal file
8
res/shaders/translucent_composite.glslv
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
layout (location = 0) in vec2 v_position;
|
||||
|
||||
out vec2 v_uv;
|
||||
|
||||
void main() {
|
||||
v_uv = v_position * 0.5 + 0.5;
|
||||
gl_Position = vec4(v_position, 0.0, 1.0);
|
||||
}
|
||||
|
|
@ -180,7 +180,8 @@ void SkeletonConfig::render(
|
|||
ModelLightingMode lightingMode,
|
||||
const glm::mat3& rotation,
|
||||
const glm::vec3& position,
|
||||
const glm::vec3& scale
|
||||
const glm::vec3& scale,
|
||||
bool translucent
|
||||
) const {
|
||||
if (skeleton.config->root == nullptr) {
|
||||
return;
|
||||
|
|
@ -220,7 +221,8 @@ void SkeletonConfig::render(
|
|||
)
|
||||
: glm::vec3(),
|
||||
model,
|
||||
&skeleton.textures
|
||||
&skeleton.textures,
|
||||
translucent
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -147,7 +147,8 @@ namespace rigging {
|
|||
ModelLightingMode lightingMode,
|
||||
const glm::mat3& rotation,
|
||||
const glm::vec3& position,
|
||||
const glm::vec3& scale
|
||||
const glm::vec3& scale,
|
||||
bool translucent = false
|
||||
) const;
|
||||
|
||||
Skeleton instance() const {
|
||||
|
|
|
|||
|
|
@ -19,6 +19,14 @@ static void set_blend_mode(BlendMode mode) {
|
|||
case BlendMode::inversion:
|
||||
glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_ONE_MINUS_SRC_ALPHA);
|
||||
break;
|
||||
case BlendMode::coverage:
|
||||
glBlendFuncSeparate(
|
||||
GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA
|
||||
);
|
||||
break;
|
||||
case BlendMode::premultiplied:
|
||||
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
146
src/graphics/core/TranslucentLayers.cpp
Normal file
146
src/graphics/core/TranslucentLayers.cpp
Normal file
|
|
@ -0,0 +1,146 @@
|
|||
#include "TranslucentLayers.hpp"
|
||||
|
||||
#include <GL/glew.h>
|
||||
#include <string>
|
||||
|
||||
#include "Mesh.hpp"
|
||||
#include "PostProcessing.hpp"
|
||||
#include "Shader.hpp"
|
||||
#include "debug/Logger.hpp"
|
||||
|
||||
static debug::Logger logger("gl-translucent-layers");
|
||||
|
||||
inline constexpr int FIRST_TEXTURE_UNIT = advanced_pipeline::TARGETS_COUNT;
|
||||
|
||||
static void setup_texture(uint texture) {
|
||||
glBindTexture(GL_TEXTURE_2D, texture);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
|
||||
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
|
||||
}
|
||||
|
||||
TranslucentLayers::Layer::Layer() {
|
||||
glGenFramebuffers(1, &fbo);
|
||||
glGenTextures(1, &color);
|
||||
glGenTextures(1, &depth);
|
||||
}
|
||||
|
||||
TranslucentLayers::Layer::~Layer() {
|
||||
glDeleteFramebuffers(1, &fbo);
|
||||
glDeleteTextures(1, &color);
|
||||
glDeleteTextures(1, &depth);
|
||||
}
|
||||
|
||||
void TranslucentLayers::Layer::bind() const {
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
|
||||
}
|
||||
|
||||
void TranslucentLayers::Layer::unbind() const {
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
}
|
||||
|
||||
void TranslucentLayers::Layer::allocate(uint width, uint height) {
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, fbo);
|
||||
|
||||
setup_texture(color);
|
||||
glTexImage2D(
|
||||
GL_TEXTURE_2D, 0, GL_RGBA8, width, height, 0,
|
||||
GL_RGBA, GL_UNSIGNED_BYTE, nullptr
|
||||
);
|
||||
glFramebufferTexture2D(
|
||||
GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, color, 0
|
||||
);
|
||||
|
||||
setup_texture(depth);
|
||||
glTexImage2D(
|
||||
GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24, width, height, 0,
|
||||
GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, nullptr
|
||||
);
|
||||
glFramebufferTexture2D(
|
||||
GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, depth, 0
|
||||
);
|
||||
|
||||
GLenum attachments[1] = {GL_COLOR_ATTACHMENT0};
|
||||
glDrawBuffers(1, attachments);
|
||||
|
||||
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE) {
|
||||
logger.error() << "translucent layer framebuffer is not complete!";
|
||||
}
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, 0);
|
||||
}
|
||||
|
||||
TranslucentLayers::TranslucentLayers(size_t count, uint width, uint height)
|
||||
: width(width), height(height) {
|
||||
for (size_t i = 0; i < count; i++) {
|
||||
auto layer = std::make_unique<Layer>();
|
||||
layer->allocate(width, height);
|
||||
layers.push_back(std::move(layer));
|
||||
}
|
||||
PostProcessingVertex meshData[] {
|
||||
{{-1.0f, -1.0f}},
|
||||
{{-1.0f, 1.0f}},
|
||||
{{1.0f, 1.0f}},
|
||||
{{-1.0f, -1.0f}},
|
||||
{{1.0f, 1.0f}},
|
||||
{{1.0f, -1.0f}},
|
||||
};
|
||||
quadMesh = std::make_unique<Mesh<PostProcessingVertex>>(meshData, 6);
|
||||
}
|
||||
|
||||
TranslucentLayers::~TranslucentLayers() = default;
|
||||
|
||||
void TranslucentLayers::resize(uint width, uint height) {
|
||||
if (this->width == width && this->height == height) {
|
||||
return;
|
||||
}
|
||||
this->width = width;
|
||||
this->height = height;
|
||||
for (auto& layer : layers) {
|
||||
layer->allocate(width, height);
|
||||
}
|
||||
}
|
||||
|
||||
void TranslucentLayers::prepare(uint sourceFbo) {
|
||||
const GLfloat transparent[4] {0.0f, 0.0f, 0.0f, 0.0f};
|
||||
for (const auto& layer : layers) {
|
||||
glBindFramebuffer(GL_READ_FRAMEBUFFER, sourceFbo);
|
||||
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, layer->fbo);
|
||||
glBlitFramebuffer(
|
||||
0, 0, width, height, 0, 0, width, height,
|
||||
GL_DEPTH_BUFFER_BIT, GL_NEAREST
|
||||
);
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, layer->fbo);
|
||||
glClearBufferfv(GL_COLOR, 0, transparent);
|
||||
}
|
||||
glBindFramebuffer(GL_FRAMEBUFFER, sourceFbo);
|
||||
}
|
||||
|
||||
Bindable& TranslucentLayers::getLayer(size_t index) {
|
||||
return *layers.at(index);
|
||||
}
|
||||
|
||||
void TranslucentLayers::compose(Shader& shader) {
|
||||
shader.use();
|
||||
int count = static_cast<int>(layers.size());
|
||||
for (int i = 0; i < count; i++) {
|
||||
int colorUnit = FIRST_TEXTURE_UNIT + i;
|
||||
int depthUnit = FIRST_TEXTURE_UNIT + count + i;
|
||||
|
||||
glActiveTexture(GL_TEXTURE0 + colorUnit);
|
||||
glBindTexture(GL_TEXTURE_2D, layers[i]->color);
|
||||
shader.uniform1i("u_layerColor" + std::to_string(i), colorUnit);
|
||||
|
||||
glActiveTexture(GL_TEXTURE0 + depthUnit);
|
||||
glBindTexture(GL_TEXTURE_2D, layers[i]->depth);
|
||||
shader.uniform1i("u_layerDepth" + std::to_string(i), depthUnit);
|
||||
}
|
||||
quadMesh->draw();
|
||||
|
||||
for (int i = 0; i < count * 2; i++) {
|
||||
glActiveTexture(GL_TEXTURE0 + FIRST_TEXTURE_UNIT + i);
|
||||
glBindTexture(GL_TEXTURE_2D, 0);
|
||||
}
|
||||
glActiveTexture(GL_TEXTURE0);
|
||||
}
|
||||
46
src/graphics/core/TranslucentLayers.hpp
Normal file
46
src/graphics/core/TranslucentLayers.hpp
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
#pragma once
|
||||
|
||||
#include "typedefs.hpp"
|
||||
#include "commons.hpp"
|
||||
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
|
||||
template<typename VertexStructure> class Mesh;
|
||||
struct PostProcessingVertex;
|
||||
class Shader;
|
||||
|
||||
class TranslucentLayers {
|
||||
public:
|
||||
TranslucentLayers(size_t count, uint width, uint height);
|
||||
~TranslucentLayers();
|
||||
|
||||
void resize(uint width, uint height);
|
||||
|
||||
void prepare(uint sourceFbo);
|
||||
|
||||
Bindable& getLayer(size_t index);
|
||||
|
||||
void compose(Shader& shader);
|
||||
|
||||
private:
|
||||
class Layer : public Bindable {
|
||||
public:
|
||||
uint fbo = 0;
|
||||
uint color = 0;
|
||||
uint depth = 0;
|
||||
|
||||
Layer();
|
||||
~Layer() override;
|
||||
|
||||
void bind() const override;
|
||||
void unbind() const override;
|
||||
|
||||
void allocate(uint width, uint height);
|
||||
};
|
||||
|
||||
uint width;
|
||||
uint height;
|
||||
std::vector<std::unique_ptr<Layer>> layers;
|
||||
std::unique_ptr<Mesh<PostProcessingVertex>> quadMesh;
|
||||
};
|
||||
|
|
@ -17,7 +17,11 @@ enum class BlendMode {
|
|||
/// @brief Additive blending mode.
|
||||
addition,
|
||||
/// @brief Subtractive blending mode.
|
||||
inversion
|
||||
inversion,
|
||||
/// @brief Normal color blending with alpha accumulated as coverage.
|
||||
coverage,
|
||||
/// @brief Blending of premultiplied-alpha color.
|
||||
premultiplied
|
||||
};
|
||||
|
||||
/// @brief Standard GLFW 3.4 cursor shapes (same order and count as in GLFW).
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -506,7 +506,8 @@ void Entities::render(
|
|||
def.lightingMode,
|
||||
transform.rot,
|
||||
pos,
|
||||
size
|
||||
size,
|
||||
def.translucent
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue