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
        }
    }
}
相关推荐
蔗理苦1 小时前
2024-12-24 NO1. XR Interaction ToolKit 环境配置
unity·quest3·xr toolkit
花生糖@1 小时前
Android XR 应用程序开发 | 从 Unity 6 开发准备到应用程序构建的步骤
android·unity·xr·android xr
向宇it2 小时前
【从零开始入门unity游戏开发之——unity篇02】unity6基础入门——软件下载安装、Unity Hub配置、安装unity编辑器、许可证管理
开发语言·unity·c#·编辑器·游戏引擎
虾球xz2 小时前
游戏引擎学习第55天
学习·游戏引擎
虾球xz4 小时前
游戏引擎学习第58天
学习·游戏引擎
ue星空6 小时前
虚幻引擎结构之UWorld
游戏引擎·虚幻
ue星空6 小时前
虚幻引擎结构之ULevel
游戏引擎·虚幻
向宇it6 小时前
【从零开始入门unity游戏开发之——unity篇01】unity6基础入门开篇——游戏引擎是什么、主流的游戏引擎、为什么选择Unity
开发语言·unity·c#·游戏引擎
神洛华8 小时前
Y3地图制作1:水果缤纷乐、密室逃脱
编辑器·游戏引擎·游戏程序
向宇it11 小时前
【从零开始入门unity游戏开发之——C#篇26】C#面向对象动态多态——接口(Interface)、接口里氏替换原则、密封方法(`sealed` )
java·开发语言·unity·c#·游戏引擎·里氏替换原则