Unity3d 角色拖影

模型逐渐透明shader

Shader "Unlit/GrabGlass" {
		Properties{
			_Color("Main Tint", Color) = (1,1,1,1)
			_MainTex("Main Tex", 2D) = "white" {}
		// 用于在透明纹理的基础上控制整体的透明度
		    _Alpha("_Alpha", Range(0,1)) = 1
		}
			SubShader{

			// RenderType标签可以让Unity
			// 把这个Shader归入到提前定义的组(Transparent)
			// 用于指明该Shader是一个使用了透明度混合的Shader

			// IgnoreProjector=True这意味着该Shader
			// 不会受到投影器(Projectors)的影响

			// 为了使用透明度混合的Shader一般都应该在SubShader
			// 中设置这三个标签

			Tags{ "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" }

			Pass{

			// 向前渲染路径的方式
			Tags{ "LightMode" = "ForwardBase" }

			// 深度写入设置为关闭状态
			ZWrite Off

			// 这是混合模式
			Blend SrcAlpha OneMinusSrcAlpha

			CGPROGRAM

#pragma vertex vert
#pragma fragment frag

#include "Lighting.cginc"

			fixed4  _Color;
		sampler2D _MainTex;
		float4 _MainTex_ST;
		fixed _Alpha;

		struct a2v {
			float4 vertex : POSITION;
			float3 normal : NORMAL;
			float4 texcoord : TEXCOORD0;
		};

		struct v2f {
			float4 pos : SV_POSITION;
			float3 worldNormal : TEXCOORD0;
			float3 worldPos : TEXCOORD1;
			float2 uv : TEXCOORD2;
		};

		v2f vert(a2v v) {
			v2f o;

			o.pos = UnityObjectToClipPos(v.vertex);

			o.worldNormal = UnityObjectToWorldNormal(v.normal);

			o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;

			o.uv = TRANSFORM_TEX(v.texcoord, _MainTex);

			return o;
		}

		fixed4 frag(v2f i) : SV_Target{

			fixed3 worldNormal = normalize(i.worldNormal);

		fixed3 worldLightDir = normalize(UnityWorldSpaceLightDir(i.worldPos));

		fixed4 texColor = tex2D(_MainTex, i.uv);

		fixed3 albedo = texColor.rgb * _Color.rgb;

		fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.xyz * albedo;

		fixed3 diffuse = _LightColor0.rgb * albedo * max(0, dot(worldNormal, worldLightDir));

		// 设置透明度通道的值
		return fixed4(ambient + diffuse ,texColor.a * _Alpha);
		}

			ENDCG
		}

		}
			FallBack "Transparent/VertexLit"
	}

ShowShadowTail.cs   

from:https://blog.csdn.net/cjb_king/article/details/52268432

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

public class ShowShadowTail : MonoBehaviour
{ 
    public float fadeSpeed = 0.6f;
    public float createSpeed =3.2f;
    public bool isOpen = false;
    private float previousTime = 0;
    private SkinnedMeshRenderer[] meshRender;
    private MeshFilter[] meshFilter;


    bool isInit = false;
    public ShowShadowTail Init()
    {
        if(isInit == false)
        {
            meshRender = this.GetComponentsInChildren<SkinnedMeshRenderer>();
            meshFilter = this.GetComponentsInChildren<MeshFilter>();
            isInit = true;
        }
        return this;
    }

    void Awake()
    {
        Init();
    }
    struct MeshInfo
    {
        public Mesh mesh;
        public Matrix4x4 _matrix_PR; //存儲位置和旋转角度;
        public Material material;
    };
    List<MeshInfo> meshInfoList = new List<MeshInfo>();
    void Update()
    {
        if(isOpen)
        {   //创建模型
            if (Time.time - previousTime > createSpeed * Time.deltaTime)
            {
                previousTime = Time.time;
                foreach (SkinnedMeshRenderer meshRender in meshRender)
                {
                    Mesh mesh = new Mesh();
                    meshRender.BakeMesh(mesh);
                    MeshInfo meshInfo = new MeshInfo();
                    meshInfo.mesh = mesh;
                    meshInfo._matrix_PR = meshRender.localToWorldMatrix;
                    meshInfo.material = new Material(meshRender.material);
                    meshInfo.material.shader = Shader.Find("Unlit/GrabGlass");
                    meshInfoList.Add(meshInfo);
                }
                foreach (MeshFilter filter in meshFilter)
                {
                    MeshInfo meshInfo = new MeshInfo();
                    meshInfo.mesh = filter.mesh;
                    meshInfo._matrix_PR = filter.transform.localToWorldMatrix;
                    meshInfo.material = new Material(Shader.Find("Unlit/GrabGlass"));
                    meshInfoList.Add(meshInfo);
                }
            }
        }

        //显示模型
        if (meshInfoList.Count > 1)
        {
            foreach (MeshInfo meshInfo in meshInfoList)
            {
                Graphics.DrawMesh(meshInfo.mesh, meshInfo._matrix_PR, meshInfo.material, gameObject.layer);
                float timer = meshInfo.material.GetFloat("_Alpha") - Time.deltaTime * fadeSpeed;
                meshInfo.material.SetFloat("_Alpha", timer > 0 ? timer : 0);
                if (meshInfo.material.GetFloat("_Alpha") < 0)
                {
                    meshInfoList.Remove(meshInfo);
                }
            }
        }
    }
}

调用方法

gameObject.AddComponent<ShowShadowTail>().Init().isOpen = true;