UnityLeapMotion流程记录

news2024/10/6 1:41:47

突然接到一个LeapMotion的项目,回想起上次做LeapMotion还是在几年前,但是当时没有去记录,所以这次就相当于是重新走了一遍流程。很苦恼,赶紧记录下来。防止之后忘记。这次的需求还是比较简单的,用手滑动控制图片序列播放。

准备

  • Unity版本2021.3.19f1c1
  • LeapMotion一台

开始接入

Downloads – Page Array – Ultraleap

首先来到官网下载LeapMotion的必备组件

选择对应的设备,我这台设备是比较旧的。

无脑安装

安装好后还需要来到github页面下载unity-leapmotion的插件

https://github.com/ultraleap/UnityPlugin

然后导入到unity工程中

导入后找到Capsule Hands场景就是官方的示例

针对需求查看了官网文档。有两种想法:

  1. 通过XRXRI and XRHands Integration - Ultraleap documentation

去用手滑动UI。

  1. 获取手部追踪数据,用程序去判断,得到值,然后去改变视频的帧。

最终选择了第二种方法,觉得第二种比较简单。

创建HandControl.cs脚本

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

using Leap;
using Leap.Unity;

public class HandControl : MonoBehaviour
{

	public static bool Gesture_left = false;
	public static bool Gesture_right = false;
	public static bool Gesture_up = false;
	public static bool Gesture_down = false;
	public static bool Gesture_zoom = false;
	public static float movePOs = 0.0f;

	private LeapProvider mProvider;
	private Frame mFrame;
	private Hand mHand;

	public ImageSwitcher imageSwitcher;


	private Vector3 leftPosition;
	private Vector3 rightPosition;
	public static float zoom = 1.0f;
	[Tooltip("Velocity (m/s) of Palm ")]

	public float smallestVelocity = 1.45f;//手掌移动的最小速度

	[Tooltip("Velocity (m/s) of Single Direction ")]
	[Range(0, 1)]
	public float deltaVelocity = 1.0f;//单方向上手掌移动的速度

	// Use this for initialization
	void Start()
	{
		mProvider = FindObjectOfType<LeapProvider>() as LeapProvider;
	}

	// Update is called once per frame
	void Update()
	{

		mFrame = mProvider.CurrentFrame;//获取当前帧
										//获得手的个数
										//print ("hand num are " + mFrame.Hands.Count);

		if (mFrame.Hands.Count > 0)
		{
			
			if (mFrame.Hands.Count == 2)
				zoom = CalcuateDistance(mFrame);

			if (mFrame.Hands.Count == 1)
				LRUDGestures(mFrame, ref movePOs);
		}
	}


	float CalcuateDistance(Frame mFrame)
	{
		Gesture_zoom = true;
		Gesture_left = false;
		Gesture_right = false;

		float distance = 0f;
		//print ("Two hands");
		foreach (var itemHands in mFrame.Hands)
		{
			if (itemHands.IsLeft)
			{
				leftPosition = itemHands.PalmPosition;
				//print ("leftPosition" + leftPosition);
			}
			if (itemHands.IsRight)
			{
				rightPosition = itemHands.PalmPosition;
				//print ("rightPosition" + rightPosition);
			}
		}

		if (leftPosition != Vector3.zero && rightPosition != Vector3.zero)
		{

			Vector3 leftPos = new Vector3(leftPosition.x, leftPosition.y, leftPosition.z);
			Vector3 rightPos = new Vector3(rightPosition.x, rightPosition.y, rightPosition.z);

			distance = 10 * Vector3.Distance(leftPos, rightPos);
			//print("distance" + distance);
		}

		if (distance != 0)
			return distance;
		else
			return distance = 1;
	}




