Skip to content

Commit 734c8bf

Browse files
committed
Make a liquid glass effect shader
Add a new sandbox layer featuring an effect known as "Liquid Glass" in Apple's new design pattern which is currently implemented as a dirty shader trick mixed with the standard batch rendering shader.
1 parent a42dd09 commit 734c8bf

File tree

15 files changed

+491
-45
lines changed

15 files changed

+491
-45
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,4 @@ OverEngineProfile-Shutdown.json
3737
Sandbox/SandboxScene.oes
3838
OverEngine.log
3939
imgui.ini
40+
/build.sh

OverEngine/src/OverEngine/Renderer/FrameBuffer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ namespace OverEngine
2525
virtual void Resize(uint32_t width, uint32_t height) = 0;
2626

2727
virtual uint32_t GetColorAttachmentRendererID() const = 0;
28+
virtual void BindColorAttachment(uint32_t slot) const = 0;
2829

2930
virtual const FrameBufferProps& GetProps() const = 0;
3031
};

OverEngine/src/OverEngine/Renderer/Renderer2D.cpp

Lines changed: 36 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,11 @@ namespace OverEngine
1616

1717
Color a_Color = Color(1.0f);
1818
int a_TexSlot = -1;
19-
Vector4 a_TexCoord = Vector4(0.0f);
19+
Vector4 a_TexCoord = Vector4(0, 0, 1, 1);
2020
Vector4 a_TexRegion = Vector4(0.0f);
2121
int a_TexRepeat = 0;
22+
23+
int a_LiquidGlass = false;
2224
};
2325

2426
// Hard-coded Limits
@@ -66,7 +68,9 @@ namespace OverEngine
6668
{ ShaderDataType::Int, "a_TexSlot" },
6769
{ ShaderDataType::Float4, "a_TexCoord" },
6870
{ ShaderDataType::Float4, "a_TexRegion" },
69-
{ ShaderDataType::Int, "a_TexRepeat" }
71+
{ ShaderDataType::Int, "a_TexRepeat" },
72+
73+
{ ShaderDataType::Int, "a_LiquidGlass" }
7074
});
7175
s_Data->QuadVA->AddVertexBuffer(s_Data->QuadVB);
7276

@@ -87,15 +91,7 @@ namespace OverEngine
8791
s_Data->QuadBufferPtr = s_Data->QuadBufferBasePtr;
8892

8993
s_Data->Shader = Shader::Create("assets/shaders/BatchRenderer2D.glsl");
90-
{
91-
int textureIDs[MaxTextureCount];
92-
93-
for (int i = 0; i < (int)MaxTextureCount; i++)
94-
textureIDs[i] = i;
95-
96-
s_Data->Shader->Bind();
97-
s_Data->Shader->UploadUniformIntArray("u_Slots", textureIDs, MaxTextureCount);
98-
}
94+
InitShader();
9995

10096
s_Statistics.Reset();
10197
}
@@ -111,6 +107,22 @@ namespace OverEngine
111107
return s_Data->Shader;
112108
}
113109

110+
void Renderer2D::ReloadShader()
111+
{
112+
s_Data->Shader->Reload();
113+
InitShader();
114+
}
115+
116+
void Renderer2D::InitShader()
117+
{
118+
int textureIDs[MaxTextureCount];
119+
for (int i = 0; i < (int)MaxTextureCount; i++)
120+
textureIDs[i] = i;
121+
122+
s_Data->Shader->Bind();
123+
s_Data->Shader->UploadUniformIntArray("u_Slots", textureIDs, MaxTextureCount);
124+
}
125+
114126
void Renderer2D::Reset()
115127
{
116128
s_Data->QuadBufferPtr = s_Data->QuadBufferBasePtr;
@@ -188,17 +200,17 @@ namespace OverEngine
188200
DrawQuad(Vector3(position, 0.0f), rotation, size, color);
189201
}
190202

191-
void Renderer2D::DrawQuad(const Vector3& position, float rotation, const Vector2& size, const Color& color)
203+
void Renderer2D::DrawQuad(const Vector3& position, float rotation, const Vector2& size, const Color& color, int liquidGlass)
192204
{
193205
Mat4x4 transform =
194206
glm::translate(Mat4x4(1.0f), position) *
195207
glm::rotate(Mat4x4(1.0f), rotation, Vector3(0, 0, 1)) *
196208
glm::scale(Mat4x4(1.0f), Vector3(size, 1.0f));
197209

198-
DrawQuad(transform, color);
210+
DrawQuad(transform, color, liquidGlass);
199211
}
200212

