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

Commit

Permalink
Fix resize border z-fighting on non-Oculus devices (#1691)
Browse files Browse the repository at this point in the history
  • Loading branch information
MortimerGoro authored and bluemarvin committed Aug 28, 2019
1 parent b926cae commit 3ad2320
Show file tree
Hide file tree
Showing 5 changed files with 54 additions and 19 deletions.
31 changes: 19 additions & 12 deletions app/src/main/cpp/BrowserWorld.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include "Pointer.h"
#include "Widget.h"
#include "WidgetMover.h"
#include "WidgetResizer.h"
#include "WidgetPlacement.h"
#include "Cylinder.h"
#include "Quad.h"
Expand Down Expand Up @@ -182,6 +183,7 @@ struct BrowserWorld::State {
VRVideoPtr vrVideo;
PerformanceMonitorPtr monitor;
WidgetMoverPtr movingWidget;
WidgetResizerPtr widgetResizer;

State() : paused(true), glInitialized(false), modelsLoaded(false), env(nullptr), cylinderDensity(0.0f), nearClip(0.1f),
farClip(300.0f), activity(nullptr), windowsInitialized(false), exitImmersiveRequested(false), loaderDelay(0) {
Expand Down Expand Up @@ -937,7 +939,8 @@ BrowserWorld::StartWidgetResize(int32_t aHandle, const vrb::Vector& aMaxSize, co
ASSERT_ON_RENDER_THREAD();
WidgetPtr widget = m.GetWidget(aHandle);
if (widget) {
widget->StartResize(aMaxSize, aMinSize);
m.widgetResizer = widget->StartResize(aMaxSize, aMinSize);
m.rootTransparent->AddNode(m.widgetResizer->GetRoot());
}
}

Expand All @@ -949,6 +952,10 @@ BrowserWorld::FinishWidgetResize(int32_t aHandle) {
return;
}
widget->FinishResize();
if (m.widgetResizer) {
m.widgetResizer->GetRoot()->RemoveFromParents();
m.widgetResizer = nullptr;
}
}

void
Expand Down Expand Up @@ -1354,24 +1361,29 @@ BrowserWorld::CreateSkyBox(const std::string& aBasePath, const std::string& aExt

float
BrowserWorld::ComputeNormalizedZ(const vrb::NodePtr& aNode) const {
WidgetPtr target;
bool pointer = false;
Widget * target = nullptr;
float zDelta = 0.0f;
for (const auto & widget: m.widgets) {
if (widget->GetRoot() == aNode) {
target = widget;
target = widget.get();
break;
}
}
if (!target) {
for (Controller& controller: m.controllers->GetControllers()) {
if (controller.pointer && controller.pointer->GetRoot() == aNode) {
target = controller.pointer->GetHitWidget();
pointer = true;
target = controller.pointer->GetHitWidget().get();
zDelta = 0.02f;
break;
}
}
}

if (!target && m.widgetResizer && m.widgetResizer ->GetRoot() == aNode) {
target = m.widgetResizer->GetWidget();
zDelta = 0.01f;
}

if (!target) {
return 1.0f;
}
Expand All @@ -1395,12 +1407,7 @@ BrowserWorld::ComputeNormalizedZ(const vrb::NodePtr& aNode) const {

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

float z = ndc.z();

if (pointer) {
z-= 0.001f;
}
return z;
return ndc.z() - zDelta;
}

} // namespace crow
Expand Down
16 changes: 13 additions & 3 deletions app/src/main/cpp/Widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ struct Widget::State {
vrb::Matrix translation = vrb::Matrix::Translation(vrb::Vector(0.0f, 0.0f, radius * scale));
cylinder->SetTransform(translation.PostMultiply(scaleMatrix));
AdjustCylinderRotation(radius * scale);
UpdateResizerTransform();
}

void AdjustCylinderRotation(const float radius) {
Expand Down Expand Up @@ -195,6 +196,12 @@ struct Widget::State {
}
borders.clear();
}

void UpdateResizerTransform() {
if (resizer) {
resizer->SetTransform(transformContainer->GetTransform().PostMultiply(transform->GetTransform()));
}
}
};

WidgetPtr
Expand Down Expand Up @@ -347,6 +354,7 @@ Widget::SetTransform(const vrb::Matrix& aTransform) {
if (m.cylinder) {
m.UpdateCylinderMatrix();
}
m.UpdateResizerTransform();
}

void
Expand Down Expand Up @@ -443,7 +451,7 @@ Widget::SetPlacement(const WidgetPlacementPtr& aPlacement) {
}
}

void
WidgetResizerPtr
Widget::StartResize(const vrb::Vector& aMaxSize, const vrb::Vector& aMinSize) {
vrb::Vector worldMin, worldMax;
GetWidgetMinAndMax(worldMin, worldMax);
Expand All @@ -452,11 +460,10 @@ Widget::StartResize(const vrb::Vector& aMaxSize, const vrb::Vector& aMinSize) {
} else {
vrb::RenderContextPtr render = m.context.lock();
if (!render) {
return;
return nullptr;
}
vrb::CreationContextPtr create = render->GetRenderThreadCreationContext();
m.resizer = WidgetResizer::Create(create, this);
m.transform->InsertNode(m.resizer->GetRoot(), 0);
}
m.resizer->SetResizeLimits(aMaxSize, aMinSize);
m.resizing = true;
Expand All @@ -465,6 +472,8 @@ Widget::StartResize(const vrb::Vector& aMaxSize, const vrb::Vector& aMinSize) {
m.quad->SetScaleMode(Quad::ScaleMode::AspectFit);
m.quad->SetBackgroundColor(vrb::Color(1.0f, 1.0f, 1.0f, 1.0f));
}
m.UpdateResizerTransform();
return m.resizer;
}

void
Expand Down Expand Up @@ -601,6 +610,7 @@ void Widget::LayoutQuadWithCylinderParent(const CylinderPtr& aCylinder) {
} else {
m.transformContainer->SetTransform(vrb::Matrix::Identity());
}
m.UpdateResizerTransform();
}

