Unity 写一个火焰shader
时间: 2023-06-15 15:02:54 浏览: 137
unity shader写实火焰
好的,以下是一个简单的基于Unity的火焰shader示例代码:
```
Shader "Custom/Fire" {
Properties {
_MainTex ("Texture", 2D) = "white" {}
_Color ("Color", Color) = (1,1,1,1)
_Speed ("Speed", Range(0,5)) = 1
_Power ("Power", Range(0,5)) = 1
_Distortion ("Distortion", Range(0,1)) = 0.2
_Mask ("Mask", 2D) = "white" {}
}
SubShader {
Tags {"Queue"="Transparent" "RenderType"="Transparent"}
CGPROGRAM
#pragma surface surf Standard
#pragma target 3.0
sampler2D _MainTex;
sampler2D _Mask;
float4 _Color;
float _Speed;
float _Power;
float _Distortion;
struct Input {
float2 uv_MainTex;
float2 uv_Mask;
float3 worldPos;
float3 worldNormal;
float3 worldRefl;
};
void surf (Input IN, inout SurfaceOutputStandard o) {
float2 mask = tex2D(_Mask, IN.uv_Mask).rg;
float2 distortion = (mask - 0.5) * _Distortion * _Power;
float2 noise1 = tex2D(_MainTex, IN.uv_MainTex + distortion * _Speed).rg;
float2 noise2 = tex2D(_MainTex, IN.uv_MainTex + distortion * _Speed * 2).rg;
float2 noise3 = tex2D(_MainTex, IN.uv_MainTex + distortion * _Speed * 3).rg;
float2 noise4 = tex2D(_MainTex, IN.uv_MainTex + distortion * _Speed * 4).rg;
float2 noise = (noise1 + noise2 + noise3 + noise4) / 4;
o.Albedo = _Color.rgb * (1 - mask.r);
o.Metallic = 0;
o.Smoothness = 1;
o.Normal = UnpackNormal(tex2D(_MainTex, IN.uv_MainTex + distortion * _Speed), 0);
o.Emission = _Power * (noise.r + noise.g) / 2;
}
ENDCG
}
FallBack "Diffuse"
}
```
这个shader使用了一个噪波纹理和一个掩码纹理来创建火焰效果。可以通过调整_Speed、_Power和_Distortion属性来控制火焰的速度、强度和扭曲程度。同时,也可以通过_Color属性来改变火焰的颜色。最后,这个shader会将火焰的亮度作为自发光来渲染。
阅读全文