201-
void Renderer2D::DrawQuad(const Mat4x4& transform, const Color& color)
213+
void Renderer2D::DrawQuad(const Mat4x4& transform, const Color& color, int liquidGlass)
202214
{
203215
if (color.a == 0)
204216
return;
@@ -212,10 +224,18 @@ namespace OverEngine
212224
s_Data->QuadBufferPtr->a_Position1 = Vector3(mat * Vector4( 0.5, -0.5, 0.0, 1.0));
213225
s_Data->QuadBufferPtr->a_Position2 = Vector3(mat * Vector4(-0.5, 0.5, 0.0, 1.0));
214226
s_Data->QuadBufferPtr->a_Position3 = Vector3(mat * Vector4( 0.5, 0.5, 0.0, 1.0));
215-
227+
228+
// OE_CORE_INFO("Emitting {}: ({}, {}) ({}, {}) ({}, {}) ({}, {})", liquidGlass,
229+
// s_Data->QuadBufferPtr->a_Position0.x, s_Data->QuadBufferPtr->a_Position0.y,
230+
// s_Data->QuadBufferPtr->a_Position1.x, s_Data->QuadBufferPtr->a_Position1.y,
231+
// s_Data->QuadBufferPtr->a_Position2.x, s_Data->QuadBufferPtr->a_Position2.y,
232+
// s_Data->QuadBufferPtr->a_Position3.x, s_Data->QuadBufferPtr->a_Position3.y);
233+
216234
s_Data->QuadBufferPtr->a_Color = color;
217235
s_Data->QuadBufferPtr->a_TexSlot = -1;
218236

237+
s_Data->QuadBufferPtr->a_LiquidGlass = liquidGlass;
238+
219239
s_Data->QuadBufferPtr++;
220240
s_Data->QuadCount++;
221241
s_Statistics.QuadCount++;
@@ -259,7 +279,7 @@ namespace OverEngine
259279
if (it == end)
260280
{
261281
uint8_t slot = s_Data->TextureCount;
262-
if (slot + 1u > RenderCommand::GetMaxTextureSlotCount())
282+
if (slot + 1u > MaxTextureCount)
263283
{
264284
NextBatch();
265285
slot = 0;

OverEngine/src/OverEngine/Renderer/Renderer2D.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,8 @@ namespace OverEngine
3737
static void Flush();
3838

3939
static void DrawQuad(const Vector2& position, float rotation, const Vector2& size, const Color& color);
40-
static void DrawQuad(const Vector3& position, float rotation, const Vector2& size, const Color& color);
41-
static void DrawQuad(const Mat4x4& transform, const Color& color);
40+
static void DrawQuad(const Vector3& position, float rotation, const Vector2& size, const Color& color, int liquidGlass = false);
41+
static void DrawQuad(const Mat4x4& transform, const Color& color, int liquidGlass = false);
4242

4343
static void DrawQuad(const Vector2& position, float rotation, const Vector2& size, const TexturedQuadProps& props = TexturedQuadProps());
4444
static void DrawQuad(const Vector3& position, float rotation, const Vector2& size, const TexturedQuadProps& props = TexturedQuadProps());
@@ -61,7 +61,9 @@ namespace OverEngine
6161

6262
static Statistics& GetStatistics() { return s_Statistics; }
6363
static Ref<Shader>& GetShader();
64+
static void ReloadShader();
6465
private:
66+
static void InitShader();
6567
static Statistics s_Statistics;
6668
};
6769
}

OverEngine/src/Platform/OpenGL/OpenGLFrameBuffer.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,8 @@ namespace OverEngine
6565

6666
Invalidate();
6767
}
68+
69+
void OpenGLFrameBuffer::BindColorAttachment(uint32_t slot) const {
70+
glBindTextureUnit(slot, m_ColorAttachment);
71+
}
6872
}

OverEngine/src/Platform/OpenGL/OpenGLFrameBuffer.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ namespace OverEngine
1818
virtual void Resize(uint32_t width, uint32_t height) override;
1919

2020
virtual uint32_t GetColorAttachmentRendererID() const override { return m_ColorAttachment; }
21+
virtual void BindColorAttachment(uint32_t slot) const override;
2122

2223
virtual const FrameBufferProps& GetProps() const override { return m_Props; }
2324
private:

OverEngine/src/Platform/OpenGL/OpenGLShader.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ namespace OverEngine
9191
{
9292
GLuint program = glCreateProgram();
9393

94-
OE_CORE_ASSERT(shaderSources.size() <= 3, "{0} shader sources got but 3 is maximim", shaderSources.size());
94+
OE_CORE_ASSERT(shaderSources.size() <= 3, "{0} shader sources got but 3 is the maximum!", shaderSources.size());
9595

9696
std::array<GLint, 3> glShaderIDs{ -1, -1, -1 };
9797
int glShaderIdIndex = 0;
@@ -239,6 +239,7 @@ namespace OverEngine
239239

240240
bool OpenGLShader::Reload(const String& filePath)
241241
{
242+
OE_CORE_INFO("Reloading shader from: {}", m_FilePath);
242243
if (filePath.empty())
243244
{
244245
if (m_FilePath.empty())

0 commit comments

Comments
 (0)