Hello Guest  /  Join  /  Sign In  /  Get Desura
Avatar

Gamieon

Christopher joined Mar 14, 2009

Offline Since
Jan 18, 2014
Country
United States United States
Gender
Male
Homepage
Gamieon.com
Contact
Private Message
Email
Members Only
ICQ
387263084
AIM
DrgnSlyr188

Red Orchestra Level

12

Gamieon is a privately owned entertainment software development company located in Tampa, Florida. Since October of 2004, we have aimed to provide quality video game software which emphasizes both intellectual and action-driven challenge to the gaming community. Gamieon depends on the talent of individuals working as a team to develop video games and video game engines with a focus on exceptional game play and surrealism.

Blog RSS Report abuse Fast Diamond Shader For Unity Android

0 comments by Gamieon on Nov 14th, 2013

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!

Comments
ArtisanCodesmith
ArtisanCodesmith Sep 24 2012, 7:38am said:

I'm new to IndieDB and ModDB and wish to invite a few people (random people picked from the online list lol) to view my game and start generating a few opinions, do you mind taking a quick look? The game is called Arkanius. Thanks, Artisan Codesmith. :)

+2 votes     reply to
AlexVSharp
AlexVSharp Jan 30 2012, 6:07pm said:

Wow, well written. You should include this article in the Unity Dev's group. ;)

+3 votes     reply to
Post a Comment
click to sign in and comment

Only registered members can share their thoughts. So come on! Join the community today (totally free - or sign in with your social account on the right) and join in the conversation.

Desura
Desura Official group with 11,167 members
Unity devs
Unity devs Hobbies & Interests group with 1,033 members
Desura Publishing
Desura Publishing Official group with 183 members
Gamieon
Gamieon Developer with 4 members
Friends
Namelessness
Namelessness friends since Apr 26, 2013
swiftseraph
swiftseraph friends since Jun 3, 2011
Original_Speeder
Original_Speeder friends since Jun 4, 2011
hermesdavidms
hermesdavidms friends since Jan 2, 2011
shaulhadar
shaulhadar friends since Nov 29, 2011
NullSoldier
NullSoldier friends since Jun 4, 2011
Accolades
Desura
Statistics
Activity Points
879
Rank
2,192 of 456,728
Watchers
5 members
Time Online
13 hours
Comments
15
Site Visits
2,000
Profile Visitors
52,118 (1 today)
Twitter
Latest tweets from @gamieon
#hamsterchase officially held a featured spot on the Windows Phone store! Now back to fixing bugs... T.co Jul 11 2014, 7:37pm
#hamsterchase is now on the Windows Phone Store! T.co Jun 29 2014, 12:08pm
Just submitted #hamsterchase to the Windows Phone store...will do some last minute testing before publishing. Jun 27 2014, 5:08pm
Finally got ads and in-apps working in #hamsterchase for Windows Phone 8. Final testing time! Jun 23 2014, 9:36pm
Got #hamsterchase to run fast on my Windows phone...but no luck with in-apps, ads and Facebook integration. Everything is a klunky beta! Jun 17 2014, 8:54pm
Initial deployment of #hamsterchase to my Windows Phone running super slow...not a good sign... May 31 2014, 9:23pm
Just upgraded to #unity3d 4.5; now investigating releasing #hyperspacepinball onto the Windows 8 Store too! May 28 2014, 1:49pm
Just now got #hamsterchase working in a #WindowsPhone emulator, too! T.co May 23 2014, 9:43pm
Just got #hyperspacepinball working on a #WindowsPhone emulator! Stay tuned for updates... T.co May 16 2014, 5:04pm
Try out the #papercowboys alpha preview! Details and screenshots at T.co May 6 2014, 4:55pm