经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » 游戏设计 » 查看文章
Unity进阶之:Shader渲染
来源:cnblogs  作者:优梦创客  时间:2019/8/15 11:42:34  对本文有异议

版权声明:

  • 本文原创发布于博客园"优梦创客"的博客空间(网址:http://www.cnblogs.com/raymondking123/)以及微信公众号"优梦创客"
  • 您可以自由转载,但必须加入完整的版权声明!

shader final

PBR渲染

Vertex and Fragment Shader Instance

glass shader

1.屏幕透明

  1. // 计算每个顶点相关的属性(位置,纹理坐标)
  2. VertOutput vert(VertInput v)
  3. {
  4. VertOutput o;
  5. o.vertex = UnityObjectToClipPos(v.vertex);
  6. o.uvgrab = ComputeGrabScreenPos(o.vertex);
  7. // 传入一个投影空间中的顶点坐标,此方法会计算出该顶点坐标在整个摄像机
  8. // 此方法会以摄像机可视范围的左下角为纹理坐标0,0点,右上角为1,1点,计算出
  9. // 当前顶点位置对应的纹理坐标
  10. // 有了贴图的uv坐标之后就把贴图贴上去
  11. return o;
  12. }
  13. half4 frag(VertOutput i) : COLOR
  14. {
  15. // 将Unity光栅化阶段经过顶点插值得到的片元(像素)的属性进行计算,得到每个片元的颜色值
  16. return tex2Dproj(_GrabTexture,i.uvgrab) * 0.5;
  17. // tex2Dproj
  18. }

2.屏幕扭曲
bump map rgb当做xyz坐标来用的

computeGrabScreenPos
tex2dproj

struct VertInput
{
float4 vertex:POSITION;
float4 color:COLOR;
float2 texcoord:TEXCOORD;
// 一个是自身的纹理坐标,还有一个是grab贴图的坐标
};
struct VertOutput
{
float4 vertex:POSITION;
fixed4 color : COLOR;
float2 texcoord : TEXCOORD0; // _Maintex的纹理坐标
float4 uvgrab:TEXCOORD1; // BumpMap的纹理坐标
};

  1. // 计算每个顶点相关的属性(位置,纹理坐标)
  2. VertOutput vert(VertInput v)
  3. {
  4. VertOutput o;
  5. o.vertex = UnityObjectToClipPos(v.vertex);
  6. o.uvgrab = ComputeGrabScreenPos(o.vertex);
  7. // 传入一个投影空间中的顶点坐标,此方法会计算出该顶点坐标在整个摄像机
  8. // 此方法会以摄像机可视范围的左下角为纹理坐标0,0点,右上角为1,1点,计算出
  9. // 当前顶点位置对应的纹理坐标
  10. // 有了贴图的uv坐标之后就把贴图贴上去
  11. o.color = v.color;
  12. o.texcoord = v.texcoord;
  13. return o;
  14. }
  15. half4 frag(VertOutput i) : COLOR
  16. {
  17. // 将Unity光栅化阶段经过顶点插值得到的片元(像素)的属性进行计算,得到每个片元的颜色值
  18. half4 mainColor = tex2D(_MainTex, i.texcoord); //本身纹理采样
  19. half4 bump = tex2D(_BumpMap, i.texcoord); // 法线贴图采样扰动值
  20. half2 distortion = UnpackNormal(bump).rg;// 纹理值转换为法线值
  21. i.uvgrab.xy += distortion * distortion * _Magnitude; // 对uvgrab进行扰动
  22. fixed4 grabColor = tex2Dproj(_GrabTexture, i.uvgrab);// 玻璃背景后面的颜色
  23. return mainColor * grabColor;
  24. }

water shader

利用这种效果,可以对流动的水使用,也可以对火焰产生的空气流的冲击波进行模拟

Animated materials动画材质

_grabTexture // 抓取背景
_NoiseTex // 随机波纹
_CausticTex // 刻蚀纹理

float2 sinusoid(float2 x, float2 m, float2 M, float2 periodo)
{
float2 excursions = M - m;
float2 coefficiente = 3.1415 * 2.0 / periodo;
return excursions / 2.0 * (1.0 + sin(x * coefficiente)) + m;
}

half4 frag(VertOutput i) : COLOR
{
fixed4 noise = tex2D(_BumpMap,i.texcoord);
fixed4 mainColor = tex2D(_MainTex, i.texcoord);

  1. float time = _Time[1] * 0.5; // Time.time
  2. float2 waterDisplacement = sinusoid
  3. (
  4. float2(time , time) + noise.xy,
  5. float2(-_WaterMagnitude, -_WaterMagnitude),
  6. float2(_WaterMagnitude,_WaterMagnitude),
  7. float2(_WaterPeriod,_WaterPeriod)
  8. );//当前值,最小值,最大值,周期
  9. i.uvgrab.xy += waterDisplacement;
  10. fixed4 grabColor = tex2Dproj(_GrabTexture,i.uvgrab);
  11. fixed4 causticColor = tex2D(_CausticTex, i.texcoord.xy*0.25 + waterDisplacement * 5);
  12. return grabColor * mainColor * causticColor * _WaterColor;
  13. }

screen shader and image effects

image effect

原文链接:http://www.cnblogs.com/raymondking123/p/11349886.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号