summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/webxr/hit-test/ar_hittest_subscription_refSpaces.https.html
blob: a30e71949c9d6af6c81f1095b90c27a899b0b3f3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="../resources/webxr_util.js"></script>
<script src="../resources/webxr_math_utils.js"></script>
<script src="../resources/webxr_test_asserts.js"></script>
<script src="../resources/webxr_test_constants.js"></script>
<script src="../resources/webxr_test_constants_fake_world.js"></script>

<script>

// 1m above world origin.
const VIEWER_ORIGIN_TRANSFORM = {
  position: [0, 1, 0],
  orientation: [0, 0, 0, 1],
};

// 0.25m above world origin.
const FLOOR_ORIGIN_TRANSFORM = {
  position: [0, 0.25, 0],
  orientation: [0, 0, 0, 1],
};

const fakeDeviceInitParams = {
  supportedModes: ["immersive-ar"],
  views: VALID_VIEWS,
  floorOrigin: FLOOR_ORIGIN_TRANSFORM,    // aka mojo_from_floor
  viewerOrigin: VIEWER_ORIGIN_TRANSFORM,  // aka mojo_from_viewer
  supportedFeatures: ALL_FEATURES,
  world: createFakeWorld(5.0, 2.0, 5.0),  // webxr_test_constants_fake_world.js has detailed description of the fake world
};

// Generates a test function given the parameters for the hit test.
// |ray| - ray that will be used to subscribe to hit test.
// |expectedPoses| - array of expected pose objects. The poses are expected to be expressed in local space.
//                   Null entries in the array mean that the given entry will not be validated.
// |refSpaceName| - XRReferenceSpaceType - either 'local', 'local-floor' or 'viewer'.
let testFunctionGenerator = function(ray, expectedPoses, refSpaceName) {
  const testFunction = function(session, fakeDeviceController, t) {
    return Promise.all([
      session.requestReferenceSpace('local'),
      session.requestReferenceSpace('viewer'),
      session.requestReferenceSpace('local-floor'),
    ]).then(([localRefSpace, viewerRefSpace, localFloorRefSpace]) => {

      const refSpaceNameToSpace = {
        'local' : localRefSpace,
        'viewer' : viewerRefSpace,
        'local-floor' : localFloorRefSpace
      };

      const hitTestOptionsInit = {
        space: refSpaceNameToSpace[refSpaceName],
        offsetRay: ray,
      };

      return session.requestHitTestSource(hitTestOptionsInit).then(
        (hitTestSource) => new Promise((resolve, reject) => {

        const requestAnimationFrameCallback = function(time, frame) {
          const hitTestResults = frame.getHitTestResults(hitTestSource);

          t.step(() => {
            assert_equals(hitTestResults.length, expectedPoses.length, "Results length should match expected results length");
            for(const [index, expectedPose] of expectedPoses.entries()) {
              const pose = hitTestResults[index].getPose(localRefSpace);
              assert_true(pose != null, "Each hit test result should have a pose in local space");
              if(expectedPose != null) {
                assert_transform_approx_equals(pose.transform, expectedPose);
              }
            }
          });

          resolve();
        };

        t.step(() => {
          assert_true(hitTestSource != null, "Hit test source should not be null");
        });

        session.requestAnimationFrame(requestAnimationFrameCallback);
      }));
    });
  };

  return testFunction;
};

// Generates a test function that will use local space for hit test subscription.
// See testFunctionGenerator for explanation of other parameters.
const localBasedTestFunctionGenerator = function(ray, expectedPoses) {
  return testFunctionGenerator(ray, expectedPoses, 'local');
};

// Generates a test function that will use viewer space for hit test subscription.
// See testFunctionGenerator for explanation of other parameters.
const viewerBasedTestFunctionGenerator = function(ray, expectedPoses) {
  return testFunctionGenerator(ray, expectedPoses, 'viewer');
};

// Generates a test function that will use local-floor space for hit test subscription.
// See testFunctionGenerator for explanation of other parameters.
const localFloorBasedTestFunctionGenerator = function(ray, expectedPoses) {
  return testFunctionGenerator(ray, expectedPoses, 'local-floor');
};

// All test cases require local-floor and hit-test.
const sessionInit = { 'requiredFeatures': ['local-floor', 'hit-test'] };

// Pose of the first expected hit test result - straight ahead of the viewer, viewer-facing.
const pose_1 = {
  position: {x: 0.0, y: 1.0, z: -2.5, w: 1.0},
  orientation: {x: 0.0, y: -0.707, z: -0.707, w: 0.0},
    // Hit test API will set Y axis to the surface normal at the intersection point,
    // Z axis towards the ray origin and X axis to cross product of Y axis & Z axis.
    // If the surface normal and Z axis would be parallel, the hit test API
    // will attempt to use `up` vector ([0, 1, 0]) as the Z axis, and if it so happens that Z axis
    // and the surface normal would still be parallel, it will use the `right` vector ([1, 0, 0]) as the Z axis.
    // In this particular case, `up` vector will work so the resulting pose.orientation
    // becomes a rotation around [0, 1, 1] vector by 180 degrees.
};

xr_session_promise_test(
  "Ensures subscription to hit test works with viewer space - straight ahead - plane",
  viewerBasedTestFunctionGenerator(new XRRay(), [pose_1]),
  fakeDeviceInitParams, 'immersive-ar', sessionInit);

xr_session_promise_test("Ensures subscription to hit test works with viewer space - straight up - no results",
  viewerBasedTestFunctionGenerator(new XRRay({}, {x: 0.0, y: 1.0, z : 0.0}), []),
  fakeDeviceInitParams, 'immersive-ar', sessionInit);

const pose_2 = {
  position: {x: 0.0, y: 0.0, z: -2.5, w: 1.0},
  orientation: {x: 0.0, y: -0.707, z: -0.707, w: 0.0},
    // See comment for pose_1.orientation for details.
    // In this case, the hit test pose will have Y and Z axis towards the ray origin so it won't be used,
    // but `up` vector will work so the resulting pose.orientation
    // becomes a rotation around [0, 1, 1] vector by 180 degrees.
};

xr_session_promise_test("Ensures subscription to hit test works with local space",
  localBasedTestFunctionGenerator(new XRRay(), [pose_2]),
  fakeDeviceInitParams, 'immersive-ar', sessionInit);

const pose_3 = {
  position: {x: 0.0, y: 0.25, z: -2.5, w: 1.0},
  orientation: {x: 0.0, y: -0.707, z: -0.707, w: 0.0},
    // See comment for pose_1.orientation for details.
    // In this case, the hit test pose will have Y and Z axis towards the ray origin so it won't be used,
    // but `up` vector will work so the resulting pose.orientation
    // becomes a rotation around [0, 1, 1] vector by 180 degrees.
};

xr_session_promise_test("Ensures subscription to hit test works with local-floor space",
  localFloorBasedTestFunctionGenerator(new XRRay(), [pose_3]),
  fakeDeviceInitParams, 'immersive-ar', sessionInit);

</script>