Reveal Non-Uniform Texture

From Unify Community Wiki
Jump to: navigation, search
Reveal Texture in action!

Author: Joe Schultz

Contents

Description

This simple shader & script is useful if you want to make a loading or progress bar using textures that are not rectilinear (i.e. boring). There's probably an easier / better way to do this, but this is what I came up with and it works fine. This is my first shader, so if you can refine it and make it better, please do!

Should work on vertex program capable hardware (Radeon 8500, GeForce3/4Ti, Intel 9xx) no problemo (though untested).

Usage

Use this shader on a base object, set the textures you want to use in the material properties, and finally use the script below (or similar) to "reveal" your base texture, based on some game parameter (percentage complete, love amount, etc.).

  • Create a material that uses the shader below (GUI/Reveal _Alpha).
  • Use this material on an object.
  • Set the Base (RGB) texture slot to the texture that you want to reveal (usually your colorful progress bar).
  • Set the Mix Mask (A) texture slot to the texture that you want to use to reveal; only the alpha of this texture matters, so get the alpha channel as you want the reveal edge to be. You can do jagged reveals, smooth blended, etc. (as you wish!)
  • NOTE: Mix Mask (A) texture should be set to Clamp via the texture properties in the Inspector for most use cases.
  • Update the Offset of the Mix Mask (A) texture in a script, such as the one provided below.
  • Enjoy!

Example

Reveal Texture shader in action! The pain bar (skeleton) to the left uses this shader. Larger version here...Updated image here

  • Reveal Texture shader in action! The pain bar (skeleton) to the left uses this shader.

ShaderLab - RevealTexture_Alpha.shader

Invalid language.

You need to specify a language like this: <source lang="html4strict">...</source>

Supported languages for syntax highlighting:

4cs, 6502acme, 6502kickass, 6502tasm, 68000devpac, abap, actionscript, actionscript3, ada, algol68, apache, applescript, apt_sources, asm, asp, autoconf, autohotkey, autoit, avisynth, awk, bascomavr, bash, basic4gl, bf, bibtex, blitzbasic, bnf, boo, c, c_loadrunner, c_mac, caddcl, cadlisp, cfdg, cfm, chaiscript, cil, clojure, cmake, cobol, coffeescript, cpp, cpp-qt, csharp, css, cuesheet, d, dcs, delphi, diff, div, dos, dot, e, ecmascript, eiffel, email, epc, erlang, euphoria, f1, falcon, fo, fortran, freebasic, fsharp, gambas, gdb, genero, genie, gettext, glsl, gml, gnuplot, go, groovy, gwbasic, haskell, hicest, hq9plus, html4strict, html5, icon, idl, ini, inno, intercal, io, j, java, java5, javascript, jquery, kixtart, klonec, klonecpp, latex, lb, lisp, llvm, locobasic, logtalk, lolcode, lotusformulas, lotusscript, lscript, lsl2, lua, m68k, magiksf, make, mapbasic, matlab, mirc, mmix, modula2, modula3, mpasm, mxml, mysql, newlisp, nsis, oberon2, objc, objeck, ocaml, ocaml-brief, oobas, oracle11, oracle8, oxygene, oz, pascal, pcre, per, perl, perl6, pf, php, php-brief, pic16, pike, pixelbender, pli, plsql, postgresql, povray, powerbuilder, powershell, proftpd, progress, prolog, properties, providex, purebasic, pycon, python, q, qbasic, rails, rebol, reg, robots, rpmspec, rsplus, ruby, sas, scala, scheme, scilab, sdlbasic, smalltalk, smarty, sql, systemverilog, tcl, teraterm, text, thinbasic, tsql, typoscript, unicon, uscript, vala, vb, vbnet, verilog, vhdl, vim, visualfoxpro, visualprolog, whitespace, whois, winbatch, xbasic, xml, xorg_conf, xpp, yaml, z80, zxbasic


Shader "GUI/Reveal _Alpha"
{
Properties {
	_Color ("Main Color", Color) = (1,1,1,0.5)
	_MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
	_Mask ("Mix Mask (A)", 2D) = "white" {}
}
SubShader {
//  ZWrite Off
//    Alphatest Greater 0
    Tags {Queue=Transparent}
   	Lighting Off Cull Off ZTest Always ZWrite Off Fog { Mode Off }

    Blend SrcAlpha OneMinusSrcAlpha  
	Pass {
//		Lighting On
		
		Material {
			Diffuse [_Color]
//			Ambient [_Color]
//			Emission [_PPLAmbient]
		}
		SetTexture [_MainTex] {
			constantColor [_Color]
			combine texture * constant, texture * constant
		}
		SetTexture [_Mask] {
			combine previous, texture
		}
		SetTexture [_MainTex] {
			combine previous * texture
		}
	}
}

FallBack " VertexLit", 1

}

