 我们可以在脚本中通过 SetCustomParticleData和 GetCustomParticleData来操作这两个数据。
自定义数据分为两种类型:Color和Vector 注意: 一般来讲我们将自定义数据和粒子系统的Renderer选项中的Custom Vector 属性联合起来进行使用。 这个属性可以关联到Shader中,控制Shader的属性
 使用Custom
 简单示例这里使用上面使用到的特效基础材质 struct appdata
{
float4 positionOS : POSITION;
float2 texcoord : TEXCOORD0;
float4 texcoord1: TEXCOORD1; //第二套数据
};
这里注意,不是真的有第二套变量,这里只是创建一个变量和 Custom1.xyzw 关联起来。 v2f vert (appdata v)
{
v2f o;
o.vertex = TransformObjectToHClip(v.positionOS.xyz);
o.uv.xy = TRANSFORM_TEX(v.texcoord, _MainTex) ;
o.uv.zw = TRANSFORM_TEX(v.texcoord, _AlphaTex);
o.uv2 = v.texcoord1; //数据信息传入片元着色器
return o;
}
 half4 frag (v2f i) : SV_Target
{
float2 uv = i.uv.xy * i.uv2.xy + i.uv2.zw; //控制UV属性
half4 col = SAMPLE_TEXTURE2D(_MainTex,sampler_MainTex,uv) * _BaseColor;
float alpha = SAMPLE_TEXTURE2D(_AlphaTex,sampler_AlphaTex,i.uv.zw).x;
return real4(col.xyz,alpha);
}
这里UV2.xy 控制UV的平铺次数,所以乘。 UV2.zw 控制UV的平移,所以是加法。 全代码 Shader "Unlit/8_CustomData"
{
Properties
{
_MainTex ("Texture", 2D) = "white" {}
_AlphaTex("_AlphaTex",2D) = "white" {}
_BaseColor("_BaseColor",Color) = (1,1,1,1)
}
SubShader
{
Tags { "RenderPipeline"="UniversalPipeline" "RenderType"="Transparent" "Queue" = "Transparent" "IgnoreProjector" = " True"} //我们不希望任何投影类型材质或者贴图,影响我们的物体或者着色器
LOD 100
Pass
{
Tags{ "LightMode"="UniversalForward" }
Blend SrcAlpha OneMinusSrcAlpha
ZWrite Off
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
CBUFFER_START(UnityPerMaterial)
float4 _MainTex_ST,_AlphaTex_ST;
float4 _BaseColor;
CBUFFER_END
struct appdata
{
float4 positionOS : POSITION;
float2 texcoord : TEXCOORD0;
float4 texcoord1: TEXCOORD1;
};
struct v2f
{
float4 uv : TEXCOORD0;
float4 vertex : SV_POSITION;
float4 uv2 : TEXCOORD1;
};
TEXTURE2D (_MainTex);
SAMPLER(sampler_MainTex);
TEXTURE2D (_AlphaTex);
SAMPLER(sampler_AlphaTex);
v2f vert (appdata v)
{
v2f o;
o.vertex = TransformObjectToHClip(v.positionOS.xyz);
o.uv.xy = TRANSFORM_TEX(v.texcoord, _MainTex) ;
o.uv.zw = TRANSFORM_TEX(v.texcoord, _AlphaTex);
o.uv2 = v.texcoord1;
return o;
}
half4 frag (v2f i) : SV_Target
{
float2 uv = i.uv.xy * i.uv2.xy + i.uv2.zw;
half4 col = SAMPLE_TEXTURE2D(_MainTex,sampler_MainTex,uv) * _BaseColor;
float alpha = SAMPLE_TEXTURE2D(_AlphaTex,sampler_AlphaTex,i.uv.zw).x;
return real4(col.xyz,alpha);
}
ENDHLSL
}
}
}
效果
 设置数值 1 1 X Y 是贴图的平移 Z W 是UV方向移动
 总结
 在Shader里增加一个变量,如果动态控制就需要增加动作文件控制变量的使用。 如果使用粒子的属性,就可以使用曲线控制变量,上面的UV流动就是使用曲线控制。
|