unity-特效-雷达扫描效果

使用后处理方式制作

cs 复制代码
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

cs 复制代码
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
        }
    }
}
相关推荐
Artistation Game3 小时前
九、怪物行为逻辑
游戏·unity·游戏引擎
百里香酚兰3 小时前
【AI学习笔记】基于Unity+DeepSeek开发的一些BUG记录&解决方案
人工智能·学习·unity·大模型·deepseek
妙为3 小时前
unreal engine5制作动作类游戏时,我们使用刀剑等武器攻击怪物或敌方单位时,发现攻击特效、伤害等没有触发
游戏·游戏引擎·虚幻·碰撞预设
dangoxiba6 小时前
[Unity Demo]从零开始制作空洞骑士Hollow Knight第十三集:制作小骑士的接触地刺复活机制以及完善地图的可交互对象
游戏·unity·visualstudio·c#·游戏引擎
先生沉默先1 天前
使用Materialize制作unity的贴图,Materialize的简单教程,Materialize学习日志
学习·unity·贴图
十画_8241 天前
Visual Studio 小技巧记录
unity·visual studio
red_redemption1 天前
cpp,git,unity学习
git·unity·游戏引擎
tealcwu1 天前
【Unity踩坑】Unity更新Google Play结算库
unity·游戏引擎
先生沉默先1 天前
unity 默认渲染管线材质球的材质通道,材质球的材质通道
unity·游戏引擎·材质
白鹭float.1 天前
【Unity AI】基于 WebSocket 和 讯飞星火大模型
人工智能·websocket·unity