mirror of
https://github.com/wahyd4/three.js.git
synced 2026-08-25 04:46:02 +10:00
322 lines
8.6 KiB
HTML
322 lines
8.6 KiB
HTML
<!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.js"></script>
|
|
|
|
<script src="js/RequestAnimationFrame.js"></script>
|
|
<script src="js/Stats.js"></script>
|
|
|
|
<script src="../src/core/Geometry.js"></script>
|
|
<script src="../src/extras/geometries/CubeGeometry.js"></script>
|
|
<script src="../src/extras/geometries/CylinderGeometry.js"></script>
|
|
<script src="../src/extras/geometries/TorusGeometry.js"></script>
|
|
<script src="../src/extras/geometries/SubdivisionGeometry.js"></script>
|
|
|
|
<script src="fonts/helvetiker_regular.typeface.js"></script>
|
|
|
|
<script>
|
|
|
|
var container, stats;
|
|
|
|
var camera, scene, renderer;
|
|
|
|
var cube, plane;
|
|
|
|
var targetRotation = 0;
|
|
var targetRotationOnMouseDown = 0;
|
|
|
|
var mouseX = 0;
|
|
var mouseXOnMouseDown = 0;
|
|
|
|
var windowHalfX = window.innerWidth / 2;
|
|
var windowHalfY = window.innerHeight / 2;
|
|
|
|
|
|
// Create subdivision geometry
|
|
function createSubdivision(geometry, repeats) {
|
|
repeats = (repeats === undefined ) ? 1 : repeats;
|
|
var smooth = geometry;
|
|
while (repeats--) {
|
|
smooth = new THREE.SubdivisionGeometry(smooth);
|
|
}
|
|
return smooth;
|
|
}
|
|
|
|
// Create new object by parameters
|
|
var createSomething = function(klass, args) {
|
|
var F = function(klass, args) {
|
|
return klass.apply(this, args);
|
|
}
|
|
F.prototype = klass.prototype;
|
|
|
|
return new F(klass, args);
|
|
};
|
|
|
|
init();
|
|
animate();
|
|
|
|
var info;
|
|
|
|
function init() {
|
|
|
|
container = document.createElement( 'div' );
|
|
document.body.appendChild( container );
|
|
|
|
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 geometry ';
|
|
container.appendChild( info );
|
|
|
|
camera = new THREE.Camera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
|
|
camera.position.y = 150;
|
|
camera.position.z = 500;
|
|
camera.target.position.y = 150;
|
|
|
|
scene = new THREE.Scene();
|
|
|
|
|
|
|
|
|
|
// Cube
|
|
|
|
var materials = [];
|
|
|
|
for ( var i = 0; i < 6; i ++ ) {
|
|
|
|
materials.push( [ new THREE.MeshBasicMaterial( { color: Math.random() * 0xffffff, wireframe: false } ) ] );
|
|
|
|
}
|
|
|
|
var createGeometry = function(i, subdivisions) {
|
|
var params = geometriesParams[i];
|
|
|
|
info.innerHTML = 'Drag to spin the geometry THREE.' + params.type
|
|
+ ' with ' + subdivisions + ' subdivisions'; //+ params.args;
|
|
|
|
geometry = createSomething(THREE[params.type], params.args);
|
|
|
|
geometry.mergeVertices();
|
|
smooth = createSubdivision(geometry, subdivisions);
|
|
return smooth;
|
|
};
|
|
|
|
|
|
var geometriesParams = [
|
|
|
|
{type: 'CubeGeometry', args: [ 200, 200, 200, 2, 2, 2, materials ] },
|
|
{type: 'TorusGeometry', args: [ 100, 60, 4, 8, Math.PI*2 ] },
|
|
{type: 'SphereGeometry', args: [ 200, 1, 1 ] },
|
|
{type: 'CylinderGeometry', args: [ 25, 75, 200, 8, 3 ] },
|
|
{type: 'TextGeometry', args: ['&', {
|
|
size: 200,
|
|
height: 50,
|
|
curveSegments: 1,
|
|
font: "helvetiker"
|
|
|
|
}]},
|
|
|
|
];
|
|
|
|
smooth = createGeometry(3, 2);
|
|
|
|
//geometry = new THREE.CubeGeometry( 200, 200, 200, 2, 2, 2, materials );
|
|
// make sure mergeVertices(); is run to fix quick duplicated vertices
|
|
// geometry.mergeVertices();
|
|
// smooth = createSubdivision(geometry, 2);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
group = new THREE.Object3D();
|
|
group.position.y = 150;
|
|
scene.add( group );
|
|
|
|
// Debug new Points
|
|
// var PI2 = Math.PI * 2;
|
|
// var program = function ( context ) {
|
|
//
|
|
// context.beginPath();
|
|
// context.arc( 0, 0, 1, 0, PI2, true );
|
|
// context.closePath();
|
|
// context.fill();
|
|
//
|
|
// }
|
|
//
|
|
// for ( var i = 0; i < smooth.vertices.length; i++ ) {
|
|
//
|
|
// particle = new THREE.Particle( new THREE.ParticleCanvasMaterial( { color: Math.random() * 0x808008 + 0x808080, program: program } ) );
|
|
// particle.position = smooth.vertices[i].position;
|
|
// var pos = smooth.vertices.position
|
|
// particle.scale.x = particle.scale.y = 5;
|
|
// group.add( particle );
|
|
// }
|
|
|
|
//Debug original points
|
|
|
|
// var drawText = function(i) {
|
|
//
|
|
// return function ( context ) {
|
|
//
|
|
// context.beginPath();
|
|
// context.scale(0.1,-0.1);
|
|
//
|
|
// context.fillText(i, 0,0);
|
|
//
|
|
// };
|
|
//
|
|
// }
|
|
// for ( var i = 0; i < geometry.vertices.length; i++ ) {
|
|
//
|
|
// particle = new THREE.Particle( new THREE.ParticleCanvasMaterial( { color: Math.random() * 0x808008 + 0x808080, program: drawText(i) } ) );
|
|
// particle.position = smooth.vertices[i].position;
|
|
// var pos = geometry.vertices.position
|
|
// particle.scale.x = particle.scale.y = 30;
|
|
// group.add( particle );
|
|
// }
|
|
|
|
|
|
var meshmaterials = [
|
|
new THREE.MeshBasicMaterial( { color: 0x405040, wireframe:true, opacity:0.8 } )
|
|
];
|
|
|
|
// new THREE.MeshLambertMaterial( { color: 0xffffff, shading: THREE.FlatShading, vertexColors: THREE.VertexColors } ),
|
|
// new THREE.MeshBasicMaterial( { color: 0x000000, shading: THREE.FlatShading, wireframe: true } )
|
|
// new THREE.MeshFaceMaterial()
|
|
// new THREE.MeshPhongMaterial( { color: 0xffffff, shading: THREE.FlatShading } );
|
|
// new THREE.MeshPhongMaterial( { color: 0xffffff, shading: THREE.SmoothShading } );
|
|
|
|
cube = new THREE.Mesh( smooth, meshmaterials );
|
|
|
|
// cube.scale.x = 200;
|
|
// cube.scale.y = 200;
|
|
// cube.scale.z = 200;
|
|
|
|
|
|
cube.doubleSided = true;
|
|
cube.position.y = 150;
|
|
cube.overdraw = true;
|
|
scene.add( cube );
|
|
|
|
// Plane
|
|
|
|
plane = new THREE.Mesh( new THREE.PlaneGeometry( 200, 200 ), new THREE.MeshBasicMaterial( { color: 0xe0e0e0 } ) );
|
|
plane.rotation.x = - 90 * ( Math.PI / 180 );
|
|
plane.overdraw = true;
|
|
scene.add( plane );
|
|
|
|
renderer = new THREE.CanvasRenderer(); // WebGLRenderer 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 );
|
|
document.addEventListener( 'touchstart', onDocumentTouchStart, false );
|
|
document.addEventListener( 'touchmove', onDocumentTouchMove, false );
|
|
}
|
|
|
|
//
|
|
|
|
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;
|
|
}
|
|
|
|
function onDocumentMouseMove( event ) {
|
|
|
|
mouseX = event.clientX - windowHalfX;
|
|
|
|
targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.02;
|
|
}
|
|
|
|
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() {
|
|
|
|
group.rotation.y = plane.rotation.z = cube.rotation.y += ( targetRotation - cube.rotation.y ) * 0.05;
|
|
renderer.render( scene, camera );
|
|
|
|
}
|
|
|
|
</script>
|
|
|
|
</body>
|
|
</html>
|