[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

相关文章

tomcat多实例部署的项目实践

《tomcat多实例部署的项目实践》Tomcat多实例是指在一台设备上运行多个Tomcat服务,这些Tomcat相互独立,本文主要介绍了tomcat多实例部署的项目实践,具有一定的参考价值,感兴趣的可... 目录1.创建项目目录,测试文China编程件2js.创建实例的安装目录3.准备实例的配置文件4.编辑实例的

SpringBoot配置Ollama实现本地部署DeepSeek

《SpringBoot配置Ollama实现本地部署DeepSeek》本文主要介绍了在本地环境中使用Ollama配置DeepSeek模型,并在IntelliJIDEA中创建一个Sprin... 目录前言详细步骤一、本地配置DeepSeek二、SpringBoot项目调用本地DeepSeek前言随着人工智能技

通过Docker Compose部署MySQL的详细教程

《通过DockerCompose部署MySQL的详细教程》DockerCompose作为Docker官方的容器编排工具,为MySQL数据库部署带来了显著优势,下面小编就来为大家详细介绍一... 目录一、docker Compose 部署 mysql 的优势二、环境准备与基础配置2.1 项目目录结构2.2 基

CentOS 7部署主域名服务器 DNS的方法

《CentOS7部署主域名服务器DNS的方法》文章详细介绍了在CentOS7上部署主域名服务器DNS的步骤,包括安装BIND服务、配置DNS服务、添加域名区域、创建区域文件、配置反向解析、检查配置... 目录1. 安装 BIND 服务和工具2.  配置 BIND 服务3 . 添加你的域名区域配置4.创建区域

OpenManus本地部署实战亲测有效完全免费(最新推荐)

《OpenManus本地部署实战亲测有效完全免费(最新推荐)》文章介绍了如何在本地部署OpenManus大语言模型,包括环境搭建、LLM编程接口配置和测试步骤,本文给大家讲解的非常详细,感兴趣的朋友一... 目录1.概况2.环境搭建2.1安装miniconda或者anaconda2.2 LLM编程接口配置2

大数据spark3.5安装部署之local模式详解

《大数据spark3.5安装部署之local模式详解》本文介绍了如何在本地模式下安装和配置Spark,并展示了如何使用SparkShell进行基本的数据处理操作,同时,还介绍了如何通过Spark-su... 目录下载上传解压配置jdk解压配置环境变量启动查看交互操作命令行提交应用spark,一个数据处理框架

如何使用Docker部署FTP和Nginx并通过HTTP访问FTP里的文件

《如何使用Docker部署FTP和Nginx并通过HTTP访问FTP里的文件》本文介绍了如何使用Docker部署FTP服务器和Nginx,并通过HTTP访问FTP中的文件,通过将FTP数据目录挂载到N... 目录docker部署FTP和Nginx并通过HTTP访问FTP里的文件1. 部署 FTP 服务器 (

C#集成DeepSeek模型实现AI私有化的流程步骤(本地部署与API调用教程)

《C#集成DeepSeek模型实现AI私有化的流程步骤(本地部署与API调用教程)》本文主要介绍了C#集成DeepSeek模型实现AI私有化的方法,包括搭建基础环境,如安装Ollama和下载DeepS... 目录前言搭建基础环境1、安装 Ollama2、下载 DeepSeek R1 模型客户端 ChatBo

Ubuntu 22.04 服务器安装部署(nginx+postgresql)

《Ubuntu22.04服务器安装部署(nginx+postgresql)》Ubuntu22.04LTS是迄今为止最好的Ubuntu版本之一,很多linux的应用服务器都是选择的这个版本... 目录是什么让 Ubuntu 22.04 LTS 变得安全?更新了安全包linux 内核改进一、部署环境二、安装系统

JAVA集成本地部署的DeepSeek的图文教程

《JAVA集成本地部署的DeepSeek的图文教程》本文主要介绍了JAVA集成本地部署的DeepSeek的图文教程,包含配置环境变量及下载DeepSeek-R1模型并启动,具有一定的参考价值,感兴趣的... 目录一、下载部署DeepSeek1.下载ollama2.下载DeepSeek-R1模型并启动 二、J