首先,我们先实现用代码在shader里面实现缩放,平移和旋转!
上代码,首先是我们的shader:
Shader "Unlit/贴图的平移缩放"
{
Properties
{
_MainTex("Texture", 2D) = "white" {}
_UA("旋转中点x",Float) = 0.5
_UB("旋转中点y",Float) = 0.5
_CenterX("平移x",float) = 0
_CenterY("平移y",float) = 0
_Scale("缩放",Float) = 10
_RotateNum("旋转角度",Range(-360,360)) = 0
}
SubShader
{
Tags { "RenderType"="Opaque" }
LOD 100
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MainTex; float4 _MainTex_ST;
float _UA;
float _UB;
float _RA;
float _Scale;
float _RotateNum;
float _CenterX;
float _CenterY;
struct v2f
{
float2 uv : TEXCOORD0;
float4 pos : SV_POSITION;
};
v2f vert (appdata_full v)
{
v2f o;
o.pos = mul(UNITY_MATRIX_MVP, v.vertex);
o.uv = v.texcoord;
return o;
}
fixed4 frag (v2f i) : SV_Target
{
//计算旋转的坐标
float Rote = (_RotateNum * 3.1415926)/180;
float sinNum = sin(Rote);
float cosNum = cos(Rote);
float2 di = float2(_UA,_UB);
//计算平移之后的坐标,需要乘平移矩阵
float2 uv = mul(float3(i.uv - di,1),float3x3(1,0,0,0,1,0,_CenterX,_CenterY,1)).xy;
//计算缩放之后的坐标,需要乘缩放矩阵
uv = mul(uv,float2x2(_Scale,0,0,_Scale));
//计算旋转之后的坐标,需要乘旋转矩阵
uv = mul(uv,float2x2(cosNum,-sinNum,sinNum,cosNum)) + di;
fixed4 col;
//用最终的坐标来采样当前的纹理,就ok了
col = tex2D(_MainTex,TRANSFORM_TEX(uv,_MainTex));;
return col;
}
ENDCG
}
}
}
记住旋转的中心点最好不要改变,就保持在当前的图像的中心!!!
关于具体参数的讲解代码已经提到了,三个矩阵的组合这是三个矩阵!根据公式带入就行了,具体矩阵怎么来的,可以查阅《3d数学基础:图形与游戏开发》一书!
接下来到了c#部分:
本部分我们讲如何更好的合并两张图:
using UnityEngine;
using System.Collections;
public class control : MonoBehaviour {
/// <summary>
/// 透明通道图,本地获取
/// </summary>
private Texture2D alpha;
/// <summary>
/// 传入的纹理图,外部获取
/// </summary>
public Texture2D tex;
/// <summary>
/// 材质球
/// </summary>
public Material material;
void Start () {
Color alphacolor;
alpha = Resources.Load<Texture2D>("path");
Color texcolor;
Texture2D texture = new Texture2D(alpha.width, alpha.height);
int beginX = alpha.width / 2 - tex.width / 2;
int endX = alpha.width / 2 + tex.width / 2;
int beginY = (alpha.height - tex.height) / 2;
int endY = (alpha.height + tex.height) / 2;
for (int i = 1; i <= alpha.width; i++)
{
for (int j = 1; j <= alpha.height; j++)
{
alphacolor = alpha.GetPixel(i, j);
alphacolor.a = 0;
if ((i> beginX&& i < endX)&&(j> beginY&&j< endY))
{
texcolor = tex.GetPixel(i - beginX, j - beginY);
texture.SetPixel(i, j, texcolor);
}
else
{
texture.SetPixel(i, j, alphacolor);
}
}
}
texture.Apply();
texture.alphaIsTransparency = true;
alpha = texture;
material.SetTexture("_Logo_1", alpha);
}
}
首先我们应该知道图形有自己的比例,比如2048x2048,那么我们就获取图片的宽和高来当作变量遍历每一个像素点,然后在你想要修改它的时候,插入另外一张图片的颜色,我们用GetPixel来获取某个点的像素颜色,当然我们这之中少不了判断:
本例我实现的是合并的图形小图在大图的正中间,实现原理是求得中间的那一块的面积,用数学方法得到x和y的最大值和最小值,让后用代码判断当前的i和它们比较,当在中间的时候,填充小图,不在则填充大图!!!!
本篇可以实现衣服上面的logo,随意移动的效果!如下:
本篇实例到此结束!本人的shader学习群是《601408323》,欢迎萌新,我也是萌新来着!哈哈!