PDA

View Full Version : Simple Model Loader using .obj File



jeeva
11-22-2012, 03:52 AM
Hi Friends,

I am new to webGL. I tried last from one month to load a model by using .obj file in web using Three.js OBJLoader(). Still Now i do not get. Please help me to laod that.


<!doctype html>
<html lang="en">
<head>
<title>three.js webgl OBJ loader</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: #000;
color: #fff;
margin: 0px;
overflow: hidden;
}
#info {
color: #fff;
position: absolute;
top: 10px;
width: 100%;
text-align: center;
z-index: 100;
display:block;
}
#info a, .button { color: #f00; font-weight: bold; text-decoration: underline; cursor: pointer }
</style>
</head>

<body>
<div id="info">
three.js (http://github.com/mrdoob/three.js) - OBJLoader test
</div>

<script src="Three.js"></script>
<script src="OBJLoader.js"></script>

<script src="Detector.js"></script>
<script src="Stats.js"></script>

<script>

var container, stats;

var camera, scene, renderer;

var mouseX = 0, mouseY = 0;

var windowHalfX = window.innerWidth / 2;
var windowHalfY = window.innerHeight / 2;


init();
animate();


function init() {

container = document.createElement( 'div' );
document.body.appendChild( container );

scene = new THREE.Scene();

camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
camera.position.z = 100;
scene.add( camera );

var ambient = new THREE.AmbientLight( 0x10A10E );
scene.add( ambient );

var directionalLight = new THREE.DirectionalLight( 0xffeedd );
directionalLight.position.set( 0, 0, 1 ).normalize();
scene.add( directionalLight );

var loader = new THREE.OBJLoader();
loader.load( "monster.obj", function ( object ) {
object.position.y = - 80;
scene.add( object );
} );

// RENDERER
renderer = new THREE.WebGLRenderer();
renderer.setSize( window.innerWidth, window.innerHeight );
container.appendChild( renderer.domElement );

document.addEventListener( 'mousemove', onDocumentMouseMove, false );

}

function onDocumentMouseMove( event ) {

mouseX = ( event.clientX - windowHalfX ) / 2;
mouseY = ( event.clientY - windowHalfY ) / 2;

}

//

function animate() {

requestAnimationFrame( animate );
render();

}

function render() {

camera.position.x += ( mouseX - camera.position.x ) * .05;
camera.position.y += ( - mouseY - camera.position.y ) * .05;

camera.lookAt( scene.position );

renderer.render( scene, camera );

}

</script>

</body>
</html>



Thanks & Regards,
Jeeva.V