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 MakakWasTaken · Mar 31, 2015 at 11:05 AM · shadercgcull

Cull shader

How can I make a shader that doesn't render itself if the distance from the camera is greater than something, here is what I have in the subshader for now:

 SubShader
 {
     Tags { "Queue"="Transparent" "IgnoreProjector"="True" "RenderType"="Transparent" }
     Blend SrcAlpha OneMinusSrcAlpha
     AlphaTest Greater .01

     LOD 200
    
     CGPROGRAM
     #pragma surface surf Lambert vertex:vert
     #include "UnityCG.cginc"
            
     uniform sampler2D _MainTex;
     uniform float4 _Wind;
     uniform fixed4 _Color;
     uniform float _CullDistance;

     struct Input {
         float2 uv_MainTex;
     };

     void vert(inout appdata_full v)
     {
         v.vertex.z += (sin(_Wind.x )/5.0) * v.color.r;
         v.vertex.x += (sin(_Wind.y )/5.0) * v.color.r;
     }

    
     void surf (Input IN, inout SurfaceOutput o) {
         fixed4 difTex = tex2D(_MainTex, IN.uv_MainTex);

         o.Albedo = difTex.rgb * _Color.rgb;
         o.Alpha = difTex.a;
     }

        
   ENDCG
   
 }
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

1 Reply

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

Answer by joelv · Mar 31, 2015 at 11:24 AM

If you want to do it on a per fragment level you can use the clip function which discards fragments that does not match a condition.

 clip(threshold - depth);

will discard pixels where depth is greater than the threshold (value to clip is less than 0).

Comment
Add comment · Show 2 · 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 MakakWasTaken · Mar 31, 2015 at 11:28 AM 0
Share

How can I use this in a surface shader?

  • $$anonymous$$arkus

avatar image joelv · Mar 31, 2015 at 11:50 AM 0
Share

As the surface shader is part of the fragment shader you can clip from within there.

 clip(difTex.r - 0.5f);

works in your example shader for example.

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

20 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 avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

Related Questions

How to force the compilation of a shader in Unity? 5 Answers

TEXCOORD 0 1 2 3 for cg shader scripting? 1 Answer

Multiple tex2D commands in fragment shader needed, is there a workaround? 0 Answers

Custom ambient lighting color in a Cg shader 2 Answers

Can I read `CGPROGRAM` `#define` constants in `.cs` at runtime? 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