misc_boxselection.html 5.1 KB

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