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

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. VB 要求对象

    vDoc = WebBrowser1.Document '提示要求对象 Set vDoc = WebBrowser1.Document '正确执行

  2. ExceL转PDF

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using Excel = ...

  3. 转 dos 下的 find 和 重定向

    1.find /i "ora-" *.* > check.log 附录: 我对findstr是如此的依赖,以至于当我向各位讲解find命令的时候,我还得老老实实地在cmd窗口 ...

  4. 转 : net use的使用

    老是忘了 net use 怎么样,今天在网上找一篇,贴在这,感谢原作者分享.     1 查看远程主机的共享资源(但看不到默认共享) net view \\IP 2向远程主机复制文件 copy \路径 ...

  5. ORA-12170: TNS:Connect timeout occurred

    VM 作为ORACLE 服务器,客户端登陆提示超时,本地连接使用网络连接正常. D:>sqlplus system/oracle123@//192.168.63.121:15021/pdb01 ...

  6. 【jsp exception】如何处理jsp页面的错误

    根据jsp对错误的处理方式不同可以将其分为局部异常处理和全局异常处理.局部异常处理适用于个别jsp页面,当这些页面发生错误后,采取特殊的处理方式:全局异常处理适用于所有jsp页面,当所有页面发生某些指 ...

  7. 开放型Modbus/TCP 规范

    修订版 1.0,1999 年3 月29 日Andy SwalesSchneider 电气公司aswales@modicon.com目录目录............................... ...

  8. over-float清除浮动++隐藏溢出

    overflow:hidden这个CSS样式是大家常用到的CSS样式,但是大多数人对这个样式的理解仅仅局限于隐藏溢出,而对于清除浮动这个含义不是很了解.一提到清除浮动,我们就会想到另外一个CSS样式: ...

  9. Lucene add、updateDocument添加、更新与search查询(转)

    package com.lucene;   import java.io.IOException;   import org.apache.lucene.analysis.standard.Stand ...

  10. HDU 4460 Friend Chains(map + spfa)

    Friend Chains Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 32768/32768K (Java/Other) Total ...