目的:
使用Unity Shader 面片火焰的实现思路1.先有一个面片
2.uv.y的映射
3.构造火焰形状
4.采样一张噪声并添加uv动画
5.结合两个mask
6.有了上面的思路,主观调整数值得到
7.加上颜色
代码:
Shader "Fire"
{
Properties
{
_BaseMap ("Texture", 2D) = "white" {}
_Hight ("Hight", Range(0, 1)) = 0
[HDR]_Color1 ("Color1", Color) = (1,1,1,1)
[HDR]_Color2 ("Color2", Color) = (1,1,1,1)
_Speed ("Speed", Range(0, 10)) = 1
}
SubShader
{
Tags { "RenderType"="Transparent" "Queue"="Transparent" }
Blend SrcAlpha OneMinusSrcAlpha
LOD 100
Pass
{
HLSLPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "Packages/com.unity.render-pipelines.universal/ShaderLibrary/Core.hlsl"
struct Attributes {
float4 positionOS : POSITION;
float2 uv : TEXCOORD0;
};
struct Varyings {
float4 positionCS : SV_POSITION;
float2 uv : TEXCOORD0;
};
CBUFFER_START(M)
float _Hight;
float3 _Color1;
float3 _Color2;
float _Speed;
CBUFFER_END
TEXTURE2D(_BaseMap); SAMPLER(sampler_BaseMap);
Varyings vert(Attributes IN)
{
Varyings OUT;
OUT.positionCS = TransformObjectToHClip(IN.positionOS.xyz);
OUT.uv = IN.uv;
return OUT;
}
half4 frag(Varyings IN) : SV_Target
{
//------shape
float
fireMask = (IN.uv.y + 0.4) * IN.uv.x * (1 - IN.uv.x);
fireMask = step(0.1, fireMask);
//return fireMask;
//------noise
float
noise = 1 - SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, IN.uv - _Time.xx * float2( 1, 1) * _Speed).r;
noise *= 1 - SAMPLE_TEXTURE2D(_BaseMap, sampler_BaseMap, IN.uv * float2(0.7, 0.7) - _Time.xx * float2(-1, 3) * _Speed).r;
noise = noise - IN.uv.y + _Hight;
noise = saturate(noise * 2) * (1 - IN.uv.y);
noise = smoothstep(0.2, 1.0, noise);
//return noise;
//------final
float
fireAlpha = fireMask * noise;
//return fireAlpha;
float3 finalColor = lerp(_Color1, _Color2, fireAlpha);
return float4(finalColor, fireAlpha);
}
ENDHLSL
}
}
}