How do I mask off specific parts from a plane or cube?

I've been messing around with various methods to create an energymeter-like GUI-element.

This is what I would like to generate:

alt text

As I see it, I have two options:

  • Create a sprite for each level the blue bar could be at.

  • Create the blue bar using a plane/cube inside Unity that I scale along the Y-axis, with the grey lines in front of the blue bar, and the "empty" texture as the bottom-layer.

Solution #2 would be the best in my opinion, but how should i do this? I've tried to do this with this culling-shader, but that only generates this result:

alt text

In other words; the masking isn't happening dynamically - as when I scale it, the culling texture is scaled as well.

Can anyone help me out? Am I trying to solve this in all the wrong ways?

Thank you for your time.

You could use the masking shader you linked to with success, but the mask texture would have to contain a ramp from white to black and you would be controlling the status (the energy percentage or whatnot) by setting the _Cutoff property of the material from a script.

Or you can forget the ramp texture and use the shader pasted below instead. :)

To use it, create a material using that shader, drop it on a quad (with good UVs of course) and assign the texture of the blue bar to it (the texture's alpha channel will be nicely taken into account as well). You can now take a look at the material in the inspector and slide the "Progress" slider to see what happens.

From a script you would control the meter by calling:

renderer.material.SetFloat("_Progress", myCurrentEnergyLevelBetween0and1);

And here's the shader:

Shader "Custom/ProgressBar" {

Properties {
    _Color ("Color", Color) = (1,1,1,1)
    _MainTex ("Main Tex (RGBA)", 2D) = "white" {}
    _Progress ("Progress", Range(0.0,1.0)) = 0.0

SubShader {
    	Tags { "Queue"="Overlay+1" }
    	ZTest Always
    	Blend SrcAlpha OneMinusSrcAlpha
        Pass {

#pragma vertex vert
#pragma fragment frag
#include "UnityCG.cginc"

uniform sampler2D _MainTex;
uniform float4 _Color;
uniform float _Progress;

struct v2f {
    float4 pos : POSITION;
    float2 uv : TEXCOORD0;

v2f vert (appdata_base v)
    v2f o;
    o.pos = mul (glstate.matrix.mvp, v.vertex);
    o.uv = TRANSFORM_UV(0);

    return o;

half4 frag( v2f i ) : COLOR
    half4 color = tex2D( _MainTex, i.uv);
    color.a *= i.uv.x < _Progress;
    return color*_Color;




You could create a custom mesh of a plane, and adjust the UVs to only show the relevant parts of the texture. Then you can draw that, either manually, using Graphics.DrawMeshNow, or using a normal MeshRenderer component.

Hi Talemon! Maybe my answer it’s a little late, but you have to replace the word “Overlay+1” in the shader with “Transparent”, so your plane respect its position in the render. I hope this helps! :slight_smile:

An thanks, Robert, for the shader, it’s working perfectly :3