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 <= )
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之流光效果的更多相关文章
- Unity3d流光效果
Material中纹理的属性都有Tiling和Offset,可以利用Offset做uv动画,从而完成各种有趣的动画,比如流光效果! 流过效果即通常一条高光光在物体上划过,模拟高光移动照射物体的效果,之 ...
- NGUI具有流光效果的UISprite
之前做过一个流光效果(http://www.cnblogs.com/jietian331/p/4748644.html). 现将其改进一下,与NGUI结合起来,提供一个具有流光效果的组件:UIWalk ...
- Unity Shader 效果(1) :图片流光效果
很多游戏Logo中可以看到这种流光效果,一般的实现方案就是对带有光条的图片uv根据时间进行移动,然后和原图就行叠加实现,不过实现过程中稍稍有点需要注意的地方.之前考虑过风宇冲的实现方式,但是考虑到sh ...
- unity3d 摄像机抖动效果 CameraShake
unity3d 摄像机抖动效果 ,利用脚本直接控制:当然也可以选择使用dotween插件,但到不至于为了使用仅一个功能,就导入了一个插件: 脚本示例: using UnityEngine; using ...
- 今天写shader流光效果,shader代码少了个括号,unity shader compiler卡死且不提示原因
今天写shader流光效果,shader代码少了个括号,unity shader compiler卡死且不提示原因 好在找到了原因,shader 代码如下,原理是提高经过的颜色亮度 void surf ...
- css流光效果
css流光效果1: <!DOCTYPE html> <html> <head> <title>ww</title> </head> ...
- Unity3D Shader 模型流光效果
Shader "Custom/FlowColor" { Properties { _MainTex ("Base (RGB)", 2D) = "whi ...
- Unity3D战争迷雾效果
原地址:http://liweizhaolili.blog.163.com/blog/static/16230744201431835652233/ 最近一直都在做Flash相关的东西,很久没有空搞U ...
- Unity3D相机震动效果
在一些格斗.射击以及动作类游戏中 相机震动效果是十分重要的 一个平凡的镜头 在关键时刻加入相机震动后 便可以展现出碰撞.危险.打斗以及激动人心的效果 相机震动的实现方式有很多 但都会涉及摄像机位置的变 ...
随机推荐
- 常见html标签
1.flash嵌入标签 <object> <embed allowscriptaccess="always" allowfullscreen="true ...
- POJ 2975 Nim 尼姆博弈
题目大意:尼姆博弈,求先手必胜的情况数 题目思路:判断 ans=(a[1]^a[2]--^a[n]),求ans^a[i] < a[i]的个数. #include<iostream> ...
- 【转】Java 内部类种类及使用解析
Java 内部类种类及使用解析 内部类Inner Class 将相关的类组织在一起,从而降低了命名空间的混乱. 一个内部类可以定义在另一个类里,可以定义在函数里,甚至可以作为一个表达式的一部分. Ja ...
- java vector 和ArrayList的区别
相同点: 1.都是使用数组存储数据 不同点: 1.Vector是显示安全的,ArrayList是线程不安全的 Vector部分代码: public synchronized boolean add(E ...
- SqlServer 查询死锁,杀死死锁进程
-- 查询死锁 select request_session_id spid, OBJECT_NAME(resource_associated_entity_id) tableName from sy ...
- 使用devcon禁用启用网卡
系统平台:win2003 情况描述: 机器上装有两块网卡,8136和8139,网卡A使用静态IP,连接内部办公网,网卡B使用DHCP,连接互联网.切换两个网络时,需要先禁用一个网卡,启用另一个网卡.来 ...
- JMeter-使用Badboy录制Web测试脚本
JMeter是纯Java编写的软件功能和性.能测试工具,其录制脚本过于笨拙和复杂.而Badboy是用C++开发的动态应用测试工具,其拥有强大的屏幕录制和回放功能,同时提供图形结果分析功能,刚好弥补了J ...
- 对比C#中==与equal方法
C#中equal与==的区别 收藏 对于值类型,如果对象的值相等,则相等运算符 (==) 返回 true,否则返回 false.对于string 以外的引用类型,如果两个对象引用同一个对象,则 == ...
- 关于java.lang.reflect.InvocationTargetException(jar 包缺少或者冲突)的错误
我在合肥那边运行了的是湖北石首市的项目没有错 可是回武汉之后 运行这个项目 点击这里的时候 就报错java.lang.reflect.InvocationTargetException 不是数据库 ...
- android脚步---不同activity之间参数传递
现在有两个activity,一个是mainactivity,一个是detectactivity 后者需要调用前者的一个参数,这里用到了intent getextras(); putextras(); ...