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

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 <= )
throw new ArgumentException("Duration");
if (LightULen <= || LightULen >= )
throw new ArgumentException("LightULen <= 0 || LightULen >= 1");
if (Size.x <= || Size.y <= )
throw new ArgumentException("Size.x <= 0 || Size.y <= 0"); GenerateRenderer(); m_t1 = ( - LightULen) * Duration;
} void GenerateRenderer()
{
// Mesh
Mesh mesh = new Mesh();
mesh.vertices = new Vector3[]
{
new Vector2(-Size.x/,Size.y/),
new Vector2(Size.x/,Size.y/),
new Vector2(-Size.x/,-Size.y/),
new Vector2(Size.x/,-Size.y/),
};
mesh.triangles = new int[] { , , , , , };
mesh.uv = new Vector2[]
{
new Vector2(,),
new Vector2(,),
new Vector2(,),
new Vector2(,),
}; 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 = ;
else if (m_timer > m_t1)
renderer.material.SetFloat("_ReachBoundary", );
else
renderer.material.SetFloat("_ReachBoundary", -);
}
} public bool Play
{
set
{
renderer.material.SetTexture("_LightTex", value ? LightTex : null);
m_timer = ;
m_play = value;
}
}
}

shader代码:

Shader "Bleach/WalkLight"
{
Properties
{
_MainTex ("Main", 2D) = "white" {}
_LightTex("Light", 2D) = "black" {}
_LightLen("Light Length", float) =
} SubShader
{
Pass
{
Tags { "RenderType"="Opaque" }
LOD 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> && i.uv.x<_LightLen? i.uv.x+ : 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

Unity3d之流光效果的更多相关文章

  1. Unity3d流光效果

    Material中纹理的属性都有Tiling和Offset,可以利用Offset做uv动画,从而完成各种有趣的动画,比如流光效果! 流过效果即通常一条高光光在物体上划过,模拟高光移动照射物体的效果,之 ...

  2. NGUI具有流光效果的UISprite

    之前做过一个流光效果(http://www.cnblogs.com/jietian331/p/4748644.html). 现将其改进一下,与NGUI结合起来,提供一个具有流光效果的组件:UIWalk ...

  3. Unity Shader 效果(1) :图片流光效果

    很多游戏Logo中可以看到这种流光效果,一般的实现方案就是对带有光条的图片uv根据时间进行移动,然后和原图就行叠加实现,不过实现过程中稍稍有点需要注意的地方.之前考虑过风宇冲的实现方式,但是考虑到sh ...

  4. unity3d 摄像机抖动效果 CameraShake

    unity3d 摄像机抖动效果 ,利用脚本直接控制:当然也可以选择使用dotween插件,但到不至于为了使用仅一个功能,就导入了一个插件: 脚本示例: using UnityEngine; using ...

  5. 今天写shader流光效果,shader代码少了个括号,unity shader compiler卡死且不提示原因

    今天写shader流光效果,shader代码少了个括号,unity shader compiler卡死且不提示原因 好在找到了原因,shader 代码如下,原理是提高经过的颜色亮度 void surf ...

  6. css流光效果

    css流光效果1: <!DOCTYPE html> <html> <head> <title>ww</title> </head> ...

  7. Unity3D Shader 模型流光效果

    Shader "Custom/FlowColor" { Properties { _MainTex ("Base (RGB)", 2D) = "whi ...

  8. Unity3D战争迷雾效果

    原地址:http://liweizhaolili.blog.163.com/blog/static/16230744201431835652233/ 最近一直都在做Flash相关的东西,很久没有空搞U ...

  9. Unity3D相机震动效果

    在一些格斗.射击以及动作类游戏中 相机震动效果是十分重要的 一个平凡的镜头 在关键时刻加入相机震动后 便可以展现出碰撞.危险.打斗以及激动人心的效果 相机震动的实现方式有很多 但都会涉及摄像机位置的变 ...

随机推荐

  1. 登录数据库后,use db很慢的问题

    mysql> use dbl Reading table information for completion of table and column names You can turn of ...

  2. checking for known struct flock definition... configure: error: Don't know how to define struct flock on this system, set --enable-opcache=

    在对php进行安装的过程中出现如下错误: 1.报错信息: 1 checking for known struct flock definition... configure: error: Don't ...

  3. 【转】ethtool 命令详解

    命令描述: ethtool 是用于查询及设置网卡参数的命令. 使用概要:ethtool ethx       //查询ethx网口基本设置,其中 x 是对应网卡的编号,如eth0.eth1等等etht ...

  4. 在DLL中导出另一静态库中的函数

    开发环境: win7_x64.VS2013 应用场景: 动态库A依赖动态库B,而动态库B又使用了静态库C:有些情况下,我们需要将C从B里面导出,然后提供给A使用. 正文: Step1: 1.新建测试静 ...

  5. 华哥倒酒<区间标记,二分>

    题目链接 #include<cstdio> #include<cstring> #include<algorithm> using namespace std; t ...

  6. 小蚂蚁搬家<贪心>

    题意: 由于预知未来可能会下雨,所以小蚂蚁决定搬家.它需要将它的所有物品都搬到新家,新家的体积为V,小蚂蚁有N件物品需要搬,每件物品的体积为Ai,但他发现:每件物品需要新家剩余体积大于等于Bi才能使它 ...

  7. 在iOS中获取UIView的所有层级结构 相关

    在iOS中获取UIView的所有层级结构 应用场景 在实际 iOS 开发中,很多时候都需要知道某个 UI 控件中包含哪些子控件,并且分清楚它们的层级结构和自个的 frame 以及 bounds ,以便 ...

  8. UIWebView 使用要注意的几点

    UIWebView 使用要注意的几点 最近有客户希望将移动端统一使用HTML5来完成,在iOS端就要用到UIWebView.遇到了以下三个主要问题: 加载HTTPS页面 不像Safari可以弹出弹框问 ...

  9. 学习笔记——建造者模式Builder

    构造者模式.外部场景如果需要一个汽车类,它不需要关心如何构造,它只需要告诉Director需要什么,就可以从Director获得. 如:CDirector(IBuilder* aBuilder); 场 ...

  10. android TextView 之探究

    1:插入图片替换 //代码 mSubjectDetailView = (TextView) findViewById(R.id.subject_detail); CharSequence text = ...