原创内容,转载请注明原文网址:http://homeqin.cn/a/wenzhangboke/jishutiandi/youxikaifa/2018/1227/279.html
常州微信公众平台开发-Unity3D纹理旋转两种实现方法
方法一:用代码实现
主要通过 MeshFilter.mesh.SetUVs函数来实现uv旋转,主要代码如下:
public void RotateUV ()
{
List uvs = new List( go.GetComponent().mesh.uv );
float speed = 0;
if ( switchfun )
speed = rotatespeed * Mathf.Deg2Rad * Time.deltaTime;
else
speed = rotatespeed * Mathf.Deg2Rad;
for ( int i = 0; i < uvs.Count; i++ )
{
Vector2 uv = uvs[i] - new Vector2( 0.5f, 0.5f );
uv = new Vector2( uv.x * Mathf.Cos( speed ) - uv.y * Mathf.Sin( speed ),
uv.x * Mathf.Sin( speed ) + uv.y * Mathf.Cos( speed ) );
uv += new Vector2( 0.5f, 0.5f );
uvs[i] = uv;
}
go.GetComponent().mesh.SetUVs( 0, uvs );
}
方法二:常州软件技术培训用shader实现
Shader "Custom/RotateShader" {
Properties
{
_MainTex("Main Tex",2D)="Write"{}
//给unity3d提供一个滑动条来控制旋转速度
_RotateSpeed("Rotate Speed",Range(0,10))=5
}
SubShader
{
Tags{"Queue"="Transparent" "RenderType"="Transparent"}
Blend SrcAlpha OneMinusSrcAlpha
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MainTex;
float _RotateSpeed;
struct v2f
{
float4 pos:POSITION;
float4 uv:TEXCOORD;
};
v2f vert(appdata_base v)
{
v2f o;
o.pos=mul(UNITY_MATRIX_MVP,v.vertex);
o.uv=v.texcoord;
return o;
}
half4 frag(v2f i):COLOR
{ //定义一个float2来存储顶点的UV的XY,减去0.5是因为uv旋转的起始是1,1为中心,XY都减去0.5是把中心点移到常州平台运营中心。
float2 uv=i.uv.xy -float2(0.5,0.5);
// _Time 是一个内置的float4时间,X是1/20,Y是1倍,Z是2倍,W是3倍
// 旋转矩阵的公式是: COS() - sin() , sin() + cos() 顺时针
// COS() + sin() , sin() - cos() 逆时针
uv = float2( uv.x*cos(_RotateSpeed * _Time.y) - uv.y*sin(_RotateSpeed*_Time.y),
uv.x*sin(_RotateSpeed * _Time.y) + uv.y*cos(_RotateSpeed*_Time.y) );
//再加回来
uv += float2(0.5,0.5);
half4 c=tex2D(_MainTex,uv);
return c;
}
ENDCG
}
}
}
//旋转后发现,四个顶点周围不太正确,哪是因为UNITY3D默认采取了 重叠纹理寻址模式,企业培训需要再贴图里改为夹取纹理寻址模式,贴图的 wrap Mode改为clamp
上篇:上一篇:常州微信网站开发-NGUI简易弹出窗口声音控制器
下篇:下一篇:常州微信小程序开发-U3d Resources.Load动态加载资源