misc_boxselection.html 4.7 KB

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