Widget::Widget(State& aState, vrb::RenderContextPtr& aContext) : m(aState) {
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/cpp/Widget.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ typedef std::shared_ptr<Quad> QuadPtr;
class Widget;
typedef std::shared_ptr<Widget> WidgetPtr;

class WidgetResizer;
typedef std::shared_ptr<WidgetResizer> WidgetResizerPtr;

class WidgetPlacement;
typedef std::shared_ptr<WidgetPlacement> WidgetPlacementPtr;

Expand Down Expand Up @@ -66,7 +69,7 @@ class Widget {
vrb::TransformPtr GetTransformNode() const;
const WidgetPlacementPtr& GetPlacement() const;
void SetPlacement(const WidgetPlacementPtr& aPlacement);
void StartResize(const vrb::Vector& aMaxSize, const vrb::Vector& aMinSize);
WidgetResizerPtr StartResize(const vrb::Vector& aMaxSize, const vrb::Vector& aMinSize);
void FinishResize();
bool IsResizing() const;
bool IsResizingActive() const;
Expand Down
17 changes: 15 additions & 2 deletions app/src/main/cpp/WidgetResizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "vrb/Color.h"
#include "vrb/CreationContext.h"
#include "vrb/Matrix.h"
#include "vrb/GLError.h"
#include "vrb/Geometry.h"
#include "vrb/RenderState.h"
#include "vrb/SurfaceTextureFactory.h"
Expand Down Expand Up @@ -255,6 +256,7 @@ struct WidgetResizer::State {
vrb::Vector minSize;
bool resizing;
vrb::TogglePtr root;
vrb::TransformPtr transform;
std::vector<ResizeHandlePtr> resizeHandles;
std::vector<ResizeBarPtr> resizeBars;
ResizeHandlePtr activeHandle;
Expand All @@ -272,6 +274,8 @@ struct WidgetResizer::State {
return;
}
root = vrb::Toggle::Create(create);
transform = vrb::Transform::Create(create);
root->AddNode(transform);
currentMin = min;
currentMax = max;
maxSize = kDefaultMaxResize;
Expand Down Expand Up @@ -313,7 +317,7 @@ struct WidgetResizer::State {
}
ResizeBarPtr result = ResizeBar::Create(create, aCenter, aScale, aBorder, aMode);
resizeBars.push_back(result);
root->AddNode(result->border->GetTransformNode());
transform->AddNode(result->border->GetTransformNode());
return result;
}

Expand All @@ -325,7 +329,7 @@ struct WidgetResizer::State {
ResizeHandlePtr result = ResizeHandle::Create(create, aCenter, aResizeMode, aBars);
result->touchRatio = aTouchRatio;
resizeHandles.push_back(result);
root->InsertNode(result->root, 0);
transform->InsertNode(result->root, 0);
return result;
}

Expand Down Expand Up @@ -629,6 +633,15 @@ WidgetResizer::IsActive() const {
return m.activeHandle && m.activeHandle->resizeState == ResizeState::Active;
}

void
WidgetResizer::SetTransform(const vrb::Matrix &aTransform){
m.transform->SetTransform(aTransform);
}

Widget*
WidgetResizer::GetWidget() const {
return m.widget;
}

WidgetResizer::WidgetResizer(State& aState, vrb::CreationContextPtr& aContext) : m(aState) {
m.context = aContext;
Expand Down
4 changes: 3 additions & 1 deletion app/src/main/cpp/WidgetResizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ typedef std::shared_ptr<WidgetResizer> WidgetResizerPtr;

class WidgetResizer {
public:
static WidgetResizerPtr Create(vrb::CreationContextPtr& aContext, Widget * aWidget);
static WidgetResizerPtr Create(vrb::CreationContextPtr& aContext, Widget* aWidget);
vrb::NodePtr GetRoot() const;
void SetSize(const vrb::Vector& aMin, const vrb::Vector& aMax);
void SetResizeLimits(const vrb::Vector& aMaxSize, const vrb::Vector& aMinSize);
Expand All @@ -33,6 +33,8 @@ class WidgetResizer {
const vrb::Vector& GetResizeMin() const;
const vrb::Vector& GetResizeMax() const;
bool IsActive() const;
Widget* GetWidget() const;
void SetTransform(const vrb::Matrix& aTransform);
protected:
struct State;
WidgetResizer(State& aState, vrb::CreationContextPtr& aContext);
Expand Down

0 comments on commit 3ad2320

Please sign in to comment.