Skip to content

Commit 64db23f

Browse files
authored
Fixed parallax, atmosphere, and skysphere issues with ortho camera (#526)
1 parent 61f1ac9 commit 64db23f

File tree

11 files changed

+100
-5
lines changed

11 files changed

+100
-5
lines changed

Resources/Engine/Materials/Atmosphere.ovmat

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<root>
22
<shader>:Shaders\Atmosphere.ovfx</shader>
33
<settings>
4+
<support_orthographic>false</support_orthographic>
5+
<support_perspective>true</support_perspective>
46
<blendable>false</blendable>
57
<backface_culling>false</backface_culling>
68
<frontface_culling>true</frontface_culling>

Resources/Engine/Materials/Default.ovmat

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<root>
22
<shader>:Shaders\StandardPBR.ovfx</shader>
33
<settings>
4+
<support_orthographic>true</support_orthographic>
5+
<support_perspective>true</support_perspective>
46
<blendable>false</blendable>
57
<backface_culling>true</backface_culling>
68
<frontface_culling>false</frontface_culling>

Resources/Engine/Materials/Skysphere.ovmat

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
<root>
22
<shader>:Shaders\Skysphere.ovfx</shader>
33
<settings>
4+
<support_orthographic>false</support_orthographic>
5+
<support_perspective>true</support_perspective>
46
<blendable>false</blendable>
57
<backface_culling>false</backface_culling>
68
<frontface_culling>true</frontface_culling>

Resources/Engine/Shaders/Common/Utils.ovfxh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ vec2 TileAndOffsetTexCoords(vec2 texCoords, vec2 tiling, vec2 offset)
1212
return vec2(mod(texCoords.x * tiling.x, 1), mod(texCoords.y * tiling.y, 1)) + offset;
1313
}
1414

15+
bool IsOrthographic(mat4 projectionMatrix)
16+
{
17+
// In an orthographic projection matrix, the [3][3] element is 1.0
18+
// In a perspective projection matrix, it's 0.0
19+
return projectionMatrix[3][3] > 0.5;
20+
}
21+
1522
// Expects a height map with values in the range [0, 1].
1623
// 1.0 means the height is at the maximum depth, 0.0 means the height is at the minimum depth.
1724
vec2 ApplyParallaxOcclusionMapping(vec2 texCoords, sampler2D heightMap, vec3 tangentViewPos, vec3 tangentFragPos, float heightScale)
@@ -58,6 +65,13 @@ vec2 ApplyParallaxOcclusionMapping(vec2 texCoords, sampler2D heightMap, vec3 tan
5865
return finalTexCoords;
5966
}
6067

68+
bool IsParallaxOutOfBounds(vec2 texCoords, mat4 projectionMatrix)
69+
{
70+
return
71+
!IsOrthographic(projectionMatrix) && // No clipping in orthographic projection (not supported)
72+
(texCoords.x < 0.0 || texCoords.x > 1.0 || texCoords.y < 0.0 || texCoords.y > 1.0);
73+
}
74+
6175
// [Deprecated] Kept for backward compatibility. Prefer using `ApplyParallaxOcclusionMapping()` instead.
6276
vec2 ApplyParallaxMapping(vec2 texCoords, sampler2D heightMap, vec3 tangentViewPos, vec3 tangentFragPos, float heightScale)
6377
{

Resources/Engine/Shaders/Standard.ovfx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ uniform float u_AlphaClippingThreshold = 0.1;
8484
#endif
8585

8686
#if defined(SHADOW_PASS)
87+
#undef PARALLAX_MAPPING // Disable parallax mapping in shadow pass
8788
uniform float u_ShadowClippingThreshold = 0.5;
8889
#endif
8990

@@ -98,7 +99,7 @@ void main()
9899

99100
#if defined(PARALLAX_MAPPING)
100101
texCoords = ApplyParallaxOcclusionMapping(texCoords, u_HeightMap, fs_in.TangentViewPos, fs_in.TangentFragPos, u_HeightScale);
101-
if (u_ParallaxClipEdges && (texCoords.x < 0.0 || texCoords.x > 1.0 || texCoords.y < 0.0 || texCoords.y > 1.0))
102+
if (u_ParallaxClipEdges && IsParallaxOutOfBounds(texCoords, ubo_Projection))
102103
{
103104
discard;
104105
}

Resources/Engine/Shaders/StandardPBR.ovfx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ uniform float u_AlphaClippingThreshold = 0.1;
8686
#endif
8787

8888
#if defined(SHADOW_PASS)
89+
#undef PARALLAX_MAPPING // Disable parallax mapping in shadow pass
8990
uniform float u_ShadowClippingThreshold = 0.5;
9091
#endif
9192

@@ -100,7 +101,7 @@ void main()
100101

101102
#if defined(PARALLAX_MAPPING)
102103
texCoords = ApplyParallaxOcclusionMapping(texCoords, u_HeightMap, fs_in.TangentViewPos, fs_in.TangentFragPos, u_HeightScale);
103-
if (u_ParallaxClipEdges && (texCoords.x < 0.0 || texCoords.x > 1.0 || texCoords.y < 0.0 || texCoords.y > 1.0))
104+
if (u_ParallaxClipEdges && IsParallaxOutOfBounds(texCoords, ubo_Projection))
104105
{
105106
discard;
106107
}

Sources/Overload/OvCore/src/OvCore/Resources/Material.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ void OvCore::Resources::Material::OnSerialize(tinyxml2::XMLDocument& p_doc, tiny
2020
tinyxml2::XMLNode* settingsNode = p_doc.NewElement("settings");
2121
p_node->InsertEndChild(settingsNode);
2222

23+
Serializer::SerializeBoolean(p_doc, settingsNode, "support_orthographic", m_supportOrthographic);
24+
Serializer::SerializeBoolean(p_doc, settingsNode, "support_perspective", m_supportPerspective);
2325
Serializer::SerializeBoolean(p_doc, settingsNode, "blendable", m_blendable);
2426
Serializer::SerializeBoolean(p_doc, settingsNode, "backface_culling", m_backfaceCulling);
2527
Serializer::SerializeBoolean(p_doc, settingsNode, "frontface_culling", m_frontfaceCulling);
@@ -125,6 +127,8 @@ void OvCore::Resources::Material::OnDeserialize(tinyxml2::XMLDocument& p_doc, ti
125127

126128
if (settingsNode)
127129
{
130+
Serializer::DeserializeBoolean(p_doc, settingsNode, "support_orthographic", m_supportOrthographic);
131+
Serializer::DeserializeBoolean(p_doc, settingsNode, "support_perspective", m_supportPerspective);
128132
Serializer::DeserializeBoolean(p_doc, settingsNode, "blendable", m_blendable);
129133
Serializer::DeserializeBoolean(p_doc, settingsNode, "backface_culling", m_backfaceCulling);
130134
Serializer::DeserializeBoolean(p_doc, settingsNode, "frontface_culling", m_frontfaceCulling);

Sources/Overload/OvEditor/src/OvEditor/Panels/MaterialEditor.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,6 +335,8 @@ void OvEditor::Panels::MaterialEditor::GenerateMaterialSettingsContent()
335335
GUIDrawer::DrawBoolean(*m_materialSettingsColumns, "Shadow Casting", std::bind(&OvCore::Resources::Material::IsShadowCaster, m_target), std::bind(&OvCore::Resources::Material::SetCastShadows, m_target, std::placeholders::_1));
336336
GUIDrawer::DrawBoolean(*m_materialSettingsColumns, "Shadow Receiving", std::bind(&OvCore::Resources::Material::IsShadowReceiver, m_target), std::bind(&OvCore::Resources::Material::SetReceiveShadows, m_target, std::placeholders::_1));
337337
GUIDrawer::DrawBoolean(*m_materialSettingsColumns, "User Interface", std::bind(&OvCore::Resources::Material::IsUserInterface, m_target), std::bind(&OvCore::Resources::Material::SetUserInterface, m_target, std::placeholders::_1));
338+
GUIDrawer::DrawBoolean(*m_materialSettingsColumns, "Orthographic Support", std::bind(&OvCore::Resources::Material::SupportsOrthographic, m_target), std::bind(&OvCore::Resources::Material::SetOrthographicSupport, m_target, std::placeholders::_1));
339+
GUIDrawer::DrawBoolean(*m_materialSettingsColumns, "Perspective Support", std::bind(&OvCore::Resources::Material::SupportsPerspective, m_target), std::bind(&OvCore::Resources::Material::SetPerspectiveSupport, m_target, std::placeholders::_1));
338340
GUIDrawer::DrawScalar<int>(*m_materialSettingsColumns, "GPU Instances", std::bind(&OvCore::Resources::Material::GetGPUInstances, m_target), std::bind(&OvCore::Resources::Material::SetGPUInstances, m_target, std::placeholders::_1), 1.0f, 0, 100000);
339341
GUIDrawer::DrawScalar<int>(*m_materialSettingsColumns, "Draw Order", std::bind(&OvCore::Resources::Material::GetDrawOrder, m_target), std::bind(&OvCore::Resources::Material::SetDrawOrder, m_target, std::placeholders::_1), 1.0f, 0, 100000);
340342
}

Sources/Overload/OvRendering/include/OvRendering/Data/Material.h

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#include <OvRendering/HAL/TextureHandle.h>
1616
#include <OvRendering/Resources/Shader.h>
1717
#include <OvRendering/Resources/Texture.h>
18+
#include <OvRendering/Settings/EProjectionMode.h>
1819

1920
namespace OvRendering::Data
2021
{
@@ -132,6 +133,18 @@ namespace OvRendering::Data
132133
*/
133134
bool IsValid() const;
134135

136+
/**
137+
* Sets the shader support for orthographic projection
138+
* @param p_supportOrthographic
139+
*/
140+
void SetOrthographicSupport(bool p_supportOrthographic);
141+
142+
/**
143+
* Sets the shader support for perspective projection
144+
* @param p_supportPerspective
145+
*/
146+
void SetPerspectiveSupport(bool p_supportPerspective);
147+
135148
/**
136149
* Sets the draw order of the material
137150
* @param p_order
@@ -150,7 +163,6 @@ namespace OvRendering::Data
150163
*/
151164
void SetUserInterface(bool p_userInterface);
152165

153-
154166
/**
155167
* Defines if the material has backface culling
156168
* @param p_backfaceCulling
@@ -293,11 +305,30 @@ namespace OvRendering::Data
293305
*/
294306
bool SupportsFeature(const std::string& p_feature) const;
295307

308+
/**
309+
* Returns true if the material supports orthopgraphic projection
310+
*/
311+
bool SupportsOrthographic() const;
312+
313+
/**
314+
* Returns true if the material supports perspective projection
315+
*/
316+
bool SupportsPerspective() const;
317+
318+
/**
319+
* Returns true if the material supports the given projection mode
320+
* @param p_projectionMode
321+
*/
322+
bool SupportsProjectionMode(OvRendering::Settings::EProjectionMode p_projectionMode) const;
323+
296324
protected:
297325
OvRendering::Resources::Shader* m_shader = nullptr;
298326
PropertyMap m_properties;
299327
Data::FeatureSet m_features;
300328

329+
bool m_supportOrthographic = true;
330+
bool m_supportPerspective = true;
331+
301332
bool m_userInterface = false;
302333
bool m_blendable = false;
303334
bool m_backfaceCulling = true;

Sources/Overload/OvRendering/src/OvRendering/Core/ABaseRenderer.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,9 +185,12 @@ void OvRendering::Core::ABaseRenderer::DrawEntity(
185185
auto material = p_drawable.material;
186186
auto mesh = p_drawable.mesh;
187187

188-
const auto gpuInstances = material.value().GetGPUInstances();
188+
OVASSERT(material.has_value(), "Missing material instance!");
189189

190-
if (mesh && material && material->IsValid() && gpuInstances > 0)
190+
const auto gpuInstances = material->GetGPUInstances();
191+
const auto projectionMode = m_frameDescriptor.camera->GetProjectionMode();
192+
193+
if (mesh && material->IsValid() && material->SupportsProjectionMode(projectionMode) && gpuInstances > 0)
191194
{
192195
p_pso.depthWriting = p_drawable.stateMask.depthWriting;
193196
p_pso.colorWriting.mask = p_drawable.stateMask.colorWriting ? 0xFF : 0x00;

Sources/Overload/OvRendering/src/OvRendering/Data/Material.cpp

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,16 @@ bool OvRendering::Data::Material::IsValid() const
284284
return HasShader();
285285
}
286286

287+
void OvRendering::Data::Material::SetOrthographicSupport(bool p_supportOrthographic)
288+
{
289+
m_supportOrthographic = p_supportOrthographic;
290+
}
291+
292+
void OvRendering::Data::Material::SetPerspectiveSupport(bool p_supportPerspective)
293+
{
294+
m_supportPerspective = p_supportPerspective;
295+
}
296+
287297
void OvRendering::Data::Material::SetDrawOrder(int p_order)
288298
{
289299
m_drawOrder = p_order;
@@ -437,3 +447,26 @@ bool OvRendering::Data::Material::SupportsFeature(const std::string& p_feature)
437447
{
438448
return m_shader->GetFeatures().contains(p_feature);
439449
}
450+
451+
bool OvRendering::Data::Material::SupportsOrthographic() const
452+
{
453+
return m_supportOrthographic;
454+
}
455+
456+
bool OvRendering::Data::Material::SupportsPerspective() const
457+
{
458+
return m_supportPerspective;
459+
}
460+
461+
bool OvRendering::Data::Material::SupportsProjectionMode(OvRendering::Settings::EProjectionMode p_projectionMode) const
462+
{
463+
using enum OvRendering::Settings::EProjectionMode;
464+
465+
switch (p_projectionMode)
466+
{
467+
case ORTHOGRAPHIC: return SupportsOrthographic();
468+
case PERSPECTIVE: return SupportsPerspective();
469+
}
470+
471+
return true;
472+
}

0 commit comments

Comments
 (0)