当前位置: 首页 > news >正文

Unity3d之流光效果

所谓流光效果,如一个图片上一条刀光从左闪到右边,以下为实现代码:

c#代码:

using System;
using UnityEngine;

public class WalkLightEffect : MonoBehaviour
{
    public Texture MainTex;
    public Texture LightTex;
    public float Duration;
    public float LightULen;
    public Vector2 Size;

    bool m_play;
    float m_timer;
    float m_t1;

    void Awake()
    {
        if (MainTex == null)
            throw new ArgumentNullException("MainTex");
        if (LightTex == null)
            throw new ArgumentNullException("LightTex");
        if (Duration <= 0)
            throw new ArgumentException("Duration");
        if (LightULen <= 0 || LightULen >= 1)
            throw new ArgumentException("LightULen <= 0 || LightULen >= 1");
        if (Size.x <= 0 || Size.y <= 0)
            throw new ArgumentException("Size.x <= 0 || Size.y <= 0");

        GenerateRenderer();

        m_t1 = (1 - LightULen) * Duration;
    }

    void GenerateRenderer()
    {
        // Mesh
        Mesh mesh = new Mesh();
        mesh.vertices = new Vector3[4] 
            { 
                new Vector2(-Size.x/2,Size.y/2),
                new Vector2(Size.x/2,Size.y/2),
                new Vector2(-Size.x/2,-Size.y/2),
                new Vector2(Size.x/2,-Size.y/2),
            };
        mesh.triangles = new int[6] { 0, 1, 2, 2, 1, 3 };
        mesh.uv = new Vector2[4] 
            { 
                new Vector2(0,1),
                new Vector2(1,1),
                new Vector2(0,0),
                new Vector2(1,0),
            };

        mesh.Optimize();

        var mf = gameObject.AddComponent<MeshFilter>();
        mf.mesh = mesh;

        // Material
        var mat = new Material(Shader.Find("Bleach/WalkLight"));
        mat.SetTexture("_MainTex", MainTex);
        mat.SetFloat("_LightLen", LightULen);

        var mr = gameObject.AddComponent<MeshRenderer>();
        mr.sharedMaterial = mat;
    }

    void Update()
    {
        if (m_play)
        {
            renderer.material.SetFloat("_TimeRate", m_timer / Duration);

            m_timer += Time.deltaTime;

            if (m_timer > Duration)
                m_timer = 0;
            else if (m_timer > m_t1)
                renderer.material.SetFloat("_ReachBoundary", 1);
            else
                renderer.material.SetFloat("_ReachBoundary", -1);
        }
    }

    public bool Play
    {
        set
        {
            renderer.material.SetTexture("_LightTex", value ? LightTex : null);
            m_timer = 0;
            m_play = value;
        }
    }
}

shader代码:

Shader "Bleach/WalkLight"
{
    Properties
    {
        _MainTex ("Main", 2D) = "white" {}
        _LightTex("Light", 2D) = "black" {}
        _LightLen("Light Length", float) = 0
    }
    
    SubShader
    {
        Pass
        {
            Tags { "RenderType"="Opaque" }
            LOD 200
            
            Cull Off
                                                
            CGPROGRAM
            #pragma vertex vert
            #pragma fragment frag

            uniform sampler2D _MainTex;
            uniform sampler2D _LightTex;
            uniform    fixed _LightLen;
            
            uniform fixed _ReachBoundary;     // 小于0表示未到边界,大于0表示到达边界
            uniform fixed _TimeRate;        // 时间/周期
            
            struct Data
            {
                fixed4 vertex:POSITION;
                fixed2 texcoord:TEXCOORD0;
            };
            
            struct V2F
            {
                fixed4 pos:SV_POSITION;
                fixed2 uv:TEXCOORD0;
            };
            
            V2F vert(Data v)
            {
                V2F o;
                o.pos=mul(UNITY_MATRIX_MVP,v.vertex);
                o.uv=v.texcoord;
                return o;
            }    
            
            fixed4 frag(V2F i):COLOR 
            {
                fixed4 main = tex2D(_MainTex,i.uv);
                fixed x = _ReachBoundary>0 && i.uv.x<_LightLen? i.uv.x+1 : i.uv.x;
                fixed lightU = (x - _TimeRate)/_LightLen;            // u=(x-timer/Duration)/_LightLen;
                fixed4 light = tex2D(_LightTex,float2(lightU,i.uv.y));
                
                return lerp(main,light,light.a);
            }
                            
            ENDCG
        }
    }
    
    FallBack "Diffuse"
}

 