	void LRUDGestures(Frame mFrame, ref float movePOs)
	{
		Gesture_zoom = false;
		foreach (var item in mFrame.Hands)
		{
			int numFinger = item.Fingers.Count;
			//print ("item is  " + numFinger);

			//print("hand are " + isOpenFullHand (item));
			// print ("isOpenFullHands is  " + isOpenFullHands(item));


			if (item.GrabStrength == 1)
			{
				

			}
			else if (item.GrabStrength == 0)
			{
				//print ("num is 5, open your hand");
				//print("PalmVelocity" + item.PalmVelocity);
				//print("PalmPosition" + item.PalmPosition);
				movePOs = item.PalmPosition.x;
				if (isMoveLeft(item))
				{
					Gesture_left = true;
					Gesture_right = false;
					//print("move left");

				}
				else if (isMoveRight(item))
				{
					Gesture_left = false;
					Gesture_right = true;
					//print("move Right");

				}
				else if (isMoveUp(item))
				{
					Gesture_left = false;
					Gesture_right = false;
					print("move Up");
					imageSwitcher.PreviousImage();
				}
				else if (isMoveDown(item))
				{
					Gesture_left = false;
					Gesture_right = false;
					print("move Down");
					imageSwitcher.NextImage();

				}
				else if (isMoveForward(item))
				{
					Gesture_left = false;
					Gesture_right = false;
					print("move Forward");
					imageSwitcher.PreviousImage();
				}
				else if (isMoveBack(item))
				{
					Gesture_left = false;
					Gesture_right = false;
					print("move back");
					imageSwitcher.NextImage();
				}
			}
		}
	}



	private bool isStone(Hand hand)
	{
		//print ("hand.GrabAngle" + hand.GrabAngle);
		return hand.GrabStrength > 2.0f;
	}
	//是否抓取
	public bool isGrabHand(Hand hand)
	{
		return hand.GrabStrength > 0.8f;        //抓取力 
	}


	//hand move four direction
	public bool isMoveRight(Hand hand)
	{

		return hand.PalmVelocity.x > deltaVelocity && !isStationary(hand);
	}

	// 手划向右边
	public bool isMoveLeft(Hand hand)
	{

		//print (hand.PalmVelocity.x );
		return hand.PalmVelocity.x < -deltaVelocity && !isStationary(hand);
	}

	//手向上 
	public bool isMoveUp(Hand hand)
	{
		//print ("hand.PalmVelocity.y" + hand.PalmVelocity.y);

		return hand.PalmVelocity.y > deltaVelocity && !isStationary(hand);
	}

	//手向下  
	public bool isMoveDown(Hand hand)
	{
		return hand.PalmVelocity.y < -deltaVelocity && !isStationary(hand);
	}


	//手向前
	public bool isMoveForward(Hand hand)
	{
		//print (hand.PalmVelocity.z);
		return hand.PalmVelocity.z > deltaVelocity && !isStationary(hand);
	}

	//手向后 
	public bool isMoveBack(Hand hand)
	{
		return hand.PalmVelocity.z < -deltaVelocity && !isStationary(hand);
	}

	//固定不动的
	public bool isStationary(Hand hand)
	{
		return hand.PalmVelocity.magnitude < smallestVelocity;      //Vector3.Magnitude返回向量的长度
	}


}

创建ImageSwitcher.cs脚本

using UnityEngine;
using UnityEngine.UI; // 引入UI命名空间

public class ImageSwitcher : MonoBehaviour
{
    public Image displayImage; // 用于显示图片的Image组件
    public Sprite[] images; // 存储所有图片的数组
    private int currentImageIndex = 0; // 当前显示的图片索引

    void Start()
    {
        // 初始化时显示第一张图片
        if (images.Length > 0)
        {
            displayImage.sprite = images[currentImageIndex];
        }
    }

    // 切换到下一张图片
    public void NextImage()
    {
        if (images.Length > 0)
        {
            currentImageIndex = (currentImageIndex + 1) % images.Length; // 使用模运算确保索引循环
            displayImage.sprite = images[currentImageIndex];
        }
    }

    // 切换到上一张图片
    public void PreviousImage()
    {
        if (images.Length > 0)
        {
            currentImageIndex = (currentImageIndex - 1 + images.Length) % images.Length; // 使用模运算确保索引循环
            displayImage.sprite = images[currentImageIndex];
        }
    }
}

将两个脚本挂载在场景中并赋值

image switcher中的images将准备好的序列帧拖入

创建Canvas和Image。将image拖入image Switcher中

最终场景是这样的结构。使用的场景是官方的Capsule Hands。

