var gl;
var square_drawable, triangle_drawable;
var model_mat;

var pick_id_val;
var angle_min;
var angle_max;
var pick_id;
var wedge_color;

//////////////////////////////////////////////////////////////////////////////

function draw_it()
{
    square_drawable.draw();
}

$().ready(function () {
    var canvas = document.getElementById("webgl");
    var camera = Facet.Camera.perspective({
        look_at: [[0, 0, 6], [0, 0, -1], [0, 1, 0]],
        field_of_view_y: 45,
        aspect_ratio: 720/480,
        near_distance: 0.1,
        far_distance: 100
    });
    model_mat = Shade.uniform("mat4", Facet.identity());

    var strings = {};
    strings[0] = "Miss";

    gl = Facet.initGL(canvas, {
        clearDepth: 1.0,
        clearColor: [0,0,0,0.2],
        display: draw_it,
        attributes: {
            alpha: true,
            depth: true
        },
        mousedown: function(event) {
            Facet.Picker.draw_pick_scene();
            var r = Facet.Picker.pick(event.offsetX, gl.viewportHeight - event.offsetY);
            $("#pickresult").html(strings[r]);
            // console.log(strings[r]);
        }
    });

    var square = Facet.model({
        type: "triangles",
        elements: [0, 1, 2, 0, 2, 3],
        vertex: [[-1,-1, 1,-1, 1,1, -1,1], 2]
    });

    var distance_from_origin = Shade.length(square.vertex);
    var angle = Shade.selection(distance_from_origin.eq(0), 
                                0, Shade.atan(square.vertex.at(1), 
                                              square.vertex.at(0)));
    var pick_id_val = Facet.fresh_pick_id(3);
    var angle_min = Shade.uniform("float", -Math.PI/3);
    var angle_max = Shade.uniform("float", Math.PI/3);
    var pick_id = Shade.uniform("vec4", Shade.id(pick_id_val));
    var wedge_color = Shade.uniform("vec4", Shade.color('rgb(255, 232, 204)'));

    square_drawable = Facet.bake(square, {
        position: camera.project(model_mat.mul(Shade.vec(square.vertex, 0, 1))),
        color: wedge_color
            .discard_if(distance_from_origin.gt(1)
                        .logical_or(angle.gt(angle_max))
                        .logical_or(angle.lt(angle_min))),
        pick_id: pick_id
    });

    strings[pick_id_val]   = "Wedge 0";
    strings[pick_id_val+1] = "Wedge 1";
    strings[pick_id_val+2] = "Wedge 2";

    gl.display();
});

@@ -236,16 +236,829 @@
 _it()%0A%7B%0A
+    var states = %5B %5B %5Bangle_min, -Math.PI/3%5D,%0A                     %5Bangle_max,  Math.PI/3%5D,%0A                     %5Bpick_id, Shade.id(pick_id_val)%5D,%0A                     %5Bwedge_color, Shade.color('red')%5D %5D,%0A                   %5B %5Bangle_min, Math.PI/3%5D,%0A                     %5Bangle_max, Math.PI/2%5D,%0A                     %5Bpick_id, Shade.id(pick_id_val+1)%5D,%0A                     %5Bwedge_color, Shade.color('blue')%5D %5D,%0A                   %5B %5Bangle_min,   Math.PI/2%5D,%0A                     %5Bangle_max, 5*Math.PI/3%5D,%0A                     %5Bpick_id, Shade.id(pick_id_val+2)%5D,%0A                     %5Bwedge_color, Shade.color('green') %5D %5D %5D;%0A    _.each(states, function(lst) %7B%0A        _.each(lst, function(pair) %7B%0A            var uniform = pair%5B0%5D,%0A                value = pair%5B1%5D;%0A            uniform.set(value);%0A        %7D);%0A    
     squa
@@ -1077,16 +1077,24 @@
 draw();%0A
+    %7D);%0A
 %7D%0A%0A$().r
@@ -2371,28 +2371,24 @@
 t(0)));%0A    
-var 
 pick_id_val 
@@ -2413,28 +2413,24 @@
 _id(3);%0A    
-var 
 angle_min = 
@@ -2454,31 +2454,15 @@
 oat%22
-, -Math.PI/3
 );%0A    
-var 
 angl
@@ -2494,30 +2494,15 @@
 oat%22
-, Math.PI/3
 );%0A    
-var 
 pick
@@ -2531,42 +2531,15 @@
 ec4%22
-, Shade.id(pick_id_val)
 );%0A    
-var 
 wedg
@@ -2572,47 +2572,270 @@
 ec4%22
-, Shade.color('rgb(255, 232, 204)'
+);%0A%0A    var angle_p1 = angle.add(Math.PI * 2);%0A    var angle_m1 = angle.sub(Math.PI * 2);%0A%0A    function inside(ang) %7B%0A        return Shade.and(ang.ge(angle_min), ang.lt(angle_max));%0A    %7D;%0A %0A    var hit = inside(angle).or(inside(angle_p1)).or(inside(angle_m1
 ));%0A
-%0A
     
@@ -3035,120 +3035,20 @@
 t(1)
-%0A                        .logical_or(angle.gt(angle_max))%0A                        .logical_or(angle.lt(angle_min
+.or(hit.not(
 ))),

correct behavior when wedge crosses -pi + 2*pi*k