<!DOCTYPE html>
<html lang="en">
<head>
<title>three.js canvas - geometry - cube</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.min.js"></script>
<script src="js/libs/stats.min.js"></script>
<script>
var container, stats;// статус и контейнер
var camera, scene, renderer;// камера сцена вращение
var cube1, cube2, cube3, plane;// кубы и план камеры
var targetRotation = 0;// движение при нажатии
var targetRotationOnMouseDown = 0;//нажатие при двжении
var mouseX = 0, mouseX1=0;
var mouseXOnMouseDown = 0;
var windowHalfX = window.innerWidth / 2;
var windowHalfY = window.innerHeight / 2;
var phi=0, R=60;
var megalit;
var projector, particleMaterial;
var menu_text1, menu_text2, menu_text3, menu_text4, menu_text5, menu_text6; //надписи текст
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 = 'Drag to spin the cube';
container.appendChild( info );
camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
camera.position.y = 150;
camera.position.z = 500;
scene = new THREE.Scene();
var menu_podarki = "Подарки";
//var hash = document.location.hash.substr( 1 );
//if ( hash.length !== 0 ) {
// menu_podarki = hash;
//}
menu_text1 = new THREE.TextGeometry( menu_podarki, {
size: 80,
height: 20,
curveSegments: 2,
font: "helvetiker"
});
menu_text1.computeBoundingBox();
var centerOffset = -0.5 * ( menu_text1.boundingBox.max.x - menu_text1.boundingBox.min.x );
var textMaterial = new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff, overdraw: true } );
var text1 = new THREE.Mesh( menu_text1, textMaterial );
text1.rotation.y = 120 * Math.PI/180;
text1.position.set = (-50* Math.cos(text1.rotation.y), 0, 50* Math.sin(text1.rotation.y));
//menu_text1.position.y = 100;
//menu_text1.position.z = 0;
//group = new THREE.Object3D();
//group.add( text1 );
//scene.add( group );
// Cube
var geometry = new THREE.CubeGeometry( 100, 300, 5 );
var material = new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff, opacity: 0.5, transparent: true });
cube1 = new THREE.Mesh( geometry, material );
cube1.rotation.y = 120 * Math.PI/180;
cube1.position.set(-50* Math.cos(cube1.rotation.y), 0, 50* Math.sin(cube1.rotation.y));
var material = new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff, opacity: 0.5, transparent: true });
cube2 = new THREE.Mesh( geometry, material );
cube2.rotation.y = 240 * Math.PI/180;
cube2.position.set(-50* Math.cos(cube2.rotation.y), 0, 50* Math.sin(cube2.rotation.y));
var material = new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff, opacity: 0.5, transparent: true });
cube3 = new THREE.Mesh( geometry, material );
cube3.rotation.y = 360 * Math.PI/180;
cube3.position.set(-50* Math.cos(cube3.rotation.y), 0, 50* Math.sin(cube3.rotation.y));
// для прорисовки
megalit = new THREE.Object3D();
megalit.add(cube1);
megalit.add(cube2);
megalit.add(cube3);
megalit.add(text1);
// добавление текста
scene.add( megalit );
// для выбора
projector = new THREE.Projector(), objects = [];
objects.push(cube1);
objects.push(cube2);
objects.push(cube3);
objects.push(text1);
// Plane
var geometry = new THREE.PlaneGeometry( 200, 200 );
geometry.applyMatrix( new THREE.Matrix4().makeRotationX( - Math.PI / 2 ) );
var material = new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff, opacity: 0.5, transparent: true } );
//plane = new THREE.Mesh( geometry, material );
//scene.add( plane );
renderer = new THREE.WebGLRenderer();
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 );
document.addEventListener( 'touchstart', onDocumentTouchStart, false );
document.addEventListener( 'touchmove', onDocumentTouchMove, false );
//
window.addEventListener( 'resize', onWindowResize, false );
var PI2 = Math.PI * 2;
particleMaterial = new THREE.SpriteCanvasMaterial( {
color: 0x000000,
program: function ( context ) {
context.beginPath();
context.arc( 0, 0, 0.5, 0, PI2, true );
context.fill();
}
});
}
function onWindowResize() {
windowHalfX = window.innerWidth / 2;
windowHalfY = window.innerHeight / 2;
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize( window.innerWidth, window.innerHeight );
}
//
function onDocumentMouseDown( event ) {
event.preventDefault();
document.addEventListener( 'mousemove', onDocumentMouseMove, false );
document.addEventListener( 'mouseup', onDocumentMouseUp, false );
document.addEventListener( 'mouseout', onDocumentMouseOut, false );
mouseXOnMouseDown = event.clientX - windowHalfX;
targetRotationOnMouseDown = targetRotation;
//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 raycaster = new THREE.Raycaster( camera.position, vector.sub( camera.position ).normalize() );//Класс Raycaster создан на основе класса Ray и обеспечивает обнаружение пересечения 3D-объектов.
var intersects = raycaster.intersectObjects( objects );//объект проверямый на пересечение с вектором
if ( intersects.length > 0 ) {//количество объектов
intersects[ 0 ].object.material.color.setHex( Math.random() * 0xffffff );//цвет материала объекта
//var particle = new THREE.Sprite( particleMaterial );//создание точки
//particle.position = intersects[ 0 ].point;// присвоение координат точке
//particle.scale.x = particle.scale.y = 16;//диаметр точки
//scene.add( particle );//добавление на сцену точки
}
}
function onDocumentMouseMove( event ) {
mouseX = event.clientX - windowHalfX;
if(mouseX>mouseX1){
phi=phi+0.05
}
else{
phi=phi-0.05;
}
mouseX1=mouseX;
targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.02;
megalit.rotation.y = phi;
}
function onDocumentMouseUp( event ) {
document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
}
function onDocumentMouseOut( event ) {
document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
document.removeEventListener( 'mouseout', onDocumentMouseOut, false );
}
function onDocumentTouchStart( event ) {
if ( event.touches.length === 1 ) {
event.preventDefault();
mouseXOnMouseDown = event.touches[ 0 ].pageX - windowHalfX;
targetRotationOnMouseDown = targetRotation;
}
}
function onDocumentTouchMove( event ) {
if ( event.touches.length === 1 ) {
event.preventDefault();
mouseX = event.touches[ 0 ].pageX - windowHalfX;
targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.05;
}
}
//
function animate() {
requestAnimationFrame( animate );
render();
stats.update();
}
function render() {
renderer.render( scene, camera );
}
</script>
</body>
</html>
|