BoYing Unity 之 Shader 笔记(一)帧动画

来源:互联网 发布:淘宝新手发布宝贝 编辑:程序博客网 时间:2024/06/10 08:42

使用unity推崇的方式编写的一个播放序列图(帧动画)的shader

Shader "Custom/M1_UV"{Properties {_Color ("Main Color", Color) = (1,1,1,1)_MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}_SizeX ("列", Float) = 4_SizeY ("行", Float) = 2_Speed ("播放速度", Float) = 150}SubShader {// 这里使用Unity3d自带光照模型Lambert// 不用做顶点处理,只需要一个表面处理函数surfCGPROGRAM#pragma surface surf Lambert alpha// 声明参数fixed4 _Color;sampler2D _MainTex;uniform fixed _SizeX;uniform fixed _SizeY;uniform fixed _Speed;// 获取_MainTex的UV信息定义输入结构体struct Input {// 在贴图变量前加上uv表示提取uv值(二维坐标)float2 uv_MainTex;};void surf (Input IN, inout SurfaceOutput o) {// 获取单元格UVfloat2 cellUV = float2(IN.uv_MainTex.x /_SizeX, IN.uv_MainTex.y /_SizeY);// UV坐标值范围为0-1,获取单元格宽度float deltaX = 1 / _SizeX; // 单元格增量宽度float deltaY = 1 / _SizeY; // 单元格增量高度// 当前播放总索引int index = _Time * _Speed;// 求列索引int col = fmod(index, _SizeX);// 求行索引int row = index / _SizeX;// 原始UV + 当前格增量cellUV.x += col * deltaX;cellUV.y += row * deltaY;// 创建tex2d(材质,uv)*主色fixed4 c = tex2D(_MainTex, cellUV) * _Color;// RGBo.Albedo = c.rgb;// 透明度o.Alpha = c.a;}ENDCG}}


0 0
原创粉丝点击