misc_boxselection.html 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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="importmap">
  29. {
  30. "imports": {
  31. "three": "../build/three.module.js",
  32. "three/addons/": "./jsm/"
  33. }
  34. }
  35. </script>
  36. <script type="module">
  37. import * as THREE from 'three';
  38. import Stats from 'three/addons/libs/stats.module.js';
  39. import { SelectionBox } from 'three/addons/interactive/SelectionBox.js';
  40. import { SelectionHelper } from 'three/addons/interactive/SelectionHelper.js';
  41. let container, stats;
  42. let camera, scene, renderer;
  43. init();
  44. function init() {
  45. container = document.createElement( 'div' );
  46. document.body.appendChild( container );
  47. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 0.1, 500 );
  48. camera.position.z = 50;
  49. scene = new THREE.Scene();
  50. scene.background = new THREE.Color( 0xf0f0f0 );
  51. scene.add( new THREE.AmbientLight( 0xaaaaaa ) );
  52. const light = new THREE.SpotLight( 0xffffff, 10000 );
  53. light.position.set( 0, 25, 50 );
  54. light.angle = Math.PI / 5;
  55. light.castShadow = true;
  56. light.shadow.camera.near = 10;
  57. light.shadow.camera.far = 100;
  58. light.shadow.mapSize.width = 1024;
  59. light.shadow.mapSize.height = 1024;
  60. scene.add( light );
  61. const geometry = new THREE.BoxGeometry();
  62. for ( let i = 0; i < 200; i ++ ) {
  63. const object = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: Math.random() * 0xffffff } ) );
  64. object.position.x = Math.random() * 80 - 40;
  65. object.position.y = Math.random() * 45 - 25;
  66. object.position.z = Math.random() * 45 - 25;
  67. object.rotation.x = Math.random() * 2 * Math.PI;
  68. object.rotation.y = Math.random() * 2 * Math.PI;
  69. object.rotation.z = Math.random() * 2 * Math.PI;
  70. object.scale.x = Math.random() * 2 + 1;
  71. object.scale.y = Math.random() * 2 + 1;
  72. object.scale.z = Math.random() * 2 + 1;
  73. object.castShadow = true;
  74. object.receiveShadow = true;
  75. scene.add( object );
  76. }
  77. renderer = new THREE.WebGLRenderer( { antialias: true } );
  78. renderer.setPixelRatio( window.devicePixelRatio );
  79. renderer.setSize( window.innerWidth, window.innerHeight );
  80. renderer.setAnimationLoop( animate );
  81. renderer.shadowMap.enabled = true;
  82. renderer.shadowMap.type = THREE.PCFShadowMap;
  83. container.appendChild( renderer.domElement );
  84. stats = new Stats();
  85. container.appendChild( stats.dom );
  86. window.addEventListener( 'resize', onWindowResize );
  87. }
  88. function onWindowResize() {
  89. camera.aspect = window.innerWidth / window.innerHeight;
  90. camera.updateProjectionMatrix();
  91. renderer.setSize( window.innerWidth, window.innerHeight );
  92. }
  93. //
  94. function animate() {
  95. renderer.render( scene, camera );
  96. stats.update();
  97. }
  98. const selectionBox = new SelectionBox( camera, scene );
  99. const helper = new SelectionHelper( renderer, 'selectBox' );
  100. document.addEventListener( 'pointerdown', function ( event ) {
  101. for ( const item of selectionBox.collection ) {
  102. item.material.emissive.set( 0x000000 );
  103. }
  104. selectionBox.startPoint.set(
  105. ( event.clientX / window.innerWidth ) * 2 - 1,
  106. - ( event.clientY / window.innerHeight ) * 2 + 1,
  107. 0.5 );
  108. } );
  109. document.addEventListener( 'pointermove', function ( event ) {
  110. if ( helper.isDown ) {
  111. for ( let i = 0; i < selectionBox.collection.length; i ++ ) {
  112. selectionBox.collection[ i ].material.emissive.set( 0x000000 );
  113. }
  114. selectionBox.endPoint.set(
  115. ( event.clientX / window.innerWidth ) * 2 - 1,
  116. - ( event.clientY / window.innerHeight ) * 2 + 1,
  117. 0.5 );
  118. const allSelected = selectionBox.select();
  119. for ( let i = 0; i < allSelected.length; i ++ ) {
  120. allSelected[ i ].material.emissive.set( 0xffffff );
  121. }
  122. }
  123. } );
  124. document.addEventListener( 'pointerup', function ( event ) {
  125. selectionBox.endPoint.set(
  126. ( event.clientX / window.innerWidth ) * 2 - 1,
  127. - ( event.clientY / window.innerHeight ) * 2 + 1,
  128. 0.5 );
  129. const allSelected = selectionBox.select();
  130. for ( let i = 0; i < allSelected.length; i ++ ) {
  131. allSelected[ i ].material.emissive.set( 0xffffff );
  132. }
  133. } );
  134. </script>
  135. </body>
  136. </html>