Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
Improve widget depth sorting (#663)
Browse files Browse the repository at this point in the history
  • Loading branch information
MortimerGoro authored Oct 25, 2018
1 parent b091086 commit 84a5242
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 1 deletion.
34 changes: 33 additions & 1 deletion app/src/main/cpp/BrowserWorld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -871,11 +871,21 @@ BrowserWorld::DrawWorld() {
m.externalVR->SetCompositorEnabled(true);
m.device->SetRenderMode(device::RenderMode::StandAlone);
vrb::Vector headPosition = m.device->GetHeadTransform().GetTranslation();
vrb::Vector headDirection = m.device->GetHeadTransform().MultiplyDirection(vrb::Vector(0.0f, 0.0f, -1.0f));
if (m.skybox) {
m.skybox->SetTransform(vrb::Matrix::Translation(headPosition));
}
m.rootTransparent->SortNodes([=](const NodePtr& a, const NodePtr& b) {
return DistanceToNode(a, headPosition) < DistanceToNode(b, headPosition);
const float kMaxFloat = 9999999.0f;
float da = DistanceToPlane(GetWidgetFromNode(a), headPosition, headDirection);
float db = DistanceToPlane(GetWidgetFromNode(b), headPosition, headDirection);
if (da < 0.0f) {
da = std::numeric_limits<float>::max();
}
if (db < 0.0f) {
db = std::numeric_limits<float>::max();
}
return da < db;
});
m.device->StartFrame();

Expand Down Expand Up @@ -1109,6 +1119,28 @@ BrowserWorld::DistanceToNode(const vrb::NodePtr& aTargetNode, const vrb::Vector&
return result;
}

WidgetPtr
BrowserWorld::GetWidgetFromNode(const vrb::NodePtr& aNode) const {
for (const auto & widget: m.widgets) {
if (widget->GetRoot() == aNode) {
return widget;
}
}
return nullptr;
}

float
BrowserWorld::DistanceToPlane(const WidgetPtr& aWidget, const vrb::Vector& aPosition, const vrb::Vector& aDirection) const {
if (!aWidget) {
return -1.0f;
}
vrb::Vector result;
bool inside = false;
float distance = -1.0f;
aWidget->GetQuad()->TestIntersection(aPosition, aDirection, result, false, inside, distance);
return distance;
}

} // namespace crow


Expand Down
4 changes: 4 additions & 0 deletions app/src/main/cpp/BrowserWorld.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ typedef std::shared_ptr<BrowserWorld> BrowserWorldPtr;
typedef std::weak_ptr<BrowserWorld> BrowserWorldWeakPtr;
class WidgetPlacement;
typedef std::shared_ptr<WidgetPlacement> WidgetPlacementPtr;
class Widget;
typedef std::shared_ptr<Widget> WidgetPtr;

class BrowserWorld {
public:
Expand Down Expand Up @@ -64,6 +66,8 @@ class BrowserWorld {
void LoadSkybox(const vrb::TransformPtr transform, const std::string& basePath);
void CreateFloor();
float DistanceToNode(const vrb::NodePtr& aNode, const vrb::Vector& aPosition) const;
WidgetPtr GetWidgetFromNode(const vrb::NodePtr& aNode) const;
float DistanceToPlane(const WidgetPtr& aNode, const vrb::Vector& aPosition, const vrb::Vector& aDirection) const;
private:
State& m;
BrowserWorld() = delete;
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/cpp/Widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,11 @@ Widget::GetRoot() const {
return m.root;
}

QuadPtr
Widget::GetQuad() const {
return m.quad;
}

vrb::TransformPtr
Widget::GetTransformNode() const {
return m.transform;
Expand Down
4 changes: 4 additions & 0 deletions app/src/main/cpp/Widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@

namespace crow {

class Quad;
typedef std::shared_ptr<Quad> QuadPtr;

class Widget;
typedef std::shared_ptr<Widget> WidgetPtr;

Expand Down Expand Up @@ -44,6 +47,7 @@ class Widget {
void TogglePointer(const bool aEnabled);
bool IsVisible() const;
vrb::NodePtr GetRoot() const;
QuadPtr GetQuad() const;
vrb::TransformPtr GetTransformNode() const;
vrb::NodePtr GetPointerGeometry() const;
void SetPointerGeometry(vrb::NodePtr& aNode);
Expand Down

0 comments on commit 84a5242

Please sign in to comment.