• 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
Question by Ehsan · Apr 25, 2012 at 05:13 PM · shadermeshinvisiblehide

Hiding parts of a mesh

Hi, In my project i want to have a cube that when it is located in front of other Gameobjects it will cause the Gameobjects won't be seen. in addition, this cube is moving in the scene and when it passes front of a mesh, those parts of mesh that are behind the cube shouldn't be seen. Is it possible? Thanks a lot.

Comment
BiG
gamberetto

People who like this

2 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 Lo0NuhtiK · Apr 25, 2012 at 05:17 PM 0
Share

Well, so long as this cube isn't invisible then when it passes in front of another object between said object and camera then you shouldn't be able to see what's behind it...

avatar image Ehsan · Apr 25, 2012 at 05:26 PM 0
Share

I forgot to say that the cube is invisible and this is my problem

1 Reply

  • Sort: 
avatar image

Answer by Piflik · Apr 25, 2012 at 05:33 PM

If I understand you correctly, try this shader:

 Shader "Matte" {
     Properties {
     }
     
     SubShader {
         Tags {
             "Queue"="Background"
             "IgnoreProjector"="False"
             "RenderType"="Opaque"
         }

         
         Cull Off
         ZWrite On
         ZTest LEqual
         ColorMask RGBA
         Blend One One
         Fog {
         }


         CGPROGRAM
         #pragma surface surf BlinnPhongEditor  vertex:vert
         #pragma target 2.0



             struct EditorSurfaceOutput {
                 half3 Albedo;
                 half3 Normal;
                 half3 Emission;
                 half3 Gloss;
                 half Specular;
                 half Alpha;
                 half4 Custom;
             };
             
             inline half4 LightingBlinnPhongEditor_PrePass (EditorSurfaceOutput s, half4 light) {
                 half3 spec = light.a * s.Gloss;
                 half4 c;
                 c.rgb = (s.Albedo * light.rgb + light.rgb * spec);
                 c.a = s.Alpha;
                 return c;
             }

             inline half4 LightingBlinnPhongEditor (EditorSurfaceOutput s, half3 lightDir, half3 viewDir, half atten) {
                 half3 h = normalize (lightDir + viewDir);
                 
                 half diff = max (0, dot ( lightDir, s.Normal ));
                 
                 float nh = max (0, dot (s.Normal, h));
                 float spec = pow (nh, s.Specular*128.0);
                 
                 half4 res;
                 res.rgb = _LightColor0.rgb * diff;
                 res.w = spec * Luminance (_LightColor0.rgb);
                 res *= atten * 2.0;

                 return LightingBlinnPhongEditor_PrePass( s, res );
             }
             
             struct Input {
                 float4 color : COLOR;
             };

             void vert (inout appdata_full v, out Input o) {
             float4 VertexOutputMaster0_0_NoInput = float4(0,0,0,0);
             float4 VertexOutputMaster0_1_NoInput = float4(0,0,0,0);
             float4 VertexOutputMaster0_2_NoInput = float4(0,0,0,0);
             float4 VertexOutputMaster0_3_NoInput = float4(0,0,0,0);
             }
             

             void surf (Input IN, inout EditorSurfaceOutput o) {
                 o.Normal = float3(0.0,0.0,1.0);
                 o.Alpha = 1.0;
                 o.Albedo = 0.0;
                 o.Emission = 0.0;
                 o.Gloss = 0.0;
                 o.Specular = 0.0;
                 o.Custom = 0.0;
                 
                 float4 Master0_0_NoInput = float4(0,0,0,0);
                 float4 Master0_1_NoInput = float4(0,0,1,1);
                 float4 Master0_2_NoInput = float4(0,0,0,0);
                 float4 Master0_3_NoInput = float4(0,0,0,0);
                 float4 Master0_4_NoInput = float4(0,0,0,0);
                 float4 Master0_5_NoInput = float4(1,1,1,1);
                 float4 Master0_7_NoInput = float4(0,0,0,0);
                 float4 Master0_6_NoInput = float4(1,1,1,1);

                 o.Normal = normalize(o.Normal);
             }
         ENDCG
     }
     Fallback "Diffuse"
 }

(Done with Strumpy's Shader Editor, so there might be some redundant code there, since I don't understand half of it)

Comment
Ehsan
Lo0NuhtiK
BiG
gamberetto

People who like this

4 Show 4 · 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 Ehsan · Apr 25, 2012 at 05:47 PM 0
Share

Thanks a lot Philipp, That is exactly what i want. Good luck

avatar image Ehsan · Apr 25, 2012 at 08:23 PM 0
Share

A problem. In unity editor this shader works good and hides behind of cube, but when i play the scene the cube surfaces stretch the view and aren't same as what i can see in the editor.why these aren't same? how can i do it?

avatar image Piflik · Apr 25, 2012 at 08:46 PM 0
Share

Ok...I see the problem, but sadly I have no idea how to solve it. I found this though, maybe it works better.

 Shader "FX/Matte Shadow" {
 Properties {
     _Color ("Main Color", Color) = (1,1,1,1)
     _MainTex ("Base (RGB) Trans (A)", 2D) = "white" {}
     
 }

 Category {
     
     Tags {"Queue"="Geometry+500"}
     LOD 200
     Alphatest Greater 0
     ZWrite Off
     AlphaToMask True
     ColorMask RGB
     Fog { Color [_AddFog] }
     Blend DstColor Zero
     
     SubShader {
         
         Pass {    
             Tags { "LightMode" = "Pixel" }

 CGPROGRAM
 #pragma fragment frag
 #pragma vertex vert
 #pragma multi_compile_builtin
 #pragma fragmentoption ARB_fog_exp2
 #pragma fragmentoption ARB_precision_hint_fastest
 #include "UnityCG.cginc"
 #include "AutoLight.cginc"

 struct v2f {
     V2F_POS_FOG;
     LIGHTING_COORDS
     float2    uv;
     float3    normal;
     float3    lightDir;
 };

 uniform float4 _MainTex_ST;

 v2f vert (appdata_base v)
 {
     v2f o;
     PositionFog( v.vertex, o.pos, o.fog );
     o.normal = v.normal;
     o.uv = TRANSFORM_TEX(v.texcoord,_MainTex);
     o.lightDir = ObjSpaceLightDir( v.vertex );
     TRANSFER_VERTEX_TO_FRAGMENT(o);
     return o;
 }

 uniform sampler2D _MainTex;
 uniform float4 _Color;

 float4 frag (v2f i) : COLOR
 {
     half4 c = _Color * 4 * LIGHT_ATTENUATION(i);
     c.a = (1-LIGHT_ATTENUATION(i));
     return c;
 }
 ENDCG

         }
     }
 }

 Fallback "Transparent/Cutout/VertexLit", 2

 }
avatar image Ehsan · Apr 25, 2012 at 09:00 PM 0
Share

thanks Philipp, but it doesn't work properly

Unity Answers is in Read-Only mode

Unity Answers content will be migrated to a new Community platform and we are aiming to launch a public beta by June 9. Please note, Unity Answers is now in read-only so we can prepare for the final data migration.

For more information and updates, please read our full announcement thread in the Unity Forum.

Follow this Question

Answers Answers and Comments

5 People are following this question.

avatar image avatar image avatar image avatar image avatar image

Related Questions

Is there any way to hide only part of an object from the camera? (unity free) 2 Answers

Objects Not Visible In Low End Computer. (Shader Error) 1 Answer

Invisible effect? 3 Answers

[ problem ] transparent shader !!! 1 Answer

Mesh with copied material not showing in Game View and Forward Rendering Path 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