Anders and Briegel in Python
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

100 lines
2.9KB

  1. var editor = {};
  2. var pi2 = Math.PI / 2;
  3. editor.nearest = undefined;
  4. editor.orientations = [
  5. new THREE.Matrix4(),
  6. new THREE.Matrix4(),
  7. new THREE.Matrix4()
  8. ];
  9. editor.orientation = 0;
  10. editor.orientations[1].makeRotationX(pi2);
  11. editor.orientations[2].makeRotationX(pi2);
  12. editor.orientations[2].makeRotationZ(pi2);
  13. editor.onFreeMove = function() {
  14. var n = editor.nearestNode(mouse.ray);
  15. if (editor.nearest !== n) {
  16. editor.nearest = n;
  17. if (n) {
  18. gui.nodeMessage("Node " + n + " (VOP:" + abj.vops[n] + ")" +
  19. "<br/>" + "Click to edit neighbourhood");
  20. } else {
  21. gui.hideNodeMessage();
  22. }
  23. }
  24. };
  25. editor.onClick = function() {
  26. var n = editor.nearestNode(mouse.ray);
  27. if (n) {
  28. var p = abj.meta[n].position;
  29. editor.grid.position.set(p.x, p.y, p.z);
  30. gui.controls.target.set(p.x, p.y, p.z);
  31. gui.hideNodeMessage();
  32. editor.nearest = undefined;
  33. gui.serverMessage("Selected node " + n + "");
  34. } else {
  35. //TODO: ghastly
  36. var intersection = mouse.ray.intersectPlane(editor.plane);
  37. intersection.x = Math.round(intersection.x, 0);
  38. intersection.y = Math.round(intersection.y, 0);
  39. intersection.z = Math.round(intersection.z, 0);
  40. var newNode = abj.order();
  41. abj.add_node(newNode, {
  42. position: intersection
  43. });
  44. editor.grid.position.set(intersection.x, intersection.y, intersection.z);
  45. gui.controls.target.set(intersection.x, intersection.y, intersection.z);
  46. graph.update();
  47. gui.serverMessage("Created node " + newNode + " at ");
  48. }
  49. };
  50. editor.prepare = function() {
  51. mouse.onFreeMove = editor.onFreeMove;
  52. mouse.onClick = editor.onClick;
  53. document.addEventListener("keydown", editor.onKey, false);
  54. editor.makeGrid();
  55. };
  56. editor.onKey = function(evt) {
  57. if (evt.keyCode == 32) {
  58. editor.grid.rotation.x += Math.PI / 2;
  59. editor.orientation = (editor.orientation+1) % 3;
  60. console.log(editor.orientation);
  61. var m = editor.orientations[editor.orientation];
  62. editor.plane.applyMatrix4(m);
  63. console.log(m);
  64. editor.grid.matrix = m;
  65. gui.render();
  66. gui.serverMessage("Rotated into the XY plane or whatever");
  67. }
  68. };
  69. editor.makeGrid = function() {
  70. editor.grid = new THREE.GridHelper(10, 1);
  71. editor.grid.rotation.x = Math.PI / 2;
  72. editor.grid.setColors(0xbbbbbb, 0xeeeeee);
  73. editor.grid.matrixAutoUpdate = false;
  74. editor.plane = new THREE.Plane(new THREE.Vector3(0, 0, 1), 0);
  75. gui.scene.add(editor.grid);
  76. };
  77. editor.update = function(){
  78. };
  79. // Gets a reference to the node nearest to the mouse cursor
  80. // TODO: get rid of meta{}
  81. editor.nearestNode = function(ray) {
  82. for (var j in abj.meta) {
  83. if (ray.distanceSqToPoint(abj.meta[j].position) < 0.03) {
  84. return j;
  85. }
  86. }
  87. return undefined;
  88. };