【问题标题】:Need .js and HTML example for displaying .STL 3D objects in a web page需要 .js 和 HTML 示例以在网页中显示 .STL 3D 对象
【发布时间】:2012-10-04 13:30:06
【问题描述】:

任何人都可以生成一个干净的“傻瓜”HTML 示例,以便使用STLLoader.js 在网页中显示ASCII(不是二进制).stl 对象文件吗?结果应该让用户在当前的 HTML5 浏览器中操作对象,并且除了灰度对象表面和背景之外没有花哨的视觉效果。

STLLoader.js 可能需要three.js 或three.min.js 的帮助,但我不知道。 STLLoader.js 包含以下使用示例,但不包含 HTML 包装器。

里面的使用示例 STLLoader.js

     /** 
     * Usage:
     *  var loader = new THREE.STLLoader();
     *  loader.addEventListener( 'load', function ( event ) {
     *
     *      var geometry = event.content;
     *      scene.add( new THREE.Mesh( geometry ) );
     *
     *  } );
     *  loader.load( './models/stl/slotted_disk.stl' );
     */

【问题讨论】:

    标签: 3d webgl three.js


    【解决方案1】:

    three.js 示例是一个很好的来源:

    https://github.com/mrdoob/three.js/blob/master/examples/webgl_loader_stl.html

    这是一个简化版:

    <!DOCTYPE html>
    <html lang="en">
        <head>
            <title>three.js webgl - STL</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: #000000;
                    margin: 0px;
                    overflow: hidden;
                }
    
                #info {
                    color: #fff;
                    position: absolute;
                    top: 10px;
                    width: 100%;
                    text-align: center;
                    z-index: 100;
                    display:block;
    
                }
    
                a { color: skyblue }
            </style>
        </head>
        <body>
            <div id="info">
                STL loader test
            </div>
    
            <script src="http://threejs.org/build/three.min.js"></script>
    
            <script src="http://threejs.org/examples/js/loaders/STLLoader.js"></script>
    
            <script>
    
                var container, camera, scene, renderer;
    
                init();
                animate();
    
                function init() {
    
                    container = document.createElement( 'div' );
                    document.body.appendChild( container );
    
                    // renderer
    
                    renderer = new THREE.WebGLRenderer( { antialias: true } );
                    renderer.setSize( window.innerWidth, window.innerHeight );
                    container.appendChild( renderer.domElement );
    
                    // scene
    
                    scene = new THREE.Scene();
    
                    // camera
    
                    camera = new THREE.PerspectiveCamera( 35, window.innerWidth / window.innerHeight, 1, 10000 );
                    camera.position.set( 3, 0.5, 3 );
                    scene.add( camera ); // required, because we are adding a light as a child of the camera
    
                    // lights
    
                    scene.add( new THREE.AmbientLight( 0x222222 ) );
    
                    var light = new THREE.PointLight( 0xffffff, 0.8 );
                    camera.add( light );
    
                    // object
    
                    var loader = new THREE.STLLoader();
                    loader.load( 'slotted_disk.stl', function ( geometry ) {
    
                        var material = new THREE.MeshPhongMaterial( { color: 0xff5533 } );
    
                        var mesh = new THREE.Mesh( geometry, material );
    
                        scene.add( mesh );
    
                    } );
    
                    window.addEventListener( 'resize', onWindowResize, false );
    
                }
    
                function onWindowResize() {
    
                    camera.aspect = window.innerWidth / window.innerHeight;
    
                    camera.updateProjectionMatrix();
    
                    renderer.setSize( window.innerWidth, window.innerHeight );
    
                }
    
                function animate() {
    
                    requestAnimationFrame( animate );
    
                    render();
    
                }
    
                function render() {
    
                    var timer = Date.now() * 0.0005;
    
                    camera.position.x = Math.cos( timer ) * 5;
                    camera.position.z = Math.sin( timer ) * 5;
    
                    camera.lookAt( scene.position );
    
                    renderer.render( scene, camera );
    
                }
    
            </script>
        </body>
    </html>
    

    three.js r.70

    【讨论】:

    • 当我小心地将其 .js 和 .stl 文件复制到我的 Web 服务器上时,该 webgl_loader_stl.html 页面在当前的 Chrome 和 Firefox 中失败。页面大部分是黑色的,顶部有一些信息。 Web 上的其他 3D .js 页面在移植到我的测试环境时可以工作,但我不知道在webgl_loader_stl.html 中声明一个故障。
    • 我为您简化了示例。没有比这更简单的了。如果您还有其他问题,请发布新帖子并尽可能提供实时链接。
    • 是否可以得到stl 模型在IE(9) 中渲染??它不工作,我收到此错误SCRIPT5009: 'DataView' is undefined STLLoader.js, line 82 character 3
    • @Uttara - 你需要发一个新帖子。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-03
    • 2022-04-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多