Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix: consider player walking while using map shader #15

Merged
merged 1 commit into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added data/images/shaders/snow.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions data/shaders/map_snow_fragment.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@

varying vec2 v_TexCoord;

uniform vec4 u_Color;
uniform sampler2D u_Tex0;
uniform sampler2D u_Tex1;
uniform float u_Time;
uniform vec2 u_WalkOffset;

vec2 snowDirection = vec2(1.0, 0.2);
float snowSpeed = 0.08;
float snowPressure = 0.4;
float snowZoom = 0.6;

void main()
{
gl_FragColor = texture2D(u_Tex0, v_TexCoord) * u_Color;

vec2 snowCoord = (v_TexCoord + vec2(u_WalkOffset.x, u_WalkOffset.y) + (snowDirection * u_Time * snowSpeed)) / snowZoom;
vec4 effectPixel = texture2D(u_Tex1, snowCoord) * snowPressure;

gl_FragColor += (effectPixel.rgba);
if(gl_FragColor.a < 0.01)
discard;
}
19 changes: 19 additions & 0 deletions data/shaders/map_snow_vertex.frag
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
attribute vec2 a_TexCoord;
attribute vec2 a_Vertex;

varying vec2 v_TexCoord;
varying vec2 v_TexCoord2;

uniform mat3 u_TextureMatrix;
uniform mat3 u_TransformMatrix;
uniform mat3 u_ProjectionMatrix;

uniform vec2 u_Offset;
uniform vec2 u_Center;

void main()
{
gl_Position = vec4((u_ProjectionMatrix * u_TransformMatrix * vec3(a_Vertex.xy, 1.0)).xy, 1.0, 1.0);
v_TexCoord = (u_TextureMatrix * vec3(a_TexCoord,1.0)).xy;
}

3 changes: 3 additions & 0 deletions modules/game_shaders/shaders.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ function init()

g_shaders.createShader("map_rainbow", "/shaders/map_rainbow_vertex", "/shaders/map_rainbow_fragment")
g_shaders.addTexture("map_rainbow", "/images/shaders/rainbow.png")

g_shaders.createShader("map_snow", "/shaders/map_snow_vertex", "/shaders/map_snow_fragment")
g_shaders.addTexture("map_snow", "/images/shaders/snow.png")

-- use modules.game_interface.gameMapPanel:setShader("map_rainbow") to set shader

Expand Down
22 changes: 21 additions & 1 deletion src/client/mapview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,27 @@ void MapView::drawMapBackground(const Rect& rect, const TilePtr& crosshairTile)
g_drawQueue->setOpacity(floorStart, fading);
}

}
if(!m_shader.empty() && isFollowingCreature()) {
g_drawQueue->setShader(m_shader);

Point walkOffset = transformPositionTo2D(getCameraPosition(), m_shaderPosition);
walkOffset.y = -walkOffset.y;

g_drawQueue->setWalkOffset(
PointF(
(walkOffset.x / static_cast<float>(m_optimizedSize.width())),
(walkOffset.y / static_cast<float>(m_optimizedSize.height()))
)
);
}
}

void MapView::setShader(const std::string& shader)
{
m_shader = shader;
if (!m_shader.empty())
m_shaderPosition = getCameraPosition();
}

void MapView::drawFloor(short floor, const Position& cameraPosition, const TilePtr& crosshairTile)
{
Expand Down
10 changes: 8 additions & 2 deletions src/client/mapview.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,8 +113,11 @@ class MapView : public LuaObject
void setFloorFading(int value) { m_floorFading = value; }
void setCrosshair(const std::string& file);

//void setShader(const PainterShaderProgramPtr& shader, float fadein, float fadeout);
//PainterShaderProgramPtr getShader() { return m_shader; }
void setShader(const std::string& shader);
std::string getShader()
{
return m_shader;
}

Position getPosition(const Point& point, const Size& mapSize);

Expand Down Expand Up @@ -156,6 +159,9 @@ class MapView : public LuaObject
bool m_drawPlayerBars = true;
stdext::boolean<true> m_smooth;

std::string m_shader;
Position m_shaderPosition;

stdext::timer m_fadingFloorTimers[Otc::MAX_Z + 1];

stdext::boolean<true> m_follow;
Expand Down
3 changes: 0 additions & 3 deletions src/client/uimap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,6 @@ void UIMap::drawSelf(Fw::DrawPane drawPane)
g_drawQueue->markMapPosition();
} else if(drawPane == Fw::MapBackgroundPane) {
m_mapView->drawMapBackground(m_mapRect, getTile(m_mousePosition));
if (!m_shader.empty()) {
g_drawQueue->setShader(m_shader);
}
} else if (drawPane == Fw::MapForegroundPane) {
m_mapView->drawMapForeground(m_mapRect);
}
Expand Down
8 changes: 5 additions & 3 deletions src/client/uimap.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,14 @@ class UIMap : public UIWidget

