unity-特效-雷达扫描效果

news2024/10/7 2:30:39

使用后处理方式制作

using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class GlobalScanEffect : MonoBehaviour
{
    public float startScanRange = 0;
    public float maxScanRange = 20;
    public float scanWidth = 3;
    public float scanSpeed = 1;
    public Color headColor;
    public Color trailColor;

    private bool isInScan = false;
    private Vector3 centerPos;
    private float scanRadius;
    private IEnumerator scanHandler = null;

    public Material mat;

    void OnEnable()
    {
        scanRadius = startScanRange;
        Camera.main.depthTextureMode = DepthTextureMode.Depth;
    }

    void Update()
    {
        if (Input.GetMouseButtonDown(0))
        {
            Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
            RaycastHit hit;
            if (Physics.Raycast(ray, out hit))
            {
                centerPos = hit.point;
                scanRadius = 0;
                if (scanRadius <= startScanRange)
                {
                    Scan();
                }
                // else
                // {
                //     ScanBack();
                // }
            }
        }
    }

    protected void OnRenderImage(RenderTexture source, RenderTexture destination)
    {
        // if (isInScan)
        if (mat != null && isInScan)
        {
            mat.SetVector("_ScanCenterPos", centerPos);
            mat.SetFloat("_ScanRadius", scanRadius);
            mat.SetFloat("_ScanWidth", scanWidth);
            mat.SetColor("_HeadColor", headColor);
            mat.SetColor("_TrailColor", trailColor);

            RaycastCornerBlit(source, destination, mat);
        }
        else
        {
            Graphics.Blit(source, destination);
        }
    }

    void RaycastCornerBlit(RenderTexture source, RenderTexture dest, Material mat)
    {
        float CameraFar = Camera.main.farClipPlane;
        float CameraFov = Camera.main.fieldOfView;
        float CameraAspect = Camera.main.aspect;

        float fovWHalf = CameraFov * 0.5f;

        Vector3 toRight = Camera.main.transform.right * Mathf.Tan(fovWHalf * Mathf.Deg2Rad) * CameraAspect;
        Vector3 toTop = Camera.main.transform.up * Mathf.Tan(fovWHalf * Mathf.Deg2Rad);

        Vector3 topLeft = (Camera.main.transform.forward - toRight + toTop);
        float CameraScale = topLeft.magnitude * CameraFar;

        topLeft.Normalize();
        topLeft *= CameraScale;

        Vector3 topRight = (Camera.main.transform.forward + toRight + toTop);
        topRight.Normalize();
        topRight *= CameraScale;

        Vector3 bottomRight = (Camera.main.transform.forward + toRight - toTop);
        bottomRight.Normalize();
        bottomRight *= CameraScale;

        Vector3 bottomLeft = (Camera.main.transform.forward - toRight - toTop);
        bottomLeft.Normalize();
        bottomLeft *= CameraScale;

        RenderTexture.active = dest;

        mat.SetTexture("_MainTex", source);

        GL.PushMatrix();
        GL.LoadOrtho();

        mat.SetPass(0);

        GL.Begin(GL.QUADS);

        GL.MultiTexCoord2(0, 0.0f, 0.0f);
        GL.MultiTexCoord(1, bottomLeft);
        GL.Vertex3(0.0f, 0.0f, 0.0f);

        GL.MultiTexCoord2(0, 1.0f, 0.0f);
        GL.MultiTexCoord(1, bottomRight);
        GL.Vertex3(1.0f, 0.0f, 0.0f);

        GL.MultiTexCoord2(0, 1.0f, 1.0f);
        GL.MultiTexCoord(1, topRight);
        GL.Vertex3(1.0f, 1.0f, 0.0f);

        GL.MultiTexCoord2(0, 0.0f, 1.0f);
        GL.MultiTexCoord(1, topLeft);
        GL.Vertex3(0.0f, 1.0f, 0.0f);

        GL.End();
        GL.PopMatrix();
    }

    void CheckAndBlock()
    {
        if (scanHandler != null)
        {
            StopCoroutine(scanHandler);
        }
    }

    void Scan()
    {
        CheckAndBlock();
        scanHandler = ScanCoroutine();
        StartCoroutine(scanHandler);
    }

    void ScanBack()
    {
        CheckAndBlock();
        scanHandler = ScanBackCoroutine();
        StartCoroutine(scanHandler);
    }

    private IEnumerator ScanCoroutine()
    {
        isInScan = true;
        while (scanRadius < maxScanRange)
        {
            scanRadius += scanSpeed;
            yield return new WaitForSecondsRealtime(.01f);
        }
        scanRadius = maxScanRange;
        isInScan = false;
    }

    private IEnumerator ScanBackCoroutine()
    {
        isInScan = true;
        while (scanRadius > startScanRange)
        {
            scanRadius -= scanSpeed;
            yield return new WaitForSecondsRealtime(.01f);
        }
        scanRadius = startScanRange;
        isInScan = false;
    }
}

shader 

