Skip to content

Instantly share code, notes, and snippets.

@enjalot
Created February 6, 2013 06:28
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 enjalot/4720756 to your computer and use it in GitHub Desktop.
Save enjalot/4720756 to your computer and use it in GitHub Desktop.
multi leap
{"description":"multi leap","endpoint":"","display":"webgl","public":true,"require":[],"fileconfigs":{"inlet.js":{"default":true,"vim":false,"emacs":false,"fontSize":12},"leap.js":{"default":true,"vim":false,"emacs":false,"fontSize":12},"config.json":{"default":true,"vim":false,"emacs":false,"fontSize":12},"_.md":{"default":true,"vim":false,"emacs":false,"fontSize":12}},"play":false,"loop":false,"restart":false,"autoinit":true,"pause":true,"loop_type":"period","bv":false,"nclones":15,"clone_opacity":0.4,"duration":3000,"ease":"linear","dt":0.01,"fullscreen":false,"thumbnail":"http://i.imgur.com/Uvl39EF.png"}
var colorScale = d3.scale.category20();
var svg = d3.select("#display")
.selectAll("svg")
.data(["shadowsvg"])
svg.enter().append("svg");
var cubes = {};
tributary.events.off("leap");
tributary.events.on("leap", function(d) {
//console.log(d.arm);
//console.log("d", d, d.arm);
/*
var hand, pointable;
for(var j = d.hands.length; j--;) {
hand = d.hands[j];
}
*/
var arm = svg.selectAll("g.arm")
.data([d.arm], function(d) { return d })
arm.enter()
.append("g").classed("arm",true);
var color = colorScale(d.arm);
var fingers = arm.selectAll("g.finger")
.data(d.pointables, function(p) {
return p.id
});
fingers.enter()
.append("g")
.classed("finger", true)
.each(function(p) {
var cube = makeCube(color);
cubes[d.arm + "_" + p.id] = cube;
tributary.scene.add(cube);
//console.log("making", d.arm, p.id, cube);
})
fingers.exit()
.each(function(p) {
var cube = cubes[d.arm + "_" + p.id];
tributary.scene.remove(cube);
//console.log("removing", d.arm, p.id, cube);
}).remove();
fingers.each(function(p) {
var pos = p.tipPosition;
var cube = cubes[d.arm + "_" + p.id];
if(cube) {
cube.position.set(pos[0], pos[1], pos[2]);
}
})
})
plane = new THREE.Mesh( new THREE.PlaneGeometry( 100, 100 ), new THREE.MeshBasicMaterial( { color: 0xa3a3a3 } ) );
plane.geometry.applyMatrix( new THREE.Matrix4().makeRotationX( - Math.PI / 2 ) );
tributary.scene.add( plane );
function toInt(color) { return parseInt(color.slice(1,7), 16) };
function makeCube(color) {
var materials = [];
for(var i = 6; i--; ) {
materials.push(new THREE.MeshBasicMaterial( { color: toInt(color)} ));
}
cube = new THREE.Mesh( new THREE.CubeGeometry( 19, 20, 20, 1, 1, 1, materials ), new THREE.MeshFaceMaterial() );
return cube
}
//tributary.useThreejsControls = false
if(!tributary.leap) tributary.leap = {};
// Support both the WebSocket and MozWebSocket objects
if ((typeof(WebSocket) == 'undefined') &&
(typeof(MozWebSocket) != 'undefined')) {
WebSocket = MozWebSocket;
}
// Create the socket with event handlers
tributary.leap.init = function() {
if(tributary.ws) {
tributary.ws.close();
}
console.log("Starting");
//Create and open the socket
tributary.ws = new WebSocket("ws://54.235.119.124:8000/");
var ws = tributary.ws;
// On successful connection
ws.onopen = function(event) {
console.log("Open");
ws.send("eye");
};
// On message received
ws.onmessage = function(event) {
tributary.events.trigger("leap", JSON.parse(event.data));
};
// On socket close
ws.onclose = function(event) {
ws = null;
}
//On socket error
ws.onerror = function(event) {
console.error("Received error");
};
}
tributary.leap.init();
tributary.events.on("restart", function() {
tributary.leap.init();
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment