Skip to content

Commit

Permalink
working version
Browse files Browse the repository at this point in the history
  • Loading branch information
maxdemarzi committed Apr 14, 2012
1 parent 80d77c0 commit 5b37f87
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 93 deletions.
5 changes: 3 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ GEM
rake (0.9.2.2)
rubyzip (0.9.7)
sinatra (1.3.2)
rack (>= 1.3.6, ~> 1.3)
rack (~> 1.3, >= 1.3.6)
rack-protection (~> 1.2)
tilt (>= 1.3.3, ~> 1.3)
tilt (~> 1.3, >= 1.3.3)
tilt (1.3.3)

PLATFORMS
ruby
x86-mingw32

DEPENDENCIES
Expand Down
2 changes: 1 addition & 1 deletion Readme.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Follow along on {Using Three.js with Neo4j}[http://wp.me/p26jdv-es]
rake neo4j:create
rackup

Then visit http://localhost:9292/index.html to see the examples.
Then visit http://localhost:9292 to see the visualization.

See an example running on heroku at {http://neothree.herokuapp.com/index.html}[http://neothree.herokuapp.com/index.html]

Expand Down
2 changes: 1 addition & 1 deletion config.ru
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
require './neo_three.rb'
run Sinatra::Application
run App
45 changes: 39 additions & 6 deletions neo_three.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
require 'rubygems'
require 'neography'
require 'sinatra'
#require 'sinatra'
require 'sinatra/base'
require 'uri'
require 'gon-sinatra'


class App < Sinatra::Base
register Gon::Sinatra

def generate_text(length=8)
chars = 'abcdefghjkmnpqrstuvwxyz'
key = ''
Expand All @@ -20,10 +17,46 @@ def create_graph
graph_exists = neo.get_node_properties(1)
return if graph_exists && graph_exists['name']

names = 200.times.collect{|x| generate_text}
commands = []

names.each_index do |n|
commands << [:create_node, {:name => names[n],
:position_x => rand(2000) - 1000,
:position_y => rand(2000) - 1000,
:position_z => rand(2000) - 1000,
:rotation_x => rand(360) * (Math::PI / 180),
:rotation_y => rand(360) * (Math::PI / 180)
}]
end

names.each_index do |from|
commands << [:add_node_to_index, "nodes_index", "type", "User", "{#{from}}"]
connected = []

# create clustered relationships
members = 20.times.collect{|x| x * 10 + (from % 5)}
members.delete(from)
rels = 2
rels.times do |x|
to = members[x]
connected << to
commands << [:create_relationship, "follows", "{#{from}}", "{#{to}}"] unless to == from
end

# create random relationships
rels = 2
rels.times do |x|
to = rand(names.size)
commands << [:create_relationship, "follows", "{#{from}}", "{#{to}}"] unless (to == from) || connected.include?(to)
end

end

batch_result = neo.batch *commands
end

class App < Sinatra::Base
register Gon::Sinatra

def nodes
neo = Neography::Rest.new
Expand Down
13 changes: 0 additions & 13 deletions public/neo_three.html

This file was deleted.

140 changes: 70 additions & 70 deletions public/neo_three.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,126 +14,126 @@ animate();

function init() {

var container, separation = 100, amountX = 50, amountY = 50,
particles, particle;
var container, separation = 100, amountX = 50, amountY = 50;

container = document.createElement('div');
document.body.appendChild(container);
container = document.createElement('div');
document.body.appendChild(container);

camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 10000 );
camera.position.z = 100;
camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 10000 );
camera.position.z = 100;

scene = new THREE.Scene();
scene = new THREE.Scene();

scene.add( camera );
scene.add( camera );

renderer = new THREE.CanvasRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild( renderer.domElement );
renderer = new THREE.CanvasRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild( renderer.domElement );

// particles
// Nodes

var PI2 = Math.PI * 2;
var material = new THREE.ParticleCanvasMaterial( {
var PI2 = Math.PI * 2;
var material = new THREE.ParticleCanvasMaterial( {

color: 0xffffff,
program: function ( context ) {
color: 0xffffff,
program: function ( context ) {

context.beginPath();
context.arc( 0, 0, 1, 0, PI2, true );
context.closePath();
context.fill();
context.beginPath();
context.arc( 0, 0, 1, 0, PI2, true );
context.closePath();
context.fill();

}
}

} );
} );

var geometry = new THREE.SphereGeometry( 50, 8, 7, false );
var material = new THREE.MeshNormalMaterial();
var geometry = new THREE.SphereGeometry( 50, 8, 7, false );
var material = new THREE.MeshNormalMaterial();

group = new THREE.Object3D();
group = new THREE.Object3D();

console.log(gon);
for (n in gon.nodes) {
for (n in gon.nodes) {

var mesh = new THREE.Mesh( geometry, material );
mesh.position.x = gon.nodes[n].position_x;
mesh.position.y = gon.nodes[n].position_y;
mesh.position.z = gon.nodes[n].position_z;
mesh.rotation.x = gon.nodes[n].rotation_x;
mesh.rotation.y = gon.nodes[n].rotation_y;
mesh.matrixAutoUpdate = false;
mesh.updateMatrix();
group.add( mesh );
var mesh = new THREE.Mesh( geometry, material );
mesh.position.x = gon.nodes[n].position_x;
mesh.position.y = gon.nodes[n].position_y;
mesh.position.z = gon.nodes[n].position_z;
mesh.rotation.x = gon.nodes[n].rotation_x;
mesh.rotation.y = gon.nodes[n].rotation_y;
mesh.matrixAutoUpdate = false;
mesh.updateMatrix();
group.add( mesh );

}

scene.add( group );

}
// Edges

scene.add( group );

// lines
for (n in gon.edges) {
var line_segment = new THREE.Geometry();
line_segment.vertices.push( new THREE.Vertex( group.children[gon.edges[n].start_id].position ) );
line_segment.vertices.push( new THREE.Vertex( group.children[gon.edges[n].end_id].position ) );
var line = new THREE.Line( line_segment, new THREE.LineBasicMaterial( { color: 0xffffff, opacity: 0.5 } ) );
scene.add(line)
}

document.addEventListener( 'mousemove', onDocumentMouseMove, false );
document.addEventListener( 'touchstart', onDocumentTouchStart, false );
document.addEventListener( 'touchmove', onDocumentTouchMove, false );
for (n in gon.edges) {
var line_segment = new THREE.Geometry();
line_segment.vertices.push( new THREE.Vertex( group.children[gon.edges[n].source - 1].position ) );
line_segment.vertices.push( new THREE.Vertex( group.children[gon.edges[n].target - 1].position ) );
var line = new THREE.Line( line_segment, new THREE.LineBasicMaterial( { color: Math.random() * 0xffffff , opacity: 0.5 } ) );

scene.add(line)
}

document.addEventListener( 'mousemove', onDocumentMouseMove, false );
document.addEventListener( 'touchstart', onDocumentTouchStart, false );
document.addEventListener( 'touchmove', onDocumentTouchMove, false );
}