JavaScript - Reveal.js

function Update()
{
	var revealOffset = someVar.actualValue / someVar.actualValueMax; 
 
	gameObject.renderer.material.SetTextureOffset ("_Mask", Vector2(0, revealOffset));
 
}

ShaderLab - Alternate RevealTexture_Alpha.shader

In this alternate shader, I decided to use a Surface Shader (which allows for lighting support and tinting with very few steps, you can fake normals ad hoc since the sprite would be always facing the camera) and keep the pipeline premultiplied alpha friendly. This shader could be a bit faster than the fixed function inspired one due to the lower texture fetches and color combining operations. Note: with surface shaders you must specify a custom lighting function to avoid the system messing up with elements without proper vertex normal data available.

Invalid language.

You need to specify a language like this: <source lang="html4strict">...</source>

Supported languages for syntax highlighting:

4cs, 6502acme, 6502kickass, 6502tasm, 68000devpac, abap, actionscript, actionscript3, ada, algol68, apache, applescript, apt_sources, asm, asp, autoconf, autohotkey, autoit, avisynth, awk, bascomavr, bash, basic4gl, bf, bibtex, blitzbasic, bnf, boo, c, c_loadrunner, c_mac, caddcl, cadlisp, cfdg, cfm, chaiscript, cil, clojure, cmake, cobol, coffeescript, cpp, cpp-qt, csharp, css, cuesheet, d, dcs, delphi, diff, div, dos, dot, e, ecmascript, eiffel, email, epc, erlang, euphoria, f1, falcon, fo, fortran, freebasic, fsharp, gambas, gdb, genero, genie, gettext, glsl, gml, gnuplot, go, groovy, gwbasic, haskell, hicest, hq9plus, html4strict, html5, icon, idl, ini, inno, intercal, io, j, java, java5, javascript, jquery, kixtart, klonec, klonecpp, latex, lb, lisp, llvm, locobasic, logtalk, lolcode, lotusformulas, lotusscript, lscript, lsl2, lua, m68k, magiksf, make, mapbasic, matlab, mirc, mmix, modula2, modula3, mpasm, mxml, mysql, newlisp, nsis, oberon2, objc, objeck, ocaml, ocaml-brief, oobas, oracle11, oracle8, oxygene, oz, pascal, pcre, per, perl, perl6, pf, php, php-brief, pic16, pike, pixelbender, pli, plsql, postgresql, povray, powerbuilder, powershell, proftpd, progress, prolog, properties, providex, purebasic, pycon, python, q, qbasic, rails, rebol, reg, robots, rpmspec, rsplus, ruby, sas, scala, scheme, scilab, sdlbasic, smalltalk, smarty, sql, systemverilog, tcl, teraterm, text, thinbasic, tsql, typoscript, unicon, uscript, vala, vb, vbnet, verilog, vhdl, vim, visualfoxpro, visualprolog, whitespace, whois, winbatch, xbasic, xml, xorg_conf, xpp, yaml, z80, zxbasic


Shader "GUI/Reveal _Alpha_Surface"
{
	Properties {
	  _MainTex ("Sprite Texture", 2D) = "white" {}
	  _Mask ("Mix Mask (A)", 2D) = "white" {}
	}
	SubShader {
        Tags { 
        	"Queue" = "Transparent" 
            "IgnoreProjector"="True" 
            "RenderType"="Transparent" 
        }
        Cull Off
        ZWrite Off // don't write to depth buffer in order not to occlude other objects
	    Lighting Off
	    Blend One OneMinusSrcAlpha
	      CGPROGRAM
		      #pragma surface surf SpritesModel
		      half4 LightingSpritesModel (SurfaceOutput s, half3 lightDir, half atten) {
		          half4 c;
		          c.rgb = s.Albedo;
		          c.a = s.Alpha;
		          return c;
		      }
		      
		      struct Input {
		          float2 uv_MainTex; 
		          float2 uv_Mask;
		      };
		      
		      sampler2D _MainTex;
		      sampler2D _Mask;
		      void surf (Input IN, inout SurfaceOutput o) {
		      	  half maskAlpha = tex2D(_Mask, IN.uv_Mask).a;
		          half4 col = tex2D (_MainTex, IN.uv_MainTex);
		          
		          o.Albedo = col.rgb * maskAlpha; //Let's premultiply the color channel.
		          o.Alpha = maskAlpha;
		      }
	      ENDCG
    } 
    Fallback "Diffuse"
}

History

  • Looks like I originally had pasted an old version. Here is the latest that actually uses Diffuse _Color. Still would like to figure a way to use the diffuse color's Alpha, but don't have a pressing need at the moment.
Personal tools
Namespaces

Variants
Actions
Navigation
Extras
Tools