Unity3D实现红外热成像效果
创作时间:
作者:
@小白创作中心
Unity3D实现红外热成像效果
引用
CSDN
1.
https://blog.csdn.net/zouxin_88/article/details/124181857
在Unity3D中实现红外热成像效果是一个有趣且实用的技术挑战。本文将详细介绍如何通过设置图层、创建相机、使用RenderTexture、编写C#脚本以及创建shader等步骤,实现一个基本的红外热成像效果。
实现步骤
设置图层
将需要在红外图像中高亮的物体设置到图层PostProcessing。创建相机CameraHighLight
新建一个相机CameraHighLight,设置其Culling Mask为PostProcessing,这样在这个相机中只有PostProcessing图层的物体可见。创建RenderTexture
新建一个RenderTexture,命名为HightLightRt,将CameraHighLight的Target Texture设为HightLightRt。创建相机Camera2
新建一个相机Camera2,这个相机用于显示其他物体。合并灰度图
接下来将CameraHighLight和Camera2中画面转化为两张灰度图,再合并成一张图。合并的过程使用OnRenderImage函数,该函数中使用shader的片断着色器处理,取到两张图相同位置的像素后,谁的灰度值高就取谁的亮度。为了突出CameraHighLight的物体,取Camera2中像素时将适当缩小亮度值。创建C#脚本ThermalVision.cs
新建C#脚本ThermalVision.cs,将ThermalVision.cs挂载到Camera2。using UnityEngine; public class ThermalVision : MonoBehaviour { public bool IsInverse = false; public RenderTexture OtherTex; private Material material1; private Material material2; // Creates a private material used to the effect void Awake() { material1 = new Material(Shader.Find("Hidden/Thermal1")); material2 = new Material(Shader.Find("Hidden/Thermal2")); } // Postprocess the image void OnRenderImage(RenderTexture source, RenderTexture destination) { if (IsInverse) { material1.SetTexture("_OtherTex", OtherTex); Graphics.Blit(source, destination, material1); } else { material2.SetTexture("_OtherTex", OtherTex); Graphics.Blit(source, destination, material2); } } }创建shader
新建两个shader,分别为Thermal1、Thermal2,一个为白色模式,一个为黑色模式。Shader "Hidden/Thermal1" { Properties{ _MainTex("Base (RGB)", 2D) = "white" {} _OtherTex("Other (RGB)", 2D) = "white" {} } SubShader{ Pass { CGPROGRAM #pragma vertex vert_img #pragma fragment frag #include "UnityCG.cginc" uniform sampler2D _MainTex; uniform sampler2D _OtherTex; float4 frag(v2f_img i) : COLOR { fixed4 renderTex = tex2D(_MainTex, i.uv); fixed4 OtherTex = tex2D(_OtherTex, i.uv); fixed gray1 = 0.2125 * renderTex.r + 0.7154 * renderTex.g + renderTex.b; fixed gray2 = 0.2125 * OtherTex.r + 0.7154 * OtherTex.g + OtherTex.b; fixed maxgr = gray1 * 0.1* step(gray2, gray1) + gray2 * step(gray1, gray2); //fixed maxgr = max(gray1, gray2); fixed3 grayColor = float3(maxgr, maxgr, maxgr); float4 result = renderTex; result.rgb = grayColor; return result; } ENDCG } } }Shader "Hidden/Thermal2" { Properties{ _MainTex("Base (RGB)", 2D) = "white" {} _OtherTex("Other (RGB)", 2D) = "white" {} } SubShader{ Pass { CGPROGRAM #pragma vertex vert_img #pragma fragment frag #include "UnityCG.cginc" uniform sampler2D _MainTex; uniform sampler2D _OtherTex; float4 frag(v2f_img i) : COLOR { fixed4 renderTex = tex2D(_MainTex, i.uv); fixed4 OtherTex = tex2D(_OtherTex, i.uv); fixed gray1 = 0.2125 * renderTex.r + 0.7154 * renderTex.g + renderTex.b; fixed gray2 = 0.2125 * OtherTex.r + 0.7154 * OtherTex.g + OtherTex.b; fixed maxgr = gray1 * 0.5* step(gray2, gray1) + gray2 * step(gray1, gray2); fixed3 grayColor = float3(maxgr, maxgr, maxgr); float4 result = renderTex; result.rgb = float3(1- grayColor.r, 1 - grayColor.g, 1 - grayColor.b); return result; } ENDCG } } }
最终效果
热门推荐
王凌航教授:病毒进化&抗病毒治疗概览
国产影视行业“她力量”在崛起
“罹”字不读luó ,也不读wéi,那正确读音是?
选择适合的机油对汽车性能有何影响?这些机油选择标准是否适用于所有车型?
无线监控摄像头:隐私与安全的双刃剑
观潮的螃蟹丨怀化对全球湘商张开了怀抱
什么是BIOS?一文搞懂BIOS(基本输入输出系统)
戴尔笔记本如何切换双显卡?切换步骤是什么?
遗产保护如何赋能乡村振兴——发现景德镇乡下的瓷业文化景观
围攻9价,国产HPV疫苗大变革
勇士王朝5年前就结束!第4冠是奇迹,克莱·汤普森重伤成关键转折
开源SDN解决方案对比:OpenDaylight vs ONOS vs Ryu
春天的节日:探究三月三的传统与习俗
亨利五世与阿金库尔战役:百年战争中的英军胜利与法军挫败
瓶装水的保质期是多长时间 瓶装水开盖后多久就不能喝了
中科院培育出饲草高粱新品种,可有效缓解优质饲草供需矛盾
文旅部推荐的这些湖南美食,有你家乡的吗?
厄贝沙坦如何用?看这篇就够了
橡胶密封圈的材质如何选择?
中国白酒文化之旅:穿越历史长河,品味现代韵味
中国游客在日本掀起消费新篇章
2025文科女生就业前景最好的十大热门专业
探索钢制办公家具的设计:融合市场,满足用户需求
如何撰写完美的项目实施方案?掌握这几个关键步骤!
城市交通管理新策略:限行时间的科学设定
汽车报废流程详解:从申请到回收的完整指南
如何快速定位并优化CPU与JVM内存性能瓶颈?
使人年轻的运动:保持活力与健康的秘密
一个小改变,延缓衰老!
贝斯自学指南:从入门到进阶的全方位指导