栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 游戏开发 > Unity3D

Unity3d实现红外热成像效果

Unity3D 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Unity3d实现红外热成像效果

1.将需要在红外图像中高亮的物体设置到图层PostProcessing。

2.新建一个相机CameraHighLight,设置其Culling Mask为PostProcessing,也就是在这个相机中只有PostProcessing图层的物体可见。

3.新建一个RenderTexture,命名为HightLightRt,将CameraHighLight的Target Texture设HightLightRt。

4.新建一个相机Camera2,这个相机用于显示其他物体。

接下来将CameraHighLight和Camera2中画面转化为两张灰度图,再合并成一张图。合并的过程使用OnRenderImage函数,该函数中使用shader的片断着色器处理,取到两张图相同位置的像素后,谁的灰度值高就取谁的亮度。为了高度CameraHighLight的物体,取Camera2中像素时将适当缩小亮度值。

5.新建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);
        }
    }
}

6.新建两个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
			}
		}
}

最终效果

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/897966.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号