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 } } }
最终效果
热门推荐
遵义羊肉粉:清代名小吃变身城市产业新名片
河南省邓州市莲花神佛像工艺厂:传承百年匠心,打造道教佛像精品
西安道教造像艺术:千年古都的文化瑰宝
草原宏宝羊羔肉汤:冬季养生的暖心之选
羊排选购攻略:专家教你挑到最好吃的羊肉
职场上下级关系处理指南:10个实用建议助你职场进阶
从“小头兵”到“大头兵”:大厂晋升全流程实战分享
清远长隆新生象宝宝萌翻众人,中国最大非洲象种群再添新丁
长隆五胞胎孟加拉虎幼仔亮相,科普活动精彩纷呈
清远长隆开园倒计时:亲子游必打卡
阀门小样图:工业建筑的秘密武器
CAD高手教你高效绘制阀门图
如何处理违停情况以避免罚款?这些实用方法请收好
羽绒服能机洗吗?看标签选对方式最安全
羽绒服清洗指南:洗衣机也能洗,但要注意这些细节
春节红包压力:如何在金钱游戏中保持情感的真实?
成都七日游攻略:德阳出发畅游古蜀名胜与美食
老年夫妻分床睡,竟引发家庭大战?
氨基酸补充剂:更快吸收助力运动表现,减少25%蛋白质摄入
氨基酸补充剂助力运动:30分钟吸收,提升肌肉恢复与耐力
专家解析:肌营养不良的饮食调理全攻略
赖氨酸:人体必需的多功能氨基酸,这些食物富含
蛋白质分解代谢:消化、代谢与产物利用的关键过程
专家建议:燕麦南瓜等五种食物助力胰岛功能保护
清血八味胶囊的作用与使用指南
汽车违停的危害及处罚措施详解
红眼病传染性强但可防可控,注意卫生可有效预防
平江路汉服拍摄攻略:两处体验馆及三大取景点详解
一文掌握平江路:交通、景点、美食、住宿全攻略
苏州平江路:唐宋格局保存至今,水陆并行展现江南风韵