Skip to content

Instantly share code, notes, and snippets.

@mattak
Created November 7, 2016 00:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattak/888dc13e5a7593d7053372e8b370d4bf to your computer and use it in GitHub Desktop.
Save mattak/888dc13e5a7593d7053372e8b370d4bf to your computer and use it in GitHub Desktop.
Shader "Custom/IntensiveLine"
{
Properties
{
_LineWidth("LineWidth", Range(0,1)) = 0.5
_CenterRadius("CenterRadius", Range(0,1)) = 0.5
}
SubShader
{
// No culling or depth
// Cull Off ZWrite Off ZTest Always
Pass
{
CGPROGRAM
// Upgrade NOTE: excluded shader from DX11, Xbox360, OpenGL ES 2.0 because it uses unsized arrays
#pragma exclude_renderers d3d11 xbox360 gles
#pragma vertex vert
#pragma fragment frag
#pragma target 3.0
#include "UnityCG.cginc"
uniform float _CenterRadius;
uniform float _LineWidth;
struct appdata
{
float4 vertex : POSITION;
float3 normal : NORMAL;
float4 texcoord : TEXCOORD0;
};
float4 vert (appdata v) : POSITION
{
return mul (UNITY_MATRIX_MVP, v.vertex);
}
float which_side(float2 p1, float2 p2, float2 p3) {
float result = p1.x * (p2.y - p3.y) + p2.x * (p3.y - p1.y) + p3.x * (p1.y - p2.y);
return result;
}
bool in_triangle(float2 xy, float2 pa, float2 pb, float2 pc) {
float pab = which_side(xy, pa, pb);
float pbc = which_side(xy, pb, pc);
float pca = which_side(xy, pc, pa);
if (0 < pab && 0 < pbc && 0 < pca) {
return true;
}
else if (0 > pab && 0 > pbc && 0 > pca) {
return true;
}
return false;
}
bool in_triangle_line(float2 xy, float2 ps, float2 pe, float width) {
float2 pa = ps;
float2 p = pe - ps;
float2 pr = normalize(float2(-p.y, p.x)) * width;
float2 pb = pe + pr;
float2 pc = pe - pr;
return in_triangle(xy, pa, pb, pc);
}
fixed4 frag (float4 sp:WPOS) : COLOR
{
// fixed2 red_green = sp.xy / _ScreenParams.xy;
float2 xy = sp.xy / _ScreenParams.xy;
float dist = distance(xy, float2(0.5, 0.5));
//if (dist < _CenterRadius) {
// clip(-1);
// return fixed4(0,0,0,0);
//}
float2 points[] = {
float2(0.5,1), float2(0,0.5), float2(0.5,0), float2(1,0.5),
float2(1,1), float2(0,1), float2(0,0), float2(1,0),
};
float2 center = float2(0.5,0.5);
float width = _LineWidth * 0.5;
if (in_triangle_line(xy, center, points[0], width)) { return fixed4(0,0,0,1); }
if (in_triangle_line(xy, center, points[1], width)) { return fixed4(0,0,0,1); }
if (in_triangle_line(xy, center, points[2], width)) { return fixed4(0,0,0,1); }
if (in_triangle_line(xy, center, points[3], width)) { return fixed4(0,0,0,1); }
if (in_triangle_line(xy, center, points[4], width)) { return fixed4(0,0,0,1); }
if (in_triangle_line(xy, center, points[5], width)) { return fixed4(0,0,0,1); }
if (in_triangle_line(xy, center, points[6], width)) { return fixed4(0,0,0,1); }
if (in_triangle_line(xy, center, points[7], width)) { return fixed4(0,0,0,1); }
clip(-1);
return fixed4(0,0,0,0);
// float red = dist;
// float green = dist;
// float blue = 0.0;
// float alpha = 1.0;
// return fixed4(red, green, blue, alpha);
}
ENDCG
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment