Skip to content

Instantly share code, notes, and snippets.

@hon454
Last active October 2, 2023 19:06
Show Gist options
  • Save hon454/b639d1a95287f78b29d261d2f81b8968 to your computer and use it in GitHub Desktop.
Save hon454/b639d1a95287f78b29d261d2f81b8968 to your computer and use it in GitHub Desktop.
[SteamVR][Unity] Tracker tracking without HMD
using UnityEngine;
using Valve.VR;
public class SteamVR_TrackingWithoutHMD : MonoBehaviour
{
private CVRSystem _vrSystem;
private TrackedDevicePose_t[] _poses = new TrackedDevicePose_t[OpenVR.k_unMaxTrackedDeviceCount];
// initialize
void Awake()
{
var err = EVRInitError.None;
_vrSystem = OpenVR.Init(ref err, EVRApplicationType.VRApplication_Other);
if (err != EVRInitError.None)
{
// handle init error
}
}
// get tracked device poses
void Update()
{
// get the poses of all tracked devices
_vrSystem.GetDeviceToAbsoluteTrackingPose(ETrackingUniverseOrigin.TrackingUniverseStanding, 0.0f, _poses);
// send the poses to SteamVR_TrackedObject components
SteamVR_Events.NewPoses.Send(_poses);
}
// shutdown
void OnDestroy()
{
OpenVR.Shutdown();
}
}
@hon454
Copy link
Author

hon454 commented Nov 6, 2017

[시스템 설정]
SteamVR 설정
. C:\Program Files (x86)\Steam\steamapps\common\SteamVR\resources\settings\default.vrsettings
. “steamvr” : {
“requiredHmd” : false
...
}

[Unity 프로젝트 설정]
import SteamVR plugin
uncheck [Edit - Preferences - SteamVR - Automatically Enable VR]
uncheck [Player Settings - XR Settings - Virtual Reality Supported]
SteamVR_TrackedObject component 등 tracker 관련 API는 그대로 사용 가능

[Build]
"Virtual Reality Supported”가 설정되어 있지 않으면 빌드시 VR 플러그인 파일이 복사되지 않으므로 수동으로 해야 함.
. Copy C:\Program Files\Unity\Editor\Data\UnityExtensions\Unity\VR\Win64\openvr_api.dll
to _Data\Plugins\

@sergiobd
Copy link

sergiobd commented Sep 13, 2018

Thanks @Kaonuri for this code!

Translation for non-Korean speakers:
As you have to uncheck the "Virtual Reality Supported” option, openvr dll is not going to be included in your build. You should put it into the YourBuildName_Data/Plugins/ folder.

I wrote a script to do this automatically in the post-build callback, if anyone needs it:

https://gist.github.com/sergiobd/5dcb2fdda182584ddb7b76af138aa249

@sergiobd
Copy link

@Kaonuri, is this code MIT Licensed? If so, would you mind adding a license for it? e.g. https://opensource.org/licenses/MIT

@bonzomi
Copy link

bonzomi commented Oct 2, 2023

Is there any way to do this on Unity 2021 or later? The openvr_api.dll is not included in the folder structure
Edit: Solved. For anyone facing the same problem, the openvr_api.dll is located under your Unity project folder instead of the Unity Editor installation folder. The path should look something like this "YourProject\Library\PackageCache\com.valvesoftware.unity.openvr...\Runtime\x64\openvr_api.dll"
Replace the 'x64' to 'x86' if your build settings are set to Intel 32-bit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment