博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Cg入门23: Fragment shader – UV动画(序列帧)
阅读量:2201 次
发布时间:2019-05-03

本文共 1518 字,大约阅读时间需要 5 分钟。

让动画从1-9循环播放此纹理
源代码:
Shader "Sbin/FragmentAnim"{	Properties	{		_MainTex ("Texture", 2D) = "white" {}	}	SubShader	{		Pass		{			CGPROGRAM			#pragma vertex vert			#pragma fragment frag			#include "UnityCG.cginc"			sampler2D _MainTex;			float4 _MainTex_ST;//纹理缩放偏移向量(Unity默认此变量赋值,变量名规则:纹理名_ST)			struct v2f{				float4 pos:POSITION;				float2 uv:TEXCOORD0;			};			v2f vert (appdata_full v)			{				v2f o;				o.pos = mul(UNITY_MATRIX_MVP, v.vertex);				o.uv = v.texcoord.xy * _MainTex_ST.xy + _MainTex_ST.zw;				return o;			}			fixed4 frag (v2f v) : COLOR			{				fixed4 col = tex2D(_MainTex, v.uv);//第一个参数:纹理,第二个参数UV向量				return col;			}			ENDCG		}	}}
using UnityEngine;using System.Collections;public class FragmentAnim : MonoBehaviour{    public int rowCount;    //行数    public int columCount;  //列数    public int fps;         //播放速度    private int currentIndex;//当前播放索引值    IEnumerator Start()    {        Material mat = GetComponent
().material; float itemWidth = 1.0f / rowCount; //每一帧宽度 float itemHeight = 1.0f / columCount; //每一帧高度 while (true) { float offset_x = currentIndex % columCount * itemWidth; float offset_y = currentIndex / rowCount * itemHeight; mat.SetTextureScale("_MainTex", new Vector2(itemWidth, itemHeight)); mat.SetTextureOffset("_MainTex", new Vector2(offset_x, offset_y)); yield return new WaitForSeconds(1 / fps); currentIndex = (++currentIndex) % (rowCount * columCount); } }}
你可能感兴趣的文章
Java多线程学习
查看>>
检查Linux服务器性能
查看>>
Java 8新的时间日期库
查看>>
Chrome开发者工具
查看>>
【LEETCODE】102-Binary Tree Level Order Traversal
查看>>
【LEETCODE】106-Construct Binary Tree from Inorder and Postorder Traversal
查看>>
【LEETCODE】202-Happy Number
查看>>
和机器学习和计算机视觉相关的数学
查看>>
十个值得一试的开源深度学习框架
查看>>
【LEETCODE】240-Search a 2D Matrix II
查看>>
【LEETCODE】53-Maximum Subarray
查看>>
【LEETCODE】215-Kth Largest Element in an Array
查看>>
【LEETCODE】241-Different Ways to Add Parentheses
查看>>
【LEETCODE】312-Burst Balloons
查看>>
【LEETCODE】232-Implement Queue using Stacks
查看>>
【LEETCODE】225-Implement Stack using Queues
查看>>
【LEETCODE】155-Min Stack
查看>>
【LEETCODE】20-Valid Parentheses
查看>>
【LEETCODE】290-Word Pattern
查看>>
【LEETCODE】36-Valid Sudoku
查看>>