mirror of
https://github.com/wahyd4/three.js.git
synced 2026-08-09 21:15:56 +10:00
191 lines
4.8 KiB
HTML
191 lines
4.8 KiB
HTML
<!DOCTYPE HTML>
|
|
<html lang="en">
|
|
<head>
|
|
<title>three.js - webgl</title>
|
|
<meta charset="utf-8">
|
|
<style type="text/css">
|
|
body {
|
|
background:#fff;
|
|
padding:0;
|
|
margin:0;
|
|
font-weight: bold;
|
|
overflow:hidden;
|
|
}
|
|
</style>
|
|
</head>
|
|
|
|
<body>
|
|
<script type="text/javascript" src="../build/Three.js"></script>
|
|
<script type="text/javascript" src="js/RequestAnimationFrame.js"></script>
|
|
<script type="text/javascript" src="js/Stats.js"></script>
|
|
|
|
<script type="text/javascript">
|
|
|
|
var container, stats;
|
|
|
|
var camera, scene, renderer;
|
|
|
|
var mesh, boxMesh, light, lightCube, light2, lightCube2, zmesh, lightMesh, geometry;
|
|
|
|
var mouseX = 0, mouseY = 0;
|
|
|
|
var windowHalfX = window.innerWidth / 2;
|
|
var windowHalfY = window.innerHeight / 2;
|
|
|
|
document.addEventListener( 'mousemove', onDocumentMouseMove, false );
|
|
|
|
init();
|
|
animate();
|
|
|
|
function init() {
|
|
|
|
container = document.createElement( 'div' );
|
|
document.body.appendChild( container );
|
|
|
|
camera = new THREE.Camera( 60, window.innerWidth / window.innerHeight, 1, 1000 );
|
|
camera.position.z = 250;
|
|
|
|
scene = new THREE.Scene();
|
|
|
|
|
|
// world
|
|
|
|
var cube = new THREE.Cube( 300, 300, 10 );
|
|
var material0 = new THREE.MeshPhongMaterial( { color:0xff00ff } );
|
|
var material1 = new THREE.MeshLambertMaterial( { color:0x00ff00 } );
|
|
var material2 = new THREE.MeshLambertMaterial( { color:0x0000ff } );
|
|
|
|
var mesh1 = new THREE.Mesh( cube, material0 );
|
|
mesh1.position.z = -150;
|
|
scene.addChild( mesh1 );
|
|
|
|
var mesh2 = new THREE.Mesh( cube, material1 );
|
|
mesh2.position.x = -150;
|
|
mesh2.rotation.y = 90 * Math.PI / 180;
|
|
scene.addChild( mesh2 );
|
|
|
|
var mesh3 = new THREE.Mesh( cube, material2 );
|
|
mesh3.position.y = -150;
|
|
mesh3.rotation.x = 90 * Math.PI / 180;
|
|
scene.addChild( mesh3 );
|
|
|
|
new THREE.ShadowVolume( mesh1 )
|
|
new THREE.ShadowVolume( mesh2 )
|
|
new THREE.ShadowVolume( mesh3 )
|
|
|
|
|
|
// moving objects
|
|
|
|
var cube = new THREE.Cube( 40, 40, 40 );
|
|
var torus = new THREE.Torus( 40, 10 );
|
|
var sphere = new THREE.Sphere( 40 );
|
|
var cylinder = new THREE.Cylinder( 10, 10, 20, 40, 0, 0 );
|
|
mesh = new THREE.Mesh( torus, material1 );
|
|
scene.addChild( mesh );
|
|
|
|
boxMesh = new THREE.Mesh( cube, material2 );
|
|
scene.addChild( boxMesh );
|
|
|
|
new THREE.ShadowVolume( mesh );
|
|
new THREE.ShadowVolume( boxMesh );
|
|
|
|
|
|
// lights
|
|
|
|
light = new THREE.PointLight( 0xffffff );
|
|
scene.addChild( light );
|
|
|
|
|
|
light = new THREE.DirectionalLight( 0xffffff );
|
|
light.castShadow = true;
|
|
light.position.set( 0, 1, 0 );
|
|
scene.addChild( light );
|
|
|
|
|
|
var cube = new THREE.Sphere( 2 );
|
|
lightCube = new THREE.Mesh( cube, material2 );
|
|
lightCube.visible = false;
|
|
scene.addChild( lightCube );
|
|
|
|
var lensFlare = new THREE.LensFlare( THREE.ImageUtils.loadTexture( "textures/lensflare0.png" ), 128, 0.0, THREE.AdditiveBlending );
|
|
lensFlare.add( THREE.ImageUtils.loadTexture( "textures/lensflare1.png" ), 256, 0.33, THREE.AdditiveBlending );
|
|
lensFlare.add( lensFlare.lensFlares[ 1 ].texture, 300, 0.66, THREE.AdditiveBlending );
|
|
lensFlare.add( lensFlare.lensFlares[ 1 ].texture, 400, 1.0, THREE.AdditiveBlending );
|
|
|
|
lightCube.addChild( lensFlare );
|
|
|
|
|
|
// renderer
|
|
|
|
renderer = new THREE.WebGLRenderer( { antialias: false } );
|
|
renderer.setClearColorHex( 0x222222, 1 );
|
|
renderer.setSize( window.innerWidth, window.innerHeight );
|
|
container.appendChild( renderer.domElement );
|
|
|
|
stats = new Stats();
|
|
stats.domElement.style.position = 'absolute';
|
|
stats.domElement.style.top = '0px';
|
|
stats.domElement.style.zIndex = 100;
|
|
container.appendChild( stats.domElement );
|
|
|
|
}
|
|
|
|
|
|
function onDocumentMouseMove(event) {
|
|
|
|
mouseX = ( event.clientX - windowHalfX );
|
|
mouseY = ( event.clientY - windowHalfY );
|
|
|
|
}
|
|
|
|
function animate() {
|
|
|
|
requestAnimationFrame( animate );
|
|
|
|
render();
|
|
stats.update();
|
|
|
|
}
|
|
|
|
var t = 0;
|
|
|
|
function render() {
|
|
|
|
mesh.position.x = Math.sin( t ) * 100;
|
|
mesh.position.y = Math.cos( t ) * 100;
|
|
|
|
mesh.rotation.x += 0.5 * Math.PI / 180;
|
|
mesh.rotation.y += 1.0 * Math.PI / 180;
|
|
mesh.rotation.z += 1.5 * Math.PI / 180;
|
|
|
|
boxMesh.position.z = Math.sin( t ) * 100;
|
|
boxMesh.rotation.x = Math.sin( t ) * 180 * Math.PI / 180;
|
|
|
|
light.position.x = Math.sin( t );
|
|
light.position.y = 0.5;
|
|
light.position.normalize();
|
|
|
|
lightCube.position.copy( light.position );
|
|
lightCube.position.multiplyScalar( 200 );
|
|
|
|
|
|
t += 0.015;
|
|
|
|
camera.position.x += ( mouseX - camera.position.x ) * .05;
|
|
camera.position.y += ( - mouseY - camera.position.y ) * .05;
|
|
|
|
renderer.render( scene, camera );
|
|
|
|
}
|
|
|
|
function log( text ) {
|
|
|
|
var e = document.getElementById("log");
|
|
e.innerHTML = text + "<br/>" + e.innerHTML;
|
|
|
|
}
|
|
</script>
|
|
|
|
</body>
|
|
</html>
|