//

function onDocumentMouseMove(event) {

mouseX = event.clientX - windowHalfX;
mouseY = event.clientY - windowHalfY;
mouseX = event.clientX - windowHalfX;
mouseY = event.clientY - windowHalfY;
}

function onDocumentTouchStart( event ) {

if ( event.touches.length > 1 ) {
if ( event.touches.length > 1 ) {

event.preventDefault();
event.preventDefault();

mouseX = event.touches[ 0 ].pageX - windowHalfX;
mouseY = event.touches[ 0 ].pageY - windowHalfY;
mouseX = event.touches[ 0 ].pageX - windowHalfX;
mouseY = event.touches[ 0 ].pageY - windowHalfY;

}
}

}

function onDocumentTouchMove( event ) {

if ( event.touches.length == 1 ) {
if ( event.touches.length == 1 ) {

event.preventDefault();
event.preventDefault();

mouseX = event.touches[ 0 ].pageX - windowHalfX;
mouseY = event.touches[ 0 ].pageY - windowHalfY;
mouseX = event.touches[ 0 ].pageX - windowHalfX;
mouseY = event.touches[ 0 ].pageY - windowHalfY;

}
}

}

//

function animate() {

requestAnimationFrame( animate );
requestAnimationFrame( animate );

render();
render();

}

function render() {

camera.position.x += ( mouseX - camera.position.x ) * .05;
camera.position.y += ( - mouseY + 200 - camera.position.y ) * .05;
camera.lookAt( scene.position );
camera.position.x += ( mouseX - camera.position.x ) * .05;
camera.position.y += ( - mouseY + 200 - camera.position.y ) * .05;
camera.lookAt( scene.position );

renderer.render( scene, camera );
renderer.render( scene, camera );

}
}

0 comments on commit 5b37f87

Please sign in to comment.