Search Unity

  1. Unity 6 Preview is now available. To find out what's new, have a look at our Unity 6 Preview blog post.
    Dismiss Notice
  2. Unity is excited to announce that we will be collaborating with TheXPlace for a summer game jam from June 13 - June 19. Learn more.
    Dismiss Notice
  3. Dismiss Notice

Hatching Shader

Discussion in 'Made With Unity' started by reissgrant, Aug 23, 2010.

  1. reissgrant

    reissgrant

    Joined:
    Aug 20, 2009
    Posts:
    726
    I wrote a hatching shader for Unity Beta 6 although it may work for earlier versions as well, since it is not written as a new "surface shader".
    The shader takes two images and displays one of the 6 RGB channels based on the amount of diffuse light it receives.
    I included two sets of hatching textures, a pencil sketch version and a duotone version.
    Additionally, I included a slot for a diffuse texture that can be used as a base color.
    I included a "Tile Factor" which you can use to tile the main hatching. The shader currently relies on your imported UV's so if they are really clean (mine are not) you get really nice results.

    The textures are setup so that each channel has a different black and white texture, from lightest to darkest:
    (This is apparent in the included PSD files in the package)

    Tex_1.Red = Lightest texture
    Tex_1.Green = Lighter texture
    Tex_1.Blue = Light texture

    Tex_2.Red = Dark texture
    Tex_2.Green = Darker texture
    Tex_2.Blue = Darkest texture

    Here's a video of what it looks like on moving objects:
    http://www.youtube.com/v/nlDxZ6n_Bs8?hd=1

    And a picture:


    Click to download:
    https://docs.google.com/leaf?id=0B_BYe3L2yjJ3N2VjYzcwZTktNTA3Mi00NmVjLTk1YmYtN2M4YTI4NWRlYTFk&hl=en
    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     
    Last edited: Feb 14, 2011
  2. Pyroman311

    Pyroman311

    Joined:
    Aug 14, 2010
    Posts:
    87
    Well done man, Thanks!
     
  3. Ippokratis

    Ippokratis

    Joined:
    Oct 13, 2008
    Posts:
    1,521
    It looks very nice !
    Would it be possible to post the unitypackage ?
    Edit: Thank you very much !
     
    Last edited: Feb 14, 2011
  4. yusefkerr

    yusefkerr

    Joined:
    Apr 5, 2011
    Posts:
    179
    That looks amazing! Would there be a way to use this with a normal map as well?
     
  5. nharde20

    nharde20

    Joined:
    Mar 12, 2011
    Posts:
    44
    I have a question. How difficult would it be to swap the texture of the shader. As in, instead of hatch lines have it display screen tone dots? Can you alter the thickness and frequency of the hatch lines/dots or is it based on the distance from the light source?
     
  6. ShadowedGold

    ShadowedGold

    Joined:
    Mar 19, 2012
    Posts:
    1
    I'm new to Unity and just learning how to use shaders. I want to implement an effect just like this, so this was really encouraging to know that what I want to achieve is already out there! Unfortunately the link to the download seems to be broken, so I can't check it out. Would you be able to post a new one please?
     
  7. glacius3000

    glacius3000

    Joined:
    Oct 5, 2012
    Posts:
    69
    This shader looks great.

    I am having an issue with it though. Turning the camera is effecting how light or dark the areas are shaded and I can never get plain white. Here is a fraps http://www.youtube.com/watch?v=l_qUDicL4Qg
     
  8. glacius3000

    glacius3000

    Joined:
    Oct 5, 2012
    Posts:
    69
    I reduced the issue by changing a few lines.
    I added the tag
    Code (csharp):
    1. "LightMode" = "Vertex"
    then changed
    Code (csharp):
    1.  
    2. float3 normalWorld = normalize(mul((float3x3)UNITY_MATRIX_MV ,v.normal));
    3.                
    4. float diffuse = min(1.0,max(0,dot(_WorldLightPos0,normalWorld)));//cant remember the light pos var he used exactly
    5. diffuse *= diffuse;
    6. diffuse *= diffuse;
    7.  
    to the following
    Code (csharp):
    1.  
    2. float3 normalWorld = normalize(mul((float3x3)UNITY_MATRIX_IT_MV ,v.normal));
    3.                
    4. float3 L = normalize(unity_LightPosition[0] - posWorld);
    5. float diff = min(1.0,max(0,dot(normalWorld,L)));
    6. diff *=  unity_LightColor[0]*n;//n is a var to individually change the light intensity
    7.  
    This seemed to fix it mostly. Its doing it more now depending on the camera distance to the object. I think it may just be a mip mapping issue. It reduced again when i used a higher res texture.

    I'd love to hear other input on this.
     
  9. RedVonix

    RedVonix

    Joined:
    Dec 13, 2011
    Posts:
    425
    I know this is an older thread, but I'm having some problems getting this shader to work. I had the exact same problems as glacius3000, so I tried using your code changes. Unfortunately I received the following errors:

    Shader error in 'Custom/Hatch Texture UV': GLSL vertex shader: ERROR: 0:330: 'n' : undeclared identifier at line 17
    Shader warning in 'Custom/Hatch Texture UV': Program 'vert', implicit cast from "float4" to "float3" at line 67
    Shader error in 'Custom/Hatch Texture UV': Program 'vert', undefined variable "n" at line 73
    Shader error in 'Custom/Hatch Texture UV': Program 'vert', unable to find compatible overloaded function "clamp(error, float, int)" at line 75

    I made no other changes... as it looks like your code is dependent on certain variables and functions, I'm wondering if something about Unity changed since this was written? Any suggestions on making the shader work in Unity 3.5.7?

    Thanks in advance!
     
  10. a.Scarselli

    a.Scarselli

    Joined:
    Oct 25, 2013
    Posts:
    16
    Looks great.I know this is an old post, but Im just checking in to see if there is a chance for a re-up
    ? Link has died