Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by eMilk · Dec 03, 2009 at 01:36 PM · cameragui

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.

Comment
Add comment
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

3 Replies

· Add your reply
  • Sort: 
avatar image
4

Answer by robert · Dec 03, 2009 at 04:25 PM

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 {

CGPROGRAM #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; }

ENDCG

 }

}

}

Comment
Add comment · Show 3 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image Talemon · Jun 13, 2011 at 10:54 AM 0
Share

It works very well but i'm using this on a 3d plane in-game and it gets drawn on over everything, and doesn't take other objects into account. Is it possible for you to revise this so it doesn't get on top?

avatar image chelnok · Nov 08, 2012 at 12:12 AM 0
Share

Works great! Thank you :) @Talemon: like Peppercat says: "you have to replace the word "Overlay+1" in the shader with "Transparent"

avatar image homer_3 · May 30, 2013 at 10:24 PM 0
Share

Awesome! This is exactly what I've been looking for! Well, almost. This makes the bar go from right to left. For anyone interested, to make it go from left to right, change "color.a = i.uv.x < _Progress;" to "color.a = i.uv.x > (1.0 - _Progress);"

avatar image
0

Answer by jonas-echterhoff · Dec 03, 2009 at 01:57 PM

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.

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image
0

Answer by Peppercat · Oct 23, 2012 at 08:04 PM

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! :)

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

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image chelnok · Nov 08, 2012 at 12:10 AM 0
Share

Thank you Peppercat for the tip!

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

4 People are following this question.

avatar image avatar image avatar image avatar image

Related Questions

New Unity GUI has messed up the scene camera 2 Answers

How do I set up a GUI around multiple cameras? 1 Answer

how can I disable all camera movements trough GUI? 2 Answers

Webplayer bug after coming back from sleep mode 0 Answers

How to Create a Multi Floor Mini Map 1 Answer


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges