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 } } }
最终效果
热门推荐
NHK纪录片揭秘731部队:精英医学研究者的堕落与历史真相
消化系统异常?这些小妙招让你远离烦恼!
中医养生:从脾胃调养到全面健康管理
高纤维食物:改善消化的金钥匙
浙江丽水旅游攻略,探索丽水的自然美景与人文风情
浙江丽水最佳自驾路线攻略:绿谷仙境之旅
面粉质量检测:企业效益提升的关键
掌握比容积,用烘焙百分比提升面包质量!
金莎巧克力:高热量背后的甜蜜秘密
宣璐:用演技诠释角色内心的温度
红烧鸡爪要好吃很简单,轻松就让鸡爪无腥味,软烂入味,太好吃了
旺销的网红蒜香柠檬鸡爪,加入自制红油,风味十足(附详细配方与红油做法)
大人发烧较快退烧方法
四川观赏红叶的更佳时期:几月份最适宜?
笔记本电脑连不上网?这15招让你秒变IT达人!
Windows系统网络连接故障大揭秘!
双十一抢购前必看:笔记本电脑网络连接问题全攻略
从五阿哥到导演:苏有朋的多重宇宙人生
从五阿哥到花无缺:苏有朋的演艺之路
苏有朋:从五阿哥到花无缺,经典角色塑造演艺传奇
春节祈福:如何化解太岁影响?
SMART原则目标管理方法
心血管疾病合并CKD患者降压治疗指南:血压控制与药物选择
鲟鱼全身都是宝:营养价值与功效详解
奥特曼新作揭秘:雷德王角色大变脸
布莱泽奥特曼12月剧照:雷德王惊喜回归!
2025年春节档,姜文、刘德华、肖战谁更值得期待?
春节档双雄:《封神》与《射雕》带你穿越历史风云
莽山旅游攻略:景点门票、开放时间及美食住宿全攻略
软件项目中合作伙伴和供应商的有效策略