XRBoundedReferenceSpace.cpp (2998B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 /* This Source Code Form is subject to the terms of the Mozilla Public 4 * License, v. 2.0. If a copy of the MPL was not distributed with this file, 5 * You can obtain one at http://mozilla.org/MPL/2.0/. */ 6 7 #include "mozilla/dom/XRBoundedReferenceSpace.h" 8 9 #include "VRDisplayClient.h" 10 #include "mozilla/dom/DOMPoint.h" 11 #include "mozilla/dom/XRRigidTransform.h" 12 13 namespace mozilla::dom { 14 15 XRBoundedReferenceSpace::XRBoundedReferenceSpace(nsIGlobalObject* aParent, 16 XRSession* aSession, 17 XRNativeOrigin* aNativeOrigin) 18 : XRReferenceSpace(aParent, aSession, aNativeOrigin, 19 XRReferenceSpaceType::Bounded_floor) {} 20 21 JSObject* XRBoundedReferenceSpace::WrapObject( 22 JSContext* aCx, JS::Handle<JSObject*> aGivenProto) { 23 return XRBoundedReferenceSpace_Binding::Wrap(aCx, this, aGivenProto); 24 } 25 26 void XRBoundedReferenceSpace::GetBoundsGeometry( 27 nsTArray<RefPtr<DOMPointReadOnly>>& result) { 28 const auto size = 29 mSession->GetDisplayClient()->GetDisplayInfo().GetStageSize(); 30 if (size.width == 0 || size.height == 0) { 31 return; 32 } 33 34 // https://immersive-web.github.io/webxr/#dom-xrboundedreferencespace-boundsgeometry 35 // bounds geometry must be premultiplied by the inverse of the origin offset. 36 gfx::PointDouble3D offset = mNativeOrigin->GetPosition(); 37 38 const auto addPoint = [&](const double x, const double z) { 39 RefPtr<DOMPointReadOnly> obj = new DOMPointReadOnly( 40 GetParentObject(), x - offset.x, 0.0f, z - offset.z, 1.0f); 41 result.EmplaceBack(obj); 42 }; 43 44 addPoint(-size.width * 0.5f, size.height * 0.5f); 45 addPoint(size.width * 0.5f, size.height * 0.5f); 46 addPoint(size.width * 0.5f, -size.height * 0.5f); 47 addPoint(-size.width * 0.5f, -size.height * 0.5f); 48 49 // TODO (Bug 1611526): Support WebXR bounded reference spaces 50 } 51 52 already_AddRefed<XRReferenceSpace> 53 XRBoundedReferenceSpace::GetOffsetReferenceSpace( 54 const XRRigidTransform& aOriginOffset) { 55 RefPtr<XRBoundedReferenceSpace> offsetReferenceSpace = 56 new XRBoundedReferenceSpace(GetParentObject(), mSession, mNativeOrigin); 57 58 // https://immersive-web.github.io/webxr/#multiply-transforms 59 // An XRRigidTransform is essentially a rotation followed by a translation 60 gfx::QuaternionDouble otherOrientation = aOriginOffset.RawOrientation(); 61 // The resulting rotation is the two combined 62 offsetReferenceSpace->mOriginOffsetOrientation = 63 mOriginOffsetOrientation * otherOrientation; 64 // We first apply the rotation of aOriginOffset to 65 // mOriginOffsetPosition offset, then translate by the offset of 66 // aOriginOffset 67 offsetReferenceSpace->mOriginOffsetPosition = 68 otherOrientation.RotatePoint(mOriginOffsetPosition) + 69 aOriginOffset.RawPosition(); 70 71 return offsetReferenceSpace.forget(); 72 } 73 74 } // namespace mozilla::dom