转载请注明出处:http://www.cnblogs.com/jietian331/p/4748644.html

转载于:https://www.cnblogs.com/jietian331/p/4748644.html

相关文章:

  • mysql 的 存储结构(储存引擎)
  • DP_ural_Metro
  • 手把手教你整合 SpringMvc+Spring+MyBatis+Maven
  • oracle根据pid查询出正在执行的执行语句
  • 国内最简单的短视频SDK
  • 【转】vxworks的default boot line说明
  • vector的reserve和resize(转)
  • 心跳多少寿命长
  • UI中的界面之间的值传递 一
  • [POJ3067]Japan
  • 将数据集导出到Excel
  • 标准输出重定向覆盖与追加
  • [中国寒龙反网络病毒联盟001]谷歌应用引擎视频(Google.Datastore.And.RSS)
  • Arduino中hex文件的保存及应用(转)
  • java.io.IOException: Malformed \uxxxx encoding.
  • 10个确保微服务与容器安全的最佳实践
  • 230. Kth Smallest Element in a BST
  • Angularjs之国际化
  • iOS帅气加载动画、通知视图、红包助手、引导页、导航栏、朋友圈、小游戏等效果源码...
  • PHP 程序员也能做的 Java 开发 30分钟使用 netty 轻松打造一个高性能 websocket 服务...
  • Redis字符串类型内部编码剖析
  • SegmentFault 社区上线小程序开发频道,助力小程序开发者生态
  • 服务器之间,相同帐号,实现免密钥登录
  • 高度不固定时垂直居中
  • 开发了一款写作软件(OSX,Windows),附带Electron开发指南
  • 开年巨制!千人千面回放技术让你“看到”Flutter用户侧问题
  • 浅谈web中前端模板引擎的使用
  • 微信开源mars源码分析1—上层samples分析
  • 为什么要用IPython/Jupyter?
  • 异步
  • 用mpvue开发微信小程序
  • 自定义函数
  • 我们雇佣了一只大猴子...
  • ​ssh-keyscan命令--Linux命令应用大词典729个命令解读
  • ###C语言程序设计-----C语言学习(3)#
  • #我与Java虚拟机的故事#连载01:人在JVM,身不由己
  • $Django python中使用redis, django中使用(封装了),redis开启事务(管道)
  • (13)[Xamarin.Android] 不同分辨率下的图片使用概论
  • (145)光线追踪距离场柔和阴影
  • (附源码)springboot教学评价 毕业设计 641310
  • (附源码)基于SpringBoot和Vue的厨到家服务平台的设计与实现 毕业设计 063133
  • (算法)Travel Information Center
  • (原创) cocos2dx使用Curl连接网络(客户端)
  • (转)shell中括号的特殊用法 linux if多条件判断
  • (转)关于多人操作数据的处理策略
  • **CI中自动类加载的用法总结
  • .NET C# 使用 SetWindowsHookEx 监听鼠标或键盘消息以及此方法的坑
  • .NET Core WebAPI中使用Log4net 日志级别分类并记录到数据库
  • .NET单元测试
  • .NET分布式缓存Memcached从入门到实战
  • .Net接口调试与案例
  • .Net转Java自学之路—基础巩固篇十三(集合)
  • @transaction 提交事务_【读源码】剖析TCCTransaction事务提交实现细节
  • [ 云计算 | Azure 实践 ] 在 Azure 门户中创建 VM 虚拟机并进行验证
  • [].shift.call( arguments ) 和 [].slice.call( arguments )