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 } } }
最终效果
热门推荐
哈士奇"二"不"二",训练是关键!
“清华女硕士蓝翔学厨”,别看错这个故事
王殿武:思维升级,获取知识的能力,比知识本身更重要
泰州:一座千年古城的奋进之路
VeraCrypt开源磁盘加密软件
移动硬盘怎么加密?移动硬盘加密软件有哪些?
秋高气爽,苏州适合徒步和闲逛的10条免费步道线路推荐
私有化部署VSCode Web:实现随时随地轻办公
如何分析股票交易的策略和技巧?这些交易策略有哪些优势和风险?
现磨咖啡粉和挂耳咖啡的区别?
冻干速溶黑咖啡好吗?口感特点如何?咖啡常识分享
微信公众号运营指南:从定位到变现的全方位策略
国内经济学专业100强高校新鲜出炉,人大和北大被评为“S级”
南北朝时期佛教叛乱之法庆事件
弗朗茨·李斯特:钢琴大师与浪漫主义音乐的代表
价值投资者的经验之谈:张磊的投资核心理念
矩阵论-第三章:矩阵的标准型
多种方法查看电脑屏幕尺寸
潮聚·向汕 | 广大潮汕侨胞心系家国故土,建言献策、发展家乡
压力大长白发吃什么好?助白发变黑的4种食物
风吹半夏剧评:时代的一束光,打在人生的海平面上——记飞天奖优秀电视剧
威廉·达福对自己在《蜘蛛侠4》的回归持开放态度
祖宅拆迁补偿是否是夫妻共同财产?
75年前 拉-200原型机首次试飞 苏联“超级截击机”被新型雷达终结
75年前 拉-200原型机首次试飞 苏联“超级截击机”被新型雷达终结
如何避免四氯化碳中毒
大承气汤:古方通便秘籍,温和调理有秘诀
大承气汤:古方通便秘籍,温和调理有秘诀
如何降低隆鼻手术潜在风险指南
电子居住证的用途及应用场景