misc_controls_drag.html 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - drag controls</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: #444;
  12. }
  13. a {
  14. color: #08f;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <div id="info">
  20. <a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - drag controls
  21. </div>
  22. <script type="module">
  23. import * as THREE from '../build/three.module.js';
  24. import Stats from './jsm/libs/stats.module.js';
  25. import { DragControls } from './jsm/controls/DragControls.js';
  26. var container, stats;
  27. var camera, scene, renderer;
  28. var objects = [];
  29. init();
  30. animate();
  31. function init() {
  32. container = document.createElement( 'div' );
  33. document.body.appendChild( container );
  34. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 5000 );
  35. camera.position.z = 1000;
  36. scene = new THREE.Scene();
  37. scene.background = new THREE.Color( 0xf0f0f0 );
  38. scene.add( new THREE.AmbientLight( 0x505050 ) );
  39. var light = new THREE.SpotLight( 0xffffff, 1.5 );
  40. light.position.set( 0, 500, 2000 );
  41. light.angle = Math.PI / 9;
  42. light.castShadow = true;
  43. light.shadow.camera.near = 1000;
  44. light.shadow.camera.far = 4000;
  45. light.shadow.mapSize.width = 1024;
  46. light.shadow.mapSize.height = 1024;
  47. scene.add( light );
  48. var geometry = new THREE.BoxBufferGeometry( 40, 40, 40 );
  49. for ( var i = 0; i < 200; i ++ ) {
  50. var object = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: Math.random() * 0xffffff } ) );
  51. object.position.x = Math.random() * 1000 - 500;
  52. object.position.y = Math.random() * 600 - 300;
  53. object.position.z = Math.random() * 800 - 400;
  54. object.rotation.x = Math.random() * 2 * Math.PI;
  55. object.rotation.y = Math.random() * 2 * Math.PI;
  56. object.rotation.z = Math.random() * 2 * Math.PI;
  57. object.scale.x = Math.random() * 2 + 1;
  58. object.scale.y = Math.random() * 2 + 1;
  59. object.scale.z = Math.random() * 2 + 1;
  60. object.castShadow = true;
  61. object.receiveShadow = true;
  62. scene.add( object );
  63. objects.push( object );
  64. }
  65. renderer = new THREE.WebGLRenderer( { antialias: true } );
  66. renderer.setPixelRatio( window.devicePixelRatio );
  67. renderer.setSize( window.innerWidth, window.innerHeight );
  68. renderer.shadowMap.enabled = true;
  69. renderer.shadowMap.type = THREE.PCFShadowMap;
  70. container.appendChild( renderer.domElement );
  71. var controls = new DragControls( objects, camera, renderer.domElement );
  72. controls.addEventListener( 'dragstart', function ( event ) {
  73. event.object.material.emissive.set( 0xaaaaaa );
  74. } );
  75. controls.addEventListener( 'dragend', function ( event ) {
  76. event.object.material.emissive.set( 0x000000 );
  77. } );
  78. stats = new Stats();
  79. container.appendChild( stats.dom );
  80. //
  81. window.addEventListener( 'resize', onWindowResize, false );
  82. }
  83. function onWindowResize() {
  84. camera.aspect = window.innerWidth / window.innerHeight;
  85. camera.updateProjectionMatrix();
  86. renderer.setSize( window.innerWidth, window.innerHeight );
  87. }
  88. //
  89. function animate() {
  90. requestAnimationFrame( animate );
  91. render();
  92. stats.update();
  93. }
  94. function render() {
  95. renderer.render( scene, camera );
  96. }
  97. </script>
  98. </body>
  99. </html>