Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We have updated the language to the Editor Terms based on feedback from our employees and community. Learn more.
    Dismiss Notice
  3. Join us on November 16th, 2023, between 1 pm and 9 pm CET for Ask the Experts Online on Discord and on Unity Discussions.
    Dismiss Notice

Hide mesh intersection

Discussion in 'Scripting' started by adnan-e94, Aug 25, 2015.

  1. adnan-e94

    adnan-e94

    Joined:
    Dec 17, 2012
    Posts:
    70
    I have 1 background mesh, which is pretty much just a plane (for now), and one generated mesh (not generated in Update())

    I made 2 types of mesh generation, first one:
    http://gfycat.com/CriminalGreenCollie

    Second one (instead of a circle mesh, its just the outline with a Z scale)
    http://gfycat.com/NervousSecondAurochs


    Now, I need to just hide parts of the background mesh which are intersecting with one of these 2 meshes (i made the second one for a boolean intersection approach, which proved way too slow for real-time, maybe a shader would help here? I have zero experience in making shaders)

    What would be the best approach for this?

    Solution:
    As the firsts reply by Gambit suggest, the solution was a stencil shader.
    Background shader:
    Code (CSharp):
    1. Shader "Custom/FogBackground"  {
    2.     Properties{
    3.         _Color("Main Color", Color) = (1,1,1,1)
    4.         _MainTex("Base (RGB)", 2D) = "white" {}
    5.     }
    6.         Category{
    7.         Lighting Off
    8.         ZWrite on
    9.         Cull Back
    10.         SubShader{
    11.  
    12.         Stencil{
    13.         Ref 1
    14.         Comp notequal
    15.         Pass Zero
    16.     }
    17.  
    18.         Tags{
    19.         "Queue" = "Geometry"
    20.         "LightMode" = "Always"
    21.         "RenderType" = "Opaque"
    22.         "IgnoreProjector" = "True"
    23.     }
    24.         Pass{
    25.         SetTexture[_MainTex]{
    26.         constantColor[_Color]
    27.         Combine texture * constant, texture * constant
    28.     }
    29.     }
    30.     }
    31.     }
    32. }
    Custom mesh shader:
    Code (CSharp):
    1. Shader "Custom/Vision" {
    2.  
    3.     Category{
    4.         Lighting Off
    5.         Cull Back
    6.         zwrite off
    7.  
    8.         SubShader{
    9.  
    10.         Colormask 0
    11.  
    12.         Stencil{
    13.         Ref 1
    14.         Comp always
    15.         Pass replace
    16.     }
    17.  
    18.         Tags{
    19.         "Queue" = "Geometry-1"
    20.         "LightMode" = "Always"
    21.         "RenderType" = "Opaque"
    22.         "IgnoreProjector" = "True"
    23.     }
    24.         Pass{
    25.     }
    26.     }
    27.     }
    28. }
    Result:
    http://gfycat.com/CoolPoliteAmericankestrel
     
    Last edited: Aug 25, 2015
  2. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    That is going to make things difficult...
    What you are trying to do can be done very well with Stenciling in your shaders. http://docs.unity3d.com/Manual/SL-Stencil.html

    Shader code is very difficult when you're starting to learn it, but it becomes super easy. To avoid this rough learning curve in this project, you could download the built-in shader code under on the same web page you download Unity, then add stenciling to those shaders.

    Graphics stuff is very touchy, let me know if you get stuck.
     
  3. adnan-e94

    adnan-e94

    Joined:
    Dec 17, 2012
    Posts:
    70
    Thank you very much!

    I'm nearly done, I reused this:
    http://stackoverflow.com/questions/27740164/using-stencil-buffer-to-make-windows

    And got it nearly working, but it isn't working from all distances/angles?
    http://gfycat.com/OblongAcademicAtlanticblackgoby

    Nevermind, fixed :) Check the OP for the shaders
     
    Last edited: Aug 25, 2015
  4. sacunha

    sacunha

    Joined:
    Apr 21, 2016
    Posts:
    148
    Hi.
    Any chance you could share this project?
    I can't reprocude your results :S