成功运行。这个项目比较简单,开发的时候也没有遇到坑,在此记录下防止后面忘了流程。

官方文档

Get started with our plugins for XR developers - Ultraleap documentation

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1796429.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

MyBatisPlus——入门到进阶

✅作者简介&#xff1a;大家好&#xff0c;我是 Meteors., 向往着更加简洁高效的代码写法与编程方式&#xff0c;持续分享Java技术内容。&#x1f34e;个人主页&#xff1a;Meteors.的博客&#x1f49e;当前专栏&#xff1a;知识分享、知识备份✨特色专栏&#xff1a; 知识分享…

C++中的priority_queue和deque以及适配器

C中的priority_queue和deque 一丶 priority_queue1.1 priority_queue的介绍1.2 priority_queue的使用1.3 priority_queue的模拟实现 二丶 deque2.1 deque的简单介绍2.2 deque的缺陷2.3 为什么要选择deque作为stack和queue的迭代器 三丶 容器适配器3.1 什么是适配器3.2 STL标准库…

kafka-消费者组(SpringBoot整合Kafka)

文章目录 1、消费者组1.1、使用 efak 创建 主题 my_topic1 并建立6个分区并给每个分区建立3个副本1.2、创建生产者发送消息1.3、application.yml配置1.4、创建消费者监听器1.5、创建SpringBoot启动类1.6、屏蔽 kafka debug 日志 logback.xml1.7、引入spring-kafka依赖1.8、消费…

【Qt秘籍】[010]-Qt常用控件

一、控件概述 在GUI&#xff08;图形用户界面&#xff09;开发领域&#xff0c;Qt无疑是众多开发者心中的首选框架之一。它不仅跨平台、功能强大&#xff0c;而且拥有丰富且灵活的控件库&#xff0c;使得开发者能够快速构建美观、高效的用户界面。对于初学者而言&#xff0…

万里长城第一步——尚庭公寓【技术概述】

简略版&#xff1a; 项目概述主要是移动端&#xff08;房源检索&#xff1b;预约看房&#xff0c;租赁管理&#xff0c;浏览历史&#xff09;和后台管理&#xff08;管理员对房源进行操作&#xff09;&#xff1b; 项目使用前后端分离的方法&#xff0c;主要以后端为主&#xf…

CSS真题合集(一)

CSS真题合集&#xff08;一&#xff09; 1. 盒子模型1.1 盒子模型的基本组成1.2 盒子模型的实际大小1.3 盒子模型的两种类型1.4 设置盒子模型1.5 弹性盒子模型 2. BFC2.1 主要用途2.2 触发BFC的方法2.2 解决外边距的塌陷问题&#xff08;垂直塌陷&#xff09; 3. 响应式布局3.1…

Django 外键关联数据

在设计数据库的时候&#xff0c;是得需要通过外键的形式将各个表进行连接。 原先的表是这样的 要想更改成这样&#xff1a; 下面是操作步骤&#xff1a; 有两张表是关联的 # 在 models.py 里创建class Department(models.Model):"""部门表""&quo…

Qt for android : 调用 OpenCV库错误集(To Be Continue)

error: error: undefined symbol: AMediaExtractor_new 在调用 VideoCapture等库时出现 解决: LIBS -lmediandk

【ROS2大白话】四、ROS2非常简单的传参方式

系列文章目录 【ROS2大白话】一、ROS2 humble及cartorgrapher安装 【ROS2大白话】二、turtlebot3安装 【ROS2大白话】三、给turtlebot3安装realsense深度相机 【ROS2大白话】四、ROS2非常简单的传参方式 文章目录 系列文章目录前言一、launch文件传参的demo1. 编写launch.py文…

Python语法详解module4(函数)

目录 一、函数基础1. 函数的概念和作用2. 函数的定义和调用3. 参数传递 二、返回值和文档字符串返回值的概念和用法1. 返回值的概念2. 使用 return 关键字返回值&#xff1a;3. 多个返回值的情况&#xff1a; 文档字符串&#xff08;docstring&#xff09;的作用和使用方法1. 文…

linux 下修改屏幕分辨率

