我怎样才能加载客户端·STL文件到three.js所现场,而不是从服务器加载的?(How can I

2019-10-17 11:04发布

我有一个“选择文件”按钮,我想用用于加载客户端的文件,而不是从服务器路径Loader.load方法上线86加载()我猜我需要使用文件API,但还没有想通如何照顾这个解决方案 。


现有的从服务器路径线86加载文件的网页:

<!DOCTYPE html>
<html>
<head>
    <style>
    body        {
            background-color:#fea47c;
                }

    div         { 
            position:relative;
            left:200px;           
            top:0px;
            background-color: #eeeeee;
            border:1px solid black;             
            width:550px;
            height:550px;
                }

    canvas      {
            width:550px;
            height:550px;
                }

    input#file      
                {
            position:relative;left:425px;
                }

    </style>
</head>

<body>

<input type="file" name="file" id="file"></input><br><br>   

    <script src="https://raw.github.com/mrdoob/three.js/master/build/three.min.js"></script>

    <script src="https://raw.github.com/mrdoob/three.js/master/examples/js/loaders/STLLoader.js"></script>

    <script src="https://raw.github.com/mrdoob/three.js/master/examples/js/controls/TrackballControls.js"></script>

    <script>            
        var container, camera, scene, renderer, controls;

        init();
        animate();

        function init() {

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

            var width = container.clientWidth;
            var height = container.clientHeight;

            camera = new THREE.PerspectiveCamera(

            35,                                     // field of view in degrees?
            width / height,                         // canvas based aspect ratio; use when canvas is smaller than page
            .1  ,                                   // distance to nearest side of rendered space
            10000                                   // distance to farthest side of rendered space
                                                );

            camera.position.set( 0, 0, 10);

            scene = new THREE.Scene();

            controls = new THREE.TrackballControls( camera , container); 
            controls.addEventListener( 'change', render );

            // object

            var loader = new THREE.STLLoader();
            loader.addEventListener( 'load', function ( event ) {

                var geometry = event.content;

                var material = new THREE.MeshLambertMaterial( { ambient: 0xff5533, color: 0xff5533 } );

                var mesh = new THREE.Mesh( geometry, material );

                scene.add( mesh );

            } );

            loader.load( 'path to .stl file on server usually goes here' );

            // lights

            scene.add( new THREE.AmbientLight( 0x222222 ) );

            var directionalLight = new THREE.DirectionalLight( 0xffffff, 1 );
            directionalLight.position = camera.position;
            scene.add( directionalLight );

            // renderer

            renderer = new THREE.WebGLRenderer( { antialias: true } );
            renderer.setSize( width , height );
            container.appendChild( renderer.domElement );

            window.addEventListener( 'resize', onWindowResize, false );


        }

        function addLight( x, y, z, color, intensity ) {

            var directionalLight = new THREE.DirectionalLight( color, intensity );
            directionalLight.position.set( x, y, z )
            scene.add( directionalLight );

        }

       function onWindowResize() {   

            camera.aspect = width / height;
            camera.updateProjectionMatrix();

            renderer.setSize( width, height );
        }

        function animate() {

            requestAnimationFrame( animate );
            controls.update();
            render();

        }

       function render() {

           camera.lookAt( scene.position );
           renderer.render( scene, camera );

       }
    </script>
</body>
</html>

Answer 1:

在服务器上的脚本? 在一般的网页浏览器做出不允许的file://引用(甚至那么你可能需要设置一些安全/调试标志,根据不同的浏览器):除了当页面本身是一个文件//引用。 所以,如果你正在运行在的Via HTTP访问的页面中的WebGL脚本://你可能是出于运气,除非你通过像Dropbox的文件服务捎带。



文章来源: How can I load client .STL file into three.js scene instead of loading from server?