Shader "LSQ/Screen Effect/GlobalScanEffect"
{
    Properties
    {
        _MainTex("Texture", 2D) = "white" {}
    }
    SubShader
    {
        // No culling or depth
		Cull Off ZWrite Off ZTest Always

        Pass
        {
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            #include "UnityCG.cginc"

            struct appdata
            {
                float4 vertex : POSITION;
                float2 uv : TEXCOORD0;
                float4 ray : TEXCOORD1;
            };

            struct v2f
            {
                float2 uv : TEXCOORD0;
                float4 vertex : SV_POSITION;
                float4 ray : TEXCOORD2;
            };

            sampler2D _MainTex;
            sampler2D _CameraDepthTexture;
            float3 _ScanCenterPos;
			float _ScanRadius;
			float _ScanWidth;
			float4 _HeadColor;
			float4 _TrailColor;

            v2f vert (appdata v)
            {
                v2f o;
                o.vertex = UnityObjectToClipPos(v.vertex);
                o.uv = v.uv;
                o.ray = v.ray;
                return o;
            }

            fixed4 frag (v2f i) : SV_Target
            {
                fixed4 col = tex2D(_MainTex, i.uv);
                //Get Pixel World Pos
                float depth = Linear01Depth(DecodeFloatRG(tex2D(_CameraDepthTexture, i.uv)));
                float4 toCameraVector = depth * i.ray;
				float3 worldPos = _WorldSpaceCameraPos + toCameraVector;
                //Get Scan Ring
                float outerRing = _ScanRadius + _ScanWidth * 0.5;
                float innerRing = _ScanRadius - _ScanWidth * 0.5;
                float distanceToCenter = distance(_ScanCenterPos, worldPos);
                float value = smoothstep(innerRing, outerRing, distanceToCenter); 
                //Get Color
                fixed4 ringColor;
                if(value >= 1 || value <= 0)
                { 
                    value = 0; 
                    ringColor = float4(1,1,1,1);
                }
                else
                {
                    ringColor = lerp(_TrailColor, _HeadColor, value);
                }
                
                return col * ringColor;
            }
            ENDCG
        }
    }
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1857083.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

数据库期末复习知识点

A卷 1. 选择题(30) 2. 判断范式(10) 判断到第三范式 3. 程序填空(20) 4. 分析填空(15) 5. 写SQL(25) 5一题 恶性 B卷 1. 单选(30) 2. 填空 (20) 3. 程序填空(20) 4. 写SQL(30) 知识点 第一章 数据库管理系统(DBMS) 主要功能 数据定义功能 (DDL, 数据定义语言, …

第二证券股市知识:小白炒股是做长线好还是短线好?

关于小白来说&#xff0c;挑选炒长线比炒短线要好一些&#xff0c;其间原因如下&#xff1a; ​ 1、对出资者技能要求相对较低 短线出资&#xff0c;需要出资者对个股的走势掌握比较精确&#xff0c;才干通过高抛低吸赚取必定的差价&#xff0c;否则很容易让散户卖飞个股&am…

快速记忆成百上千个账号密码

在日常生活中&#xff0c;我们不仅需要记忆6位数字的银行卡密码&#xff0c;还需要记忆各式各样网站和应用的账号密码&#xff0c;可能我们自己也不记得曾经注册过多少个账号。账号和密码如此繁多&#xff0c;管理这些账号和密码&#xff0c;也是一个让人头疼的问题。 最原始的…

前两天上线了一个小功能,差点把我们项目搞崩溃

项目场景&#xff1a; 最近一直在迭代公司的系统&#xff0c;业务提出需要增加一个消息通知的功能&#xff0c;对接完需求之后&#xff0c;我们就 开始热火朝天的编码、测试、上线&#xff0c; 就是右上角这个小图标&#xff0c;为了提升用户体验&#xff0c;我们采用每隔20S…

有效利用MRP能为中小企业带来什么?

在离散制造企业&#xff0c;主流的生产模式主要为面向订单生产和面向库存生产&#xff08;又称为预测生产&#xff09;&#xff0c;在中小企业中&#xff0c;一般为面向订单生产&#xff0c;也有部分面向库存和面向订单混合的生产方式&#xff08;以面向订单为主&#xff0c;面…

windows系统docker镜像导出

docker镜像导入导出(windows)_windowdocker下载镜像导出-CSDN博客https://blog.csdn.net/qq_22211217/article/details/93936363

网页设计软件Bootstrap Studio6.7.1

Bootstrap Studio是一个适用于Windows的程序,允许您使用流行的fre***orca Bootstrap创建和原型网站。您可以将现成的组件拖动到工作区并直观地自定义它们。该程序生成干净和语义的PDF、CSS和JS代码,所有Web浏览器都支持这些代码。 Bootstrap Studio有一个漂亮而强大的界面,它…

[个人感悟] MySQL应该考察哪些问题?

前言 数据存储一直是软件开发中必不可少的一环, 从早期的文件存储txt, Excel, Doc, Access, 以及关系数据库时代的MySQL,SQL Server, Oracle, DB2, 乃至最近的大数据时代f非关系型数据库:Hadoop, HBase, MongoDB. 此外还有顺序型数据库InfluxDB, 图数据库Neo4J, 分布式数据库T…

linux系统指令查漏补缺

目录 一.磁盘操作 二.lvm 三.top 4.nohup 一.磁盘操作 1. lsblk -f 显示磁盘和它的相关内容 2.tuen2fs -c -1 /dev/sdx 关闭某个磁盘的自检 3.修改配置&#xff0c;使文件系统不要开机自检 cat /etc/fstab 全0表示开机不自检 全1表示开机自检 同时在这个文件中可添加…

计算机网络面试HTTP篇二

HTTP/1.1 如何优化&#xff1f; 问你一句&#xff1a;「你知道 HTTP/1.1 该如何优化吗&#xff1f;」 我们可以从下面这三种优化思路来优化 HTTP/1.1 协议&#xff1a; 尽量避免发送 HTTP 请求&#xff1b;在需要发送 HTTP 请求时&#xff0c;考虑如何减少请求次数&#xff…

Nature推荐的三种ChatGPT论文写作指令

1. 润色学术论文 ChatGPT学术润色指令&#xff1a; “I’m writing a paper on [topic]for a leading [discipline] academic journal. WhatItried to say in the following section is [specific point]. Please rephrase itfor clarity, coherence and conciseness, ensuri…

C语言---数据结构(1)--时间复杂和空间复杂度计算

1.什么是时间复杂度和空间复杂度 1.1算法效率 算法效率分为时间效率和空间效率 时间效率被称为时间复杂度&#xff0c;而空间效率被称作空间复杂度。 时间复杂度主要衡量的是一个算法的运行速度&#xff0c;而空间复杂度主要衡量一个算法所需要的额外空间&#xff0c;在计算…

麦肯锡:量子传感究竟在何处可以发光发热

量子传感技术已经提供价值&#xff0c;潜在的应用案例可以塑造多个行业。有四种核心技术具有应用前景&#xff1a;固态自旋、中性原子、超导电路和离子阱&#xff0c;它们具有在广泛的物理属性上的传感能力&#xff0c;包括磁场、电场、旋转、温度、重力、时间和压力。选择哪种…

spring cloud Alibaba 整合 seata AT模式

准备工作&#xff1a; 1、MySQL正常安装并启动 2、nacos正常部署并启动 3、下载 Seata-1.4.2 源码包和 seata-server-1.4.2 服务端源码包&#xff08;版本根据自己的需要选择&#xff0c;我这里选择1.4.2&#xff09; 下载地址&#xff1a; Seata&#xff1a;https://gite…

区间问题——905. 区间选点

目录 区间问题 定义 运用情况 注意事项 解题思路 AcWing 905. 区间选点 题目描述 运行代码 代码思路 改进思路 其它代码 代码思路 区间问题 定义 区间通常是指一个连续的范围&#xff0c;可以用数轴上的一段来表示。 运用情况 在数学中广泛用于表示变量的取值范…

【初阶数据结构】深入解析栈:探索底层逻辑

&#x1f525;引言 本篇将深入解析栈:探索底层逻辑&#xff0c;理解底层是如何实现并了解该接口实现的优缺点&#xff0c;以便于我们在编写程序灵活地使用该数据结构。 &#x1f308;个人主页&#xff1a;是店小二呀 &#x1f308;C语言笔记专栏&#xff1a;C语言笔记 &#x1…

Paimon Trino Presto的关系 分布式查询引擎

Paimon支持的引擎兼容性矩阵&#xff1a; Trino 是 Presto 同项目的不同版本&#xff0c;是原Faceboo Presto创始人团队核心开发和维护人员分离出来后开发和维护的分支&#xff0c;Trino基于Presto&#xff0c;目前 Trino 和 Presto 都仍在继续开发和维护。 Trino 生态系统-客…

YOLOv8改进 | SPPF | 具有多尺度带孔卷积层的ASPP【CVPR2018】

&#x1f4a1;&#x1f4a1;&#x1f4a1;本专栏所有程序均经过测试&#xff0c;可成功执行&#x1f4a1;&#x1f4a1;&#x1f4a1; 专栏目录 &#xff1a;《YOLOv8改进有效涨点》专栏介绍 & 专栏目录 | 目前已有40篇内容&#xff0c;内含各种Head检测头、损失函数Loss、…

基于SSM+Jsp的校园餐厅管理

开发语言&#xff1a;Java框架&#xff1a;ssm技术&#xff1a;JSPJDK版本&#xff1a;JDK1.8服务器&#xff1a;tomcat7数据库&#xff1a;mysql 5.7&#xff08;一定要5.7版本&#xff09;数据库工具&#xff1a;Navicat11开发软件&#xff1a;eclipse/myeclipse/ideaMaven包…

【C语言】函数执行背后的秘密:函数栈帧的创建和销毁超详解

✨个人主页&#xff1a; 熬夜学编程的小林 &#x1f497;系列专栏&#xff1a; 【C语言详解】 【数据结构详解】 目录 1. 什么是函数栈帧 2. 理解函数栈帧能解决什么问题呢&#xff1f; 3. 函数栈帧的创建和销毁解析 3.1 什么是栈&#xff1f; 3.2 认识相关寄存器和汇编指…