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

Improve widgets depth sorting #1533

Merged
merged 1 commit into from
Aug 8, 2019
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
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ protected void initializeWidgetPlacement(WidgetPlacement aPlacement) {
aPlacement.anchorX = 0.0f;
aPlacement.anchorY = 0.0f;
aPlacement.translationY = WidgetPlacement.dpDimension(getContext(), R.dimen.video_projection_menu_translation_y);
aPlacement.translationZ = 30.0f;
aPlacement.translationZ = 2.0f;
}

public void setParentWidget(UIWidget aParent) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ protected void initializeWidgetPlacement(WidgetPlacement aPlacement) {
aPlacement.anchorX = 0.0f;
aPlacement.anchorY = 0.0f;
aPlacement.translationY = WidgetPlacement.dpDimension(getContext(), R.dimen.video_projection_menu_translation_y);
aPlacement.translationZ = 30.0f;
aPlacement.translationZ = 2.0f;
}

public void setParentWidget(UIWidget aParent) {
Expand Down
42 changes: 24 additions & 18 deletions app/src/main/cpp/BrowserWorld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1181,20 +1181,13 @@ BrowserWorld::DrawWorld() {
if (m.fadeAnimation) {
m.fadeAnimation->UpdateAnimation();
}
vrb::Vector headPosition = m.device->GetHeadTransform().GetTranslation();
vrb::Vector headDirection = m.device->GetHeadTransform().MultiplyDirection(vrb::Vector(0.0f, 0.0f, -1.0f));
const vrb::Vector headPosition = m.device->GetHeadTransform().GetTranslation();
if (m.skybox) {
m.skybox->SetTransform(vrb::Matrix::Translation(headPosition));
}
m.rootTransparent->SortNodes([=](const NodePtr& a, const NodePtr& b) {
float da = DistanceToPlane(a, headPosition, headDirection);
float db = DistanceToPlane(b, headPosition, headDirection);
if (da < 0.0f) {
da = std::numeric_limits<float>::max();
}
if (db < 0.0f) {
db = std::numeric_limits<float>::max();
}
float da = ComputeNormalizedZ(a);
float db = ComputeNormalizedZ(b);
return da < db;
});
m.device->StartFrame();
Expand Down Expand Up @@ -1354,7 +1347,7 @@ BrowserWorld::CreateSkyBox(const std::string& aBasePath, const std::string& aExt
}

float
BrowserWorld::DistanceToPlane(const vrb::NodePtr& aNode, const vrb::Vector& aPosition, const vrb::Vector& aDirection) const {
BrowserWorld::ComputeNormalizedZ(const vrb::NodePtr& aNode) const {
WidgetPtr target;
bool pointer = false;
for (const auto & widget: m.widgets) {
Expand All @@ -1374,21 +1367,34 @@ BrowserWorld::DistanceToPlane(const vrb::NodePtr& aNode, const vrb::Vector& aPos
}

if (!target) {
return -1.0f;
return 1.0f;
}
vrb::Vector result;

const vrb::Vector headPosition = m.device->GetHeadTransform().GetTranslation();
const vrb::Vector headDirection = m.device->GetHeadTransform().MultiplyDirection(vrb::Vector(0.0f, 0.0f, -1.0f));

vrb::Vector hitPoint;
vrb::Vector normal;
bool inside = false;
float distance = -1.0f;
float distance;
if (target->GetQuad()) {
target->GetQuad()->TestIntersection(aPosition, aDirection, result, normal, false, inside, distance);
target->GetQuad()->TestIntersection(headPosition, headDirection, hitPoint, normal, true, inside, distance);
} else if (target->GetCylinder()) {
distance = target->GetCylinder()->DistanceToBackPlane(aPosition, aDirection);
target->GetCylinder()->TestIntersection(headPosition, headDirection, hitPoint, normal, true, inside, distance);
}

const vrb::Matrix& projection = m.device->GetCamera(device::Eye::Left)->GetPerspective();
const vrb::Matrix& view = m.device->GetCamera(device::Eye::Left)->GetView();
vrb::Matrix viewProjection = projection.PostMultiply(view);

vrb::Vector ndc = viewProjection.MultiplyPosition(hitPoint);

float z = ndc.z();

if (pointer) {
distance-= 0.001f;
z-= 0.001f;
}
return distance;
return z;
}

} // namespace crow
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/cpp/BrowserWorld.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ class BrowserWorld {
void DrawLoadingAnimation();
void DrawSplashAnimation();
void CreateSkyBox(const std::string& aBasePath, const std::string& aExtension);
float DistanceToPlane(const vrb::NodePtr& aNode, const vrb::Vector& aPosition, const vrb::Vector& aDirection) const;
float ComputeNormalizedZ(const vrb::NodePtr& aNode) const;
private:
State& m;
BrowserWorld() = delete;
Expand Down