在使用麒麟虚拟机时&#xff0c;不知道咋回事&#xff0c;会自动改变分辨率。 使用界面设置分辨率选项修改时&#xff0c;下面的保存修改按钮显示不出来&#xff0c;无法完成设置。 所以需要使用命令行修改一下分辨率&#xff0c;修改命令如下所示&#xff1a; 1、执行xrand…

Questflow借助MongoDB Atlas以AI重新定义未来工作方式

MongoDB客户案例导读 Questflow借助MongoDB Atlas赋能AI员工&#xff0c;助力中小型初创企业自动化工作流程&#xff0c;简化数据分析&#xff0c;提升客户体验&#xff0c;推动AI与员工的协作&#xff0c;重新定义未来工作方式。 协作式AI自动化平台 无需编码即可拥有自己的…

动态规划9:LCR 099. 最小路径和

动态规划解题步骤&#xff1a; 1.确定状态表示&#xff1a;dp[i]是什么 2.确定状态转移方程&#xff1a;dp[i]等于什么 3.初始化&#xff1a;确保状态转移方程不越界 4.确定填表顺序&#xff1a;根据状态转移方程即可确定填表顺序 5.确定返回值 题目链接&#xff1a;LCR …

Leetcode3170. 删除星号以后字典序最小的字符串

Every day a Leetcode 题目来源&#xff1a;3170. 删除星号以后字典序最小的字符串 解法1&#xff1a;栈 由于要去掉最小的字母&#xff0c;为了让字典序尽量小&#xff0c;相比去掉前面的字母&#xff0c;去掉后面的字母更好。 从左到右遍历字符串 s&#xff0c;用 26 个栈…

AI大底座核心平台:百度百舸AI异构计算平台(AI IaaS)与AI中台(AI PaaS)

AI大底座正是整合了以上端到端全要素技术能力&#xff0c;将基础架构IaaS与应用平台PaaS能力深度融合&#xff0c;面向企业和产业AI生 产与应用的全生命周期提供完整解决方案。 百舸AI异构计算平台是AI IaaS层的核心平台&#xff0c;包括AI计算、AI存储、AI加速、AI容器四层套件…

frp开启Dashboard

公网服务器 公网服务器frps.toml bindPort 81 # 127.0.0.1 Dashboard 端口&#xff0c;后面会进行内网穿透&#xff0c;因此不用配置ip为0.0.0.0 webServer.port 82auth.token "token令牌"公网服务器frpc.toml # 因为是在公网服务器的frpc所以直接配127.0.0.1 …

计算机科学(学习笔记三)

内容来源&#xff1a;计算机科学 指令和程序 指令&#xff1a;指示计算机要做什么的代码&#xff0c;多条指令共同组成程序。 计算机指令长度 由于早期计算机每个字只有8位&#xff0c;指令只占4位&#xff0c;意味着只能有16个指令&#xff0c;这远远不够。 现代计算机有两…

【Hive SQL 每日一题】统计各个商品今年销售额与去年销售额的增长率及排名变化

文章目录 测试数据需求说明需求实现分步解析 测试数据 -- 创建商品表 DROP TABLE IF EXISTS products; CREATE TABLE products (product_id INT,product_name STRING );INSERT INTO products VALUES (1, Product A), (2, Product B), (3, Product C), (4, Product D), (5, Pro…

【调试笔记-20240606-Linux-为 OpenWrt 的 nginx 服务器添加Shell CGI 支持】

调试笔记-系列文章目录 调试笔记-20240606-Linux-为 OpenWrt 的 nginx 服务器添加Shell CGI 支持 文章目录 调试笔记-系列文章目录调试笔记-20240606-Linux-为 OpenWrt 的 nginx 服务器添加Shell CGI 支持 前言一、调试环境操作系统&#xff1a;Windows 10 专业版调试环境调试…

【Qt知识】部分QWidget属性表格

QWidget是Qt库中所有图形用户界面组件的基类&#xff0c;它提供了大量属性以供自定义和配置控件的行为和外观。下面列出了一些主要的QWidget属性及其作用。 属性 作用 accessibleName 控件的辅助技术名称&#xff0c;用于无障碍访问。 accessibleDescription 控件的辅助技…