Advertisement
axoila

intersection with plane shader

Aug 8th, 2018
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Shader "PlaneIntersection"{
  2.     //show values to edit in inspector
  3.     Properties{
  4.         [HDR]_Color ("Color", Color) = (0, 0, 0, 1)
  5.         _Thickness ("Thickness", float) = 0
  6.     }
  7.  
  8.     SubShader{
  9.         //the material is transparent and is rendered at the same time as the other transparent geometry
  10.         Tags{ "RenderType"="Transparent" "Queue"="Transparent"}
  11.  
  12.         Pass{
  13.             //render front and back, and render as transparent object without drawing to the zbuffer
  14.             Cull Off
  15.             Blend SrcAlpha OneMinusSrcAlpha
  16.             ZWrite Off
  17.  
  18.             CGPROGRAM
  19.             #include "UnityCG.cginc"
  20.  
  21.             #pragma vertex vert
  22.             #pragma fragment frag
  23.  
  24.             fixed4 _Color;
  25.             float4 _Plane;
  26.             float _Thickness;
  27.  
  28.             struct appdata{
  29.                 float4 vertex : POSITION;
  30.             };
  31.  
  32.             struct v2f{
  33.                 float4 position : SV_POSITION;
  34.                 float3 worldPos : TEXCOORD0;
  35.             };
  36.  
  37.             v2f vert(appdata v){
  38.                 v2f o;
  39.                 //calculate the position in clip space to render the object
  40.                 o.position = UnityObjectToClipPos(v.vertex);
  41.                 //calculate world position of vertex
  42.                 float4 worldPos = mul(unity_ObjectToWorld, v.vertex);
  43.                 o.worldPos = worldPos.xyz;
  44.                 return o;
  45.             }
  46.  
  47.             fixed4 frag(v2f i) : SV_TARGET{
  48.                 //calculate signed distance to plane
  49.                 float distance = dot(i.worldPos, _Plane.xyz);
  50.                 distance = distance + _Plane.w;
  51.  
  52.                 //how much does the plane distance change in the neighboring pixels
  53.                 float aa = fwidth(distance);
  54.  
  55.                 //calculate how the mesh should be cut off above and below the plane
  56.                 //(the aa varable makes the result antialiased because it smoothes the edge)
  57.                 float cutoffAbove = smoothstep(_Thickness + aa, _Thickness, distance);
  58.                 float cutoffBelow = smoothstep(-_Thickness-aa, -_Thickness, distance);
  59.                 //combine the cutoff values
  60.                 float cutoff = cutoffAbove * cutoffBelow;
  61.  
  62.                 //use the cutoff value as transparency
  63.                 float4 col = float4(_Color.rgb, cutoff);
  64.                 return col;
  65.             }
  66.  
  67.             ENDCG
  68.         }
  69.     }
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement