Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using three.js JSONLoader

Tags:

Just can't see models imported into three.js scene. The geometry looks fine but the model isn't displaying no matter what material I apply to it.

I'm new to WebGL so it's hard for me to diagnose, but my guess is that something is going wrong during the JSONLoader callback.

Thanks for all help.

var camera, scene, renderer, mesh, loader;  init(); animate();  function init() {      camera = new THREE.PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 1, 10000 );     camera.position.z = 1000;      scene = new THREE.Scene();      loader = new THREE.JSONLoader();      loader.load( "scripts/model.js", function( geometry ) {         mesh = new THREE.Mesh( geometry, new THREE.MeshNormalMaterial() );         mesh.scale.set( 10, 10, 10 );         mesh.position.y = 150;         mesh.position.x = 0;     } );      scene.add( mesh );      var ambientLight = new THREE.AmbientLight(0x555555);     scene.add(ambientLight);      var directionalLight = new THREE.DirectionalLight(0xffffff);     directionalLight.position.set(1, 1, 1).normalize();     scene.add(directionalLight);      renderer = new THREE.WebGLRenderer();     renderer.setSize( window.innerWidth, window.innerHeight );      document.body.appendChild( renderer.domElement );  }  function animate() {      requestAnimationFrame( animate );      mesh.rotation.x += 0.05;      renderer.render( scene, camera ); } 
like image 477
rob-gordon Avatar asked Dec 01 '12 00:12

rob-gordon


People also ask

Is Three Js an API?

js is a cross-browser JavaScript library and application programming interface (API) used to create and display animated 3D computer graphics in a web browser using WebGL. The source code is hosted in a repository on GitHub.

What is Object Loader?

A loader for loading a JSON resource in the JSON Object/Scene format. This uses the FileLoader internally for loading files.

What can three Js be used for?

Three. js is a powerful library for creating three-dimensional models and games. With just a few lines of JavaScript, you can create anything from simple 3D patterns to photorealistic, real-time scenes. You can build simple and complex 3D geometrics, animate and move objects through a lifelike scene, and more.


2 Answers

You are adding the mesh to the scene before the model finishes loading.

Move the line

scene.add( mesh ); 

into the loader callback function.

like image 108
WestLangley Avatar answered Oct 02 '22 15:10

WestLangley


thought this might help anyone who searches for a more accurate answer :

loader.onLoadComplete=function(){scene.add( mesh )}  

also for complete Loader refrence , please refer to here :

https://threejs.org/docs/index.html#api/loaders/Loader

hope this helps.

like image 24
ProllyGeek Avatar answered Oct 02 '22 16:10

ProllyGeek