Search Unity

combining textures and alpha

Discussion in 'Shaders' started by Richard_B, Feb 6, 2006.

  1. Richard_B

    Richard_B

    Joined:
    Jul 22, 2005
    Posts:
    436
    Hi,
    I am trying to use an alpha channel (or another texture) as a mask for a second texture. Normally something like the following works fine
    Code (csharp):
    1.  
    2.  Pass{
    3.             Material {
    4.                 Diffuse [_Color]
    5.             }                        
    6.             Lighting On
    7.             // Set up alpha blending
    8.                 Blend SrcAlpha OneMinusSrcAlpha
    9.             SetTexture [_PathTex] {
    10.                 Combine texture * primary, texture
    11.             }    
    12.  
    but what I need is for the texture "_PathTex" to be tiled with a much higher frequency than the alpha mask ie. the "Scale" in the placement for the texture might be say 16, whilst the "Scale" for the alpha mask might just be 1. Does this make sence?

    thanks,
    Richard.
     
  2. Richard_B

    Richard_B

    Joined:
    Jul 22, 2005
    Posts:
    436
    It's OK - I think I have sorted it for the moment:

    Code (csharp):
    1.  
    2. Blend OneMinusSrcAlpha SrcColor  
    3.     SetTexture [_HillTex_B] {
    4.           Combine texture * previous
    5. }
    6.  
    seems to do the trick.

    Richard.
     
  3. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
    If you want to control the tiling of textures use the placement button next to the texture slot.
     
  4. Richard_B

    Richard_B

    Joined:
    Jul 22, 2005
    Posts:
    436
    Hi Nicholas,
    That is what I have been doing - I'll post some images shortly to try illustrate what I am after.

    thanks,
    Richard.
     
  5. Richard_B

    Richard_B

    Joined:
    Jul 22, 2005
    Posts:
    436
    Well, I am stuck. This is how far I have got:
    Code (csharp):
    1.  
    2. Shader "TerrainShaderMkII" {
    3.     Properties {
    4.         _Color ("Main Color", Color) = (1,1,1,1)  
    5.  
    6.         _PathTex ("Path  Texture (RGB) Mask (A)", 2D) = "white"  
    7.         _PathMaskTex ("Path  Mask (Grey) ", 2D) = "white"
    8.        
    9.         _PlainTex_A ("PlainTex_A (RGB) Mask (A)", 2D) = "white"
    10.         _PlainTex_B ("PlainTex_B (RGB) Mask (A)", 2D) = "white"
    11.        
    12.     }
    13.     SubShader {  
    14. // Base texture  - bottom layer (rendered first)  
    15.         Pass{
    16.             Material {        
    17.                Diffuse [_Color]  
    18.             }                        
    19.             Lighting On
    20.            
    21.         } //end pass
    22.  
    23.  
    24. // =============       
    25. // path Texture  
    26. // =============
    27.     Pass{
    28.         Material {
    29.             Diffuse [_Color]
    30.         }                        
    31.         Lighting On
    32.         SetTexture [_PathTex] {
    33.         Combine texture *  primary
    34.         }
    35.      }   // end pass  
    36.  
    37. // =============
    38. // path  mask
    39. // =============         
    40.     Pass{
    41.         Material {
    42.             Emission (1,1,1,1)    
    43.         }                        
    44.         Lighting On          
    45. // Blend current_tex_pass  previous_tex_pass
    46.         Blend  OneMinusSrcColor SrcColor         
    47.         SetTexture [_PathMaskTex] {
    48.             Combine texture * previous    
    49.         }
    50.     } //end pass    
    51.                      
    52. // =============
    53. // path texture
    54. // =============
    55. //  Pass{
    56. //          Material {
    57. //              Diffuse [_Color]
    58. //          }                        
    59. //          Lighting On
    60. //          Blend OneMinusSrcAlpha SrcColor  
    61. //          SetTexture [PlainTex_A] {
    62. //              Combine texture * previous
    63. //
    64. //          }          
    65. //  } // end pass    
    66.     }
    67.     FallBack " Diffuse", 1
    68. }
    69.  
    and this is what I get (see image). As you can see the cobbles tile with a large Scale=32, whilst the path mask only has scale=1. That gives the path where the mask is white which is fine. However, I now want the black reagion of the mask to render with the "PlainTex_A" texture, also with a large scale=32. I am stuck - do I need multi texturing to do this?

    thanks,
    Richard.
     

    Attached Files:

  6. NicholasFrancis

    NicholasFrancis

    Joined:
    Apr 8, 2005
    Posts:
    1,587
  7. Richard_B

    Richard_B

    Joined:
    Jul 22, 2005
    Posts:
    436
    Great - I'll chase that up tomorrow.

    thanks,
    Richard.
     
  8. Richard_B

    Richard_B

    Joined:
    Jul 22, 2005
    Posts:
    436
    Couldn't wait till tomorrow :) Now for the spec and bump and ....
     

    Attached Files:

  9. antenna-tree

    antenna-tree

    Joined:
    Oct 30, 2005
    Posts:
    5,324
    Richard, that is a really cool shader combining 2 different tiling frequencies with alpha. Is the above code snippet you posted what it takes to get it to work?

    Nicholas, what's this http://tikiwiki.otee.dk/tikiwiki site. Is it a wiki run by OTEE, why is this the first time I've seen it? Is this going to be another wiki in parallel to Outcast's wiki?
     
  10. freyr

    freyr

    Joined:
    Apr 7, 2005
    Posts:
    1,148
    It's the internal wiki used at OTEE for writing the Unity documentation. It's public access to the bleeding edge changes in the docs before they are extracted and put up on http://otee.dk/ . Being a life snap shot into a work in progress, the docs will contain more errors than the published ones, but it is occasionally useful to get external parties to take a look at it before release to iron them out.
     
  11. Richard_B

    Richard_B

    Joined:
    Jul 22, 2005
    Posts:
    436
    I'll post some code and screen snaps in a day or two - it is working quite well now -- just wish we had lod for terrains :(

    richard.
     
  12. forestjohnson

    forestjohnson

    Joined:
    Oct 1, 2005
    Posts:
    1,370
    I have been wishing for some system like this for a while. Good going and I hope you will be willing to share! :)
     
  13. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    Nice work! Any chance of getting a copy of that shader pretty please? :) I am doing some terrain and would like to use this method for terrain details (roads, etc.)

    Thanks,
    -Jeremy
     
  14. Richard_B

    Richard_B

    Joined:
    Jul 22, 2005
    Posts:
    436
    Sure - happy to post the code - will do so tonight!
    Richard.
     
  15. Richard_B

    Richard_B

    Joined:
    Jul 22, 2005
    Posts:
    436
    Here you go:

    Code (csharp):
    1.  
    2. Shader "Terrain3L" {
    3.     Properties {
    4.         _Color ("Main Color", Color) = (1,1,1,1)  
    5.         _Shininess ("Shininess", Range (0.01, 1)) = 0.7
    6.  
    7.         _PathTex ("Path Texture (RGB) Mask (A)", 2D) = "black"  
    8.         _PathMaskTex ("Path Mask (A)", 2D) = "black"
    9.        
    10.            
    11.         _MudTex ("Mud (RGB)", 2D) = "white"
    12.         _MudMaskTex ("Mud Mask (A)", 2D) = "black"
    13.        
    14.         _GrassTex ("Grass", 2D) = "white"
    15.         _GrassMaskTex ("Grass Mask (A)", 2D) = "black"
    16.        
    17.        
    18.     }
    19.     SubShader {  
    20.      
    21.     Pass{        
    22.  
    23.              Material {
    24.             Diffuse (1,1,1,1)
    25.           }                        
    26.            Lighting On    
    27.           SetTexture [_PathTex] { combine texture * previous  }    
    28.          SetTexture [_PathMaskTex] {  Combine texture lerp (texture) previous   }
    29.          
    30.        
    31.    
    32.  
    33.      }   // end pass  
    34.      Pass{        
    35.         Blend one one
    36.  
    37.           SetTexture [_GrassTex] { combine texture   }    
    38.          SetTexture [_GrassMaskTex] {  Combine texture lerp (texture) previous   }
    39.          
    40.        
    41.    
    42.  
    43.      }   // end pass  
    44. Pass{        
    45.         Blend one one
    46.  
    47.           SetTexture [_MudTex] { combine texture   }    
    48.          SetTexture [_MudMaskTex] {  Combine texture lerp (texture) previous   }
    49.          
    50.        
    51.    
    52.  
    53.      }   // end pass
    54.     }
    55.    FallBack " Diffuse", 1
    56. }
    57.  
    58.  
    59.  

    The shader above has three layers but you can add as many as you like (And your card can take). It is rather crude code as I am not a shader expert - but it works in simple cases. Attached should be a screen snap of the shader in Unity and an image of the three alpha masks. Note the scale of the placments for the various textures.

    cheers,
    Richard.

    Edit: Ooops - forgot to open the Grass placement - the scale is the same as the Mud: 150.
     

    Attached Files:

  16. Richard_B

    Richard_B

    Joined:
    Jul 22, 2005
    Posts:
    436
  17. jeremyace

    jeremyace

    Joined:
    Oct 12, 2005
    Posts:
    1,661
    Thanks a lot man. Very nice of you to share your code with us. :) I have been talking to Bill and using his shader which works great. Do you notice much slowdowns with this shader with each layer in it's own pass? I have very little knowledge of shaders and was just curious. It is so cool what you can do with this stuff...I have too much I want to learn, far too little time lol.

    Thanks again,
    -Jeremy
     
  18. Richard_B

    Richard_B

    Joined:
    Jul 22, 2005
    Posts:
    436
    No problem! I havene't really tested the shader in any time critical game (or a marginal graphics card) so don't really know how it would perform. I only found Bills shader after I had written mine so will now give his a go to see if the masking is easier re workflow.

    cheers,
    Richard.
     
  19. pete

    pete

    Joined:
    Jul 21, 2005
    Posts:
    1,647
    i'm using a similar approach to yours Richard and all seems fine by me. here's mine.

    Code (csharp):
    1. Shader "4LayerTerrain" {
    2.    Properties {
    3.       _Color ("Main Color", Color) = (0,0,0,0)  
    4.  
    5.       _SnowTex ("Snow Texture (RGB)", 2D) = "white"  
    6.       _SnowMask ("Snow Mask (Gray)", 2D) = "white"
    7.        
    8.          
    9.       _RockTex ("Rock Texture (RGB)", 2D) = "white"
    10.       _RockMask ("Rock Mask (Gray)", 2D) = "white"
    11.        
    12.       _GrassTex ("Grass Texture (RGB)", 2D) = "white"
    13.       _GrassMask ("Grass Mask (Gray)", 2D) = "white"
    14.      
    15.       _PathsTex ("Path Texture (RGB)", 2D) = "white"
    16.       _PathsMask ("Path Mask (Gray)", 2D) = "white"
    17.  
    18.    }
    19.    SubShader {  
    20.  
    21.      Pass{        
    22.  
    23.              Material {
    24.               Diffuse (0,0,0,0)
    25.             }
    26.          SetTexture [_SnowTex]
    27.          SetTexture [_SnowMask]{ combine texture }
    28.            
    29.      }   // end pass  
    30.  
    31.      Pass{        
    32.         Blend one one
    33.           SetTexture [_RockMask]  
    34.           SetTexture [_RockTex] { combine texture * previous}
    35.  
    36.      }   // end pass
    37.  
    38.      Pass{        
    39.         Blend one one
    40.           SetTexture [_GrassMask]    
    41.           SetTexture [_GrassTex] { combine texture * previous}
    42.  
    43.         }   // end pass  
    44.  
    45.      Pass{        
    46.         Blend one one
    47.           SetTexture [_PathsMask]    
    48.           SetTexture [_PathsTex]{ combine texture * previous}
    49.  
    50.         }   // end pass  
    51.  
    52.  
    53.    }
    54.    
    55.    FallBack " Diffuse", 1
    56. }
    i'll try to post a better screenie later...
     

    Attached Files: