Search Unity

Rotate parallax texture in shader?

Discussion in 'Shaders' started by ioFlow, Oct 29, 2014.

  1. ioFlow

    ioFlow

    Joined:
    May 30, 2014
    Posts:
    16
    Hi Everyone,

    I'm learning how to make my own terrain shaders at the moment, and I need a little help.

    I've got the hang of scaling and rotating diffuse and normal textures in a terrain shader, so that's pretty cool. But if I try and rotate a texture that I'm doing parallax on, the parallax effect screws up. I think it has something to do with the viewDir not being rotated with the UVs but I'm not very good with matrices yet so I'm not sure.

    Here's what it looks like at the moment (normal parallax on the left, naughty parallax on the right):
    141029_162600_parallaxTerrainShader.png

    P.S. Many thanks to TerraUnity for showing me how to do parallax :)

    Here's the function I'm using in my (surface) shader to calculate parallax data:

    Code (CSharp):
    1. struct parallaxData {
    2.     fixed4 dPixel;
    3.     fixed4 nPixel;
    4. };
    5.  
    6. parallaxData getParallax(sampler2D splatTex, sampler2D normTex, fixed2 uv_coord, float uv_scale, float uv_rotation, float height, fixed3 viewDir)
    7. {      
    8.     // rotate the UV
    9.     float cos_angle = radians(uv_rotation);
    10.     float sin_angle = sqrt(1.0 - cos_angle * cos_angle);
    11.     float2x2 mat_angle = float2x2(cos_angle, -sin_angle, sin_angle, cos_angle);
    12.     uv_coord = mul(mat_angle, uv_coord) / 3.6719;
    13.                              
    14.     // scale the UV
    15.     uv_coord *= uv_scale;
    16.    
    17.     // parallax offset the UV
    18.     uv_coord = uv_coord + ParallaxOffset (tex2D (splatTex, uv_coord).w, height, viewDir);
    19.  
    20.     // get the color of the specified diffuse and normal pixel
    21.     parallaxData output;
    22.     output.dPixel = tex2D (splatTex, uv_coord);
    23.     output.nPixel = tex2D (normTex, uv_coord);
    24.    
    25.     return output;
    26. }
    Any suggestions?