Search Unity

Approach for displaying intersection between plane and sphere

Discussion in 'General Graphics' started by MarcoMeter, Jul 28, 2015.

  1. MarcoMeter

    MarcoMeter

    Joined:
    Jun 14, 2013
    Posts:
    69
    Hello everybody,

    right now I'm working on a virtual Math Laboratory to examine intersections in the field of linear algebra with the Oculus Rift.

    So far I couldn't imagine a feasible solution to display a plane which intersects a sphere. For the first tries I used the standard transparent shader for both objects. Depending on the camera angle, sometimes the plane looks like that it is completely behind or in front of the sphere.

    spherePlaneIntersection1.png spherePlaneIntersection2.png spherePlaneIntersection3.png


    Do you have any ideas for displaying this case focusing on the intersection circle?

    I was thinking about boolean operations between the plane and the sphere to cut out the intersection circle of the sphere and highlight it with a brigther stroke. But I have no clue how to solve this on runtime.

    intersection.png

    The next thing I'll try will be something like a glas shader. I'll follow a tutorial for shader forge.


    I'm looking forward to any tips and recommendations!
     
  2. MarcoMeter

    MarcoMeter

    Joined:
    Jun 14, 2013
    Posts:
    69
  3. MarcoMeter

    MarcoMeter

    Joined:
    Jun 14, 2013
    Posts:
    69
    Nobody?
     
  4. MarcoMeter

    MarcoMeter

    Joined:
    Jun 14, 2013
    Posts:
    69
    It's still an issue
     
  5. bgolus

    bgolus

    Joined:
    Dec 7, 2012
    Posts:
    12,352
    I wrote a bit about transparency sorting here:
    Render Mode - Transparent doesn't work see video

    TLDR: It's working as expected. The efficient sorting of transparency in real time rendering is an unsolved problem, especially in the case of two transparent surfaces intersecting.

    The solution for you would likely be to have to cut the model up on the CPU side and send it over as multiple parts that will be sorted properly ... use those boolean operations you're trying to show off for real!

    As for the outline, that's a tough one as well. There are plenty of ways to do this in a shader when mixing opaque and transparent geometry, but for all transparent you're going to again have to rely on doing this on the CPU. Likely you'll need to generate a mesh for the lines lines around the intersection and the mesh you want to highlight (which will also need to be split up if you want it to sort). You could also modify the model's vertex colors or write to a texture that's displayed, or pass information to the shader about the intersection plane and sphere and do distance field tests, but these get ugly fast for anything more than a single intersection and the texture copy back to the GPU can be slow which is bad for VR.
     
  6. MarcoMeter

    MarcoMeter

    Joined:
    Jun 14, 2013
    Posts:
    69
    Thanks for your replay bgolus.

    My approach now is to get this CGS library running with Unity primitives. After completing the step of boolean operations of two meshes, I'll try to create proper material to display the intersection properly.
     
  7. fxcarl

    fxcarl

    Joined:
    Apr 8, 2010
    Posts:
    16
    I guess your wants may be just an invert soft particle, like this



    I modified a particle shader for this

    Before Ctrl+C and Ctrl+V . You need to let soft particles work : turn on soft particles in quality setting

    Code (CSharp):
    1. Shader "Particles/Intersection Additive" {
    2.     Properties{
    3.         _TintColor("Tint Color", Color) = (0.5,0.5,0.5,0.5)
    4.         _MainTex("Particle Texture", 2D) = "white" {}
    5.     _InvFade("Soft Particles Factor", Range(0.1,100.0)) = 1.0
    6.     }
    7.  
    8.         Category{
    9.         Tags{ "Queue" = "Transparent" "IgnoreProjector" = "True" "RenderType" = "Transparent" }
    10.         Blend SrcAlpha OneMinusSrcAlpha
    11.         ColorMask RGB
    12.         Cull Off Lighting Off ZWrite Off ZTest LESS
    13.  
    14.         SubShader{
    15.         Pass{
    16.  
    17.         CGPROGRAM
    18. #pragma vertex vert
    19. #pragma fragment frag
    20. #pragma multi_compile_particles
    21. #pragma multi_compile_fog
    22.  
    23. #include "UnityCG.cginc"
    24.  
    25.         sampler2D _MainTex;
    26.     fixed4 _TintColor;
    27.  
    28.     struct appdata_t {
    29.         float4 vertex : POSITION;
    30.         fixed4 color : COLOR;
    31.         float2 texcoord : TEXCOORD0;
    32.     };
    33.  
    34.     struct v2f {
    35.         float4 vertex : SV_POSITION;
    36.         fixed4 color : COLOR;
    37.         float2 texcoord : TEXCOORD0;
    38.         UNITY_FOG_COORDS(1)
    39. #ifdef SOFTPARTICLES_ON
    40.             float4 projPos : TEXCOORD2;
    41. #endif
    42.     };
    43.  
    44.     float4 _MainTex_ST;
    45.  
    46.     v2f vert(appdata_t v)
    47.     {
    48.         v2f o;
    49.         o.vertex = mul(UNITY_MATRIX_MVP, v.vertex);
    50. #ifdef SOFTPARTICLES_ON
    51.         o.projPos = ComputeScreenPos(o.vertex);
    52.         COMPUTE_EYEDEPTH(o.projPos.z);
    53. #endif
    54.         o.color = v.color;
    55.         o.texcoord = TRANSFORM_TEX(v.texcoord,_MainTex);
    56.         UNITY_TRANSFER_FOG(o,o.vertex);
    57.         return o;
    58.     }
    59.  
    60.     sampler2D_float _CameraDepthTexture;
    61.     float _InvFade;
    62.  
    63.     fixed4 frag(v2f i) : SV_Target
    64.     {
    65. #ifdef SOFTPARTICLES_ON
    66.         float sceneZ = LinearEyeDepth(SAMPLE_DEPTH_TEXTURE_PROJ(_CameraDepthTexture, UNITY_PROJ_COORD(i.projPos)));
    67.     float partZ = i.projPos.z;
    68.     float fade = saturate(_InvFade * (sceneZ - partZ));
    69.     i.color.a += saturate(1 - fade) * (1 - i.color.a);
    70.     _TintColor.a += saturate(1 - fade) * (1 - _TintColor.a);
    71. #endif
    72.  
    73.     fixed4 col = 2.0f * i.color * _TintColor * tex2D(_MainTex, i.texcoord);
    74.     UNITY_APPLY_FOG_COLOR(i.fogCoord, col, fixed4(0,0,0,0)); // fog towards black due to our blend mode
    75.     return col;
    76.     }
    77.         ENDCG
    78.     }
    79.     }
    80.     }
    81. }
    82.  
     

    Attached Files:

    Last edited: Nov 12, 2015
  8. MarcoMeter

    MarcoMeter

    Joined:
    Jun 14, 2013
    Posts:
    69
    schnittgebilde.png
    I love the result. Thanks a lot!
     
  9. MarcoMeter

    MarcoMeter

    Joined:
    Jun 14, 2013
    Posts:
    69
    Good morning all together,

    kinda got back to this thread, because there is a desire to extend this visualization to something like this (the drawn outline and especially the dotted part):

    Source: Geogebra


    Does anybody have an idea for this approach, maybe using the package Vectrosity?

    So far I can think of calculating many points of the circle's circumference, draw the visible ones as straight line and the hidden ones as dotted,

    I just don't know yet how to determine if a point is hidden or not. And I still have to figure out, how to make the hidden part of the circle visible.
     
    Last edited: Feb 23, 2016