HoloLens MRTK模型切割算法解析
HoloLens MRTK模型切割算法解析
- 脚本解析
- 算法实现
- 修改shader实现不同的功能
- 修改Clipping Plane裁剪的范围
- 修改模型透明度
- 多个ClippingPrimitive对同一个模型进行切割
- Two different ClippingPrimitives clip a render
- Two of the same ClippingPrimitives clip a render
脚本解析
例程中主要用到的脚本有位于Assets/MRTK/Core/Utilities/StandardShader路径下的ClippingPlane.cs
、ClippingBox.cs、ClippingSphere.cs、ClippingPrimitive.cs(继承关系),位于Assets/MRTK/Core/StandardAssets/Shaders路径下的MixedRealityStandard.shader
着色器,以及位于Assets/MRTK/Core/StandardAssets/Shaders路径下的MixedRealityShaderUtils.cginc
。它们三个的关系如下:
清晰框图及注释见:https://download.csdn.net/download/qq_41452267/12986383
算法实现
面切割算法:对于心脏模型的每一个片元,在世界坐标系下,使用平面的法线向量n与平面中心点到待渲染点的方向向量d做点乘。根据计算结果的正负号将模型划分为平面的内侧和外侧两部分。
球切割算法:对于心脏模型的每一个片元,计算其到球心的距离,如果小于半径,则位于内侧,反之,位于外侧。
立方体切割算法:对于心脏模型的每一个片元,将片元坐标变换到立方体的模型坐标系下得到坐标position,用position的每个坐标分量取绝对值再减去立方形大小。最终得到的坐标中最大值大于零的,说明在立方体外,最大值小于零的说明在立方体内。
修改shader实现不同的功能
修改Clipping Plane裁剪的范围
在原工程中,Clipping Plane会将整个空间一分为二,空间的一侧被切割,另一侧不被切割。
需求:希望Clipping Plane根据自身范围进行切割,只有接触到的部分被切割,未接触到的部分不予切割,效果如下图:
实现方法
①在场景中修改变量至合适大小,默认大小为1米x1米。这里设置切片为0.5米
①在ClippingPlane.cs
文件中声明变量,并获取切片信息
③在着色器MixedRealityStandard.shader
中调用变量和函数,函数定义在MixedRealityShaderUtils.cginc
文件中:
MixedRealityStandard.shader
如下:
primitiveDistance = min(primitiveDistance, PointVsPlanePart(i.worldPosition.xyz, _ClipPlane, _ClipPlanePosition, _ClipPlaneUp, _ClipPlaneRight, _ClipPlaneForward) * _ClipPlaneSide);
MixedRealityShaderUtils.cginc
定义函数如下:
//部分截取,返回值为负时显示,为正时不显示
inline float PointVsPlanePart(float3 worldPosition, float4 plane,float3 planeRawPosition,float3 planeUp, float3 planeRight, float3 planeForward)
{ float3 planePosition = plane.xyz * plane.w;float3 distance = worldPosition - planeRawPosition;//计算面片与平面中心坐标差float x = dot(distance, planeRight);//计算面片在平面坐标系下的x y值float z = dot(distance, planeForward);float y = dot(distance, planeUp);if (y<0 || z<-0.25 || z>0.25 || x<-0.25 || x>0.25)return -0.2;//超过面片范围显示显示elsereturn 0.2;//不显示//return dot(worldPosition - planePosition, plane.xyz);//plane.xyz是平面法向量
}
修改模型透明度
需求:原始可切割模型默认为不透明渲染模式,希望调整模型为半透明可切割,且透明度可调整。
①创建材质球,选择着色器为mixedrealitytoolkit/Standard(这里最好自己创一个新的shader,否则很多场景都将无法正常显示),选择Rendering Mode为Transparent
②在MixedRealityStandard.shader
中(或者是自己创建的shader),修改最终的output如下:
// Perform non-alpha clipped primitive clipping on the final output.
#if defined(_CLIPPING_PRIMITIVE) && !defined(_ALPHA_CLIP)//output *= saturate(primitiveDistance * (1.0f / _BlendedClippingWidth));//saturate函数将返回值限制在0-1之间if (primitiveDistance > 0) {output.a = 0.3;}else {output *= 0;}#endifreturn output;}ENDCG}
output.a即是调整透明度的地方。
多个ClippingPrimitive对同一个模型进行切割
By default only one ClippingPrimitive
can clip a renderer at a time. If your project requires more than one ClippingPrimitive
to influence a renderer the sample code below demonstrates how to achieve this.
Two different ClippingPrimitives clip a render
How to have two different ClippingPrimitives clip a render. For example a ClippingSphere and ClippingBox at the same time:
// Within MRTK/Core/StandardAssets/Shaders/MixedRealityStandard.shader (or another MRTK shader) change:#pragma multi_compile _ _CLIPPING_PLANE _CLIPPING_SPHERE _CLIPPING_BOX// to:#pragma multi_compile _ _CLIPPING_PLANE
#pragma multi_compile _ _CLIPPING_SPHERE
#pragma multi_compile _ _CLIPPING_BOX
Two of the same ClippingPrimitives clip a render
How to have two of the same ClippingPrimitives clip a render. For example two ClippingBoxes at the same time:
// 1) Add the below MonoBehaviour to your project:[ExecuteInEditMode]
public class SecondClippingBox : ClippingBox
{/// <inheritdoc />protected override string Keyword{get { return "_CLIPPING_BOX2"; }}/// <inheritdoc />protected override string ClippingSideProperty{get { return "_ClipBoxSide2"; }}/// <inheritdoc />protected override void Initialize(){base.Initialize();clipBoxSizeID = Shader.PropertyToID("_ClipBoxSize2");clipBoxInverseTransformID = Shader.PropertyToID("_ClipBoxInverseTransform2");}
}// 2) Within MRTK/Core/StandardAssets/Shaders/MixedRealityStandard.shader (or another MRTK shader) add the following multi_compile pragma:#pragma multi_compile _ _CLIPPING_BOX2// 3) In the same shader change:#if defined(_CLIPPING_PLANE) || defined(_CLIPPING_SPHERE) || defined(_CLIPPING_BOX)// to:#if defined(_CLIPPING_PLANE) || defined(_CLIPPING_SPHERE) || defined(_CLIPPING_BOX) || defined(_CLIPPING_BOX2)// 4) In the same shader add the following shader variables:#if defined(_CLIPPING_BOX2)fixed _ClipBoxSide2;float4 _ClipBoxSize2;float4x4 _ClipBoxInverseTransform2;
#endif// 5) In the same shader change:#if defined(_CLIPPING_BOX)primitiveDistance = min(primitiveDistance, PointVsBox(i.worldPosition.xyz, _ClipBoxSize.xyz, _ClipBoxInverseTransform) * _ClipBoxSide);
#endif// to:#if defined(_CLIPPING_BOX)primitiveDistance = min(primitiveDistance, PointVsBox(i.worldPosition.xyz, _ClipBoxSize.xyz, _ClipBoxInverseTransform) * _ClipBoxSide);
#endif
#if defined(_CLIPPING_BOX2)primitiveDistance = min(primitiveDistance, PointVsBox(i.worldPosition.xyz, _ClipBoxSize2.xyz, _ClipBoxInverseTransform2) * _ClipBoxSide2);
#endif
Finally, add a ClippingBox and SecondClippingBox component to your scene and specify the same renderer for both boxes. The renderer should now be clipped by both boxes simultaneously.
详情见:https://microsoft.github.io/MixedRealityToolkit-Unity/Documentation/Rendering/ClippingPrimitive.html
HoloLens MRTK模型切割算法解析相关推荐
- Mmseg中文分词算法解析
Mmseg中文分词算法解析 @author linjiexing 开发中文搜索和中文词库语义自己主动识别的时候,我採用都是基于mmseg中文分词算法开发的Jcseg开源project.使用场景涉及搜索 ...
- CVPR目标检测与实例分割算法解析:FCOS(2019),Mask R-CNN(2019),PolarMask(2020)
CVPR目标检测与实例分割算法解析:FCOS(2019),Mask R-CNN(2019),PolarMask(2020) 目标检测:FCOS(CVPR 2019) 目标检测算法FCOS(FCOS: ...
- 脑机接口主流算法解析课程视频汇总
目录 讲座1--SSVEP算法解析 讲座2--ERP/P300算法解析 讲座3--运动想象算法解析 讲座4--情感脑机接口算法解析 本分享为脑机学习者Rose整理发表于公众号:脑机接口社区 .QQ交流 ...
- yolo类检测算法解析——yolo v3
原文:https://www.cnblogs.com/cvtoEyes/p/8608205.html yolo类检测算法解析--yolo v3 计算机视觉的发展史可谓很长了,它的分支很多,而且理论那是 ...
- 【机器学习】树回归和聚类算法解析和应用
[机器学习]树回归和聚类算法解析和应用 文章目录 1 树回归 2 CART ( Classification And Regression Tree) 分类回归树 3 K-means3.1 合理选择 ...
- 【机器学习】通俗的k-近邻算法算法解析和应用
[机器学习]通俗的k-近邻算法算法解析和应用 文章目录 1 概述 2 KNN 场景 3 KNN 原理 4 实例:改进约会网站的配对效果 5 算法总结 6 KNN算法的优缺点 7 图像分类应用 1 概述 ...
- 激光IMU融合——LIO-Mapping / LIOM / LINS / LIO-SAM算法解析
激光IMU融合--LIO-Mapping / LIOM / LINS / LIO-SAM算法解析 激光IMU融合--LIO-Mapping / LIOM / LINS / LIO-SAM算法解析 1. ...
- AI学习笔记(七)图像滤波器、OpenCV算法解析
AI学习笔记之图像滤波器.OpenCV算法解析 图像滤波器 图像噪声 噪声的产生 信噪比 高斯噪声 椒盐噪声 其他噪声 图像滤波 滤波的目的 滤波的要求 各种滤波器 均值滤波 中值滤波 最大最小值滤波 ...
- 武器目标分配问题研究进展: 模型、算法与应用
源自:系统公正与电子技术 作者:李梦杰 常雪凝 石建迈 陈超 黄金才 刘忠 摘 要 武器目标分配问题是指挥控制与任务规划领域的关键难点之一, 也是军事运筹领域的基础研究课题.经过多年研究, ...
最新文章
- 017_Cookie
- 三星I9000挖煤模式和R模式进入方法
- for-each循环的认识、定义、适用对象、举例、局限性
- 神奇的文本编辑,惊人的移花接木 | ACM MM 2019 论文赏析
- 【Robot Framework】字符串判断,if语句多执行条件,多执行语句
- 深入浅出:Java多线程编程实战(一)
- 怎样在你的团队做 Code Review ?
- IB中文阅读书单推荐
- 用Unity同时开发【微信小游戏】【安卓】【IOS】游戏#6.2 WebSocket通信
- java protected 构造方法_Java中protected语义解释
- Occam‘s razor - 奥卡姆剃刀
- 移动端H5页面编辑器开发实战--经验技巧篇
- 情感分析之PMI互信息
- 计算机组装配置(Win10蓝屏错误代码大全详解)
- AutoCAD .Net 不同文档间复制对象
- 荣耀轻薄本MagicBook 14使用体验分享 性能拉满续航无敌
- [渝粤教育] 郑州工程技术学院 大学计算机基础 参考 资料
- PostgreSQL 创建分区表
- SQL2000客户端连接不上
- CAS-搭建CAS Server服务端(静态认证)