Search Unity

Simple Displacement Shader

Discussion in 'Shaders' started by Cosmas, Jan 21, 2012.

  1. Cosmas

    Cosmas

    Joined:
    Jan 9, 2010
    Posts:
    133
    So I've been looking all over the forums but this is a silent topic. Has anyone successfully created a displacement shader using a heightmap?? Seems pretty commonplace as far as shaders go.
     
  2. Cameron_SM

    Cameron_SM

    Joined:
    Jun 1, 2009
    Posts:
    915
    Not commonplace in real time games. There's stuff on the asset store that can do this. Or you could write one and share it with the community.
     
  3. elveatles

    elveatles

    Joined:
    May 2, 2009
    Posts:
    147
    Last edited: Jan 27, 2012
  4. MarioRuiz

    MarioRuiz

    Joined:
    Nov 7, 2009
    Posts:
    161
    Hmm... im trying to get a hang on shaders but after checking what you just said I see i still don't get a few things... I mean how do i get the rbg or alpha from the texture there? Can't see a way to do it without writing a lot more stuff... could you (if it's not a bother) be more specific?

    this is the bulgy soldier shader section...
    void vert (inout appdata_full v) {
    v.vertex.xyz += v.normal * "how do i get the texture color here?";
    }
     
  5. MarioRuiz

    MarioRuiz

    Joined:
    Nov 7, 2009
    Posts:
    161
    Hmm... im trying to get a hang on shaders but after checking what you just said I see i still don't get a few things... I mean how do i easily get the rbg or alpha from the texture there? Can't see a way to do it without writing a lot more stuff... could you (if it's not a bother) be more specific?

    this is the bulgy soldier shader section...
    void vert (inout appdata_full v) {
    v.vertex.xyz += v.normal * "how do i get the texture color here?";
    }
     
  6. AcidArrow

    AcidArrow

    Joined:
    May 20, 2010
    Posts:
    11,797
    No it's not. The only decent uses are with conjunction with tesselation to say, add some detail to distant low poly mountains (BF3 does that I think).

    Otherwise I can't really think of any uses that will result in a visual improvement that will be worth the fps hit.

    Also, we can't use tesselation in unity because it's a DX10+ feature.
     
  7. FL_FLow

    FL_FLow

    Joined:
    Sep 6, 2013
    Posts:
    1
    Lol, no! I'm used tessellation any years ago on windows XP Sp2. :3
     
  8. Jiexi

    Jiexi

    Joined:
    Feb 28, 2013
    Posts:
    1
    I really would like to know the same thing, but i keep getting referred to the " shader examples", but I can't really get the solution there either.
     
  9. Jonathan Marin

    Jonathan Marin

    Joined:
    Oct 7, 2013
    Posts:
    6
    Here is how you get texture color (here just get the Red value) and apply it to the normal:

    Taken from this page on tesselation
     
  10. Psyco92

    Psyco92

    Joined:
    Nov 15, 2013
    Posts:
    22
    Apologies for reviving an ancient thread, but it is the top Google result for "Unity mobile displacement"

    After following some points on this thread and then some I have ended up with the standard surface shader with : Albedo, Metallic, Occlusion, Emission, Smoothness aaaand Vertex displacement via a displacement map.
    This does not use tessellation.


    Code (CSharp):
    1. Shader "Mobile/Displacement"
    2. {
    3.     Properties
    4.     {
    5.         [NoScaleOffset]        _Albedo("Albedo (RGB), Alpha (A)", 2D) = "white" {}
    6.         [NoScaleOffset]        _Metallic("Metallic (R), Occlusion (G), Emission (B), Smoothness (A)", 2D) = "black" {}
    7.         [NoScaleOffset]        _Normal("Normal (RGB)", 2D) = "bump" {}
    8.         [NoScaleOffset]        _DispTex("Displacement Texture", 2D) = "white" {}
    9.         _Amount("Displacement Amount", Range(0,3)) = 0.5
    10.     }
    11.  
    12.     SubShader
    13.     {
    14.         Tags
    15.         {
    16.             "Queue" = "Geometry"
    17.             "RenderType" = "Opaque"
    18.         }
    19.  
    20.         CGINCLUDE
    21.         #define _GLOSSYENV 1
    22.         ENDCG
    23.  
    24.         CGPROGRAM
    25.         #pragma target 3.0
    26.         #include "UnityPBSLighting.cginc"
    27.         #pragma surface surf Standard vertex:vert
    28.         #pragma exclude_renderers gles
    29.  
    30.         struct Input
    31.         {
    32.             float2 uv_Albedo;
    33.         };
    34.  
    35.         sampler2D _Albedo;
    36.         sampler2D _Normal;
    37.         sampler2D _Metallic;
    38.  
    39.         float _Amount;
    40.         sampler2D _DispTex;
    41.  
    42.         void vert(inout appdata_full v)
    43.         {
    44.             v.vertex.xyz += v.normal * tex2Dlod(_DispTex, float4(v.texcoord.xy, 0, 0)).r * _Amount;
    45.         }
    46.  
    47.         void surf(Input IN, inout SurfaceOutputStandard o)
    48.         {
    49.             fixed4 albedo = tex2D(_Albedo, IN.uv_Albedo);
    50.             fixed4 metallic = tex2D(_Metallic, IN.uv_Albedo);
    51.             fixed3 normal = UnpackScaleNormal(tex2D(_Normal, IN.uv_Albedo), 1);
    52.  
    53.             o.Albedo = albedo.rgb;
    54.             o.Alpha = albedo.a;
    55.             o.Normal = normal;
    56.             o.Smoothness = metallic.a;
    57.             o.Occlusion = metallic.g;
    58.             o.Emission = metallic.b;
    59.             o.Metallic = metallic.r;
    60.         }
    61.         ENDCG
    62.     }
    63.  
    64.         FallBack "Diffuse"
    65. }