Skip to content

Commit

Permalink
[Lua API] Added properties 'fog_color' and 'fog_depth'.
Browse files Browse the repository at this point in the history
  • Loading branch information
Unarelith committed Jun 29, 2020
1 parent bd7fa53 commit e919cbb
Show file tree
Hide file tree
Showing 11 changed files with 88 additions and 6 deletions.
18 changes: 18 additions & 0 deletions docs/lua-api-block.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,24 @@ Example:
draw_type = "solid" -- this is the default value
```

### `fog_color`

Color of the fog drawn for players inside the block.

Example:
```lua
fog_color = {255, 0, 255}
```

### `fog_depth`

Depth of the fog drawn for players inside the block.

Example:
```lua
fog_depth = 20
```

### `groups`

Groups of the block. They can be used in recipes, and can also filter Lua-defined inventory widgets.
Expand Down
2 changes: 1 addition & 1 deletion external/gamekit
8 changes: 8 additions & 0 deletions mods/default/blocks.lua
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,15 @@ mod:block {
mod:block {
id = "water",
name = "Water",

tiles = "water.png",
color_multiplier = {51, 115, 255, 217},

draw_type = "liquid",
is_opaque = false,

fog_depth = 20.0,
fog_color = {0, 128, 255},
}

mod:block {
Expand Down Expand Up @@ -237,6 +242,9 @@ mod:block {
draw_type = "liquid",
is_light_source = true,
is_opaque = false,

fog_depth = 10.0,
fog_color = {255, 128, 0},
}

mod:block {
Expand Down
11 changes: 8 additions & 3 deletions resources/shaders/screen.f.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@ varying vec2 v_coord2d;

uniform sampler2D screenTexture;
uniform sampler2D depthTexture;
uniform int effectType;

uniform int u_effectType;
uniform float u_fogDepth;
uniform vec4 u_fogColor;

void main() {
vec4 color = texture2D(screenTexture, v_texCoord);
float depth = texture2D(depthTexture, v_texCoord).r;

// Underwater effect
if (effectType == 1)
color.rgb = mix(color.rgb, vec3(0, 0.5, 1), clamp(pow(depth, 20.0), 0.0, 1.0));
if (u_effectType == 1) {
/* color.rgb = mix(color.rgb, vec3(0, 0.5, 1), clamp(pow(depth, 20.0), 0.0, 1.0)); */
color.rgb = mix(color.rgb, u_fogColor.rgb, clamp(pow(depth, u_fogDepth), 0.0, 1.0));
}

// Grayscale
/* float average = (color.r + color.g + color.b) / 3.0; // Basic */
Expand Down
5 changes: 5 additions & 0 deletions source/client/core/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,11 @@ u8 Config::mouseSensitivity = 8;
std::string Config::defaultUsername = "";
std::string Config::defaultServerAddress = "localhost:4242";

// Temporary
u16 Config::currentScreenEffect = 0;
float Config::fogDepth = 0;
gk::Color Config::fogColor = gk::Color::White;

void Config::loadConfigFromFile(const char *filename) {
if (gk::Filesystem::fileExists(filename)) {
sol::state lua;
Expand Down
6 changes: 6 additions & 0 deletions source/client/core/Config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <string>

#include <gk/core/IntTypes.hpp>
#include <gk/graphics/Color.hpp>

namespace Config {
// Gameplay
Expand Down Expand Up @@ -63,6 +64,11 @@ namespace Config {
extern std::string defaultUsername;
extern std::string defaultServerAddress;

// Temporary
extern u16 currentScreenEffect;
extern float fogDepth;
extern gk::Color fogColor;

void loadConfigFromFile(const char *filename);
void saveConfigToFile(const char *filename);
}
Expand Down
5 changes: 5 additions & 0 deletions source/client/graphics/Framebuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <gk/core/Exception.hpp>
#include <gk/gl/GLCheck.hpp>

#include "Config.hpp"
#include "Framebuffer.hpp"

Framebuffer::Framebuffer(u16 width, u16 height) {
Expand Down Expand Up @@ -150,6 +151,10 @@ void Framebuffer::end() const {
}

gk::Shader::bind(&m_shader);
m_shader.setUniform("u_effectType", Config::currentScreenEffect);
m_shader.setUniform("u_fogDepth", Config::fogDepth);
m_shader.setUniform("u_fogColor", Config::fogColor);

gk::VertexBuffer::bind(&m_vbo);

glEnableVertexAttribArray(0);
Expand Down
12 changes: 12 additions & 0 deletions source/client/world/ClientPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,18 @@ void ClientPlayer::updatePosition(const ClientWorld &world) {

if (Config::isFlyModeEnabled)
m_velocity.z = 0.f;

// Checking to block at camera position to enable specific effects
u16 blockID = world.getBlock(m_camera.getDPosition().x, m_camera.getDPosition().y, m_camera.getDPosition().z);
const Block &block = Registry::getInstance().getBlock(blockID);
if (block.fogDepth() != 0) {
Config::currentScreenEffect = 1;
Config::fogDepth = block.fogDepth();
Config::fogColor = block.fogColor();
}
else {
Config::currentScreenEffect = 0;
}
}

void ClientPlayer::setPosition(double x, double y, double z) {
Expand Down
6 changes: 4 additions & 2 deletions source/common/world/Block.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,8 @@ void Block::serialize(sf::Packet &packet) const {
packet << u32(m_id) << m_stringID << m_label << u8(m_drawType)
<< m_hardness << m_harvestRequirements << m_itemDrop << m_itemDropAmount << m_tiles
<< m_boundingBox << m_isOpaque << m_isLightSource << m_canUpdate << m_canBeActivated
<< m_colorMultiplier << m_isRotatable << m_inventoryImage << m_groups;
<< m_colorMultiplier << m_isRotatable << m_inventoryImage << m_groups
<< m_fogDepth << m_fogColor;
}

void Block::deserialize(sf::Packet &packet) {
Expand All @@ -54,7 +55,8 @@ void Block::deserialize(sf::Packet &packet) {
packet >> id >> m_stringID >> m_label >> drawType
>> m_hardness >> m_harvestRequirements >> m_itemDrop >> m_itemDropAmount >> m_tiles
>> m_boundingBox >> m_isOpaque >> m_isLightSource >> m_canUpdate >> m_canBeActivated
>> m_colorMultiplier >> m_isRotatable >> m_inventoryImage >> m_groups;
>> m_colorMultiplier >> m_isRotatable >> m_inventoryImage >> m_groups
>> m_fogDepth >> m_fogColor;

m_id = id;
m_drawType = BlockDrawType(drawType);
Expand Down
9 changes: 9 additions & 0 deletions source/common/world/Block.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,12 @@ class Block : public gk::ISerializable {
return it->second;
}

float fogDepth() const { return m_fogDepth; }
void setFogDepth(float fogDepth) { m_fogDepth = fogDepth; }

const gk::Color &fogColor() const { return m_fogColor; }
void setFogColor(const gk::Color &fogColor) { m_fogColor = fogColor; }

static void initUsertype(sol::state &lua);

protected:
Expand Down Expand Up @@ -156,6 +162,9 @@ class Block : public gk::ISerializable {
std::string m_inventoryImage;

std::unordered_map<std::string, u16> m_groups;

float m_fogDepth = 0;
gk::Color m_fogColor = gk::Color::White;
};

#endif // BLOCK_HPP_
12 changes: 12 additions & 0 deletions source/server/lua/loader/LuaBlockLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,18 @@ inline void LuaBlockLoader::loadProperties(ServerBlock &block, const sol::table
block.setOnBlockDestroyed(table["on_block_destroyed"]);
block.setRotatable(table["is_rotatable"].get_or(false));
block.setInventoryImage(table["inventory_image"].get_or<std::string>(""));
block.setFogDepth(table["fog_depth"].get_or<float>(0));

if (block.fogDepth()) {
sol::optional<sol::table> fogColor = table["fog_color"];
if (fogColor != sol::nullopt) {
block.setFogColor(gk::Color{
fogColor.value().get<u8>(1),
fogColor.value().get<u8>(2),
fogColor.value().get<u8>(3),
});
}
}
}

inline void LuaBlockLoader::loadBoundingBox(ServerBlock &block, const sol::table &table) const {
Expand Down

0 comments on commit e919cbb

Please sign in to comment.