unity3d对材质球的uv进行平移,缩放和旋转

  • Post author:
  • Post category:其他





平移和缩放比较好解决:





平移:





v2 = new Vector2(0f,v);






renderer.material.mainTextureOffset = v2;






v是一个浮点型的值。





缩放:





Vector2 size = new Vector2(3f, 3f);

renderer.material.SetTextureScale(“_MainTex”, size);






旋转就比较复杂:





需要自己学一个shader。





然后控制其中的一个值。





shader:





Shader “Custom/RotateUVs” {


Properties {


_MainTex (“Base (RGB)”, 2D) = “white” {}

_RotationSpeed (“Rotation Speed”, Float) = 2.0

}

SubShader {


Tags { “RenderType”=”Opaque” }

LOD 200

CGPROGRAM

#pragma surface surf Lambert vertex:vert

sampler2D _MainTex;

struct Input {


float2 uv_MainTex;

};

float _RotationSpeed;

void vert (inout appdata_full v) {


v.texcoord.xy -=0.5;

float s = sin ( _RotationSpeed);



float c = cos ( _RotationSpeed);

float2x2 rotationMatrix = float2x2( c, -s, s, c);

rotationMatrix *=0.5;

rotationMatrix +=0.5;

rotationMatrix = rotationMatrix * 2-1;

v.texcoord.xy = mul ( v.texcoord.xy, rotationMatrix );

v.texcoord.xy += 0.5;

}

void surf (Input IN, inout SurfaceOutput o) {

half4 c = tex2D (_MainTex, IN.uv_MainTex);

o.Albedo = c.rgb;

o.Alpha = c.a;

}

ENDCG

}

FallBack “Diffuse”

}






只要控制



_RotationSpeed 的值即可。











源码已上传到资源里面。





























版权声明:本文为u010851844原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。