Search Unity

Selective Object Cutting/Clipping

Discussion in 'Shaders' started by rob_vld, Jan 11, 2015.

  1. rob_vld

    rob_vld

    Joined:
    Jul 9, 2013
    Posts:
    191
    So in my initial post i asked for a solution of the bottom image... after testing i came to the conclusion that the examples provided below had some bad side-effects concerning the angle of the camera...

    The top image is what i am after... the green area representing the visible area...

    How would i clip/cut an object/mesh/particle/etc as soon as it leaves the green area based on position(not height) ? And if possible in combination to have this transition being done in a fading way?

    Note that i have no shader coding experience... :( so i am hoping for a solution, not hints or snippets...
     
    Last edited: Jan 18, 2015
  2. Vade-Mecum

    Vade-Mecum

    Joined:
    Dec 10, 2012
    Posts:
    17
    It is possible, there's a shader that cuts object, depending on camera posotion. Although i can't get it to work on samsung devices.
    Code (CSharp):
    1. Shader "PinballShaders/BoundaryGlowShader2"
    2. {
    3.    Properties
    4.    {
    5.     _Color ("Main Color", Color) = (1,1,1,1)
    6.     _Shift ("Shift", Float) = 1.0
    7.     _MainTex ("Texture", 2D) = "white" {}
    8.    }
    9.    SubShader
    10.    {
    11.    Tags {"Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent"}
    12.    Lighting Off
    13.    Blend SrcAlpha OneMinusSrcAlpha
    14.    Cull Off
    15.       Pass
    16.       {
    17. CGPROGRAM
    18. #pragma vertex vert
    19. #pragma fragment frag
    20. #include "UnityCG.cginc"
    21. float4 _Color;
    22. sampler2D _MainTex;
    23. float4 _Fade;
    24. float _Shift;
    25.  
    26. struct v2f {
    27.     float4 pos : POSITION;
    28.     float4 color : COLOR0;
    29.     float2  uv : TEXCOORD0;
    30.     float alpha:TEXCOORD1;
    31.     float4 fragPos : TEXCOORD2;
    32.     float4 fade : TEXCOORD3;
    33. };
    34.  
    35. float4 _MainTex_ST;
    36.  
    37. v2f vert (appdata_base v)
    38. {
    39.     v2f o;
    40.     o.color = _Color;
    41.     o.pos = mul (UNITY_MATRIX_MVP, v.vertex);
    42.     o.uv = TRANSFORM_TEX (v.texcoord, _MainTex);
    43.     o.fragPos = mul (UNITY_MATRIX_MV, v.vertex);
    44.     o.fade = mul (UNITY_MATRIX_MV, _Fade);
    45.     return o;
    46. }
    47.  
    48. half4 frag (v2f i) : COLOR
    49.  
    50. {
    51.     float4 outColor = i.color;
    52.     half4 texcol = tex2D (_MainTex, i.uv);
    53.     float dist = distance(i.fade, i.fragPos);
    54.     float4 objectOrigin = mul(_Object2World, float4(0.0,0.0,0.0,1.0) );
    55.     float dist2 = distance(objectOrigin, _WorldSpaceCameraPos) + _Shift;
    56.     if (dist < dist2)
    57.                 {
    58.                     discard;
    59.                 }
    60.         return texcol * outColor;
    61. }
    62. ENDCG
    63.       }
    64.    }
    65.    FallBack "VertexLit"
    66. }
     
  3. rob_vld

    rob_vld

    Joined:
    Jul 9, 2013
    Posts:
    191
    Thank you for replying and offering me a possible solution Vade Mecum!

    I have tried testing this... but i can't really tell what's going on....it seems to be doing this in reverse?
    So i drag this shader onto the red cube, then the bear should clip, depending on the current camera position?
     
  4. PixelMind

    PixelMind

    Joined:
    Aug 16, 2013
    Posts:
    101
  5. rob_vld

    rob_vld

    Joined:
    Jul 9, 2013
    Posts:
    191
    Thanks PixelMind!

    A very nice solution indeed! Have not come across this page... it has its faults but nice find :D
     
  6. rob_vld

    rob_vld

    Joined:
    Jul 9, 2013
    Posts:
    191
    *bump*

    I have updated my first post to form a new question
     
  7. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    I'll see if I can get any traction. This doesn't seem trivial, so trying to solve it might be tough without experience in shader writing.
     
  8. rob_vld

    rob_vld

    Joined:
    Jul 9, 2013
    Posts:
    191
    Appreciate it! :)
     
  9. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    So, a couple of things:

    ANYONE: with shader experience, don't be afraid to chip in. We'll appreciate it.

    Rob: Can you tell us *why* you want this shader? Not the technical images you've supplied, but the final desired output, and what it's being used for?

    Sometimes it's easier to find a working solution when we know your end goal, rather than chasing the specific issue you think will achieve that end goal. Can you describe what you want to "see" on the screen, and how you want to implement this and why?
     
  10. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    So - correct me if I'm wrong.

    You want to define an area/volume in the game, and if the object leaves the area, you don't want it rendered...

    ... but more importantly, if you have an object that's partly within this area, only the parts INSIDE the area are rendered and the rest of the object is NOT rendered?

    Correct?
     
  11. rob_vld

    rob_vld

    Joined:
    Jul 9, 2013
    Posts:
    191
    Yes that is correct, the screenshot i made in my first post should help i hope visualize that...

    "Anything" showing on the green area is fully visible, until it goes over to the red area, that part should simply clip/cut off!
     
  12. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Pedantism 101:

    You mean this:


    or arguably this:
     
  13. rob_vld

    rob_vld

    Joined:
    Jul 9, 2013
    Posts:
    191
    exactly :D
     
  14. rob_vld

    rob_vld

    Joined:
    Jul 9, 2013
    Posts:
    191
    *bump*

    still looking for a solution :'(
     
  15. varfare

    varfare

    Joined:
    Feb 12, 2013
    Posts:
    227
    You may want to try this:
    1. Get your bear model vertices position in world space
    2. In C#/JS do shader.SetGlobalFloat or SetGlobalMatrix and pass your green box vertices position in world space into it
    3. Now all of your shaders can access to the GreenBox vertices positions (with the name of your choice)
    4. You now need to get GreenPox matrix/floats into your bear shader, iterpolate this data per-pixel. Also, interpolate Bear WS pos per pixel
    5. Do this:
    Code (CSharp):
    1. if (BearWorldSpacePositionPerPixel > GreenBoxWorldSpacePositionPerPixel)
    2. discard;
    So theoretically it should compare distance between GreenBox volume and Bear volume. If any Bear pixel is away from GreenBox volume it should not be rendered.
     
  16. varfare

    varfare

    Joined:
    Feb 12, 2013
    Posts:
    227
  17. Adam-Buckner

    Adam-Buckner

    Joined:
    Jun 27, 2007
    Posts:
    5,664
    Oh! Varfare! Thanks for the help, research and video!
     
  18. varfare

    varfare

    Joined:
    Feb 12, 2013
    Posts:
    227
    No problemo :) I'm 100% sure that there is an easier and faster way to do this. Logical comparisons are not the best option for the GPU code. But since I don't know other way... It is an easy task to extend this method for shapes like sphere or hemisphere. For sphere you simply define a sphere radius and position. Then you check if an object's pixels are further away from the point than the distance.