实现效果
扭曲效果最常见的有用于模拟热空气,模拟玻璃水晶透明物体遮罩等等。最近在做施放特别攻击和落地出场等效果,也用到了模拟热空气扭曲。只是简单地用粒子系统的上升模拟流体,要更完美模拟热空气的流动要再加上flow map计算。
过程摘要
主要是实现一个扭曲屏幕像素位置的shader,再配合粒子动画来模拟热空气上升。Distortion Shader参考了Unity官方发布的Distortion Shader in Unity 2019 with Shader Graph! (Tutorial)。
最核心的节点流程实际上只有3步。
- 用一张NormalMap来取样,每个颜色点代表屏幕像素坐标将要被扭曲的程度, 对取样后的点做坐标系转换,从UV空间转化到屏幕空间,把ScreenPosition和SampleTexture的点做相乘即可
- 做了空间转换后的点与原ScreenPosition相加,“扭曲”掉原有的坐标
- 最后的SceneColor节点最巧妙,把屏幕坐标系当做UV,取屏幕的颜色赋值到材质上
Provides access to the current Camera‘s color buffer using input UV, which is expected to be normalized screen coordinates.
Scene Color Node Description
官方案例实现的Distortion只能控制ScreenPosition往同一个方向扭曲,只是每个点扭曲的程度不同,于是我加了一个噪声图,Remap之后取值范围是[-1,1],以此来控制扭曲的程度以及方向,使得扭曲更扭曲。
细节
Shader Graph Scene Color Node
Unity Shader Graph的Scene Color节点在LWRP里面使用,必须开启LWRP Asset中的Opaque Texture选项,因为Scene Color节点使用的贴图Camera Opaque Texture,LWRP默认情况下是没有把屏幕颜色写入该贴图的,详见Scene Color Node说明。
Note: The behaviour of this Node is undefined globally. The executed HLSL code for this Node is defined per Render Pipeline, and different Render Pipelines may produce different results. Custom Render Pipelines that wish to support this Node will also need to explicitly define the behaviour for it. If undefined this Node will return 0 (black).
Scene Color Node Note
Note: In Lightweight Render Pipeline this Node returns the value of the Camera Opaque Texture. See the Lightweight Render Pipeline for more documentation on this feature. The contents of this texture are only available for Transparent objects. Set the Surface Type dropdown on the Material Options panel of the Master Node to Transparent to receive the correct values from this node.
Scene Color Node Note
Particle System Sorting Fudge
Unity粒子系统的sorting fudge可控制粒子显示层级,不想Distortion粒子遮断了某些其他特效,可以把sorting fudge调大。
end