void setShader(const std::string& shader)
{
m_shader = shader;
if(m_mapView)
m_mapView->setShader(shader);
}
std::string getShader()
{
return m_shader;
if(m_mapView)
return m_mapView->getShader();
return "";
}

protected:
Expand All @@ -115,7 +118,6 @@ class UIMap : public UIWidget
bool m_limitVisibleRange;
int m_maxZoomIn;
int m_maxZoomOut;
std::string m_shader;
};

#endif
5 changes: 5 additions & 0 deletions src/framework/core/graphicalapplication.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,11 @@ void GraphicalApplication::run()
PainterShaderProgramPtr shader = nullptr;
if (!toDrawMapQueue->getShader().empty()) {
shader = g_shaders.getShader(toDrawMapQueue->getShader());

if(shader) {
auto walkOffset = toDrawMapQueue->getWalkOffset();
shader->updateWalkOffset(walkOffset);
}
}
if (shader) {
g_painter->setShaderProgram(shader);
Expand Down
11 changes: 11 additions & 0 deletions src/framework/graphics/drawqueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,16 @@ class DrawQueue {
return m_shader;
}

void setWalkOffset(const PointF& offset)
{
m_walkOffset = offset;
}

const PointF& getWalkOffset()
{
return m_walkOffset;
}

private:
std::vector<DrawQueueItem*> m_queue;
std::vector<DrawQueueCondition*> m_conditions;
Expand All @@ -336,6 +346,7 @@ class DrawQueue {
bool m_useFrameBuffer = false;
float m_scaling = 1.f;
std::string m_shader;
PointF m_walkOffset;

friend struct DrawQueueConditionMark;
};
Expand Down
14 changes: 14 additions & 0 deletions src/framework/graphics/paintershaderprogram.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ void PainterShaderProgram::setupUniforms()
bindUniformLocation(OFFSET_UNIFORM, "u_Offset");
bindUniformLocation(CENTER_UNIFORM, "u_Center");

bindUniformLocation(MAP_WALKOFFSET_UNIFORM, "u_WalkOffset");

// VALUES
setUniformValue(TRANSFORM_MATRIX_UNIFORM, m_transformMatrix);
setUniformValue(PROJECTION_MATRIX_UNIFORM, m_projectionMatrix);
Expand All @@ -80,6 +82,7 @@ void PainterShaderProgram::setupUniforms()
setUniformValue(RESOLUTION_UNIFORM, (float)m_resolution.width(), (float)m_resolution.height());
setUniformValue(OFFSET_UNIFORM, (float)m_offset.x, (float)m_offset.y);
setUniformValue(CENTER_UNIFORM, (float)m_center.x, (float)m_center.y);
setUniformValue(MAP_WALKOFFSET_UNIFORM, (float)m_walkOffset.x, (float)m_walkOffset.y);
}

bool PainterShaderProgram::link()
Expand Down Expand Up @@ -202,6 +205,17 @@ void PainterShaderProgram::updateTime()
m_time = time;
}

void PainterShaderProgram::updateWalkOffset(const PointF& offset)
{
if (m_walkOffset == offset)
return;

m_walkOffset = offset;

bind();
setUniformValue(MAP_WALKOFFSET_UNIFORM, m_walkOffset.x, m_walkOffset.y);
}

void PainterShaderProgram::addMultiTexture(const std::string& file)
{
if (m_multiTextures.size() > 3)
Expand Down
5 changes: 4 additions & 1 deletion src/framework/graphics/paintershaderprogram.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@ class PainterShaderProgram : public ShaderProgram {

RESOLUTION_UNIFORM = 13,
OFFSET_UNIFORM = 14,
CENTER_UNIFORM = 15
CENTER_UNIFORM = 15,
MAP_WALKOFFSET_UNIFORM = 16
};

friend class Painter;
Expand All @@ -75,6 +76,7 @@ class PainterShaderProgram : public ShaderProgram {
void setOffset(const Point& offset);
void setCenter(const Point& center);
void updateTime();
void updateWalkOffset(const PointF& offset);

void addMultiTexture(const std::string& file);
void bindMultiTextures();
Expand All @@ -95,6 +97,7 @@ class PainterShaderProgram : public ShaderProgram {
Matrix3 m_textureMatrix;
Size m_resolution;
Point m_offset;
PointF m_walkOffset;
Point m_center;
float m_time;
std::vector<TexturePtr> m_multiTextures;
Expand Down
Loading