Gamieon is a one-developer part-time studio focused on creating video games for the desktop and mobile platforms. Since 2010 Gamieon has developed and released six games as well as six more prototypes. Including beta distributions, Gamieon's products have netted a combined 300,000 downloads! Below this bio are images from all my games and prototypes. Check them out and don't forget to follow me on Twitter @[Gamieon](members:gamieon:321769)

Report RSS Fast Diamond Shader For Unity Android

Posted by on

When developing Domino Arena, I experimented with using diamond/crystal shaders for the dominoes while deciding the look and feel of the game. I found a mobile diamond shader at Wiki.unity3d.com by BurningThumb. It worked great for most PC's (there was one report of the dominoes not rendering however), but it was slow on my Asus EEE tablet. I could not get a frame rate better than 30 fps during normal game play.

The Unity profiler revealed that much of the time spent working in each frame was with transparent rendering; partly because of the dominoes, and partly because I called GUI.DrawTexture() way too much (which I fixed).

I'm very inexperienced with shader development, but I figured the least I could try was commenting out all code in the shader that had the word "Transparent" in it. To my surprise it actually had the desired effect: The frame rate went back up to 60 fps, and the diamond dominoes still looked like diamond dominoes with an unexpected caveat: They look transparent where they did not before. I don't exactly know why (again, I'm not an experienced shader developer) but it wasn't a big deal since dominoes don't obstruct each other during normal game play anyway.

Here's the shader code I ended up with (you can tell which parts I commented out):

code:
Shader "FX/Diamond Mobile"
{
  Properties {
    _Color ("Color", Color) = (1,1,1,1)
    _Fog("Fog", Color) = (0,0,0,0)
    _ReflectTex ("Reflection Texture", Cube) = "dummy.jpg" {
      TexGen CubeReflect
    }
    _RefractTex ("Refraction Texture", Cube) = "dummy.jpg" {
      TexGen CubeReflect
    }
    _RefractTexlow ("Refraction LowGPU", 2D) = "dummy.jpg" {
      TexGen SphereMap
    }
    _ReflectTexlow ("Reflect LowGPU", 2D) = "dummy.jpg" {
      TexGen SphereMap
    }

    _Shininess ("Shininess", Range (0.01, 1)) = 0.7
        _SpecColor ("Specular", Color) = (1,1,1,1)
        _Emission ("Emissive", Color) = (1,1,1,1)
  }

  SubShader {
    //Tags {
    //"Queue" = "Transparent"
    //}
    // First pass - here we render the backfaces of the diamonds. Since those diamonds are more-or-less
    // convex objects, this is effectively rendering the inside of them
    Pass {
      Color (0,0,0,0)
      Offset  -1, -1
      Cull Front
      ZWrite Off
      SetTexture [_RefractTex] {
        constantColor [_Color]
        combine texture * constant, primary
      }
      SetTexture [_ReflectTex] {
        combine previous, previous +- texture
      }
    }

    // Second pass - here we render the front faces of the diamonds.
    Pass {
      Fog { Color (0,0,0,0)}
      ZWrite on
      Blend One One
      SetTexture [_RefractTex] {
        constantColor [_Color]
        combine texture * constant
      }
      SetTexture [_ReflectTex] {
        combine texture + previous, previous +- texture
      }
    }
  }

  // Older cards. Here we remove the bright specular highlight
  SubShader {
    //Tags{"Queue" = "Transparent"}
    // First pass - here we render the backfaces of the diamonds. Since those diamonds are more-or-less
    // convex objects, this is effectively rendering the inside of them
    Pass {
      Color (0,0,0,0)
      Cull Front
      SetTexture [_RefractTex] {
        constantColor [_Color]
        combine texture * constant, primary
      }
    }

    // Second pass - here we render the front faces of the diamonds.
         
    Pass {
      Fog { Color (0,0,0,0)}
      ZWrite on
      Blend DstColor Zero  
      SetTexture [_RefractTex] {
        constantColor [_Color]
        combine texture * constant
      }
    }
  }

/////////// Start iphone code////////////////
//This will cause a nice gem texture to be rendered using the low-GPU textures defined in the inspector//
//This section of the code is provided by BURNING THUMB SOFTWARE, 2010//

  SubShader {
        Pass {
         
    Lighting On
            SeparateSpecular On
         
      Color (0,0,0,0)
    //  Offset  -1, -1
      Cull Front
      //Blend OneMinusSrcAlpha One
      SetTexture [_ReflectTexlow] {
        constantColor [_Color]
        combine texture * constant, primary
      }
    }

    // Second pass - here we render the front faces of the diamonds.
    Pass {
   

      Fog { Color [_Fog]}
      ZWrite on
      Blend One One
      SetTexture [_RefractTexlow] {
        constantColor [_Emission]
        combine texture * constant
      }
    }
             
  }
 
  // We need this for shadows
  FallBack "Diffuse" 
}


If you want to see it in action, just check out my game Domino Arena when it comes to Android!

Check out my homepage and social feeds

And my projects!

Post a comment

Your comment will be anonymous unless you join the community. Or sign in with your social account: