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

Cutout + Surface Shader

Discussion in 'Shaders' started by Glader, Jul 14, 2015.

  1. Glader

    Glader

    Joined:
    Aug 19, 2013
    Posts:
    449
    Hi, I'm pretty new to shaders and I only really have experience with surface shaders in Unity.

    Basically, I want to have a cutout-like effect dictated by a secondary texture's alpha but instead of not drawing a pixel I'd like to instead draw another texture. Right now I can the first part but the second part eludes me.

    Whenever I tried that I could only seem to figure out how to draw the third texture when the secondary texture, used to sample alpha value, had alpha. I couldn't achieve the cutout-like effect no matter what I tried.

    This is the desired look:

    But that is currently achieved by duplicating the mesh and having two different materials on each. I cannot figure out how to achieve filling out cutout-like alpha effect with the lava-like texture.

    P.S. I know it looks terrible but it is true to its source material: http://www.pso-world.com/images/items/BB-YAMATO-ACTION-1.jpg
     
  2. Glader

    Glader

    Joined:
    Aug 19, 2013
    Posts:
    449
    Well, I kind of got it but I'd prefer not to use an if. Those are supposedly poor for performance right?

    Code (CSharp):
    1. if(cutoffTexAlpha - _CutOff < 0)
    2. {
    3.      o.Albedo = tex2D(_EmissionTex, IN.uv_EmissionTex);
    4.      o.Emission = _Emission * o.Albedo;
    5. }
    That's about all I could come up with. It works but I'm not sure it's a good way to do it.
     
  3. Fuegan

    Fuegan

    Joined:
    Dec 5, 2012
    Posts:
    20
    You could replace your if statement by a lerp

    Code (CSharp):
    1. half4 mc = tex2D (_MainTex, IN.uv_MainTex);
    2. half4 sc = tex2D (_SecondTex, IN.uv_SecondTex);
    3. half4 bc = tex2D (_BlendTex, IN.uv_BlendTex);
    4. o.Albedo = mc.rgb * (1f - bc.rgb) + sc.rgb * bc.rgb;
    There I use a black and white blending texture. If the pixel is white th second texture is used and if the pixel is black the the main texture is used. This may not be the solution but I think it's better than the if statement.

    I hope that'll help.
     
  4. Glader

    Glader

    Joined:
    Aug 19, 2013
    Posts:
    449
    Hmmmm, well I don't necessarily want to use the emission texture based on a white or black pixel or alpha directly. I need an alpha cutoff-like effect so that I can finely control the amount of cracks in my lava cracked rocks seen here: https://i.gyazo.com/b5dc1b889c923d08e116df8099a4d730.mp4.

    The question I face now is if I were to find a way to remove the if/branch would it be more efficient? I'd have to do a texture lookup all the time for the emission texture.