文章目录
前言
通过shader实现万花筒效果,shader放在文末。由右图图像生成左图万花筒。需要去掉背景,只留车的图,并且要从正上方视角看下去的图片才可以产生左侧的效果。
1.Shader参数
画布比例:显示图片image的比例
图片比例:texture2d图片的比例
行数:是指再画布上显示多少行的万花筒图片
列数:是指再画布上显示多少列的万花筒图片
Tim:通过调节可调节万花筒效果
样式:整数,可调节万花筒样式,一般零即可
2.通过相机获取图片
通过一个Quad添加此shader 的Albedo绑定一个RenderTexture,并把此RenderTexture付给车的相机,这样就可以获取持续的万花筒效果,再增加一个正交相机来单独渲染Quad,即可通过相机截图来获取图片。
3.Shader代码
Shader "Custom/GaussBlur" {
Properties{
_MainTex("Albedo (RGB)", 2D) = "white" {
}
_Plansize("画布比例", float) = 1
_Texturesize("图片比例", float) = 1
_SizeZT("缩放zT", Range(0, 20)) = 0
_Whith("行数", float) = 1
_Hight("列数", float) = 1
_Tim("Tim", float) = 1
_A("透明度", Range(0, 1)) = 1
_style("样式", int) = 0//0单旋 1散开 2回旋 3慢旋 4散聚
}
SubShader{
Tags{
"Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" }
LOD 100
ZWrite Off
Blend SrcAlpha OneMinusSrcAlpha
Pass{
CGPROGRAM
#pragma exclude_renderers gles
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
sampler2D _MainTex;
int _RepeatNUM;
float _Centrifuge;
float _Plansize;
float _Texturesize;
float _ElaT;
float _Dx;
float _Dy;
float _Size;
float _Size2;
float _SizeZT;
float K;
float _Whith;
float _Hight;
float _JZ, _JZ2, _Tim;
float _A;
int _style;
struct appdata
{
float4 vertex : POSITION;
float2 uv : TEXCOORD0;
};
struct v2f {
float4 pos : SV_POSITION;
float2 uv : TEXCOORD0;
};
v2f vert(appdata_img v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv = v.texcoord.xy;
return o;
}
float4 Repeatpicture(float2 uv)
{
float4 color1 = tex2D(_MainTex, uv);
if (uv.x<0 || uv.x>1 || uv.y<0 || uv.y>1) {
color1.a = 0;
}
uv = uv + 0.2;
float4 color2 = tex2D(_MainTex, uv);
if (uv.x<0 || uv.x>1 || uv.y<0 || uv.y>1) {
color2.a = 0;
}
float4 colorTmp = color1*(1 - color2.a)*color1.a + color2*color2.a;
return colorTmp;
}
float BezierADm(float tim1, float tim2, float na, float nb, float ka, float kb, float Time)//时间从tim1到tim2,数值从na到nb。曲线斜率ka,kb。总时间Time
{
float num;
float L = (tim2 - tim1) / 2.0f;
if (Time < tim1)
{
num = na;
}
else
{
if (Time <= tim2)
{
float t = (Time - tim1) / (tim2 - tim1);
float3 p0 = float3(tim1, na, 0);
float3 p1 = float3(L, tan(ka) * L, 0);
float3 p2 = float3(-L, tan(kb) * L, 0);
float3 p3 = float3(tim2, nb, 0);
float3 b0 = float3(0, 0, 0);
float3 b1 = float3(0, 0, 0);
float3 b2 = float3(0, 0, 0);
float3 b3 = float3(0, 0, 0);
float Ax, Ay, Az, Bx, By, Bz, Cx, Cy, Cz;
Cx = 3.0 * ((p0.x + p1.x) - p0.x);
Bx = 3.0 * ((p3.x + p2.x) - (p0.x + p1.x)) - Cx;
Ax = p3.x - p0.x - Cx - Bx;
Cy = 3.0 * ((p0.y + p1.y) - p0.y);
By = 3.0 * ((p3.y + p2.y) - (p0.y