Skip to content

NCustomInputTrackedDevice组件

在应用开发过程中,往往需要更加自由的方式与场景中的UI,物体交互。并且按需进行点击,确认操作。而内置的设备此时往往可能并不能满足开发者的需要,此时自定义交互设备便可以满足这种交互需要。

接口名称返回值接口作用
SetCustomDeviceRayvoid设置自定义设备射线
SetCustomDeviceScreenPositionvoid设置自定义设备射线屏幕位置
SetHandActivebool设置手势激活
GetTrackedTypeTrackedDeviceType获取设备类型
SetHandJointDatabool设置手势数据
SetEnableDeviceTrackingvoid设置开启追踪
IsTrackingbool设备正在追踪

示例代码:

  • 创建自定义设备
cpp
void CustomRay::Start()
{
    for (int i = 0; i < 1; i++)
    {
        CustomTrackDevice trackDevice;
        trackDevice.m_CustomRay.origin = NActorManager::GetActor("cube")->GetPosition();
        trackDevice.m_CustomRay.direction = NActorManager::GetActor("cube")->GetComponent<NTransformComponent>()->GetDirection();
        trackDevice.m_TrackDevice = NInput::CreateCustomTrackingDevice();
        trackDevice.m_TrackDevice->SetEnableDeviceTracking(true);
        m_CustomTrackDevices.push_back(trackDevice);
    }
    //控制头控不进行拾取的设置
    NInput::SetEnableDeviceTracking(TrackedDeviceType::HMD, false);
}
void CustomRay::Update()
{
    m_CustomTrackDevices[0].m_CustomRay.origin = NActorManager::GetActor("cube")->GetPosition();
    m_CustomTrackDevices[0].m_CustomRay.direction = NActorManager::GetActor("cube")->GetComponent<NTransformComponent>()->GetDirection();
    if (NInput::GetKey(KeyboardKeys::Android_DpadRight))
    {
        NActorManager::GetActor("cube")->SetLocalRotation(NActorManager::GetActor("cube")->GetLocalRotation().ToEulerZXY()+Vector3(0,0.1,0));
    }
    if (NInput::GetKey(KeyboardKeys::Android_DpadLeft))
    {
        NActorManager::GetActor("cube")->SetLocalRotation(NActorManager::GetActor("cube")->GetLocalRotation().ToEulerZXY() + Vector3(0, -0.1, 0));
    }
    if (NInput::GetKey(KeyboardKeys::Android_DpadUp))
    {
        NActorManager::GetActor("cube")->SetLocalRotation(NActorManager::GetActor("cube")->GetLocalRotation().ToEulerZXY() + Vector3(-0.1, 0, 0));
    }
    if (NInput::GetKey(KeyboardKeys::Android_DpadDown))
    {
        NActorManager::GetActor("cube")->SetLocalRotation(NActorManager::GetActor("cube")->GetLocalRotation().ToEulerZXY() + Vector3(0.1, 0, 0));
    }
    if (m_CustomTrackDevices.size() > 0)
    {
        for (int i = 0; i < m_CustomTrackDevices.size(); i++)
        {
            auto direction = m_CustomTrackDevices[i].m_CustomRay.direction.NormalizedCopy();
            NGraphics::DrawLine(m_CustomTrackDevices[i].m_CustomRay.origin, m_CustomTrackDevices[i].m_CustomRay.origin + direction * 100, i == 0 ? Color::Red : Color::Green);
            m_CustomTrackDevices[i].m_TrackDevice->SetCustomDeviceRay(m_CustomTrackDevices[i].m_CustomRay.origin, direction);
        }
    }
}

对接标准HID输入设备

在接收了标准设备的输入之后,面对不同的平台,往往会出现必要的独特设备需要接入到应用中,例如:方向盘,自定义按钮,异形手柄等。为了开发者可以和这些设备进行对接,引擎支持自定义按钮注入功能。 即识别到自定义按键后,可以将其转化为标准按键事件,注入引擎的事件系统中,使其可以与交互系统对接。

示例代码:

  • Window接入手柄后注入按键
cpp
void KeyTest::Update()
{
    if (isKeyDown()) 
    {
		auto key = GetKey();
		auto event= GetKeyEventn(key);
		NInput::InjectKeyEvent(key, event.type == 0 ? InputEventType::KeyUp : InputEventType::KeyDown, ETargetWindow::EWindow1);
        }
    }
}

对接手势输入设备

当今,手势识别与对接已经是在xr,vr中绕不开的话题。手势识别的算法也百花齐放,百家争鸣。为了兼容不同手势算法数据。引擎除了内置的手势识别外,同样支持传入自定义的手势参数驱动手势模拟功能。 只需要通过自定义算法获取手势关节点的位置和旋转值,便可以直接驱动引擎内置的手势功能。

示例代码:

  • 创建自定义手势设备,并传入数值。
cpp
void hand::Start()
{
	// Triggered before the first execution of Update
   m_CustomHandDevice = NInput::CreateCustomTrackingDevice(TrackedDeviceType::HAND);
}
void hand::Update()
{
    for (int i = 0; i < 2; i++)
    {
        m_CustomHandDevice->SetHandActive(i==0?EHandType::Left: EHandType::Right, GetHandActive(i));
        NHandJoint* Data = GetHandData();
        m_CustomHandDevice->SetHandJointData(i == 0 ? EHandType::Left : EHandType::Right,  Data, GetJointCount(i)t);
    }
}