Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Displacement Image Effect

Discussion in 'Image Effects' started by a3dline, Aug 25, 2016.

  1. a3dline

    a3dline

    Joined:
    Dec 28, 2013
    Posts:
    17
    Incorrect image effect result.
    For second camera (it is render normal map to render texture) and material (using render texture in shader) and script on first camera:

    Code (CSharp):
    1. [ExecuteInEditMode]
    2.     [RequireComponent(typeof(Camera))]
    3.     public class DisplacementEffect : MonoBehaviour
    4.     {
    5.         public Material material;
    6.         public Shader shader;
    7.         private bool support = true;
    8.  
    9.         void Awake()
    10.         {
    11.             if (!SystemInfo.supportsImageEffects
    12.                 || !SystemInfo.supportsRenderTextures
    13.                 || !SystemInfo.SupportsRenderTextureFormat(RenderTextureFormat.RGB565)
    14.                 || !shader.isSupported)
    15.             {
    16.                 support = false;
    17.             }
    18.         }
    19.  
    20.         void OnRenderImage(RenderTexture source, RenderTexture destination)
    21.         {
    22.             if (Time.timeScale == 0 || !support) Graphics.Blit(source, destination);
    23.             else Graphics.Blit(source, destination, material);
    24.         }
    25.     }
    Shader:

    Code (CSharp):
    1. Shader "Hidden/DisplacementEffect"
    2. {
    3.     Properties
    4.     {
    5.         _MainTex("Base (RGB)", 2D) = "white" {}
    6.         _DisplacementTex("Displacement rt", 2D) = "white" {}
    7.         _DisplacementPower("Displacement power", Float) = 0.025
    8.     }
    9.  
    10.     SubShader
    11.     {
    12.         Pass
    13.         {
    14.             CGPROGRAM
    15.             #pragma vertex vert_img
    16.             #pragma fragment frag
    17.  
    18.             #include "UnityCG.cginc"
    19.  
    20.             uniform sampler2D _MainTex;
    21.             uniform sampler2D _DisplacementTex;
    22.             uniform float _DisplacementPower;
    23.  
    24.             float4 frag(v2f_img i) : COLOR
    25.             {
    26.                 fixed4 displacementVector = tex2D(_DisplacementTex, i.uv);
    27.                 fixed2 uv_distorted = i.uv + _DisplacementPower * displacementVector.xy;
    28.  
    29.                 return tex2D(_MainTex, uv_distorted);
    30.             }
    31.             ENDCG
    32.         }
    33.     }
    34. }
    Project:
    Screenshot_58.png

    Effect Preview:


    It work correctly on a lot of devices, but only PowerVR GPU has this result pixelization like low resolution:
    unnamed.jpg
     
    wavekeyboard likes this.
  2. a3dline

    a3dline

    Joined:
    Dec 28, 2013
    Posts:
    17
    do u have any ideas?
     
  3. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,248
    Using "fixed" for uvs is your problem. Use float like this:

    float2 uv_distorted = i.uv + _DisplacementPower * displacementVector.xy;
     
    wavekeyboard likes this.
  4. a3dline

    a3dline

    Joined:
    Dec 28, 2013
    Posts:
    17
    thx for help. It's resolved