[Hololens]基于MMD4Mecanim插件的HoloLens MMD部署

2023-10-10 19:59

本文主要是介绍[Hololens]基于MMD4Mecanim插件的HoloLens MMD部署,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

Chapter0 - 前言

最近在根据微软的HoloLens教程学习HoloLens的基础,昨天突发奇想用电脑现有的mmd模型、动作数据加入Unity中然后部署到HoloLens进行基于环境的放置与跳舞操作。(老师不在实验室系列)百度了一下之后找到了日本大佬的MMD4Mecanim插件,可以将MMD模型以及音乐和动作导入到Unity中,便开始进行部署。

Chapter1 - 基本工作与环境

  1. 使用的工具包括 Unity 2018.1Visual Studio 2017MMD4Mecanim插件,HoloLens emulatorHoloLens
  2. MMD4Mecanim下载地址,同时也是作者的官网。
  3. 想要导入的MMD模型文件,动作文件与匹配的音乐文件(本文未获得作者允许,故不提供模型与动作下载链接)。
  4. 其他东西百度很快能找到。
    [注] 若只是要导入MMD模型到Unity只需要Unity和插件就行。

Chapter2 - 导入模型到Unity

  1. 新建一个Unity项目,注意选择3D。
  2. 在资源栏右键添加Unity Package,将下载的插件中的 MMD4Mecanim.unitypackage 导入Asset当中。
  3. 将想要导入的MMD模型文件直接拖入Asset当中,以及需要的动作数据文件和音乐。
  4. 导入后点击MMD文件夹可以看到插件自动转化的.MMD4Mecanim文件,选择一个点击,在右方Inspector面板最下方选中所有协议,同意。
  5. 在下一个界面中,将动作文件.vmd拖入相应的vmd处,单击process。此时插件会开始自动导入模型文件然后变成Unity文件。
  6. 有时候模型有多个组件,可只导入人物模型。
  7. 导入完成后,拖拽生成的模型到Hierarchy中,模型导入完成。
    在这里插入图片描述
    在这里插入图片描述

Chapter3 - 添加动作与音乐

  1. 在模型文件夹下右键 Create > Animator Controller 添加一个新的动作控制器,双击打开;
  2. 将模型中的vmd文件拖拽到控制器中与Entry连接;
    在这里插入图片描述
    在这里插入图片描述
  3. 在Hierarchy中单击模型,在右方 Animator 中找到 Controller ;在Hierarchy中单击模型,在右方 Animator 中找到 Controller
    在这里插入图片描述
  4. 单击齿轮选择之前创建的动作控制器,此时点击播放按钮,模型已经可以开始跳舞了;
  5. 在Hierarchy中单击模型,在Inspector最下方单击 Add Component 。选择 Audio > Audio Source,将音乐文件拖入AudioClip中,下方Volume可以调节音量;
  6. 因为实在HoloLens中实现,故将Spatial Blend调至3D,声音最大距离也可随意修改。

Chapter4 - 添加光标,地图以及放置代码

  1. 在微软HoloLens官网教程中下载素材文件;
    HoloLens教程素材
  2. 在Unity项目的Asset中新建文件夹 101-Asset 存放素材,导入 Origami 中的 Asset 文件;
  3. Hologram 中的 Cursor 以及 Spatial Mapping 添加入Hierarchy中;
    在这里插入图片描述
  4. 单击MMD模型,右键新建一个方块来作为模型的底座;
  5. 调整方块位置以及模型大小,依照自我喜好调整,cursor是用来作为凝视点光标的,改变模型大小的同时也可以修改cursor的大小;
  6. 调整 Main Camera 到合适的位置,能够拍摄到模型,将 Clear Flags 改为 Solid ColorBackground 改为纯黑色;
  7. 在Asset中新建文件夹 Script 用于存放代码;
  8. 在 script 中右键新建 C# Script ,命名为 WorldCursor,双击在vs中打开,添加如下代码,特别注意,命名要正确:
using UnityEngine;public class WorldCursor : MonoBehaviour
{private MeshRenderer meshRenderer;// Use this for initializationvoid Start(){// Grab the mesh renderer that's on the same object as this script.meshRenderer = this.gameObject.GetComponentInChildren<MeshRenderer>();}// Update is called once per framevoid Update(){// Do a raycast into the world based on the user's// head position and orientation.var headPosition = Camera.main.transform.position;var gazeDirection = Camera.main.transform.forward;RaycastHit hitInfo;if (Physics.Raycast(headPosition, gazeDirection, out hitInfo)){// If the raycast hit a hologram...// Display the cursor mesh.meshRenderer.enabled = true;// Move thecursor to the point where the raycast hit.this.transform.position = hitInfo.point;// Rotate the cursor to hug the surface of the hologram.this.transform.rotation = Quaternion.FromToRotation(Vector3.up, hitInfo.normal);}else{// If the raycast did not hit a hologram, hide the cursor mesh.meshRenderer.enabled = false;}}
}
  1. 完成后保存,将脚本拖拽到Hierarchy中的cursor上,成功完成后cursor的component中会有该脚本;
    在这里插入图片描述
  2. 在script中右键新建C# script,命名为GazeGestureManager,双击后添加如下代码:
