unity-特效-雷达扫描效果

使用后处理方式制作

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
        }
    }
}

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
### 回答1: Spine-Unity-3.8-2021是一个Spine动画的Unity插件,可以让开发人员在游戏开发过程中轻松实现Spine动画。这个插件包含了最新的Spine Runtime库,可以让开发人员在使用Spine动画时获得更好的性能和可靠性。此版本还提供了对C# 9的支持,并修复了在以前的Spine Unity版本中存在的一些问题和异常。此外,此版本也提供了一些改进和优化,能够让开发人员更快速、更容易地实现动画效果并提高游戏的运行效率。总的来说,Spine-Unity-3.8-2021是一个值得开发人员关注和使用的插件,它可以帮助游戏开发人员更好地实现动画效果,提升游戏的质量和用户体验。 ### 回答2: spine-unity-3.8-2021是一款在Unity中使用的Spine动画软件包。Spine是一种2D骨骼动画软件,它可以帮助制作2D游戏中的角色动画。与传统的逐帧动画相比,Spine骨骼动画具有更高的效率和更好的表现效果。 spine-unity-3.8-2021是Spine运行在Unity中的版本。它提供了一个简便的方式来将Spine制作的动画集成到Unity游戏中。spine-unity-3.8-2021拥有许多实用的功能,例如动画的播放、循环、暂停和停止等。 此外,spine-unity-3.8-2021还支持动画的混合、遮罩、缩放等高级特性。这些功能可以大大提升2D游戏的动画表现效果。 总之,spine-unity-3.8-2021是一款强大的Spine骨骼动画软件包,它可以帮助Unity开发者更方便、更高效地制作2D游戏中的动画效果。 ### 回答3: Spine-Unity-3.8-2021是Spine动画引擎的一个版本,其主要特点是可以与Unity引擎无缝集成,提供了高效、灵活、可定制的动画解决方案。此版本相比之前版本,主要增加了一些新功能和改进,如支持GPU动画混合、2D环境的自适应、高效的顶点色边框渲染、Spine Atlas纹理集加载、支持Mecanim(动画过渡和状态机和蒙太奇)和 IK姿势、环境光遮蔽、大量优化和 bug 修复等。除此之外,它还易于使用和实现,并具有快速迭代的能力,可以让开发者轻松创建精美的动画效果,提升游戏的用户体验。该版本是Spine引擎的主要升级版本之一,同时也体现出Spine开发团队对于产品需求和用户反馈的重视和努力,不仅提高了动画制作领域的生产力和创造力,也为游戏行业推陈出新提供了有力支持。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值