Search Unity

Creating a Normal Map within a Shader

Discussion in 'Shaders' started by Meganubis, Apr 17, 2017.

  1. Meganubis

    Meganubis

    Joined:
    Apr 19, 2013
    Posts:
    3
    Hey guys, I am attempting to convert an old c# ripple effect into a shader to quicken performance, I use to take the 0-1 range and apply it to the pixels as so
    Code (CSharp):
    1. for (int y=0; y < bumpTexture.height; ++y) {
    2.       for (int x=0; x < bumpTexture.width; ++x) {
    3.  
    4.              float xLeft = BumpMap.GetPixel(x-1,y).grayscale;
    5.  
    6.              float xRight = BumpMap.GetPixel(x+1,y).grayscale;
    7.  
    8.              float yUp = BumpMap.GetPixel(x,y-1).grayscale;
    9.  
    10.              float yDown = BumpMap.GetPixel(x,y+1).grayscale;
    11.  
    12.              float xDelta = ((xLeft-xRight)+1)*0.5f;
    13.  
    14.              float yDelta = ((yUp-yDown)+1)*0.5f;
    15.  
    16.          
    17.         bumpTexture.SetPixel(x,y,new Color(xDelta,yDelta,1.0f,1.0f));
    18.        }
    19. }        
    but using an example from a unity asset, I am attempting to use a render texture in place of setting all the pixels manually, but applying the normal map fixed4 of 0.5,0.5,1,1. However that is not as simple as I thought.

    Code (CSharp):
    1. fixed4 frag(v2f i) : COLOR
    2.     {
    3.         fixed orig = tex2D(_MainTex, i.uv[0]).r;
    4.         return orig + step(1 - _DropSize, 1 - length(_MousePos.xy - i.uv[1]));
    5.     }
    6.  
    7.     fixed4 fragPropogate(v2f i) : COLOR
    8.     {
    9.         float sample = tex2D(_MainTex, i.uv[1]).r;
    10.         sample += tex2D(_MainTex, i.uv[2]).r;
    11.         sample += tex2D(_MainTex, i.uv[3]).r;
    12.         sample += tex2D(_MainTex, i.uv[4]).r;
    13.  
    14.         sample /= 4.0;
    15.  
    16.         float newValue = sample * 2.0 + -tex2D(_PrevTex, i.uv[0]).r;
    17.  
    18.         return newValue * _Damping;
    19.     }
    Right now there are white ripples on a black texture, ive tried multiple approaches like changing the format of the render texture, manually manipulating the rgba of the fixed4. What am I missing?
     
    Last edited: Apr 17, 2017
  2. hippocoder

    hippocoder

    Digital Ape

    Joined:
    Apr 11, 2010
    Posts:
    29,723
    Texture set to wrap etc? also check your uvs have valid data. It should be showing something.
     
  3. Meganubis

    Meganubis

    Joined:
    Apr 19, 2013
    Posts:
    3
    UVs seem to be fine, as for the output, using the Frag on a _maintex is fine, but applying it on a _bumpmap yeilds nothing.