Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Need .js and HTML example for displaying .STL 3D objects in a web page

Tags:

three.js

3d

webgl

Can anybody produce a clean "for dummies" example of HTML for using STLLoader.js to display ASCII (not binary) .stl object files in a web page? Result should let users manipulate object in current HTML5 browsers and with no fancy visuals beyond a grayscale object surface and background.

STLLoader.js might need the help of three.js or three.min.js but I wouldn't know. STLLoader.js contains below usage example but did not include an HTML wrapper.

Usage example inside 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' );
     */
like image 312
ForumLeech Avatar asked Oct 14 '12 09:10

ForumLeech


People also ask

How do I view an STL file in HTML?

Click inside the file drop area to upload STL file or drag & drop STL file. Click on Convert button. Your STL files will be uploaded and converted to HTML result format. Download link of result files will be available instantly after conversion.

What are STL files used for?

STL is a file format commonly used for 3D printing and computer-aided design (CAD). The name STL is an acronym that stands for stereolithography — a popular 3D printing technology. You might also hear it referred to as Standard Triangle Language or Standard Tessellation Language.


1 Answers

The three.js examples are a good source:

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

Here is a simplified version:

<!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

like image 158
WestLangley Avatar answered Oct 16 '22 20:10

WestLangley