ユニティスクリーン特殊効果の熱波歪み効果の実現



Realization Heat Wave Distortion Effect Unity Screen Special Effects



目次

ユニティスクリーン特殊効果の熱波歪み効果の実現



1.はじめに

2.主要テクノロジー



3つ目は注意が必要な事項

第四に、効果のプレビュー

5つの実装ステップ




1.はじめに

いわゆる画面後処理は、簡単に言えば、レンダリングパイプラインの最終段階です。 HDR、モーションブラー、その他の効果など、シーン全体で生成された画像を処理します。画面空間の後処理により、ゲーム全体のスタイルを変えることができます。または効果。したがって、画面の後処理を行うには、後処理効果をレンダリングするために使用されるシェーダーと、このレンダリングを呼び出すために必要なスクリプトの2つが必要です。

実装の原則:

  • ノイズクラウド画像を使用してランダムオフセットを取得し、次に値をメイン画像に割り当ててuv値を移動し、歪みを実現して熱波効果を実現します。

2.主要テクノロジー

1.シェーダーで、ノイズクラウドイメージのuvを取得し、それをメインイメージuvに割り当てると、uvは時間とともに変化します

2. OnRenderImage関数は、各フレームのすべてのコンテンツをレンダリングした後に実行されます。各フレームにMaterialのさまざまなパラメーターを設定し、Material.SetXXX( 'name'、value)を介してさまざまなパラメーターをシェーダーに渡すことができます。

3.キーコード

float4 noise = tex2D(_NoiseTex, i.uv - _Time.xy * _NoiseTimeScale) float2 offset = noise.xy * _UVScale return tex2D(_MainTex, i.uv + offset)

3つ目は注意が必要な事項

1.さまざまな後処理効果を重ね合わせることができます。ここでの目的地は必ずしも画面ではありません。ただし、後処理は非常にパフォーマンスを消費します。一方では、ピクセルシェーダーのフルスクリーンオーバードロー、他方では、特に高解像度の電話では、レンダーテクスチャが大量のメモリを消費します。複数の後処理効果により、メモリが枯渇する可能性があります。崩壊

2.画面の特殊効果を使用できるかどうかを判断するために注意してください

3.パラメータ値は実際の効果に応じて合理的に調整する必要があることに注意してください

第四に、効果のプレビュー

5つの実装ステップ

1.次のように、新しいUnityプロジェクトを作成し、モデルをインポートして、新しいスクリプトを作成します。

2. 2つの新しいカメラを作成します。Left_Cameraはスクリプトをマウントせず、処理も行いません。Right_Cameraは歪み処理のためにスクリプトにハングアップし、2つのカメラビューポートは次のように効果調整の比較として使用されます。

3.テスト結果

4.シェーダースクリプト

Shader 'Unlit/WaveWarp' { Properties { _MainTex('Base (RGB)', 2D) = 'white' {} _NoiseTex('Noise (RGB)', 2D) = 'white' {} _UVScale('uv scale ', Range(0.0, 1.0)) = 1.0 _NoiseTimeScale('Noise Time Scale ', Range(0,1)) = 1 } SubShader { Pass{ CGPROGRAM #pragma vertex vert_img #pragma fragment frag #include 'UnityCG.cginc' uniform sampler2D _MainTex uniform sampler2D _NoiseTex fixed _UVScale float _NoiseTimeScale fixed4 frag(v2f_img i) : COLOR { float4 noise = tex2D(_NoiseTex, i.uv - _Time.xy * _NoiseTimeScale) float2 offset = noise.xy * _UVScale return tex2D(_MainTex, i.uv + offset) } ENDCG } } FallBack 'Diffuse' }

5. C#スクリプト

using UnityEngine //The effect is also triggered when it is not running [ExecuteInEditMode] //Screen post-processing special effects generally need to be bound to the camera [RequireComponent(typeof(Camera))] //Provide a post-processing base class, the main function is to directly drag the shader through the Inspector panel to generate the material corresponding to the shader public class ScreenPostEffectBase : MonoBehaviour { //Drag directly on the Inspector panel public Shader shader = null private Material _material = null public Material _Material { get { if (_material == null) _material = GenerateMaterial(shader) return _material } } //Create a material for screen special effects according to the shader protected Material GenerateMaterial(Shader shader) { // Does the system support if (!SystemInfo.supportsImageEffects) { return null } if (shader == null) return null //Need to determine whether the shader supports if (shader.isSupported == false) return null Material material = new Material(shader) material.hideFlags = HideFlags.DontSave if (material) return material return null } void OnDisable() { if (_material != null) { DestroyImmediate(_material) } } } using UnityEngine public class WaveWarp : ScreenPostEffectBase { public Texture noiseTexure [Range(0.0f, 1.0f)] public float noiseTimeScale = 1.0f [Range(0.0f, 1.0f)] public float uvScale = 0.15f void OnRenderImage(RenderTexture sourceTexture, RenderTexture destTexture) { if (_Material != null) { _Material.SetFloat('_UVScale', uvScale) _Material.SetFloat('_NoiseTimeScale', noiseTimeScale) _Material.SetTexture('_NoiseTex', noiseTexure) Graphics.Blit(sourceTexture, destTexture, _Material) } else { Graphics.Blit(sourceTexture, destTexture) } } }