You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

217 lines
5.0 KiB

<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js webgl - drag controls</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
<link type="text/css" rel="stylesheet" href="main.css">
<style>
body {
background-color: #f0f0f0;
color: #444;
}
a {
color: #08f;
}
</style>
</head>
<body>
<div id="info">
<a href="https://threejs.org" target="_blank" rel="noopener">three.js</a> webgl - drag controls<br />
Use "Shift+Click" to add/remove objects to/from a group.<br />
Grouped objects can be transformed as a union.
</div>
<!-- Import maps polyfill -->
<!-- Remove this when import maps will be widely supported -->
<script async src="https://unpkg.com/es-module-shims@1.3.6/dist/es-module-shims.js"></script>
<script type="importmap">
{
"imports": {
"three": "../build/three.module.js",
"three/addons/": "./jsm/"
}
}
</script>
<script type="module">
import * as THREE from 'three';
import { DragControls } from 'three/addons/controls/DragControls.js';
let container;
let camera, scene, renderer;
let controls, group;
let enableSelection = false;
const objects = [];
const mouse = new THREE.Vector2(), raycaster = new THREE.Raycaster();
init();
function init() {
container = document.createElement( 'div' );
document.body.appendChild( container );
camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 5000 );
camera.position.z = 1000;
scene = new THREE.Scene();
scene.background = new THREE.Color( 0xf0f0f0 );
scene.add( new THREE.AmbientLight( 0x505050 ) );
const light = new THREE.SpotLight( 0xffffff, 1.5 );
light.position.set( 0, 500, 2000 );
light.angle = Math.PI / 9;
light.castShadow = true;
light.shadow.camera.near = 1000;
light.shadow.camera.far = 4000;
light.shadow.mapSize.width = 1024;
light.shadow.mapSize.height = 1024;
scene.add( light );
group = new THREE.Group();
scene.add( group );
const geometry = new THREE.BoxGeometry( 40, 40, 40 );
for ( let i = 0; i < 200; i ++ ) {
const 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.setPixelRatio( window.devicePixelRatio );
renderer.setSize( window.innerWidth, window.innerHeight );
renderer.shadowMap.enabled = true;
renderer.shadowMap.type = THREE.PCFShadowMap;
container.appendChild( renderer.domElement );
controls = new DragControls( [ ... objects ], camera, renderer.domElement );
controls.addEventListener( 'drag', render );
//
window.addEventListener( 'resize', onWindowResize );
document.addEventListener( 'click', onClick );
window.addEventListener( 'keydown', onKeyDown );
window.addEventListener( 'keyup', onKeyUp );
render();
}
function onWindowResize() {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
render();
}
function onKeyDown( event ) {
enableSelection = ( event.keyCode === 16 ) ? true : false;
}
function onKeyUp() {
enableSelection = false;
}
function onClick( event ) {
event.preventDefault();
if ( enableSelection === true ) {
const draggableObjects = controls.getObjects();
draggableObjects.length = 0;
mouse.x = ( event.clientX / window.innerWidth ) * 2 - 1;
mouse.y = - ( event.clientY / window.innerHeight ) * 2 + 1;
raycaster.setFromCamera( mouse, camera );
const intersections = raycaster.intersectObjects( objects, true );
if ( intersections.length > 0 ) {
const object = intersections[ 0 ].object;
if ( group.children.includes( object ) === true ) {
object.material.emissive.set( 0x000000 );
scene.attach( object );
} else {
object.material.emissive.set( 0xaaaaaa );
group.attach( object );
}
controls.transformGroup = true;
draggableObjects.push( group );
}
if ( group.children.length === 0 ) {
controls.transformGroup = false;
draggableObjects.push( ...objects );
}
}
render();
}
function render() {
renderer.render( scene, camera );
}
</script>
</body>
</html>