Anders and Briegel in Python
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

173 lines
5.4KB

  1. // IE9
  2. if(typeof console === "undefined") { var console = { log: function (logMsg) { } }; }
  3. var controls, renderer, raycaster, scene, info, nodeGeometry, selection;
  4. var mouse = {"x":0, "y":0};
  5. var materials={};
  6. var curveProperties = {splineDensity: 30, curvature: 10};
  7. var camera;
  8. // Run on startup
  9. window.onload=init;
  10. // Add a curved edge between two points
  11. function makeEdge(e) {
  12. // Make the geometry of the curve
  13. var a = new THREE.Vector3(e.start[0], e.start[1], e.start[2]);
  14. var b = new THREE.Vector3(e.end[0], e.end[1], e.end[2]);
  15. var length = new THREE.Vector3().subVectors(a, b).length();
  16. var bend = new THREE.Vector3(length/curveProperties.curvature, length/curveProperties.curvature, 0);
  17. var mid = new THREE.Vector3().add(a).add(b).multiplyScalar(0.5).add(bend);
  18. var spline = new THREE.CatmullRomCurve3([a, mid, b]);
  19. var geometry = new THREE.Geometry();
  20. var splinePoints = spline.getPoints(curveProperties.splineDensity);
  21. Array.prototype.push.apply(geometry.vertices, splinePoints);
  22. // Make the actual Object3d thing
  23. var line = new THREE.Line(geometry, materials.edge);
  24. return line;
  25. }
  26. // Clear the whole scene
  27. function makeScene(){
  28. // Scene, controls, camera and so on
  29. var myScene = new THREE.Scene();
  30. // Materials
  31. var lineStyle = {color: "gray", transparent: false, linewidth:1};
  32. materials.edge = new THREE.LineBasicMaterial(lineStyle);
  33. var pointStyle = { size: 0.2, map: materials.sprite, alphaTest: 0.5,
  34. transparent: true, vertexColors:THREE.VertexColors};
  35. materials.point = new THREE.PointsMaterial(pointStyle);
  36. // Build all the edges
  37. //var edgeGroup = new THREE.Object3D();
  38. // Build all the nodes
  39. nodeGeometry = new THREE.Geometry();
  40. nodeGeometry.labels = [];
  41. nodeGeometry.colors = [];
  42. for (var i=0; i < 10; ++i) {
  43. for (var j=0; j < 10; ++j) {
  44. var vertex = new THREE.Vector3(i-5, j-5, 0);
  45. nodeGeometry.vertices.push(vertex);
  46. nodeGeometry.colors.push(new THREE.Color(0.5, 0.5, 0.5));
  47. nodeGeometry.labels.push(i + " " + j + " ");
  48. }
  49. }
  50. var particles = new THREE.Points(nodeGeometry, materials.point);
  51. var grid = makeGrid(10, 10, "lightgray");
  52. myScene.add(grid);
  53. // Add the above stuff into the scene and return
  54. //myScene.add(edgeGroup);
  55. myScene.add(particles);
  56. return myScene;
  57. }
  58. // Gets a reference to the node nearest to the mouse cursor
  59. function nearestNode() {
  60. raycaster.setFromCamera(mouse, camera);
  61. for (var i=0; i < nodeGeometry.vertices.length; ++i) {
  62. if (raycaster.ray.distanceSqToPoint(nodeGeometry.vertices[i]) < 0.01){ return i;}
  63. }
  64. return undefined;
  65. }
  66. // Find out: what is the mouse pointing at?
  67. function checkIntersections() {
  68. var new_selection = nearestNode();
  69. if (new_selection != selection){
  70. selection = new_selection;
  71. info.className = selection ? "visible" : "hidden";
  72. info.innerHTML = selection ? nodeGeometry.labels[new_selection] : info.innerHTML;
  73. render();
  74. }
  75. }
  76. // Make a grid
  77. function makeGrid(side, n, color){
  78. var markers = new THREE.Object3D();
  79. var gridStyle = { color: color, transparent: true, linewidth: 1, opacity:0.5};
  80. var material = new THREE.LineBasicMaterial(gridStyle);
  81. for (var i=-n/2; i < n/2; ++i) {
  82. var geometry = new THREE.Geometry();
  83. geometry.vertices.push(new THREE.Vector3(side*i/n, -side/2, 0));
  84. geometry.vertices.push(new THREE.Vector3(side*i/n, side/2, 0));
  85. var line = new THREE.Line(geometry, material);
  86. var line90 = line.clone();
  87. line90.rotation.z=Math.PI/2;
  88. markers.add(line);
  89. markers.add(line90);
  90. }
  91. return markers;
  92. }
  93. // Handle mouse movement
  94. function onMouseMove(event) {
  95. mouse.wasClick = false;
  96. mouse.absx = event.clientX;
  97. mouse.absy = event.clientY;
  98. mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
  99. mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
  100. w = 200;
  101. h = 15;
  102. info.style.top = mouse.absy-h-40+"px";
  103. info.style.left = mouse.absx-w/2+"px";
  104. checkIntersections();
  105. }
  106. // Render the current frame to the screen
  107. function render() {
  108. renderer.render(scene, camera);
  109. }
  110. // This is the main control loop
  111. function loopForever() {
  112. controls.update();
  113. requestAnimationFrame(loopForever);
  114. }
  115. // This just organises kickoff
  116. function startMainLoop() {
  117. scene = makeScene();
  118. document.addEventListener("mousemove", onMouseMove, false );
  119. controls.addEventListener("change", render);
  120. loopForever();
  121. }
  122. // Called on startup
  123. function init() {
  124. // Measure things, get references
  125. var width = window.innerWidth;
  126. var height = window.innerHeight;
  127. info = document.getElementById("infoholder");
  128. materials.sprite = new THREE.Texture(document.getElementById("ball"));
  129. materials.sprite.needsUpdate = true;
  130. // Renderer
  131. renderer = new THREE.WebGLRenderer( { antialias: true });
  132. renderer.setSize(width, height);
  133. renderer.setClearColor(0xffffff, 1);
  134. document.querySelector("body").appendChild(renderer.domElement);
  135. // Camera, controls, raycaster
  136. camera = new THREE.PerspectiveCamera(45, width/height, 0.3, 100);
  137. controls = new THREE.OrbitControls(camera);
  138. raycaster = new THREE.Raycaster();
  139. // Center the camera
  140. controls.center.set(0, 0, 0);
  141. controls.rotateSpeed = 0.2;
  142. camera.position.set(0, 0, 20);
  143. // Run
  144. startMainLoop();
  145. }