[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

相关文章

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

Maven 插件配置分层架构深度解析

《Maven插件配置分层架构深度解析》:本文主要介绍Maven插件配置分层架构深度解析,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录Maven 插件配置分层架构深度解析引言:当构建逻辑遇上复杂配置第一章 Maven插件配置的三重境界1.1 插件配置的拓扑

ubuntu如何部署Dify以及安装Docker? Dify安装部署指南

《ubuntu如何部署Dify以及安装Docker?Dify安装部署指南》Dify是一个开源的大模型应用开发平台,允许用户快速构建和部署基于大语言模型的应用,ubuntu如何部署Dify呢?详细请... Dify是个不错的开源LLM应用开发平台,提供从 Agent 构建到 AI workflow 编排、RA

ubuntu16.04如何部署dify? 在Linux上安装部署Dify的技巧

《ubuntu16.04如何部署dify?在Linux上安装部署Dify的技巧》随着云计算和容器技术的快速发展,Docker已经成为现代软件开发和部署的重要工具之一,Dify作为一款优秀的云原生应用... Dify 是一个基于 docker 的工作流管理工具,旨在简化机器学习和数据科学领域的多步骤工作流。它

Idea插件MybatisX失效的问题解决

《Idea插件MybatisX失效的问题解决》:本文主要介绍Idea插件MybatisX失效的问题解决,详细的介绍了4种问题的解决方法,具有一定的参考价值,感兴趣的可以了解一下... 目录一、重启idea或者卸载重装MyBATis插件(无需多言)二、检查.XML文件与.Java(该文件后缀Idea可能会隐藏

Nginx部署React项目时重定向循环问题的解决方案

《Nginx部署React项目时重定向循环问题的解决方案》Nginx在处理React项目请求时出现重定向循环,通常是由于`try_files`配置错误或`root`路径配置不当导致的,本文给大家详细介... 目录问题原因1. try_files 配置错误2. root 路径错误解决方法1. 检查 try_f

Spring Boot项目部署命令java -jar的各种参数及作用详解

《SpringBoot项目部署命令java-jar的各种参数及作用详解》:本文主要介绍SpringBoot项目部署命令java-jar的各种参数及作用的相关资料,包括设置内存大小、垃圾回收... 目录前言一、基础命令结构二、常见的 Java 命令参数1. 设置内存大小2. 配置垃圾回收器3. 配置线程栈大小