[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

相关文章

MySQL 主从复制部署及验证(示例详解)

《MySQL主从复制部署及验证(示例详解)》本文介绍MySQL主从复制部署步骤及学校管理数据库创建脚本,包含表结构设计、示例数据插入和查询语句,用于验证主从同步功能,感兴趣的朋友一起看看吧... 目录mysql 主从复制部署指南部署步骤1.环境准备2. 主服务器配置3. 创建复制用户4. 获取主服务器状态5

golang程序打包成脚本部署到Linux系统方式

《golang程序打包成脚本部署到Linux系统方式》Golang程序通过本地编译(设置GOOS为linux生成无后缀二进制文件),上传至Linux服务器后赋权执行,使用nohup命令实现后台运行,完... 目录本地编译golang程序上传Golang二进制文件到linux服务器总结本地编译Golang程序

java使用protobuf-maven-plugin的插件编译proto文件详解

《java使用protobuf-maven-plugin的插件编译proto文件详解》:本文主要介绍java使用protobuf-maven-plugin的插件编译proto文件,具有很好的参考价... 目录protobuf文件作为数据传输和存储的协议主要介绍在Java使用maven编译proto文件的插件

如何在Ubuntu 24.04上部署Zabbix 7.0对服务器进行监控

《如何在Ubuntu24.04上部署Zabbix7.0对服务器进行监控》在Ubuntu24.04上部署Zabbix7.0监控阿里云ECS服务器,需配置MariaDB数据库、开放10050/1005... 目录软硬件信息部署步骤步骤 1:安装并配置mariadb步骤 2:安装Zabbix 7.0 Server

浏览器插件cursor实现自动注册、续杯的详细过程

《浏览器插件cursor实现自动注册、续杯的详细过程》Cursor简易注册助手脚本通过自动化邮箱填写和验证码获取流程,大大简化了Cursor的注册过程,它不仅提高了注册效率,还通过友好的用户界面和详细... 目录前言功能概述使用方法安装脚本使用流程邮箱输入页面验证码页面实战演示技术实现核心功能实现1. 随机

CnPlugin是PL/SQL Developer工具插件使用教程

《CnPlugin是PL/SQLDeveloper工具插件使用教程》:本文主要介绍CnPlugin是PL/SQLDeveloper工具插件使用教程,具有很好的参考价值,希望对大家有所帮助,如有错... 目录PL/SQL Developer工具插件使用安装拷贝文件配置总结PL/SQL Developer工具插

maven中的maven-antrun-plugin插件示例详解

《maven中的maven-antrun-plugin插件示例详解》maven-antrun-plugin是Maven生态中一个强大的工具,尤其适合需要复用Ant脚本或实现复杂构建逻辑的场景... 目录1. 核心功能2. 典型使用场景3. 配置示例4. 关键配置项5. 优缺点分析6. 最佳实践7. 常见问题

Web技术与Nginx网站环境部署教程

《Web技术与Nginx网站环境部署教程》:本文主要介绍Web技术与Nginx网站环境部署教程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录一、Web基础1.域名系统DNS2.Hosts文件3.DNS4.域名注册二.网页与html1.网页概述2.HTML概述3.

MyBatis分页插件PageHelper深度解析与实践指南

《MyBatis分页插件PageHelper深度解析与实践指南》在数据库操作中,分页查询是最常见的需求之一,传统的分页方式通常有两种内存分页和SQL分页,MyBatis作为优秀的ORM框架,本身并未提... 目录1. 为什么需要分页插件?2. PageHelper简介3. PageHelper集成与配置3.

Nginx使用Keepalived部署web集群(高可用高性能负载均衡)实战案例

《Nginx使用Keepalived部署web集群(高可用高性能负载均衡)实战案例》本文介绍Nginx+Keepalived实现Web集群高可用负载均衡的部署与测试,涵盖架构设计、环境配置、健康检查、... 目录前言一、架构设计二、环境准备三、案例部署配置 前端 Keepalived配置 前端 Nginx