OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 // Adapted from: | |
6 // https://github.com/googlevr/gvr-unity-sdk/blob/master/Samples/DaydreamLabsCon trollerPlayground/Assets/GoogleVR/Scripts/Controller/GvrArmModel.cs | |
7 | |
8 #include "chrome/browser/android/vr_shell/elbow_model.h" | |
9 | |
10 #include <cmath> | |
11 | |
12 #include "chrome/browser/android/vr_shell/vr_math.h" | |
13 | |
14 namespace vr_shell { | |
15 | |
16 namespace { | |
17 | |
18 constexpr gvr::Quatf kNoRotation = {0.f, 0.f, 0.f, 1.f}; | |
19 constexpr gvr::Vec3f kDefaultShoulderRight = {0.19f, -0.19f, 0.03f}; | |
20 constexpr float kFadeDistanceFromFace = 0.34f; | |
21 constexpr gvr::Vec3f kDefaultRelativeElbow = {0.195f, -0.5f, 0.075f}; | |
22 constexpr gvr::Vec3f kDefaultRelativeWrist = {0.f, 0.f, -0.25f}; | |
23 constexpr gvr::Vec3f kForward = {0.f, 0.f, -1.f}; | |
24 constexpr gvr::Vec3f kUp = {0.f, 1.f, 0.f}; | |
25 constexpr float kMinExtensionAngle = 7.f; | |
26 constexpr float kMaxExtenstionAngle = 60.f; | |
27 constexpr float kExtensionWeight = 0.4f; | |
28 constexpr float kDeltaAlpha = 3.f; | |
29 | |
30 } // namespace | |
31 | |
32 ElbowModel::ElbowModel(gvr_controller_handedness handedness) | |
33 : handedness_(handedness), alpha_value_(1.f), torso_direction_{0, 0, 0} {} | |
34 | |
35 ElbowModel::~ElbowModel() = default; | |
36 | |
37 void ElbowModel::UpdateHandedness() { | |
38 handed_multiplier_ = {handedness_ == GVR_CONTROLLER_RIGHT_HANDED ? 1.f : -1.f, | |
39 1.f, 1.f}; | |
40 shoulder_rotation_ = kNoRotation; | |
41 shoulder_position_ = | |
42 PointwiseVectorMul(kDefaultShoulderRight, handed_multiplier_); | |
43 } | |
44 | |
45 void ElbowModel::Update(const UpdateData& update) { | |
46 UpdateHandedness(); | |
47 UpdateTorsoDirection(update); | |
48 ApplyArmModel(update); | |
49 UpdateTransparency(update); | |
50 } | |
51 | |
52 void ElbowModel::UpdateTorsoDirection(const UpdateData& update) { | |
53 auto head_direction = update.head_direction; | |
54 head_direction.y = 0; | |
55 NormalizeVector(head_direction); | |
56 | |
57 // Determine the gaze direction horizontally. | |
58 float angular_velocity = VectorLength(update.gyro); | |
59 float gaze_filter_strength = | |
60 Clampf((angular_velocity - .2f) / 45.f, 0.f, 0.1f); | |
cjgrant
2017/04/03 19:24:59
0.2f here (and everywhere please). Ie, not .2f or
acondor_
2017/04/19 18:44:27
Done.
| |
61 torso_direction_ = | |
62 QuatSlerp(torso_direction_, head_direction, gaze_filter_strength); | |
63 | |
64 // Rotate the fixed joints. | |
65 auto gaze_rotation = FromToRotation(kForward, torso_direction_); | |
66 shoulder_rotation_ = gaze_rotation; | |
67 shoulder_position_ = | |
68 MatrixVectorRotate(QuatToMatrix(gaze_rotation), shoulder_position_); | |
69 } | |
70 | |
71 void ElbowModel::ApplyArmModel(const UpdateData& update) { | |
72 // Controller's orientation relative to the player. | |
cjgrant
2017/04/03 19:24:59
s/player/user/ everywhere?
acondor_
2017/04/19 18:44:27
Done.
| |
73 auto controller_orientation = update.orientation; | |
74 controller_orientation = | |
75 QuatMultiply(QuatInverted(shoulder_rotation_), controller_orientation); | |
76 | |
77 // Relative positions of the joints. | |
78 elbow_position_ = | |
79 PointwiseVectorMul(kDefaultRelativeElbow, handed_multiplier_); | |
80 wrist_position_ = | |
81 PointwiseVectorMul(kDefaultRelativeWrist, handed_multiplier_); | |
82 auto arm_extension_offset = | |
83 PointwiseVectorMul({-0.13f, 0.14f, -0.08f}, handed_multiplier_); | |
cjgrant
2017/04/03 19:24:59
The vector here should be a constant as well.
acondor_
2017/04/19 18:44:27
Done.
| |
84 | |
85 // Extract just the x rotation angle. | |
86 auto controller_forward = | |
87 MatrixVectorRotate(QuatToMatrix(controller_orientation), kForward); | |
88 float x_angle = 90.f - VectorAngleDegrees(controller_forward, kUp); | |
89 | |
90 // Remove the z rotation from the controller | |
91 auto x_y_rotation = FromToRotation(kForward, controller_forward); | |
92 | |
93 // Offset the elbow by the extension. | |
94 float normalized_angle = (x_angle - kMinExtensionAngle) / | |
95 (kMaxExtenstionAngle - kMinExtensionAngle); | |
96 float extension_ratio = Clampf(normalized_angle, 0.f, 1.f); | |
97 elbow_position_ = VectorAdd( | |
98 elbow_position_, VectorScalarMul(arm_extension_offset, extension_ratio)); | |
99 | |
100 // Calculate the lerp interpolation factor. | |
101 float total_angle = QuatAngleDegrees(x_y_rotation, kNoRotation); | |
102 float lerp_suppresion = 1.f - pow(total_angle / 180.f, 6); | |
103 float lerp_value = | |
104 lerp_suppresion * (0.4f + 0.6f * extension_ratio * kExtensionWeight); | |
asimjour1
2017/04/19 18:58:12
please move the magic numbers from here and define
acondor_
2017/04/21 21:04:31
Done.
| |
105 | |
106 // Apply the absolute rotations to the joints. | |
107 auto lerp_rotation = QuatLerp(kNoRotation, x_y_rotation, lerp_value); | |
108 elbow_rotation_ = QuatMultiply( | |
109 QuatMultiply(shoulder_rotation_, QuatInverted(lerp_rotation)), | |
110 controller_orientation); | |
111 wrist_rotation_ = QuatMultiply(shoulder_rotation_, controller_orientation); | |
112 | |
113 // Determine the relative positions. | |
114 elbow_position_ = | |
115 MatrixVectorRotate(QuatToMatrix(shoulder_rotation_), elbow_position_); | |
116 wrist_position_ = VectorAdd( | |
117 elbow_position_, | |
118 MatrixVectorRotate(QuatToMatrix(elbow_rotation_), wrist_position_)); | |
119 } | |
120 | |
121 void ElbowModel::UpdateTransparency(const UpdateData& update) { | |
asimjour1
2017/04/19 18:58:12
IIUC alpha_value/opacity can be obtained from the
acondor_
2017/04/21 21:04:31
Actually, this is an animation (so it depends on t
| |
122 float distance_to_face = VectorLength(wrist_position_); | |
123 float alpha_change = kDeltaAlpha * update.delta_time_seconds; | |
124 alpha_value_ = Clampf(distance_to_face < kFadeDistanceFromFace | |
125 ? alpha_value_ - alpha_change | |
126 : alpha_value_ + alpha_change, | |
127 0.f, 1.f); | |
128 } | |
129 | |
130 } // namespace vr_shell | |
OLD | NEW |