Three.js Center 3D模型

时间:2018-12-18 18:28:00

标签: javascript three.js center viewport 3d-model

这是我第一次在stackoverflow上写...几天之后,我试图使用JavaScript库Three.js(实际上是n°r99版),我正确地输入了3D模型,但是当我看到它时是从后面看到加载的并且不在视口中居中,我该如何解决?

代码是这样的:

<script>

var scene = new THREE.scene();

var camera = new THREE.PerspectiveCamera ( 50, window.innerWidth / window.innerHeight, 1, 1000);

camera.position.z = 15;

var render = new THREE.WebGLRenderer();
renderer.setSize ( window.InnerWidth, window.InnerHeight );
document.body.appendChild (renderer.domElement);
window.addEventListener('resize', function(){
var width = window.innerWidth;
var height = window.innerHeight;
renderer.setSize( width, height );
camera.aspect = width / height;
camera.updateProjectMatrix();
});

controls = new THREE.OrbitControls ( camera, renderer.domElement );

var loader = new THREE.ObjectLoader();
loader.load("/models/teschio.json",function (obj)
{
scene.add( obj );
});

var animate = function (){
requestAnimateFrame ( animate );
renderer.render (scene, camera);
}
animate();

</script>

,这是当前的线框屏幕: actual wireframe

提前感谢您的回答。

1 个答案:

答案 0 :(得分:1)

类似这样的东西:

var loader = new THREE.ObjectLoader();
loader.load("/models/teschio.json",function (obj)
{
  var box = new THREE.Box3().setFromObject( obj );
  var center = new THREE.Vector3();
  box.getCenter( center );
  obj.position.sub( center ); // center the model
  obj.rotation.y = Math.PI;   // rotate the model
  scene.add( obj );
});