庄懂-BoyanTata的个人空间_哔哩哔哩_Bilibili
序列帧_Sequence
小人是一个AB的模型,外面鬼火是AD模式,是一个双Pass的Shader

代码部分
Pass可以都是有一个名字的,这样一个Pass写完之后,其它的Shader想调用这个Pass,然后里面内容完全不想改,可以使用"
UsePass
(调用Pass名字)"
LightMode
Unity的手游一般都是ForwardBase前项渲染
使用SRP自定义渲染管线是可以自己声明一个LightMode,你的相机在渲染场景的时候,它会有一个模式,这个模式只会渲染这个模式下的Shader,相当于为什么你写的那个shader没有原先写的那个角色那些shader,为什么你完全都没有写关于阴影的事情,你只把Autolight和Lighting两个cginclude包含进来,它有就投影了呢?因为FallBack回退的shader里面它会有一个Pass,它的LightMode是shadowcast产生阴影,然后是它在渲染阴影的时候,它的相机的LightMode是shadowcast,然后它会在你的shader里面去找,我要找一个Pass,里面的LightMode是shadowcast,然后看看这个是LightMode是ForrwardBase,啊,不是的,这个是LightMode是ForwardAdd,啊也不是的!这个是shadowcast,那我就用这个shadowcast去渲染阴影,其实这个
渲染阴影是一个渲染距离
火焰序列图_Wrap Modex选择
Repeat