using UnityEngine;
using UnityEngine.XR.WSA.Input;public class GazeGestureManager : MonoBehaviour
{public static GazeGestureManager Instance { get; private set; }// Represents the hologram that is currently being gazed at.public GameObject FocusedObject { get; private set; }GestureRecognizer recognizer;// Use this for initializationvoid Start(){Instance = this;// Set up a GestureRecognizer to detect Select gestures.recognizer = new GestureRecognizer();recognizer.Tapped += (args) =>{// Send an OnSelect message to the focused object and its ancestors.if (FocusedObject != null){FocusedObject.SendMessageUpwards("OnSelect", SendMessageOptions.DontRequireReceiver);}};recognizer.StartCapturingGestures();}// Update is called once per framevoid Update(){// Figure out which hologram is focused this frame.GameObject oldFocusObject = FocusedObject;// Do a raycast into the world based on the user's// head position and orientation.var headPosition = Camera.main.transform.position;var gazeDirection = Camera.main.transform.forward;RaycastHit hitInfo;if (Physics.Raycast(headPosition, gazeDirection, out hitInfo)){// If the raycast hit a hologram, use that as the focused object.FocusedObject = hitInfo.collider.gameObject;}else{// If the raycast did not hit a hologram, clear the focused object.FocusedObject = null;}// If the focused object changed this frame,// start detecting fresh gestures again.if (FocusedObject != oldFocusObject){recognizer.CancelGestures();recognizer.StartCapturingGestures();}}
}
  1. 拖拽该脚本到MMD模型上;
  2. 新建C# Script 命名为 TapToPlaceParent ,添加如下代码:
using UnityEngine;public class TapToPlaceParent : MonoBehaviour
{bool placing = false;// Called by GazeGestureManager when the user performs a Select gesturevoid OnSelect(){// On each Select gesture, toggle whether the user is in placing mode.placing = !placing;// If the user is in placing mode, display the spatial mapping mesh./*if (placing){SpatialMapping.Instance.DrawVisualMeshes = true;}// If the user is not in placing mode, hide the spatial mapping mesh.else{SpatialMapping.Instance.DrawVisualMeshes = false;}*/SpatialMapping.Instance.DrawVisualMeshes = true;	//添加}// Update is called once per framevoid Update(){// If the user is in placing mode,// update the placement to match the user's gaze.if (placing){// Do a raycast into the world that will only hit the Spatial Mapping mesh.var headPosition = Camera.main.transform.position;var gazeDirection = Camera.main.transform.forward;RaycastHit hitInfo;if (Physics.Raycast(headPosition, gazeDirection, out hitInfo,30.0f, SpatialMapping.PhysicsRaycastMask)){// Move this object's parent object to// where the raycast hit the Spatial Mapping mesh.this.transform.parent.position = hitInfo.point;// Rotate this object's parent object to face the user./*Quaternion toQuat = Camera.main.transform.localRotation;toQuat.x = 0;toQuat.z = 0;this.transform.parent.rotation = toQuat;*/this.transform.parent.LookAt(Camera.main.transform);	//添加Quaternion toQuat = this.transform.parent.localRotation;	//toQuat.x = 0;toQuat.z = 0;			//this.transform.parent.rotation = toQuat;		//添加}}}
}

[注] 该代码中相对于微软源码有两处代码进行了修改,第一处是用于拿起模型等待放置时显示地图的网格,微软源码是拿起时显示网格,放下隐藏网格。我改为了始终显示网格,可根据个人需要选择代码。第二处为了使模型正面面对相机。

  1. 保存,将脚本拖拽到MMD模型下属的 Cube 上。
  2. 脚本基本完成,开始进行build。

Chapter5 - Building Setting

  1. 开始进入build环节,在 Edit > Project Setting > Quality 中将质量改为最低;
    在这里插入图片描述
  2. 点击 File > Build Setting 选择 Universal Windows Platform ,点击 Swich Platform ,然后点击 Player Settings ,在 Other Settings 中把 Scripting Backend 改为 .NET 。在 XR Settings 中勾选 Visual Reality Supported
  3. Publishing settingsCapability 中勾选 SpatialPerception
  4. 设定如图,SDK和vs可以直接默认,点击 Build ,新建文件夹 App 然后build;
    在这里插入图片描述
  5. 等待完成后打开App文件夹,双击.sln文件,在vs上运行。

