Search Unity

how to create a magnify or pinch effect using a perlin noise map for UVs

Discussion in 'Shaders' started by stationx, Jan 10, 2016.

  1. stationx

    stationx

    Joined:
    Jul 9, 2012
    Posts:
    251
    Hello!
    Like the subject, I am trying to create some variation for a texture by distorting the UV map.
    I am looking for a way to create a magnify / pinch effect using a perlin noise map. (or cloud map)
    This is not a flowmap, it should make the texture on some areas slightly bigger, more organic.

    Does one know how to that?

    regards! Tom
     
  2. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    Using a texture to displace a UV is fairly trivial, but just using the noise straight will never get you the pinch / zoom look you want, for that you need something like a normal map which you can generate from any noise pattern either offline or in the shader.

    The basic noise UV displacement would be something like this.

    float noise = tex2d(_NoiseTex, i.uv).r; // or some noise function
    float2 displacedUV = i.uv + noise * _UVNoiseScale;
    fixed4 albedo = tex2d(_MainTex, displacedUV);

    If you do this then the texture just looks wavy rather than pinch and zoom. Now as I said you can just take a noise texture and do a height to normal map in an external program or on import and store it as a normal map, in which case you'd treat it similar to a normal map in the shader but not quite.

    float2 noise = tex2d(_NoiseTex, i.uv).ag;

    The last option is the one where you generate the same "slope" information that a normal map would have in the shader. This is useful if you're using a noise function instead of a texture.

    float noise = tex2d(_NoiseTex, i.uv).r;
    float noiseU = tex2d(_NoiseTex, i.uv + float2(_NoiseTex_TexelSize.x, 0.0)).r;
    float noiseV = tex2d(_NoiseTex, i.uv + float2(0.0, _NoiseTex_TexelSize.y)).r;
    float2 displacedUV = i.uv + float2(noiseU - noise, noiseV - noise) * _UVNoiseScale;
     
    morepixels likes this.