Shader "AP01/L18/Sequence"
{
Properties
{
_MainTex ("RGB:颜色 A:透贴", 2d) = "gray"{}
_Opacity ("透明度", range(0, 1)) = 0.5
_Sequence ("序列帧", 2d) = "gray"{}
_RowCount ("行数", int) = 1
_ColCount ("列数", int) = 1
_Speed ("速度", range(0.0, 15.0)) = 1
}
SubShader
{
Tags
{
"Queue"="Transparent" // 调整渲染顺序
"RenderType"="Transparent" // 对应改为Cutout
"ForceNoShadowCasting"="True" // 关闭阴影投射
"IgnoreProjector"="True" // 不响应投射器
}
Pass
{
Name "FORWARD"
Tags { "LightMode"="ForwardBase" }
Blend One OneMinusSrcAlpha // 修改混合方式One/SrcAlpha OneMinusSrcAlpha
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#pragma multi_compile_fwdbase_fullshadows
#pragma target 3.0
// 输入参数
uniform sampler2D _MainTex;
uniform half _Opacity;
// 输入结构
struct VertexInput
{
float4 vertex : POSITION; // 顶点位置 总是必要
float2 uv : TEXCOORD0; // UV信息 采样贴图用
};
// 输出结构
struct VertexOutput
{
float4 pos : SV_POSITION; // 顶点位置 总是必要
float2 uv : TEXCOORD0; // UV信息 采样贴图用
};
// 输入结构>>>顶点Shader>>>输出结构
VertexOutput vert (VertexInput v)
{
VertexOutput o = (VertexOutput)0;
o.pos = UnityObjectToClipPos( v.vertex); // 顶点位置 OS>CS
o.uv = v.uv; // UV信息 支持TilingOffset
return o;
}
// 输出结构>>>像素
half4 frag(VertexOutput i) : COLOR
{
half4 var_MainTex = tex2D(_MainTex, i.uv); // 采样贴图 RGB颜色 A透贴
half3 finalRGB = var_MainTex.rgb;
half opacity = var_MainTex.a * _Opacity;
return half4(finalRGB * opacity, opacity); // 返回值
}
ENDCG
}
Pass
{
Name "FORWARD"
Tags { "LightMode"="ForwardBase" }
Blend One One // 混合方式
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#pragma multi_compile_fwdbase_fullshadows
#pragma target 3.0
// 输入参数
uniform sampler2D _Sequence; uniform float4 _Sequence_ST;
uniform half _Opacity;
uniform half _RowCount;
uniform half _ColCount;
uniform half _Speed;
// 输入结构
struct VertexInput
{
float4 vertex : POSITION; // 顶点位置 总是必要
float3 normal : NORMAL;
float2 uv : TEXCOORD0; // UV信息 采样贴图用
};
// 输出结构
struct VertexOutput
{
float4 pos : SV_POSITION; // 顶点位置 总是必要
float2 uv : TEXCOORD0; // UV信息 采样贴图用
};
// 输入结构>>>顶点Shader>>>输出结构
VertexOutput vert (VertexInput v)
{
VertexOutput o = (VertexOutput)0;
v.vertex.xyz += v.normal * 0.03; // 顶点位置法向挤出
o.pos = UnityObjectToClipPos(v.vertex); // 顶点位置 OS>CS
o.uv = TRANSFORM_TEX(v.uv, _Sequence); // 前置UV ST操作
float id = floor(_Time.z * _Speed); // 计算序列id
float idV = floor(id / _ColCount); // 计算V轴id
float idU = id - idV * _ColCount; // 计算U轴id
float stepU = 1.0 / _ColCount; // 计算U轴步幅
float stepV = 1.0 / _RowCount; // 计算V轴步幅
float2 initUV = o.uv * float2(stepU, stepV) + float2(0.0, stepV * (_ColCount - 1.0)); // 计算初始UV
o.uv = initUV + float2(idU * stepU, idV * stepV); // 计算序列帧UV
return o;
}
// 输出结构>>>像素
half4 frag(VertexOutput i) : COLOR
{
half4 var_Sequence = tex2D(_Sequence, i.uv); // 采样贴图 RGB颜色 A透贴
half3 finalRGB = var_Sequence.rgb;
half opacity = var_Sequence.a * _Opacity;
return half4(finalRGB * opacity, opacity); // 返回值
}
ENDCG
}
}
}
极坐标_PolarCoord_代码部分
极坐标的换算只能在像素Shader里面去做
Shader "AP01/L18/PolarCoord"
{
Properties
{
_MainTex ("RGB:颜色 A:透贴", 2d) = "gray"{}
[HDR]_Color ("混合颜色", color) = (1.0, 1.0, 1.0, 1.0)
_Opacity ("透明度", range(0, 1)) = 0.5
}
SubShader
{
Tags
{
"Queue"="Transparent" // 调整渲染顺序
"RenderType"="Transparent" // 对应改为Cutout
"ForceNoShadowCasting"="True" // 关闭阴影投射
"IgnoreProjector"="True" // 不响应投射器
}
Pass
{
Name "FORWARD"
Tags { "LightMode"="ForwardBase" }
Blend One OneMinusSrcAlpha // 修改混合方式
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#pragma multi_compile_fwdbase_fullshadows
#pragma target 3.0
// 输入参数
uniform sampler2D _MainTex;
uniform half _Opacity;
uniform half3 _Color;
// 输入结构
struct VertexInput
{
float4 vertex : POSITION; // 顶点位置 总是必要
float2 uv : TEXCOORD0; // UV信息 采样贴图用
float4 color : COLOR;
};
// 输出结构
struct VertexOutput
{
float4 pos : SV_POSITION; // 顶点位置 总是必要
float2 uv : TEXCOORD0; // UV信息 采样贴图用
float4 color : COLOR;
};
// 输入结构>>>顶点Shader>>>输出结构
VertexOutput vert (VertexInput v)
{
VertexOutput o = (VertexOutput)0;
o.pos = UnityObjectToClipPos( v.vertex); // 顶点位置 OS>CS
o.uv = v.uv; // UV信息 支持TilingOffset
o.color = v.color;
return o;
}
// 直角坐标转极坐标方法
float2 RectToPolar(float2 uv, float2 centerUV)
{
uv = uv - centerUV;
float theta = atan2(uv.y, uv.x); // atan()值域[-π/2, π/2]一般不用; atan2()值域[-π, π]
float r = length(uv);
return float2(theta, r);
}
// 输出结构>>>像素
half4 frag(VertexOutput i) : COLOR
{
// 直角坐标转极坐标
float2 thetaR = RectToPolar(i.uv, float2(0.5, 0.5));
// 极坐标转纹理采样UV
float2 polarUV = float2(
thetaR.x / 3.141593 * 0.5 + 0.5, // θ映射到[0, 1]
thetaR.y + frac(_Time.x * 3.0) // r随时间流动
);
// 采样MainTex
half4 var_MainTex = tex2D(_MainTex, polarUV);
// 处理最终输出
half3 finalRGB = (1 - var_MainTex.rgb) * _Color;
half opacity = (1 - var_MainTex.r) * _Opacity * i.color.r;
// 返回值
return half4(finalRGB * opacity, opacity);
}
ENDCG
}
}
}