消融:
Shader "Unlit/36"
{
Properties
{
_MainTex("Texture", 2D) = "white" {}
_BumpMap("BumpMap",2D) = "bump" {}
_BurnAmount("BurnAmount",Range(0,1)) = 0
_LineWidth("LineWidth",Range(0,1)) = 0
_BurnFirstColor("BurnFistColor",Color) = (1,1,1,1)
_BurnSecondColor("BurnSencondColor",Color) = (1,1,1,1)
_BurnMap("BurnMap",2D) = "white"{}
}
SubShader
{
Pass
{
Tags{"LightMode" = "ForwardBase"}
Cull Off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_fwdbase
#include "UnityCG.cginc"
#include "Lighting.cginc"
#include "AutoLight.cginc"
sampler2D _MainTex;
float4 _MainTex_ST;
sampler2D _BumpMap;
float4 _BumpMap_ST;
sampler2D _BurnMap;
float4 _BurnMap_ST;
fixed _BurnAmount;
fixed _LineWidth;
fixed4 _BurnFirstColor;
fixed4 _BurnSecondColor;
struct v2f
{
float4 pos:SV_POSITION;
float2 uv:TEXCOORD0;
float3 worldPos:TEXCOORD1;
float2 burnUV:TEXCOORD2;
float2 bumpUV:TEXCOORD3;
float3 lightDir:TEXCOORD4;
UNITY_SHADOW_COORDS(5)
};
v2f vert(appdata_tan v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv = TRANSFORM_TEX(v.texcoord,_MainTex);
o.burnUV = TRANSFORM_TEX(v.texcoord, _BurnMap);
o.bumpUV = TRANSFORM_TEX(v.texcoord, _BumpMap);
TANGENT_SPACE_ROTATION;
o.lightDir = mul(rotation,ObjSpaceLightDir(v.vertex)).xyz;
o.worldPos = mul(unity_ObjectToWorld, v.vertex).xyz;
TRANSFER_SHADOW(o);
return o;
}
fixed4 frag(v2f i) :SV_Target
{
fixed3 burn = tex2D(_BurnMap,i.burnUV).rgb;
clip(burn.r - _BurnAmount);
float3 tangentLightDir = normalize(i.lightDir);
fixed3 tangentNormal = UnpackNormal(tex2D(_BumpMap,i.bumpUV));
fixed3 abledo = tex2D(_MainTex,i.uv).rgb;
fixed3 ambient = UNITY_LIGHTMODEL_AMBIENT.rgb * abledo;
fixed3 diffuse = _LightColor0.rgb * abledo * saturate(dot(tangentNormal,tangentLightDir));
fixed t = 1 - smoothstep(0,_LineWidth,burn.r - _BurnAmount);
fixed3 burnColor = lerp(_BurnFirstColor, _BurnSecondColor, t);
burnColor = pow(burnColor,10);
UNITY_LIGHT_ATTENUATION(atten, i, i.worldPos);
fixed3 finalColor = lerp(ambient + diffuse * atten,burnColor,t * step(0.001,_BurnAmount));
return fixed4(finalColor,1);
}
ENDCG
}
Pass
{
Tags{"LightMode" = "ShadowCaster"}
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#pragma multi_compile_shadowcaster
#include "UnityCG.cginc"
sampler2D _BurnMap;
float4 _BurnMap_ST;
fixed _BurnAmount;
struct v2f
{
V2F_SHADOW_CASTER;
float2 burnUV:TEXCOORD0;
};
v2f vert(appdata_base v)
{
v2f o;
TRANSFER_SHADOW_CASTER_NORMALOFFSET(o)
o.burnUV = TRANSFORM_TEX(v.texcoord,_BurnMap);
return o;
}
fixed4 frag(v2f i) :SV_Target
{
fixed3 burn = tex2D(_BurnMap,i.burnUV).rgb;
clip(burn.r - _BurnAmount);
SHADOW_CASTER_FRAGMENT(i)
}
ENDCG
}
}
}
水波:
Shader "Unlit/37"
{
Properties
{
_Color("Color",Color) = (1,1,1,1)
_MainTex("Texture", 2D) = "white" {}
_WaveMap("WaveMap",2D) = "white"{}
_EnvMap("EnvMap",Cube) = "_Skybox"{}
_WaveXSpeed("WaveXSpeed",Range(-0.1,0.1)) = 0
_WaveYSpeed("WaveYSpeed",Range(-0.1,0.1)) = 0
_Distortion("Distortion",Range(0,2000)) = 1
}
SubShader
{
Tags{"Queue" = "Transparent" "RenderType" = "opaque"}
GrabPass{"_RefractionTex"}
Pass
{
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"
#include "Lighting.cginc"
#include "AutoLight.cginc"
fixed4 _Color;
sampler2D _MainTex;
float4 _MainTex_ST;
sampler2D _WaveMap;
float4 _WaveMap_ST;
samplerCUBE _EnvMap;
fixed _WaveXSpeed;
fixed _WaveYSpeed;
float _Distortion;
sampler2D _RefractionTex;
float4 _RefractionTex_TexelSize;
struct v2f
{
float4 pos:SV_POSITION;
float4 uv : TEXCOORD0;
float4 screenPos:TEXCOORD1;
float4 ttw1:TEXCOORD2;
float4 ttw2:TEXCOORD3;
float4 ttw3:TEXCOORD4;
};
v2f vert(appdata_tan v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.screenPos = ComputeGrabScreenPos(o.pos);
o.uv.xy = TRANSFORM_TEX(v.texcoord,_MainTex);
o.uv.zw = TRANSFORM_TEX(v.texcoord,_WaveMap);
float3 worldPos = mul(unity_ObjectToWorld,v.vertex);
fixed3 worldNormal = UnityObjectToWorldNormal(v.normal);
fixed3 worldTangent = UnityObjectToWorldDir(v.tangent.xyz);
fixed3 worldBinormal = cross(worldNormal,worldTangent) * v.tangent.w;
o.ttw1 = float4(worldTangent.x, worldBinormal.x, worldNormal.x, worldPos.x);
o.ttw2 = float4(worldTangent.y, worldBinormal.y, worldNormal.y, worldPos.y);
o.ttw3 = float4(worldTangent.z, worldBinormal.z, worldNormal.z, worldPos.z);
return o;
}
fixed4 frag(v2f i) :SV_Target
{
float3 worldPos = float3(i.ttw1.w,i.ttw2.w,i.ttw3.w);
fixed3 viewDir = normalize(UnityWorldSpaceViewDir(worldPos));
float2 speed = _Time.y * float2(_WaveXSpeed,_WaveYSpeed);
fixed3 bump1 = UnpackNormal(tex2D(_WaveMap,i.uv.zw + speed)).rgb;
fixed3 bump2 = UnpackNormal(tex2D(_WaveMap,i.uv.zw - speed)).rgb;
fixed3 bump = normalize(bump1 + bump2);
float2 offset = bump.xy * _Distortion * _RefractionTex_TexelSize.xy;
i.screenPos.xy = offset * i.screenPos.z + i.screenPos.xy;
fixed3 refractColor = tex2D(_RefractionTex,i.screenPos.xy/i.screenPos.w).rgb;
bump = normalize(half3(dot(i.ttw1.xyz,bump),dot(i.ttw2.xyz,bump),dot(i.ttw3.xyz,bump)));
fixed4 texColor = tex2D(_MainTex,i.uv.xy + speed);
fixed3 reflectDir = reflect(-viewDir,bump);
fixed3 reflectColor = texCUBE(_EnvMap,reflectDir).rgb * texColor.rgb * _Color.rgb;
fixed fresnel = pow(1 - saturate(dot(viewDir,bump)),4);
fixed3 finalColor = reflectColor * fresnel + refractColor * (1 - fresnel);
return fixed4(finalColor,1);
}
ENDCG
}
}
}
全局雾效:
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class FogWithNoise : PostEffectBase
{
private Camera myCamera;
public Camera MyCamera
{
get
{
if (myCamera == null)
{
myCamera = GetComponent<Camera>();
}
return myCamera;
}
}
private Transform cameraTransform;
public Transform CameraTransform
{
get
{
if (cameraTransform == null)
{
cameraTransform = MyCamera.transform;
}
return cameraTransform;
}
}
[Range(0, 3f)]
public float fogDensity = 0.5f;
public Color fogColor = Color.white;
public float fogStart = 0;
public float fogEnd = 2;
public Texture2D noiseTex;
[Range(-0.5f, 0.5f)]
public float fogXSpeed = 0;
[Range(-0.5f, 0.5f)]
public float fogYSpeed = 0;
[Range(0, 5)]
public float NoiseAmount = 0;
private void OnEnable()
{
MyCamera.depthTextureMode |= DepthTextureMode.Depth;
}
private void OnRenderImage(RenderTexture source, RenderTexture destination)
{
if (material != null)
{
Matrix4x4 frustumCorners = Matrix4x4.identity;
float fov = MyCamera.fieldOfView;
float near = MyCamera.nearClipPlane;
float far = MyCamera.farClipPlane;
float aspect = MyCamera.aspect;
float halfHeight = near * Mathf.Tan(fov * 0.5f * Mathf.Deg2Rad);
Vector3 toRight = CameraTransform.right * halfHeight * aspect;
Vector3 toTop = CameraTransform.up * halfHeight;
Vector3 topLeft = CameraTransform.forward * near + toTop - toRight;
float scale = topLeft.magnitude / near;
topLeft.Normalize();
topLeft *= scale;
Vector3 topRight = CameraTransform.forward * near + toTop + toRight;
topRight.Normalize();
toRight *= scale;
Vector3 bottomLeft = CameraTransform.forward * near - toTop - toRight;
bottomLeft.Normalize();
bottomLeft *= scale;
Vector3 bottomRight = CameraTransform.forward * near - toTop + toRight;
bottomRight.Normalize();
bottomRight *= scale;
frustumCorners.SetRow(0, bottomLeft);
frustumCorners.SetRow(1, bottomRight);
frustumCorners.SetRow(2, topRight);
frustumCorners.SetRow(3, topLeft);
material.SetMatrix("_FrustumCornersRay", frustumCorners);
material.SetMatrix("_ViewProjectionInverseMatrix", (MyCamera.projectionMatrix * MyCamera.worldToCameraMatrix).inverse);
material.SetFloat("_FogDensity", fogDensity);
material.SetColor("_FogColor", fogColor);
material.SetFloat("_FogStart", fogStart);
material.SetFloat("_FogEnd", fogEnd);
material.SetTexture("_NoiseTex",noiseTex);
material.SetFloat("_FogXSpeed",fogXSpeed);
material.SetFloat("_FogYSpeed",fogYSpeed);
material.SetFloat("_NoiseAmount",NoiseAmount);
Graphics.Blit(source, destination, material);
}
else
{
Graphics.Blit(source, destination);
}
}
}
Shader "Unlit/38"
{
Properties
{
_MainTex("Base (RGB)", 2D) = "white" {}
_FogDensity("FogDensity",Float) = 1.0
_FogColor("FogColor",Color) = (1,1,1,1)
_FogStart("FogStart",Float) = 0.0
_FogEnd("FogEnd",Float) = 2.0
_NoiseTex("NoiseTex",2D)="white"{}
_FogXSpeed("FogXSpeed",Float)=0
_FogYSpedd("FogYSpeed",Float)=0
_NoiseAmount("NoiseAmount",Float)=0
}
SubShader
{
CGINCLUDE
#include "UnityCG.cginc"
float4x4 _FrustumCornersRay;
sampler2D _MainTex;
half4 _MainTex_TexelSize;
sampler2D _CameraDepthTexture;
half _FogDensity;
fixed4 _FogColor;
float _FogStart;
float _FogEnd;
sampler2D _NoiseTex;
float _FogXSpeed;
float _FogYSpeed;
float _NoiseAmount;
struct v2f {
float4 pos : SV_POSITION;
half2 uv : TEXCOORD0;
half2 uv_depth : TEXCOORD1;
float4 interpolatedRay : TEXCOORD2;
};
v2f vert(appdata_img v)
{
v2f o;
o.pos = UnityObjectToClipPos(v.vertex);
o.uv = v.texcoord;
o.uv_depth = v.texcoord;
#if UNITY_UV_STATS_AT_TOP
if (_MainTex_TexelSize.y < 0)
o.uv_depth.y = 1 - o.uv_depth.y;
#endif
int index = 0;
if (v.texcoord.x < 0.5 && v.texcoord.y < 0.5)
{
index = 0;
}
else if (v.texcoord.x > 0.5 && v.texcoord.y < 0.5)
{
index = 1;
}
else if (v.texcoord.x > 0.5 && v.texcoord.y > 0.5)
{
index = 2;
}
else
{
index = 3;
}
#if UNITY_UV_STARTS_AT_TOP
if (_MainTex_TexelSize.y < 0)
index = 3 - index;
#endif
o.interpolatedRay = _FrustumCornersRay[index];
return o;
}
fixed4 frag(v2f i) :SV_Target
{
float linearDepth = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE(_CameraDepthTexture,i.uv_depth));
float3 worldPos = _WorldSpaceCameraPos + linearDepth * i.interpolatedRay.xyz;
float2 speed = _Time.y * float2(_FogXSpeed,_FogYSpeed);
float noise = (tex2D(_NoiseTex, i.uv + speed).r - 0.5) * _NoiseAmount;
float fogDensity = (_FogEnd - worldPos.y) / (_FogEnd - _FogStart);
fogDensity = saturate(fogDensity * _FogDensity * (1 + noise));
fixed4 finalColor = tex2D(_MainTex,i.uv);
finalColor.rgb = lerp(finalColor.rgb, _FogColor.rgb, fogDensity);
return finalColor;
}
ENDCG
Pass
{
ZTest Always Cull Off ZWrite Off
CGPROGRAM
#pragma vertex vert
#pragma fragment frag
ENDCG
}
}
Fallback Off
}