123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- <!DOCTYPE html>
- <html lang="en">
- <head>
- <title>three.js webgl - draggable cubes</title>
- <meta charset="utf-8">
- <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
- <style>
- body {
- font-family: Monospace;
- background-color: #f0f0f0;
- margin: 0px;
- overflow: hidden;
- }
- </style>
- </head>
- <body>
- <script src="../build/three.js"></script>
- <script src="js/controls/DragControls.js"></script>
- <script src="js/controls/TrackballControls.js"></script>
- <script src="js/libs/stats.min.js"></script>
- <script>
- var container, stats;
- var camera, controls, scene, renderer;
- var objects = [];
- init();
- animate();
- function init() {
- container = document.createElement( 'div' );
- document.body.appendChild( container );
- camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 10000 );
- camera.position.z = 1000;
- controls = new THREE.TrackballControls( camera );
- controls.rotateSpeed = 1.0;
- controls.zoomSpeed = 1.2;
- controls.panSpeed = 0.8;
- controls.noZoom = false;
- controls.noPan = false;
- controls.staticMoving = true;
- controls.dynamicDampingFactor = 0.3;
- scene = new THREE.Scene();
- scene.add( new THREE.AmbientLight( 0x505050 ) );
- var light = new THREE.SpotLight( 0xffffff, 1.5 );
- light.position.set( 0, 500, 2000 );
- light.castShadow = true;
- light.shadow = new THREE.LightShadow( new THREE.PerspectiveCamera( 50, 1, 200, 10000 ) );
- light.shadow.bias = - 0.00022;
- light.shadow.mapSize.width = 2048;
- light.shadow.mapSize.height = 2048;
- scene.add( light );
- var geometry = new THREE.BoxGeometry( 40, 40, 40 );
- for ( var i = 0; i < 200; i ++ ) {
- var object = new THREE.Mesh( geometry, new THREE.MeshLambertMaterial( { color: Math.random() * 0xffffff } ) );
- object.position.x = Math.random() * 1000 - 500;
- object.position.y = Math.random() * 600 - 300;
- object.position.z = Math.random() * 800 - 400;
- object.rotation.x = Math.random() * 2 * Math.PI;
- object.rotation.y = Math.random() * 2 * Math.PI;
- object.rotation.z = Math.random() * 2 * Math.PI;
- object.scale.x = Math.random() * 2 + 1;
- object.scale.y = Math.random() * 2 + 1;
- object.scale.z = Math.random() * 2 + 1;
- object.castShadow = true;
- object.receiveShadow = true;
- scene.add( object );
- objects.push( object );
- }
- renderer = new THREE.WebGLRenderer( { antialias: true } );
- renderer.setClearColor( 0xf0f0f0 );
- renderer.setPixelRatio( window.devicePixelRatio );
- renderer.setSize( window.innerWidth, window.innerHeight );
- renderer.sortObjects = false;
- renderer.shadowMap.enabled = true;
- renderer.shadowMap.type = THREE.PCFShadowMap;
- container.appendChild( renderer.domElement );
- var dragControls = new THREE.DragControls( camera, objects, renderer.domElement );
- dragControls.enabled = true;
- dragControls.addEventListener( 'dragstart', function ( e ) { controls.enabled = false; } );
- dragControls.addEventListener( 'dragend', function ( e ) { controls.enabled = true; } );
- var info = document.createElement( 'div' );
- info.style.position = 'absolute';
- info.style.top = '10px';
- info.style.width = '100%';
- info.style.textAlign = 'center';
- info.innerHTML = '<a href="http://threejs.org" target="_blank">three.js</a> webgl - draggable cubes';
- container.appendChild( info );
- stats = new Stats();
- container.appendChild( stats.dom );
- //
- window.addEventListener( 'resize', onWindowResize, false );
- }
- function onWindowResize() {
- camera.aspect = window.innerWidth / window.innerHeight;
- camera.updateProjectionMatrix();
- renderer.setSize( window.innerWidth, window.innerHeight );
- }
- //
- function animate() {
- requestAnimationFrame( animate );
- render();
- stats.update();
- }
- function render() {
- controls.update();
- renderer.render( scene, camera );
- }
- </script>
- </body>
- </html>
|