misc_boxselection.html 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - box selection</title>
  5. <meta charset="utf-8">
  6. <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
  7. <link type="text/css" rel="stylesheet" href="main.css">
  8. <style>
  9. body {
  10. background-color: #f0f0f0;
  11. color: #000;
  12. }
  13. a {
  14. color: #08e;
  15. }
  16. .selectBox {
  17. border: 1px solid #55aaff;
  18. background-color: rgba(75, 160, 255, 0.3);
  19. position: fixed;
  20. }
  21. </style>
  22. </head>
  23. <body>
  24. <div id="info">
  25. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - box selection
  26. </div>
  27. <script type="module">
  28. import * as THREE from '../build/three.module.js';
  29. import Stats from './jsm/libs/stats.module.js';
  30. import { SelectionBox } from './jsm/interactive/SelectionBox.js';
  31. import { SelectionHelper } from './jsm/interactive/SelectionHelper.js';
  32. var container, stats;
  33. var camera, scene, renderer;
  34. init();
  35. animate();
  36. function init() {
  37. container = document.createElement( 'div' );
  38. document.body.appendChild( container );
  39. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 5000 );
  40. camera.position.z = 1000;
  41. scene = new THREE.Scene();
  42. scene.background = new THREE.Color( 0xf0f0f0 );
  43. scene.add( new THREE.AmbientLight( 0x505050 ) );
  44. var light = new THREE.SpotLight( 0xffffff, 1.5 );
  45. light.position.set( 0, 500, 2000 );
  46. light.angle = Math.PI / 9;
  47. light.castShadow = true;
  48. light.shadow.camera.near = 1000;
  49. light.shadow.camera.far = 4000;
  50. light.shadow.mapSize.width = 1024;
  51. light.shadow.mapSize.height = 1024;
  52. scene.add( light );
  53. var geometry = new THREE.BoxBufferGeometry( 20, 20, 20 );
  54. for ( var i = 0; i < 200; i ++ ) {
  55. var object = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: Math.random() * 0xffffff } ) );
  56. object.position.x = Math.random() * 1600 - 800;
  57. object.position.y = Math.random() * 900 - 450;
  58. object.position.z = Math.random() * 900 - 500;
  59. object.rotation.x = Math.random() * 2 * Math.PI;
  60. object.rotation.y = Math.random() * 2 * Math.PI;
  61. object.rotation.z = Math.random() * 2 * Math.PI;
  62. object.scale.x = Math.random() * 2 + 1;
  63. object.scale.y = Math.random() * 2 + 1;
  64. object.scale.z = Math.random() * 2 + 1;
  65. object.castShadow = true;
  66. object.receiveShadow = true;
  67. scene.add( object );
  68. }
  69. renderer = new THREE.WebGLRenderer( { antialias: true } );
  70. renderer.setPixelRatio( window.devicePixelRatio );
  71. renderer.setSize( window.innerWidth, window.innerHeight );
  72. renderer.shadowMap.enabled = true;
  73. renderer.shadowMap.type = THREE.PCFShadowMap;
  74. container.appendChild( renderer.domElement );
  75. stats = new Stats();
  76. container.appendChild( stats.dom );
  77. window.addEventListener( 'resize', onWindowResize, false );
  78. }
  79. function onWindowResize() {
  80. camera.aspect = window.innerWidth / window.innerHeight;
  81. camera.updateProjectionMatrix();
  82. renderer.setSize( window.innerWidth, window.innerHeight );
  83. }
  84. //
  85. function animate() {
  86. requestAnimationFrame( animate );
  87. render();
  88. stats.update();
  89. }
  90. function render() {
  91. renderer.render( scene, camera );
  92. }
  93. var selectionBox = new SelectionBox( camera, scene );
  94. var helper = new SelectionHelper( selectionBox, renderer, 'selectBox' );
  95. document.addEventListener( 'mousedown', function ( event ) {
  96. for ( var item of selectionBox.collection ) {
  97. item.material.emissive.set( 0x000000 );
  98. }
  99. selectionBox.startPoint.set(
  100. ( event.clientX / window.innerWidth ) * 2 - 1,
  101. - ( event.clientY / window.innerHeight ) * 2 + 1,
  102. 0.5 );
  103. } );
  104. document.addEventListener( 'mousemove', function ( event ) {
  105. if ( helper.isDown ) {
  106. for ( var i = 0; i < selectionBox.collection.length; i ++ ) {
  107. selectionBox.collection[ i ].material.emissive.set( 0x000000 );
  108. }
  109. selectionBox.endPoint.set(
  110. ( event.clientX / window.innerWidth ) * 2 - 1,
  111. - ( event.clientY / window.innerHeight ) * 2 + 1,
  112. 0.5 );
  113. var allSelected = selectionBox.select();
  114. for ( var i = 0; i < allSelected.length; i ++ ) {
  115. allSelected[ i ].material.emissive.set( 0xffffff );
  116. }
  117. }
  118. } );
  119. document.addEventListener( 'mouseup', function ( event ) {
  120. selectionBox.endPoint.set(
  121. ( event.clientX / window.innerWidth ) * 2 - 1,
  122. - ( event.clientY / window.innerHeight ) * 2 + 1,
  123. 0.5 );
  124. var allSelected = selectionBox.select();
  125. for ( var i = 0; i < allSelected.length; i ++ ) {
  126. allSelected[ i ].material.emissive.set( 0xffffff );
  127. }
  128. } );
  129. </script>
  130. </body>
  131. </html>