mirror of
https://github.com/wahyd4/three.js.git
synced 2026-08-09 04:56:01 +10:00
155 lines
4.2 KiB
HTML
155 lines
4.2 KiB
HTML
<!doctype html>
|
|
<html lang="en">
|
|
<head>
|
|
<title>three.js canvas - interactive - cubes tween</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/Tween.js"></script>
|
|
<script src="js/RequestAnimationFrame.js"></script>
|
|
<script src="js/Stats.js"></script>
|
|
|
|
<script>
|
|
|
|
var container, stats;
|
|
var camera, scene, projector, renderer;
|
|
|
|
init();
|
|
animate();
|
|
|
|
function init() {
|
|
|
|
container = document.createElement( 'div' );
|
|
document.body.appendChild( container );
|
|
|
|
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://github.com/mrdoob/three.js" target="_blank">three.js</a> - clickable objects';
|
|
container.appendChild( info );
|
|
|
|
camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 10000 );
|
|
camera.position.y = 300;
|
|
camera.position.z = 500;
|
|
|
|
scene = new THREE.Scene();
|
|
|
|
var geometry = new THREE.CubeGeometry( 100, 100, 100 );
|
|
|
|
for ( var i = 0; i < 20; i ++ ) {
|
|
|
|
var object = new THREE.Mesh( geometry, [ new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff, opacity: 0.5 } ), new THREE.MeshBasicMaterial( { color: 0xffffff, opacity: 0.5, wireframe: true } ) ] );
|
|
object.position.x = Math.random() * 800 - 400;
|
|
object.position.y = Math.random() * 800 - 400;
|
|
object.position.z = Math.random() * 800 - 400;
|
|
object.scale.x = Math.random() * 2 + 1;
|
|
object.scale.y = Math.random() * 2 + 1;
|
|
object.scale.z = Math.random() * 2 + 1;
|
|
object.rotation.x = ( Math.random() * 360 ) * Math.PI / 180;
|
|
object.rotation.y = ( Math.random() * 360 ) * Math.PI / 180;
|
|
object.rotation.z = ( Math.random() * 360 ) * Math.PI / 180;
|
|
scene.add( object );
|
|
|
|
}
|
|
|
|
projector = new THREE.Projector();
|
|
|
|
renderer = new THREE.CanvasRenderer();
|
|
renderer.setSize( window.innerWidth, window.innerHeight );
|
|
|
|
container.appendChild(renderer.domElement);
|
|
|
|
stats = new Stats();
|
|
stats.domElement.style.position = 'absolute';
|
|
stats.domElement.style.top = '0px';
|
|
container.appendChild( stats.domElement );
|
|
|
|
document.addEventListener( 'mousedown', onDocumentMouseDown, false );
|
|
|
|
}
|
|
|
|
function onDocumentMouseDown( event ) {
|
|
|
|
event.preventDefault();
|
|
|
|
var vector = new THREE.Vector3( ( event.clientX / window.innerWidth ) * 2 - 1, - ( event.clientY / window.innerHeight ) * 2 + 1, 0.5 );
|
|
projector.unprojectVector( vector, camera );
|
|
|
|
var ray = new THREE.Ray( camera.position, vector.subSelf( camera.position ).normalize() );
|
|
|
|
var intersects = ray.intersectScene( scene );
|
|
|
|
if ( intersects.length > 0 ) {
|
|
|
|
new TWEEN.Tween( intersects[ 0 ].object.position ).to( {
|
|
x: Math.random() * 800 - 400,
|
|
y: Math.random() * 800 - 400,
|
|
z: Math.random() * 800 - 400 }, 2000 )
|
|
.easing( TWEEN.Easing.Elastic.EaseOut).start();
|
|
|
|
new TWEEN.Tween( intersects[ 0 ].object.rotation ).to( {
|
|
x: ( Math.random() * 360 ) * Math.PI / 180,
|
|
y: ( Math.random() * 360 ) * Math.PI / 180,
|
|
z: ( Math.random() * 360 ) * Math.PI / 180 }, 2000 )
|
|
.easing( TWEEN.Easing.Elastic.EaseOut).start();
|
|
|
|
}
|
|
|
|
/*
|
|
// Parse all the faces
|
|
for ( var i in intersects ) {
|
|
|
|
intersects[ i ].face.material[ 0 ].color.setHex( Math.random() * 0xffffff | 0x80000000 );
|
|
|
|
}
|
|
*/
|
|
}
|
|
|
|
//
|
|
|
|
function animate() {
|
|
|
|
requestAnimationFrame( animate );
|
|
|
|
render();
|
|
stats.update();
|
|
|
|
}
|
|
|
|
var radius = 600;
|
|
var theta = 0;
|
|
|
|
function render() {
|
|
|
|
TWEEN.update();
|
|
|
|
theta += 0.2;
|
|
|
|
camera.position.x = radius * Math.sin( theta * Math.PI / 360 );
|
|
camera.position.y = radius * Math.sin( theta * Math.PI / 360 );
|
|
camera.position.z = radius * Math.cos( theta * Math.PI / 360 );
|
|
camera.lookAt( scene.position );
|
|
|
|
renderer.render( scene, camera );
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
</body>
|
|
</html>
|