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 } } }
最终效果
热门推荐
陈其美:孙中山的得力助手,上海光复的首功之臣
从当铺学徒到上海都督:陈其美与辛亥革命的传奇人生
快餐店管理基本流程中如何保证食品安全?
新交规:亲属可代扣驾驶证分,每年限3次12分
车牌靓号过户指南:条件、流程与费用详解
充电桩欧盟CE认证简析
冬至必喝桂圆羊肉汤:补气血、驱寒气,附详细烹饪步骤
广东省皮肤病防治院:如何选择专业的皮肤科医院
新西兰鹿肉:秋冬养生的“长寿之肉”,鹿骨汤滋补正当时
鹿肉配韭菜香菇等5种食材,冬季养生这样吃最好
营养价值超鸡鸭猪,鹿肉成健康饮食新宠
红焖鹿肉这样做最好:焯水、炒糖色等关键步骤详解
新西兰鹿肉:秋冬进补的健康之选,高蛋白低脂肪更营养
菜籽油营养超橄榄油,专家解析安全食用指南
中医教你辨识咳嗽类型,科学养生告别久咳
中医辨识痰:揭秘健康密码
中医教你如何通过辨痰止咳
菜籽油富含ω-3更益心,橄榄油抗氧化更适合凉拌
菜籽油选购指南:从分类到使用,四大要素全解析
菜籽油橄榄油搭配食用,专家推荐糖尿病患者这样选油
当归羊肉汤:冬至时节增强免疫力的养生佳品
老母鸡汤:营养超群,养生助眠的滋补佳品
牛肉汤、羊肉汤:冬季进补的科学选择指南
一碗养生汤:5大类别、四季饮用全攻略
民进乌鲁木齐市委会开展艾滋病防治专题讲座进校园活动
蓉昌高速带火阿坝州冬季游
蓉昌高速复线成汶段:连接川藏的重要交通动脉
猎头佣金揭秘:企业招聘的秘密武器
焯水、防粘、炒肉:五个技巧提升你的烹饪水平
健康烹饪全攻略:4种方式+食材搭配,让家人吃出好身体