webgl_interactive_draggablecubes.html 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <title>three.js webgl - draggable cubes</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="http://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - draggable cubes
  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 { TrackballControls } from './jsm/controls/TrackballControls.js';
  26. import { DragControls } from './jsm/controls/DragControls.js';
  27. var container, stats;
  28. var camera, controls, scene, renderer;
  29. var objects = [];
  30. init();
  31. animate();
  32. function init() {
  33. container = document.createElement( 'div' );
  34. document.body.appendChild( container );
  35. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 5000 );
  36. camera.position.z = 1000;
  37. scene = new THREE.Scene();
  38. scene.background = new THREE.Color( 0xf0f0f0 );
  39. scene.add( new THREE.AmbientLight( 0x505050 ) );
  40. var light = new THREE.SpotLight( 0xffffff, 1.5 );
  41. light.position.set( 0, 500, 2000 );
  42. light.angle = Math.PI / 9;
  43. light.castShadow = true;
  44. light.shadow.camera.near = 1000;
  45. light.shadow.camera.far = 4000;
  46. light.shadow.mapSize.width = 1024;
  47. light.shadow.mapSize.height = 1024;
  48. scene.add( light );
  49. var geometry = new THREE.BoxBufferGeometry( 40, 40, 40 );
  50. for ( var i = 0; i < 200; i ++ ) {
  51. var object = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: Math.random() * 0xffffff } ) );
  52. object.position.x = Math.random() * 1000 - 500;
  53. object.position.y = Math.random() * 600 - 300;
  54. object.position.z = Math.random() * 800 - 400;
  55. object.rotation.x = Math.random() * 2 * Math.PI;
  56. object.rotation.y = Math.random() * 2 * Math.PI;
  57. object.rotation.z = Math.random() * 2 * Math.PI;
  58. object.scale.x = Math.random() * 2 + 1;
  59. object.scale.y = Math.random() * 2 + 1;
  60. object.scale.z = Math.random() * 2 + 1;
  61. object.castShadow = true;
  62. object.receiveShadow = true;
  63. scene.add( object );
  64. objects.push( object );
  65. }
  66. renderer = new THREE.WebGLRenderer( { antialias: true } );
  67. renderer.setPixelRatio( window.devicePixelRatio );
  68. renderer.setSize( window.innerWidth, window.innerHeight );
  69. renderer.shadowMap.enabled = true;
  70. renderer.shadowMap.type = THREE.PCFShadowMap;
  71. container.appendChild( renderer.domElement );
  72. controls = new TrackballControls( camera, renderer.domElement );
  73. controls.rotateSpeed = 1.0;
  74. controls.zoomSpeed = 1.2;
  75. controls.panSpeed = 0.8;
  76. controls.noZoom = false;
  77. controls.noPan = false;
  78. controls.staticMoving = true;
  79. controls.dynamicDampingFactor = 0.3;
  80. var dragControls = new DragControls( objects, camera, renderer.domElement );
  81. dragControls.addEventListener( 'dragstart', function () {
  82. controls.enabled = false;
  83. } );
  84. dragControls.addEventListener( 'dragend', function () {
  85. controls.enabled = true;
  86. } );
  87. stats = new Stats();
  88. container.appendChild( stats.dom );
  89. //
  90. window.addEventListener( 'resize', onWindowResize, false );
  91. }
  92. function onWindowResize() {
  93. camera.aspect = window.innerWidth / window.innerHeight;
  94. camera.updateProjectionMatrix();
  95. renderer.setSize( window.innerWidth, window.innerHeight );
  96. }
  97. //
  98. function animate() {
  99. requestAnimationFrame( animate );
  100. render();
  101. stats.update();
  102. }
  103. function render() {
  104. controls.update();
  105. renderer.render( scene, camera );
  106. }
  107. </script>
  108. </body>
  109. </html>