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.

94 lines
2.8KB

  1. var controls, renderer, raycaster, scene, selection, camera;
  2. var mouseprevpos = {};
  3. // Run on startup
  4. window.onload = init;
  5. // Clear the whole scene
  6. function makeScene() {
  7. var myScene = new THREE.Scene();
  8. var grid = new THREE.GridHelper(20, 2);
  9. grid.rotation.x = Math.PI/2;
  10. grid.setColors(0xdddddd, 0xeeeeee);
  11. myScene.add(grid);
  12. return myScene;
  13. }
  14. // Render the current frame to the screen
  15. function render() {
  16. requestAnimationFrame(function () {
  17. renderer.render(scene, camera);
  18. });
  19. }
  20. // Someone resized the window
  21. function onWindowResize(evt){
  22. camera.aspect = window.innerWidth / window.innerHeight;
  23. camera.updateProjectionMatrix();
  24. renderer.setSize(window.innerWidth, window.innerHeight);
  25. render();
  26. }
  27. function bind_events() {
  28. window.addEventListener("resize", onWindowResize, false);
  29. renderer.domElement.addEventListener("mousedown", function (event) {
  30. var mouse = new THREE.Vector2(); // create once and reuse
  31. mouse.x = ( event.clientX / renderer.domElement.width ) * 2 - 1;
  32. mouse.y = - ( event.clientY / renderer.domElement.height ) * 2 + 1;
  33. mouseprevpos.x = mouse.x;
  34. mouseprevpos.y = mouse.y;
  35. });
  36. renderer.domElement.addEventListener("mouseup", function (event) {
  37. var mouse = new THREE.Vector2(); // create once and reuse
  38. mouse.x = ( event.clientX / renderer.domElement.width ) * 2 - 1;
  39. mouse.y = - ( event.clientY / renderer.domElement.height ) * 2 + 1;
  40. if (mouse.x != mouseprevpos.x || mouse.y != mouseprevpos.y ){return;}
  41. var raycaster = new THREE.Raycaster(); // create once and reuse
  42. raycaster.setFromCamera( mouse, camera );
  43. var plane = new THREE.Plane(new THREE.Vector3(0, 0, 1), 0);
  44. var intersection = raycaster.ray.intersectPlane(plane);
  45. console.log(intersection);
  46. intersection.x = Math.round(intersection.x);
  47. intersection.y = Math.round(intersection.y);
  48. add_node(Object.keys(vops).length, {"position":intersection});
  49. updateScene();
  50. });
  51. controls.addEventListener("change", render);
  52. }
  53. // Called on startup
  54. function init() {
  55. // Renderer
  56. renderer = new THREE.WebGLRenderer({"antialias":true});
  57. renderer.setSize(window.innerWidth, window.innerHeight);
  58. renderer.setClearColor(0xffffff, 1);
  59. document.querySelector("body").appendChild(renderer.domElement);
  60. // Time to load the materials
  61. loadMaterials();
  62. // Camera, controls, raycaster
  63. camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.3, 100);
  64. controls = new THREE.OrbitControls(camera);
  65. // Center the camera
  66. // TODO: frustrum
  67. controls.center.set(0, 0, 0);
  68. controls.rotateSpeed = 0.2;
  69. camera.position.set(0, 0, 40);
  70. // Run
  71. bind_events();
  72. scene = makeScene();
  73. connect_to_server();
  74. render();
  75. }