OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/android/vr_shell/ui_scene_manager.h" | 5 #include "chrome/browser/android/vr_shell/ui_scene_manager.h" |
6 | 6 |
7 #include "base/memory/ptr_util.h" | 7 #include "base/memory/ptr_util.h" |
8 #include "chrome/browser/android/vr_shell/textures/ui_texture.h" | 8 #include "chrome/browser/android/vr_shell/textures/ui_texture.h" |
9 #include "chrome/browser/android/vr_shell/ui_elements/permanent_security_warning
.h" | 9 #include "chrome/browser/android/vr_shell/ui_elements/permanent_security_warning
.h" |
10 #include "chrome/browser/android/vr_shell/ui_elements/textured_element.h" | 10 #include "chrome/browser/android/vr_shell/ui_elements/textured_element.h" |
11 #include "chrome/browser/android/vr_shell/ui_elements/transient_security_warning
.h" | 11 #include "chrome/browser/android/vr_shell/ui_elements/transient_security_warning
.h" |
12 #include "chrome/browser/android/vr_shell/ui_elements/ui_element.h" | 12 #include "chrome/browser/android/vr_shell/ui_elements/ui_element.h" |
13 #include "chrome/browser/android/vr_shell/ui_scene.h" | 13 #include "chrome/browser/android/vr_shell/ui_scene.h" |
14 | 14 |
15 namespace vr_shell { | 15 namespace vr_shell { |
16 | 16 |
17 namespace { | 17 namespace { |
18 | 18 |
19 static constexpr int kWarningTimeoutSeconds = 30; | 19 static constexpr int kWarningTimeoutSeconds = 30; |
20 static constexpr float kWarningDistance = 0.7; | 20 static constexpr float kWarningDistance = 0.7; |
21 static constexpr float kWarningAngleRadians = 16.3 * M_PI / 180.0; | 21 static constexpr float kWarningAngleRadians = 16.3 * M_PI / 180.0; |
22 | |
23 static constexpr float kPermanentWarningHeight = 0.226; | 22 static constexpr float kPermanentWarningHeight = 0.226; |
24 static constexpr float kPermanentWarningWidth = 0.078; | 23 static constexpr float kPermanentWarningWidth = 0.078; |
25 static constexpr float kTransientWarningHeight = 0.512; | 24 static constexpr float kTransientWarningHeight = 0.512; |
26 static constexpr float kTransientWarningWidth = 0.160; | 25 static constexpr float kTransientWarningWidth = 0.160; |
27 | 26 |
28 static constexpr float kContentHeight = 2.4; | 27 static constexpr float kContentWidth = 2.4; |
29 static constexpr float kContentWidth = 1.6; | 28 static constexpr float kContentHeight = 1.6; |
30 static constexpr float kContentDistance = 2.5; | 29 static constexpr float kContentDistance = 2.5; |
31 static constexpr float kContentVerticalOffset = -0.26; | 30 static constexpr float kContentVerticalOffset = -0.26; |
| 31 static constexpr float kBackplaneSize = 1000.0; |
| 32 static constexpr float kBackgroundDistanceMultiplier = 1.414; |
| 33 |
| 34 static constexpr float kSceneSize = 25.0; |
| 35 static constexpr float kSceneHeight = 4.0; |
| 36 static constexpr int kFloorGridlineCount = 40; |
| 37 static constexpr vr::Colorf kBackgroundHorizonColor = {0.57, 0.57, 0.57, 1.0}; |
| 38 static constexpr vr::Colorf kBackgroundCenterColor = {0.48, 0.48, 0.48, 1.0}; |
| 39 |
| 40 // Tiny distance to offset textures that should appear in the same plane. |
| 41 static constexpr float kTextureOffset = 0.01; |
32 | 42 |
33 } // namespace | 43 } // namespace |
34 | 44 |
35 UiSceneManager::UiSceneManager(UiScene* scene) | 45 UiSceneManager::UiSceneManager(UiScene* scene) |
36 : scene_(scene), weak_ptr_factory_(this) { | 46 : scene_(scene), weak_ptr_factory_(this) { |
37 std::unique_ptr<UiElement> element; | 47 std::unique_ptr<UiElement> element; |
38 | 48 |
39 // For now, use an ID range that does not conflict with the HTML UI. | 49 CreateBackground(); |
40 int id = 1000; | 50 CreateContentQuad(); |
| 51 CreateSecurityWarnings(); |
| 52 } |
41 | 53 |
42 // Permanent WebVR security warning. | 54 UiSceneManager::~UiSceneManager() {} |
| 55 |
| 56 void UiSceneManager::CreateSecurityWarnings() { |
| 57 std::unique_ptr<UiElement> element; |
| 58 |
43 // TODO(mthiesse): Programatically compute the proper texture size for these | 59 // TODO(mthiesse): Programatically compute the proper texture size for these |
44 // textured UI elements. | 60 // textured UI elements. |
45 element = base::MakeUnique<PermanentSecurityWarning>(512); | 61 element = base::MakeUnique<PermanentSecurityWarning>(512); |
46 element->id = id++; | 62 element->id = AllocateId(); |
47 element->name = "Permanent security warning"; | |
48 element->fill = vr_shell::Fill::NONE; | 63 element->fill = vr_shell::Fill::NONE; |
49 element->size = {kPermanentWarningWidth, kPermanentWarningHeight, 1}; | 64 element->size = {kPermanentWarningWidth, kPermanentWarningHeight, 1}; |
50 element->scale = {kWarningDistance, kWarningDistance, 1}; | 65 element->scale = {kWarningDistance, kWarningDistance, 1}; |
51 element->translation = {0, kWarningDistance * sin(kWarningAngleRadians), | 66 element->translation = {0, kWarningDistance * sin(kWarningAngleRadians), |
52 -kWarningDistance * cos(kWarningAngleRadians)}; | 67 -kWarningDistance * cos(kWarningAngleRadians)}; |
53 element->rotation = {1.0f, 0, 0, kWarningAngleRadians}; | 68 element->rotation = {1.0f, 0, 0, kWarningAngleRadians}; |
54 element->visible = false; | 69 element->visible = false; |
55 element->hit_testable = false; | 70 element->hit_testable = false; |
56 element->lock_to_fov = true; | 71 element->lock_to_fov = true; |
57 permanent_security_warning_ = element.get(); | 72 permanent_security_warning_ = element.get(); |
58 scene_->AddUiElement(std::move(element)); | 73 scene_->AddUiElement(std::move(element)); |
59 | 74 |
60 // Transient WebVR security warning. | |
61 element = base::MakeUnique<TransientSecurityWarning>(1024); | 75 element = base::MakeUnique<TransientSecurityWarning>(1024); |
62 element->id = id++; | 76 element->id = AllocateId(); |
63 element->name = "Transient security warning"; | |
64 element->fill = vr_shell::Fill::NONE; | 77 element->fill = vr_shell::Fill::NONE; |
65 element->size = {kTransientWarningWidth, kTransientWarningHeight, 1}; | 78 element->size = {kTransientWarningWidth, kTransientWarningHeight, 1}; |
66 element->scale = {kWarningDistance, kWarningDistance, 1}; | 79 element->scale = {kWarningDistance, kWarningDistance, 1}; |
67 element->translation = {0, 0, -kWarningDistance}; | 80 element->translation = {0, 0, -kWarningDistance}; |
68 element->visible = false; | 81 element->visible = false; |
69 element->hit_testable = false; | 82 element->hit_testable = false; |
70 element->lock_to_fov = true; | 83 element->lock_to_fov = true; |
71 transient_security_warning_ = element.get(); | 84 transient_security_warning_ = element.get(); |
72 scene_->AddUiElement(std::move(element)); | 85 scene_->AddUiElement(std::move(element)); |
| 86 } |
73 | 87 |
74 // Main web content quad. | 88 void UiSceneManager::CreateContentQuad() { |
| 89 std::unique_ptr<UiElement> element; |
| 90 |
75 element = base::MakeUnique<UiElement>(); | 91 element = base::MakeUnique<UiElement>(); |
76 element->id = id++; | 92 element->id = AllocateId(); |
77 element->name = "Content"; | |
78 element->fill = vr_shell::Fill::CONTENT; | 93 element->fill = vr_shell::Fill::CONTENT; |
79 element->size = {kContentWidth, kContentHeight, 1}; | 94 element->size = {kContentWidth, kContentHeight, 1}; |
80 element->translation = {0, kContentVerticalOffset, -kContentDistance}; | 95 element->translation = {0, kContentVerticalOffset, -kContentDistance}; |
81 element->visible = false; | 96 element->visible = false; |
82 main_content_ = element.get(); | 97 main_content_ = element.get(); |
| 98 browser_ui_elements_.push_back(element.get()); |
83 scene_->AddUiElement(std::move(element)); | 99 scene_->AddUiElement(std::move(element)); |
| 100 |
| 101 // Place an invisible but hittable plane behind the content quad, to keep the |
| 102 // reticle roughly planar with the content if near content. |
| 103 element = base::MakeUnique<UiElement>(); |
| 104 element->id = AllocateId(); |
| 105 element->fill = vr_shell::Fill::NONE; |
| 106 element->size = {kBackplaneSize, kBackplaneSize, 1.0}; |
| 107 element->translation = {0.0, 0.0, -kTextureOffset}; |
| 108 element->parent_id = main_content_->id; |
| 109 browser_ui_elements_.push_back(element.get()); |
| 110 scene_->AddUiElement(std::move(element)); |
| 111 |
| 112 // Limit reticle distance to a sphere based on content distance. |
| 113 scene_->SetBackgroundDistance(main_content_->translation.z() * |
| 114 -kBackgroundDistanceMultiplier); |
84 } | 115 } |
85 | 116 |
86 UiSceneManager::~UiSceneManager() {} | 117 void UiSceneManager::CreateBackground() { |
| 118 std::unique_ptr<UiElement> element; |
| 119 |
| 120 // Floor. |
| 121 element = base::MakeUnique<UiElement>(); |
| 122 element->id = AllocateId(); |
| 123 element->size = {kSceneSize, kSceneSize, 1.0}; |
| 124 element->translation = {0.0, -kSceneHeight / 2, 0.0}; |
| 125 element->rotation = {1.0, 0.0, 0.0, -M_PI / 2.0}; |
| 126 element->fill = vr_shell::Fill::OPAQUE_GRADIENT; |
| 127 element->edge_color = kBackgroundHorizonColor; |
| 128 element->center_color = kBackgroundCenterColor; |
| 129 element->draw_phase = 0; |
| 130 browser_ui_elements_.push_back(element.get()); |
| 131 scene_->AddUiElement(std::move(element)); |
| 132 |
| 133 // Ceiling. |
| 134 element = base::MakeUnique<UiElement>(); |
| 135 element->id = AllocateId(); |
| 136 element->fill = vr_shell::Fill::OPAQUE_GRADIENT; |
| 137 element->size = {kSceneSize, kSceneSize, 1.0}; |
| 138 element->translation = {0.0, kSceneHeight / 2, 0.0}; |
| 139 element->rotation = {1.0, 0.0, 0.0, M_PI / 2}; |
| 140 element->fill = vr_shell::Fill::OPAQUE_GRADIENT; |
| 141 element->edge_color = kBackgroundHorizonColor; |
| 142 element->center_color = kBackgroundCenterColor; |
| 143 element->draw_phase = 0; |
| 144 browser_ui_elements_.push_back(element.get()); |
| 145 scene_->AddUiElement(std::move(element)); |
| 146 |
| 147 // Floor grid. |
| 148 element = base::MakeUnique<UiElement>(); |
| 149 element->id = AllocateId(); |
| 150 element->fill = vr_shell::Fill::GRID_GRADIENT; |
| 151 element->size = {kSceneSize, kSceneSize, 1.0}; |
| 152 element->translation = {0.0, -kSceneHeight / 2 + kTextureOffset, 0.0}; |
| 153 element->rotation = {1.0, 0.0, 0.0, -M_PI / 2}; |
| 154 element->fill = vr_shell::Fill::GRID_GRADIENT; |
| 155 element->center_color = kBackgroundHorizonColor; |
| 156 vr::Colorf edge_color = kBackgroundHorizonColor; |
| 157 edge_color.a = 0.0; |
| 158 element->edge_color = edge_color; |
| 159 element->gridline_count = kFloorGridlineCount; |
| 160 element->draw_phase = 0; |
| 161 browser_ui_elements_.push_back(element.get()); |
| 162 scene_->AddUiElement(std::move(element)); |
| 163 |
| 164 scene_->SetBackgroundColor(kBackgroundHorizonColor); |
| 165 } |
87 | 166 |
88 base::WeakPtr<UiSceneManager> UiSceneManager::GetWeakPtr() { | 167 base::WeakPtr<UiSceneManager> UiSceneManager::GetWeakPtr() { |
89 return weak_ptr_factory_.GetWeakPtr(); | 168 return weak_ptr_factory_.GetWeakPtr(); |
90 } | 169 } |
91 | 170 |
92 void UiSceneManager::SetWebVRMode(bool web_vr) { | 171 void UiSceneManager::SetWebVRMode(bool web_vr) { |
93 web_vr_mode_ = web_vr; | 172 web_vr_mode_ = web_vr; |
94 main_content_->visible = !web_vr_mode_; | 173 |
| 174 // Make all VR scene UI elements visible if not in WebVR. |
| 175 for (UiElement* element : browser_ui_elements_) { |
| 176 element->visible = !web_vr_mode_; |
| 177 } |
| 178 |
95 ConfigureSecurityWarnings(); | 179 ConfigureSecurityWarnings(); |
96 } | 180 } |
97 | 181 |
98 void UiSceneManager::SetWebVRSecureOrigin(bool secure) { | 182 void UiSceneManager::SetWebVRSecureOrigin(bool secure) { |
99 secure_origin_ = secure; | 183 secure_origin_ = secure; |
100 ConfigureSecurityWarnings(); | 184 ConfigureSecurityWarnings(); |
101 } | 185 } |
102 | 186 |
103 void UiSceneManager::ConfigureSecurityWarnings() { | 187 void UiSceneManager::ConfigureSecurityWarnings() { |
104 bool enabled = web_vr_mode_ && !secure_origin_; | 188 bool enabled = web_vr_mode_ && !secure_origin_; |
105 permanent_security_warning_->visible = enabled; | 189 permanent_security_warning_->visible = enabled; |
106 transient_security_warning_->visible = enabled; | 190 transient_security_warning_->visible = enabled; |
107 if (enabled) { | 191 if (enabled) { |
108 security_warning_timer_.Start( | 192 security_warning_timer_.Start( |
109 FROM_HERE, base::TimeDelta::FromSeconds(kWarningTimeoutSeconds), this, | 193 FROM_HERE, base::TimeDelta::FromSeconds(kWarningTimeoutSeconds), this, |
110 &UiSceneManager::OnSecurityWarningTimer); | 194 &UiSceneManager::OnSecurityWarningTimer); |
111 } else { | 195 } else { |
112 security_warning_timer_.Stop(); | 196 security_warning_timer_.Stop(); |
113 } | 197 } |
114 } | 198 } |
115 | 199 |
116 void UiSceneManager::OnSecurityWarningTimer() { | 200 void UiSceneManager::OnSecurityWarningTimer() { |
117 transient_security_warning_->visible = false; | 201 transient_security_warning_->visible = false; |
118 } | 202 } |
119 | 203 |
| 204 int UiSceneManager::AllocateId() { |
| 205 return next_available_id_++; |
| 206 } |
| 207 |
120 } // namespace vr_shell | 208 } // namespace vr_shell |
OLD | NEW |