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
0
Question by FatWednesday · Sep 12, 2012 at 10:34 AM · shaderrenderingtimervertexlit

Vertex Lit Shaders

Hello folks,

Due to performance reasons on older iPads, certain changes in our scenes to improve performance included switching the rendering path over to vertex lit. Overall this seems to have provided the boost we needed, however it meant that one of our shaders no longer works.

We have a shader that uses a texture with a gradient, and floating point cuttoff value, the idea being that the gradient combined with the cuttoff value create a mask for the underlying texture. This was done in a surface shader before. but I'm trying now to convert it over to a fragment and vertex shader to work with the vertex lit rendering path....I'm not succeeding.

Here is the original code from the surface shader.

 Shader "Custom/TimerSurface" 
 {
     Properties 
     {
         _Colour ("Color", Color) = (0.0, 0.0, 0.0)
         _GradientMask ("Gradient", 2D) = "black" {}
         _MainTex ("Main Texutre", 2D) = "gray" {}
         _Cutoff ("Cutoff Value", Range(0.0,1.0)) = 0.0
     }
 
     SubShader 
     {
         Tags {"Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent"}
         LOD 200
         
         CGPROGRAM
         #pragma surface surf Lambert alpha
 
         sampler2D _MainTex;
         sampler2D _GradientMask;
         fixed4 _Colour;
         fixed _Cutoff;
 
         struct Input 
         {
             half2 uv_MainTex;
         };
 
         void surf (Input IN, inout SurfaceOutput o)
         {
             fixed alpha = tex2D(_MainTex, IN.uv_MainTex).a;
             fixed grad = tex2D(_GradientMask, IN.uv_MainTex).r;
             o.Albedo = _Colour;
             if(grad >= _Cutoff && alpha > 0.95)
             {
                 o.Alpha = alpha;
             }
             else
             {
                 o.Alpha = 0;
             }
         }
         ENDCG
     } 
     FallBack "VertexLit"
 }
 

And here was my attempt to convert it to a vert+frag shader.

 Shader "Custom/Timer Vert+Frag" 
 {
     Properties 
     {
         _Colour ("Color", Color) = (0.0, 0.0, 0.0)
         _GradientMask ("Gradient", 2D) = "black" {}
         _MainTex ("Main Texture", 2D) = "gray" {}
         _Cutoff ("Cutoff Value", Range(0.0,1.0)) = 0.0
     }
 
     SubShader 
     {
         Tags {"Queue"="AlphaTest"}
         Pass
         {
             CGPROGRAM
             #pragma vertex vert
             #pragma fragment frag
             #include "UnityCG.cginc"
                         
             sampler2D _MainTex;
             sampler2D _GradientMask;
             fixed4 _Colour;
             fixed _Cutoff;
             float4 _MainTex_ST;
             float4 _GradientMask_ST;
 
             struct v2f 
             {
                 float4 pos : SV_POSITION;
                 float2  uv : TEXCOORD0;
             };
 
             v2f vert (appdata_base v)
             {
                 v2f o;
                 o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
                 o.uv = TRANSFORM_TEX (v.texcoord, _MainTex);
                 return o;
             }
 
             half4 frag (v2f i) : COLOR
             {
                 fixed alpha = tex2D(_MainTex, i.uv).a;
                 fixed grad = tex2D(_GradientMask, i.uv).r;
                 if(grad < _Cutoff || alpha < 0.95)
                 {
                     alpha = 0;
                 }
                 return half4(_Colour.r, _Colour.g, _Colour.b, alpha);
             }
 
             ENDCG
         }
     } 
     FallBack "VertexLit"
 }

I am by no means a shader programmer, so I wouldn't be surprised if there are a number of things I'm doing here that could be done better (if so I'd really appreciate you point them out + suggest how to change them).

Attached are the Gradient and main textures that I'm using. the material is then just applied to a simple Plane mesh.

[1]: /storage/temp/3423-circle_timer.png

circle_timer.png (4.0 kB)
circle_grad.png (6.6 kB)
Comment
Add comment · Show 2
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 FatWednesday · Sep 12, 2012 at 02:00 PM 0
Share

It's a step forward, but it does raise other issues (perhaps I should have mentioned this in the original post, I didn't realise it would cause an issue)

EDIT: This additional depth buffer issue has been fixed, For those curious I just replaced the Alpha = 0; line with a discard command.

avatar image FatWednesday · Sep 14, 2012 at 11:51 AM 0
Share

also mike if you want to post that comment as an answer I can mark it as correct.

1 Reply

· Add your reply
  • Sort: 
avatar image
3
Best Answer

Answer by push_Mike · Sep 12, 2012 at 01:20 PM

Everything you wrote is correct, Just add the Blend Command in the Top of Tags

Blend SrcAlpha OneMinusSrcAlpha

It Will Work

Thanks

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

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

10 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

Using Color.Lerp with Lightweight Render Pipeline 1 Answer

Layer Dependent Reflections 1 Answer

how to do a simple multi-pass rendering? 0 Answers

Forward Rendering for Terrain, overriding the shader? 0 Answers

How to remove URP from the project 0 Answers


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