three.js - json模型在屏幕上看起来太大了

时间:2016-12-20 18:36:39

标签: javascript json three.js

我做了三个.js,我用JSON文件将它加载到THREE.js中,我的JSON文件是来自clara.io的狗。一切正常,问题是3D模型太大了。有没有办法减小其加载到屏幕上的大小。

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>three.js webgl - loaders - JSON 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>

        <script src="three.js"></script>

        <script src="Detector.js"></script>
        <script src="stats.min.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 );
                camera = new THREE.PerspectiveCamera( 45, window.innerWidth / window.innerHeight, 1, 2000 );
                camera.position.z = 4;
                // scene
                scene = new THREE.Scene();
                var ambient = new THREE.AmbientLight( 0x444444 );
                scene.add( ambient );
                var directionalLight = new THREE.DirectionalLight( 0xffeedd );
                directionalLight.position.set( 0, 0, 1 ).normalize();
                scene.add( directionalLight );
                // BEGIN Clara.io JSON loader code
                var objectLoader = new THREE.ObjectLoader();
                objectLoader.load("blue-dog.json", function ( obj ) {
                    scene.add( obj );
                } );
                // END Clara.io JSON loader code
                renderer = new THREE.WebGLRenderer();
                renderer.setPixelRatio( window.devicePixelRatio );
                renderer.setSize( window.innerWidth, window.innerHeight );
                container.appendChild( renderer.domElement );
                document.addEventListener( 'mousemove', onDocumentMouseMove, false );
                //
                window.addEventListener( 'resize', onWindowResize, false );
            }
            function onWindowResize() {
                windowHalfX = window.innerWidth / 2;
                windowHalfY = window.innerHeight / 2;
                camera.aspect = window.innerWidth / window.innerHeight;
                camera.updateProjectionMatrix();
                renderer.setSize( window.innerWidth, window.innerHeight );
            }
            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>

如果有人可以提供任何帮助,请感谢谢谢。我也在WAMP上托管它。 JSON文件很难粘贴。

1 个答案:

答案 0 :(得分:2)

您可以设置对象的比例:

objectLoader.load("blue-dog.json", function( obj ){
    obj.scale.set( .5, .5, .5 );
    scene.add( obj );
});

或者缩小你的相机,但说实话,我不能给你这个例子,因为我不能100%确定你想要做什么关于相机(你的渲染功能中的奇怪的+ = - =让我感到困惑位)。你可以试试这个:

camera.position.multiplyScalar( 2 ); 
// This should double the distance between your camera and the center of the scene

但是只尝试一次(如果你在渲染循环中重复这个,它会永远缩小,而且会很快发生)。