Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 12 Next capture
2021 2022 2023
1 capture
12 Jun 22 - 12 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 ImFromTheFuture · Jun 26, 2017 at 04:15 PM · meshshadersinvisiblestencilintersection

Unity 5.5: Mesh Intersection to be rendered invisible with shader? (Pictures)

I'm trying to make a single shader that checks if it has collided with a mesh (with the same shader), if it has, then it should not render that collided part at all. For both meshes.

I have used a crosssection shader to achieve this but it's not what I want: This is what I get

This is the shader code:

 // Upgrade NOTE: replaced 'glstate.matrix.invtrans.modelview[0]' with 'UNITY_MATRIX_IT_MV'
 // Upgrade NOTE: replaced 'glstate.matrix.mvp' with 'UNITY_MATRIX_MVP'
 
 // 2 Pass Edition
 
 Shader "cross_section_v004a"
 {
     Properties 
     {
         section_depth ("section depth (x, y, z, depth)", vector) = (0,0,0,0.15)
         section_color ("section color", color) = (0.5,0.1, 0.1, 1)
 
         color_map ("color map", 2D) = "white" {}
 
     }   
     SubShader 
     {   
         Pass
         {           
             CULL OFF
 
 CGPROGRAM //--------------
 //#pragma target 3.0
 #pragma vertex   vertex_shader
 #pragma fragment fragment_shader
 
 #include "UnityCG.cginc"
 
 uniform float4 section_depth;
 uniform float4 section_color;
 uniform sampler2D color_map;    
 
 float4x4 rotate(float3 r) 
 { 
     float3 c, s; 
     sincos(r.x, s.x, c.x); 
     sincos(r.y, s.y, c.y); 
     sincos(r.z, s.z, c.z);
     return float4x4( c.y*c.z,    -s.z,     s.y, 0, 
                          s.z, c.x*c.z,    -s.x, 0, 
                         -s.y,     s.x, c.x*c.y, 0, 
                            0,       0,       0, 1 );
 } 
 
 struct a2v 
 {
     float4 vertex   : POSITION;
     float4 color    : COLOR;
     float2 texcoord : TEXCOORD;
     float3 normal   : NORMAL;
 };
 
 struct v2f
 {
     float4 position : POSITION;
     float2 texcoord : TEXCOORD0;                                
     float4 normal   : TEXCOORD1; 
     float4 vertex   : TEXCOORD2;
     float4 mask     : TEXCOORD3;                
 };
 
 v2f vertex_shader( a2v IN )
 {                                                   
     v2f OUT;
 
     float4x4 r   = rotate(radians(section_depth.xyz) +_SinTime.xyz);
     float4 c     = float4(IN.vertex.xyz,1);
 
     OUT.mask     = mul(r, c);
     OUT.position = mul(UNITY_MATRIX_MVP, IN.vertex);
     OUT.texcoord = IN.texcoord;
 
     r *= float4x4(   1,-1,-1, 0,
                     -1, 1,-1, 0,
                     -1,-1, 1, 0,
                      0, 0, 0, 1 ); // the section_depth.xyz need to be inverted !
 
     OUT.normal   = mul(r, float4(1,0,0,1));
     OUT.vertex   = IN.vertex;
 
     return OUT;
 }
 
 void fragment_shader(   v2f IN,
                         out float4 finalcolor : COLOR)  
 
 {
     if(IN.mask.x > section_depth.w)
         discard;
 
     float3 N = IN.normal.xyz;
 
     N = mul(UNITY_MATRIX_IT_MV, float4(N, 1));              
     //float diffuse = saturate(dot(glstate.light[0].position, N));  
 
     finalcolor = float4(0,0,0,1);                               
     finalcolor.xyz = section_color *(0.6 +0.4);
 }
 ENDCG //--------------
 
         } // Pass
 
 //---------------------------------------------------------------------------------
 
         Pass
         {           
             CULL BACK
 
 CGPROGRAM //--------------
 #pragma vertex   vertex_shader
 #pragma fragment fragment_shader
 
 #include "UnityCG.cginc"
 
 uniform float4 section_depth;
 uniform float4 section_color;
 uniform sampler2D color_map;    
 
 float4x4 rotate(float3 r) 
 { 
     float3 c, s; 
     sincos(r.x, s.x, c.x); 
     sincos(r.y, s.y, c.y); 
     sincos(r.z, s.z, c.z);
     return float4x4( c.y*c.z,    -s.z,     s.y, 0, 
                          s.z, c.x*c.z,    -s.x, 0, 
                         -s.y,     s.x, c.x*c.y, 0, 
                            0,       0,       0, 1 );
 } 
 
 struct a2v 
 {
     float4 vertex   : POSITION;
     float4 color    : COLOR;
     float2 texcoord : TEXCOORD;
     float3 normal   : NORMAL;
 };
 
 struct v2f
 {
     float4 position : POSITION;
     float2 texcoord : TEXCOORD0;                                
     float3 normal   : TEXCOORD1; 
     float4 vertex   : TEXCOORD2;
     float4 mask     : TEXCOORD3;                
 };
 
 v2f vertex_shader( a2v IN )
 {                                                   
     v2f OUT;
 
     float4x4 r   = rotate(radians(section_depth.xyz) +_SinTime.xyz);
     float4 c     = float4(IN.vertex.xyz,1);
 
     OUT.mask     = mul(r, c);
     OUT.position = mul(UNITY_MATRIX_MVP, IN.vertex);
     OUT.texcoord = IN.texcoord;
     OUT.normal   = IN.normal;
     OUT.vertex   = IN.vertex;
 
     return OUT;
 }
 
 void fragment_shader(   v2f IN,
                         out float4 finalcolor : COLOR)  
 
 {
     if(IN.mask.x > section_depth.w)
         discard;
 
     float3 N = IN.normal;
 
     N = mul(UNITY_MATRIX_IT_MV, float4(N, 1));              
     //float diffuse = saturate(dot(glstate.light[0].position, N));
 
     finalcolor = float4(0,0,0,1);                                           
     finalcolor.xyz = tex2D(color_map, IN.texcoord).xyz *(0.6 +0.4);
 }
 ENDCG //--------------
 
         } // Pass
 
     } // SubShader
 } // Shader

However, the rest of the object should be visible, I mean to say that I don't want the parts that are NOT getting intersected to be invisible (when viewed from the invisible parts) so Stencil shaders won't or aren't doing the trick. I mean to get something like this: I want to get this

I want to know what is the correct way to approach to this problem. I'm fairly new to shader programming and I don't know to solve this issue. Any help would be appreciated.

Thanks!

whatiget.png (3.0 kB)
unity-2017-06-26-16-33-49.png (97.8 kB)
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

0 Replies

· Add your reply
  • Sort: 

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

75 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 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 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 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

Adding a Stencil to Particle Standard Surface shader 0 Answers

Add to existing shader at runtime 1 Answer

Standard Shader Still Visible through Stencil Shader 0 Answers

Problems importing plane mesh from blender to unity 1 Answer

How to show only the intersection between two meshes 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