zoukankan      html  css  js  c++  java
  • Unity攻击敌人时产生泛白效果

    Shader的代码如下,主要是将透明度为1的像素点输出为白色,其中_BeAttack表示角色被攻击的泛白状态

    // Upgrade NOTE: replaced 'mul(UNITY_MATRIX_MVP,*)' with 'UnityObjectToClipPos(*)'

    Shader "Custom/BeAttackTest" {
    Properties{
    _MainTex("texture", 2D) = "black"{}
    _Color("add color", Color) = (1,1,1,1)
    //_BeAttack("BeAttack",Int)=0
    }

    SubShader{
    Tags{ "QUEUE" = "Transparent" "IGNOREPROJECTOR" = "true" "RenderType" = "Transparent" }
    ZWrite Off
    Blend SrcAlpha OneMinusSrcAlpha
    LOD 100
    Cull Off//设置双面渲染,避免角色缩放翻转时无渲染情况

    Pass{
    CGPROGRAM
    #pragma vertex vert
    #pragma fragment frag
    #include "UnityCG.cginc"

    sampler2D _MainTex;
    fixed4 _MainTex_ST;
    fixed4 _Color;
    int _BeAttack;//对外参数表示是否被攻击了

    struct vIn {
    half4 vertex:POSITION;
    float2 texcoord:TEXCOORD0;
    fixed4 color : COLOR;
    };

    struct vOut {
    half4 pos:SV_POSITION;
    float2 uv:TEXCOORD0;
    fixed4 color : COLOR;
    };

    vOut vert(vIn v) {
    vOut o;
    o.pos = UnityObjectToClipPos(v.vertex);
    o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);
    o.color = v.color;
    return o;
    }

    fixed4 frag(vOut i) :COLOR{
    fixed4 tex = tex2D(_MainTex, i.uv).rgba;
    /*
    if (tex.a == 1)return fixed4(1, 1, 1, 1);
    else return fixed4(0, 0, 0, 0);
    */
    if (_BeAttack==1) {//是否被攻击
    if (tex.a == 1)return fixed4(1, 1, 1, 1);//对透明度为1的像素输出为白色
    else return fixed4(0, 0, 0, 0);
    }
    else {
    return tex;
    }

    }
    ENDCG
    }
    }
    }

    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

    角色被攻击的代码如下,通过设置时间参数控制泛白的持续时间:

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

    public class Enemy01Die : MonoBehaviour,IDie {

    SpriteRenderer spriteRenderer;

    public float whiteCoolTime;//泛白效果的持续时间长度
    float whiteCoolTimer;

    private void Awake()
    {
    spriteRenderer = GetComponent<SpriteRenderer>();
    }

    void Update()
    {
    //被攻击状态冷却,设置被攻击参数为0
    if (whiteCoolTimer <= 0)
    spriteRenderer.material.SetInt("_BeAttack", 0);
    else
    whiteCoolTimer -= Time.deltaTime;
    }

    public void Die()
    {
    Destroy(gameObject);
    }

    public void BeAttack()
    {

    //被攻击的时候设置shader的被攻击参数为1
    spriteRenderer.material.SetInt("_BeAttack", 1);
    whiteCoolTimer = whiteCoolTime;
    }
    }

    public interface IDie
    {
    void Die();
    void BeAttack();
    }

  • 相关阅读:
    python实现矩阵的点乘,转置与求逆
    C#用嵌套的 for 循环实现打印图形
    visual studio 2019的安装方法
    人工智能之python实现函数微积分
    人工智能之python实现矩阵运算
    人工智能之实现简单神经网络之数据解析和可视化
    人工智能之实现简单神经网络之权重更新算法
    ubuntu服务器版相关命令
    php的windows各版本客户端下载
    谷歌快捷工具地址
  • 原文地址:https://www.cnblogs.com/xiaoahui/p/9973727.html
Copyright © 2011-2022 走看看