2014-06-19 3 views
0

J'ai un modèle de temple créé dans Google Sketchup (fichier .obj). Je le convertis en JSON et le charge dans mon application. Mais le problème est que le maillage est déplacé vers une position éloignée, donc je ne peux pas le voir. J'essaie de changer de position mais j'ai échoué jusqu'ici. Voici mon code. Pouvez-vous me dire ce qui ne va pas? Je suis nouveau à Three.js et je suis à une perteThree.js - Impossible de modifier la position JSON chargée

// Set up the scene, camera, and renderer as global variables. 
var scene, camera, renderer; 

init(); 
animate(); 

// Sets up the scene. 
function init() { 

    // Create the scene and set the scene size. 
    scene = new THREE.Scene(); 
    var WIDTH = window.innerWidth, 
     HEIGHT = window.innerHeight; 

    // Create a renderer and add it to the DOM. 
    renderer = new THREE.WebGLRenderer({antialias:true}); 
    renderer.setSize(WIDTH, HEIGHT); 
    document.body.appendChild(renderer.domElement); 

    // Create a camera, zoom it out from the model a bit, and add it to the scene. 
    camera = new THREE.PerspectiveCamera(45, WIDTH/HEIGHT, 0.1, 20000); 
    camera.position.set(0,16,0); 
    scene.add(camera); 

    // Create an event listener that resizes the renderer with the browser window. 
    window.addEventListener('resize', function() { 
    var WIDTH = window.innerWidth, 
     HEIGHT = window.innerHeight; 
    renderer.setSize(WIDTH, HEIGHT); 
    camera.aspect = WIDTH/HEIGHT; 
    camera.updateProjectionMatrix(); 
    }); 

    // Set the background color of the scene. 
    renderer.setClearColor(0x333F47, 1); 

    // Create a light, set its position, and add it to the scene. 
    var light = new THREE.PointLight(0xffffff); 
    light.position.set(-100,200,100); 
    scene.add(light); 

    // Load in the mesh and add it to the scene. 
    var loader = new THREE.JSONLoader(); 

    loader.load("models/naos_apollona.js", function(geometry){ 
    var material = new THREE.MeshLambertMaterial({color: 0x55B663}); 
    mesh = new THREE.Mesh(geometry, material); 
//mesh.position.set(1,1,1) 
    scene.add(mesh); 
    }); 

    // Add OrbitControls so that we can pan around with the mouse. 
    controls = new THREE.OrbitControls(camera, renderer.domElement); 

} 

// Renders the scene and updates the render as needed. 
function animate() { 

    // Read more about requestAnimationFrame at http://www.paulirish.com/2011/requestanimationframe-for-smart-animating/ 
    requestAnimationFrame(animate); 

    // Render the scene. 
    renderer.render(scene, camera); 
    controls.update(); 

} 

Répondre

0

Vous n'avez vraiment aucune idée d'où votre modèle est dans l'espace. Donc, ce que vous devez faire est d'utiliser la boîte englobante du modèle, trouver son centre et soustraire cela de la position.

mesh = new THREE.Mesh ...; 
mesh.geometry.computeBoundingBox(); 
var bBox = mesh.geometry.boundingBox; 
mesh.position.set (bBox.min.x-bBox.max.x, bBox.min.y-bBox.max.y, bBox.min.z-bBox.max.z); 

et il y a aussi une boîte englobante aide THREE.BoundingBoxHelper() pour vous aider à visualiser la zone de délimitation de votre modèle.

+0

Merci. Je vais vérifier – Katagramma

+0

Si c'était la réponse, certains points brownie serait bien. – gaitat

+0

Oui, je suis désolé. J'ai mis le projet en attente il y a un moment et j'ai oublié ... – Katagramma

Questions connexes