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
        }
    }
}
相关推荐
charon877810 小时前
UE ARPG | 虚幻引擎战斗系统
游戏引擎
小春熙子11 小时前
Unity图形学之Shader结构
unity·游戏引擎·技术美术
Sitarrrr13 小时前
【Unity】ScriptableObject的应用和3D物体跟随鼠标移动:鼠标放置物体在场景中
3d·unity
极梦网络无忧13 小时前
Unity中IK动画与布偶死亡动画切换的实现
unity·游戏引擎·lucene
逐·風21 小时前
unity关于自定义渲染、内存管理、性能调优、复杂物理模拟、并行计算以及插件开发
前端·unity·c#
_oP_i1 天前
Unity Addressables 系统处理 WebGL 打包本地资源的一种高效方式
unity·游戏引擎·webgl
代码盗圣1 天前
GODOT 4 不用scons编译cpp扩展的方法
游戏引擎·godot
Leoysq1 天前
【UGUI】实现点击注册按钮跳转游戏场景
游戏·unity·游戏引擎·ugui
PandaQue1 天前
《潜行者2切尔诺贝利之心》游戏引擎介绍
游戏引擎
_oP_i1 天前
unity中 骨骼、纹理和材质关系
unity·游戏引擎·材质