Monday, 7 September 2020

How to use THREE.MeshLine in my javascript project?

When looking for a way to create a three.js line with a changed width I came across this article which suggests to use a THREE.MeshLine class from here. However, when trying to follow the documentation on how to use it in my script I got an error:

require.js:5 Uncaught Error: Module name "three" has not been loaded yet for context: _. Use require([])
http://requirejs.org/docs/errors.html#notloaded
    at makeError (require.js:5)
    at Object.o [as require] (require.js:5)
    at requirejs (require.js:5)
    at test_line.html:13

Here are the lines of code I used in my script:

<html>
    <head>
        <script src="https://requirejs.org/docs/release/2.3.5/minified/require.js"></script>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r119/three.min.js"></script>
        <script src="https://raw.githubusercontent.com/spite/THREE.MeshLine/master/src/THREE.MeshLine.js"></script>
        <title>Test</title>
    </head>
    <body>  
        <script>
            var THREE = require( 'three' );
            var MeshLine = require( 'three.meshline' );
            ...

Do I miss something? (Hint: I am a beginner with javascript)

I also tried to follow the suggestions HERE, but as soon as I added type="module" in the script part I got an error container is not defined.

Here is a complete, stand-alone, static, easy, direct working html example of a single line in a browser window. I'd appreciate if the answer could contain a complete, stand-alone, static, easy, direct working example of the code fixed!

<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
<meta content="utf-8" http-equiv="encoding">
<html>
    <head>
        <script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r119/three.min.js"></script>
        <title>Test</title>
    </head>
    <body>
        
        <script>
            container = document.createElement('div');
            document.body.appendChild(container);
            camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 1, 10000);
            camera.position.set(0, 0, 150);
            scene = new THREE.Scene();
            scene.add(camera);
            renderer = new THREE.WebGLRenderer({
                clearAlpha: 1
            });
            renderer.setSize(window.innerWidth, window.innerHeight);
            renderer.setClearColor(0xcaffee, 1);
            document.body.appendChild(renderer.domElement);
            
            var material = new THREE.LineBasicMaterial( { color: 0xff0000, linewidth: 10000000 } );
            var points = [];
            points.push( new THREE.Vector3(-50, 0, 0 ) );
            points.push( new THREE.Vector3( 50, 0, 0 ) );
            var geometry = new THREE.BufferGeometry().setFromPoints( points );
            var line = new THREE.Line( geometry, material );
            scene.add( line );
            renderer.render( scene, camera );
            
            animate();
            function animate() {
                requestAnimationFrame(animate);
                renderer.render(scene, camera);
            }
            
        </script>
    </body>
</html>


from How to use THREE.MeshLine in my javascript project?

No comments:

Post a Comment