webgl_interactive_draggablecubes.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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. <style>
  8. body {
  9. font-family: Monospace;
  10. background-color: #f0f0f0;
  11. margin: 0px;
  12. overflow: hidden;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <script src="../build/three.js"></script>
  18. <script src="js/controls/DragControls.js"></script>
  19. <script src="js/controls/TrackballControls.js"></script>
  20. <script src="js/libs/stats.min.js"></script>
  21. <script>
  22. var container, stats;
  23. var camera, controls, scene, renderer;
  24. var objects = [];
  25. init();
  26. animate();
  27. function init() {
  28. container = document.createElement( 'div' );
  29. document.body.appendChild( container );
  30. camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 10000 );
  31. camera.position.z = 1000;
  32. controls = new THREE.TrackballControls( camera );
  33. controls.rotateSpeed = 1.0;
  34. controls.zoomSpeed = 1.2;
  35. controls.panSpeed = 0.8;
  36. controls.noZoom = false;
  37. controls.noPan = false;
  38. controls.staticMoving = true;
  39. controls.dynamicDampingFactor = 0.3;
  40. scene = new THREE.Scene();
  41. scene.add( new THREE.AmbientLight( 0x505050 ) );
  42. var light = new THREE.SpotLight( 0xffffff, 1.5 );
  43. light.position.set( 0, 500, 2000 );
  44. light.castShadow = true;
  45. light.shadow = new THREE.LightShadow( new THREE.PerspectiveCamera( 50, 1, 200, 10000 ) );
  46. light.shadow.bias = - 0.00022;
  47. light.shadow.mapSize.width = 2048;
  48. light.shadow.mapSize.height = 2048;
  49. scene.add( light );
  50. var geometry = new THREE.BoxGeometry( 40, 40, 40 );
  51. for ( var i = 0; i < 200; i ++ ) {
  52. var object = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: Math.random() * 0xffffff } ) );
  53. object.position.x = Math.random() * 1000 - 500;
  54. object.position.y = Math.random() * 600 - 300;
  55. object.position.z = Math.random() * 800 - 400;
  56. object.rotation.x = Math.random() * 2 * Math.PI;
  57. object.rotation.y = Math.random() * 2 * Math.PI;
  58. object.rotation.z = Math.random() * 2 * Math.PI;
  59. object.scale.x = Math.random() * 2 + 1;
  60. object.scale.y = Math.random() * 2 + 1;
  61. object.scale.z = Math.random() * 2 + 1;
  62. object.castShadow = true;
  63. object.receiveShadow = true;
  64. scene.add( object );
  65. objects.push( object );
  66. }
  67. renderer = new THREE.WebGLRenderer( { antialias: true } );
  68. renderer.setClearColor( 0xf0f0f0 );
  69. renderer.setPixelRatio( window.devicePixelRatio );
  70. renderer.setSize( window.innerWidth, window.innerHeight );
  71. renderer.sortObjects = false;
  72. renderer.shadowMap.enabled = true;
  73. renderer.shadowMap.type = THREE.PCFShadowMap;
  74. container.appendChild( renderer.domElement );
  75. var dragControls = new THREE.DragControls( camera, objects, renderer.domElement );
  76. dragControls.enabled = true;
  77. dragControls.addEventListener( 'dragstart', function ( e ) { controls.enabled = false; } );
  78. dragControls.addEventListener( 'dragend', function ( e ) { controls.enabled = true; } );
  79. var info = document.createElement( 'div' );
  80. info.style.position = 'absolute';
  81. info.style.top = '10px';
  82. info.style.width = '100%';
  83. info.style.textAlign = 'center';
  84. info.innerHTML = '<a href="http://threejs.org" target="_blank">three.js</a> webgl - draggable cubes';
  85. container.appendChild( info );
  86. stats = new Stats();
  87. container.appendChild( stats.dom );
  88. //
  89. window.addEventListener( 'resize', onWindowResize, false );
  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. controls.update();
  104. renderer.render( scene, camera );
  105. }
  106. </script>
  107. </body>
  108. </html>