Chapter6 - vs中运行模拟器或部署到设备

  1. 配置如图所示,部署到虚拟机;
    在这里插入图片描述
  2. 直接点击或 Ctrl + F5;
  3. 部署完成后光标移至底座然后点击就可在环境中进行放置。

运行结果:
在这里插入图片描述
在这里插入图片描述

这篇关于[Hololens]基于MMD4Mecanim插件的HoloLens MMD部署的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/182727

相关文章

ElasticSearch+Kibana通过Docker部署到Linux服务器中操作方法

《ElasticSearch+Kibana通过Docker部署到Linux服务器中操作方法》本文介绍了Elasticsearch的基本概念,包括文档和字段、索引和映射,还详细描述了如何通过Docker... 目录1、ElasticSearch概念2、ElasticSearch、Kibana和IK分词器部署

部署Vue项目到服务器后404错误的原因及解决方案

《部署Vue项目到服务器后404错误的原因及解决方案》文章介绍了Vue项目部署步骤以及404错误的解决方案,部署步骤包括构建项目、上传文件、配置Web服务器、重启Nginx和访问域名,404错误通常是... 目录一、vue项目部署步骤二、404错误原因及解决方案错误场景原因分析解决方案一、Vue项目部署步骤

Linux流媒体服务器部署流程

《Linux流媒体服务器部署流程》文章详细介绍了流媒体服务器的部署步骤,包括更新系统、安装依赖组件、编译安装Nginx和RTMP模块、配置Nginx和FFmpeg,以及测试流媒体服务器的搭建... 目录流媒体服务器部署部署安装1.更新系统2.安装依赖组件3.解压4.编译安装(添加RTMP和openssl模块

0基础租个硬件玩deepseek,蓝耘元生代智算云|本地部署DeepSeek R1模型的操作流程

《0基础租个硬件玩deepseek,蓝耘元生代智算云|本地部署DeepSeekR1模型的操作流程》DeepSeekR1模型凭借其强大的自然语言处理能力,在未来具有广阔的应用前景,有望在多个领域发... 目录0基础租个硬件玩deepseek,蓝耘元生代智算云|本地部署DeepSeek R1模型,3步搞定一个应

redis群集简单部署过程

《redis群集简单部署过程》文章介绍了Redis,一个高性能的键值存储系统,其支持多种数据结构和命令,它还讨论了Redis的服务器端架构、数据存储和获取、协议和命令、高可用性方案、缓存机制以及监控和... 目录Redis介绍1. 基本概念2. 服务器端3. 存储和获取数据4. 协议和命令5. 高可用性6.

Deepseek R1模型本地化部署+API接口调用详细教程(释放AI生产力)

《DeepseekR1模型本地化部署+API接口调用详细教程(释放AI生产力)》本文介绍了本地部署DeepSeekR1模型和通过API调用将其集成到VSCode中的过程,作者详细步骤展示了如何下载和... 目录前言一、deepseek R1模型与chatGPT o1系列模型对比二、本地部署步骤1.安装oll

nginx部署https网站的实现步骤(亲测)

《nginx部署https网站的实现步骤(亲测)》本文详细介绍了使用Nginx在保持与http服务兼容的情况下部署HTTPS,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值... 目录步骤 1:安装 Nginx步骤 2:获取 SSL 证书步骤 3:手动配置 Nginx步骤 4:测

Tomcat高效部署与性能优化方式

《Tomcat高效部署与性能优化方式》本文介绍了如何高效部署Tomcat并进行性能优化,以确保Web应用的稳定运行和高效响应,高效部署包括环境准备、安装Tomcat、配置Tomcat、部署应用和启动T... 目录Tomcat高效部署与性能优化一、引言二、Tomcat高效部署三、Tomcat性能优化总结Tom

如何在本地部署 DeepSeek Janus Pro 文生图大模型

《如何在本地部署DeepSeekJanusPro文生图大模型》DeepSeekJanusPro模型在本地成功部署,支持图片理解和文生图功能,通过Gradio界面进行交互,展示了其强大的多模态处... 目录什么是 Janus Pro1. 安装 conda2. 创建 python 虚拟环境3. 克隆 janus

本地私有化部署DeepSeek模型的详细教程

《本地私有化部署DeepSeek模型的详细教程》DeepSeek模型是一种强大的语言模型,本地私有化部署可以让用户在自己的环境中安全、高效地使用该模型,避免数据传输到外部带来的安全风险,同时也能根据自... 目录一、引言二、环境准备(一)硬件要求(二)软件要求(三)创建虚拟环境三、安装依赖库四、获取 Dee