【Unity Shaders and Effects Cookbook】Using Textures for Effects

使用纹理可以使得Shaders更快速,也更逼真;使用的同时,需要控制纹理数目,避免过多加载纹理的运行消耗

下面逐步说明使用纹理制作流行水面的效果:

1.定义 X方向以及Y方向的速度变量(因为UV 是二维的),控制纹理的流动速度

Properties
{
   _MainTint("Diffuse Tint",Color) = (1,1,1,1)
   _MainTex("Base (RGB)",2D) = "White"{}
   _ScrollXSpeed ("X Scroll Speed", Range(0,10)) = 2
   _ScrollYSpeed( "Y Scroll Speed", Range(0,10)) = 2
}

2. 在CGPROGRAM 段 中添加 变量,这样可以访问到1中定义的 properties。

 fixed4 _MainTint;
 fixed  _ScrollXSpeed;
 fixed  _ScrollYSpeed;
 sampler2D  _MainTex;

3. 修改 surface function,使用内置_Time 时间变量,控制Texture 的流动

void surf (Input IN, inout SurfaceOutput o)
{
   //Create a separate variable to store out uvs
   //before ve pass them to the tex2D() function
   fixed2 scrolledUV = IN.uv_MainTex;
 // Create variables that store the individual x and y
 // components for the uvs scaled by time
  fixed xScrollValue = _ScrollXSpeed * _Time;
  fixed yScrollValue = _ScrollYSpeed * _Time;

 //Apply the final uv offset
  scrolledUV += fixed2(xScrollValue, yScrollValue);
 // Apply textures and tint
  half4 c = tex2D(_MainTex, scrolledUV);
  o.Albedo = c.rgb * _MainTint;
  o.Alpha = C.a;

}

  

解释:

Input 结构体中,定义了 float2 uv_MainTex, 所以在surf 函数中,首先把存储到scrolledUV 中。 这样,可以

通过控制speed,在后面给 ScrolledUV offset, 关于 _Time ,float4 变量,常使用来控制动画。


Whole Project Download here


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Book Description: Since their introduction to Unity, Shaders have been notoriously difficult to understand and implement in games: complex mathematics have always stood in the way of creating your own Shaders and attaining that level of realism you crave. With Shaders, you can transform your game into a highly polished, refined product with Unity’s post-processing effects. Unity Shaders and Effects Cookbook is the first of its kind to bring you the secrets of creating Shaders for Unity3D―guiding you through the process of understanding vectors, how lighting is constructed with them, and also how textures are used to create complex effects without the heavy math. We’ll start with essential lighting and finishing up by creating stunning screen Effects just like those in high quality 3D and mobile games. You’ll discover techniques including normal mapping, image-based lighting, and how to animate your models inside a Shader. We’ll explore the secrets behind some of the most powerful techniques, such as physically based rendering! With Unity Shaders and Effects Cookbook, what seems like a dark art today will be second nature by tomorrow. What You Will Learn Understand physically based rendering to fit the aesthetic of your game Enter the world of post-processing effects to make your game look visually stunning Add life to your materials, complementing Shader programming with interactive scripts Design efficient Shaders for mobile platforms without sacrificing their realism Use state-of-the-art techniques such as volumetric explosions and fur shading Build your knowledge by understanding how Shader models have evolved and how you can create your own Discover what goes into the structure of Shaders and why lighting works the way it does Master the math and algorithms behind the most used lighting models

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不负初心

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值