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

Blending normal maps and moving them in opposite directions

Discussion in 'Shaders' started by dzibarik, Mar 26, 2015.

  1. dzibarik

    dzibarik

    Joined:
    Mar 24, 2015
    Posts:
    8
    Hi everyone!

    I'm new to Unity and don't know much about coding. I need to make an aimated texture of a fluid. I have used a modified function in Unreal 4 which takes an input map and moves it in 4 opposing directions + blending 4 images together.

    Here is how it looks in Unreal:



    Here is an effect I want to repoduce in Unity:



    I've looked into a default water shader but looking through code didn't help at all. I don't know how to make it move in different directions and work on non-planar surfaces.

    Any suggestions on how to approach this?
     
  2. Farfarer

    Farfarer

    Joined:
    Aug 17, 2010
    Posts:
    2,249
    I think you probably want something like this?

    Code (csharp):
    1. // Get scroll offset.
    2.                 float scrollSpeed = 0.1;
    3.                 float scroll = _Time.y * scrollSpeed;
    4.                 // Sample normal maps.
    5.                 float3 normal1 = UnpackNormal (tex2D (_BumpMap,IN.uv_MainTex + float2(scroll, 0.0))) + float3(0.0, 0.0, 1.0);
    6.                 float3 normal2 = UnpackNormal (tex2D (_BumpMap,IN.uv_MainTex + float2(-scroll, 0.0))) * float3(-1.0, -1.0, 1.0);
    7.                 float3 normal3 = UnpackNormal (tex2D (_BumpMap,IN.uv_MainTex + float2(0.0, scroll))) * float3(-1.0, -1.0, 1.0);
    8.                 float3 normal4 = UnpackNormal (tex2D (_BumpMap,IN.uv_MainTex + float2(0.0, -scroll))) * float3(-1.0, -1.0, 1.0);
    9.                 // Combine normals.
    10.                 float3 normalFinal = (normal1 * dot (normal1, normal2)) - (normal2 * normal1.z);
    11.                 normalFinal += float3(0.0, 0.0, 1.0);
    12.                 normalFinal = (normalFinal * dot (normalFinal, normal3)) - (normal3 * normalFinal.z);
    13.                 normalFinal += float3(0.0, 0.0, 1.0);
    14.                 normalFinal = (normalFinal * dot (normalFinal, normal4)) - (normal4 * normalFinal.z);
    15.                 normalFinal = normalize (normalFinal);
    16.  
     
    Last edited: Mar 26, 2015
  3. dzibarik

    dzibarik

    Joined:
    Mar 24, 2015
    Posts:
    8
    thanks! looks like what I need, now I'll try to implement it...