webgl_interactive_draggablecubes.html 4.0 KB

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