0

我为点提供了输入文本框。现在,当我单击绘图按钮时,只会显示顶点。实际上,我想根据点(XYZ 坐标)给出的路径创建管几何。

请找到以下代码和 我的 jsfiddle 链接

    <!DOCTYPE html>
    <html lang="en">
        <head>
            <title>3d Model using HTML5 and three.js</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: #f0f0f0;
                    margin: 0px;
                    overflow: hidden;
                }
                #Find_panel {
                    position: absolute;
                    right: 5px;
                    top: 5px;
                    color: #fff;
                    text-align: left;
                    background: rgba(0, 0, 0, 0.5);
                    padding: 10px;
                    width: 290px;
                    height: 120px;
                    border: solid 1px black;
                    border-radius: 5px;
                }
                #Find_panel input {
                    padding: 2px 4px;
                    margin-bottom: 3px;
                }
            </style>
        </head>
        <body>  
            <div id="Find_panel" align="right">
                X1: <input type="text" id="txtX1" name="p1[]" maxlength="2" size="2" />&nbsp;Y1: <input type="text" id="txtY1" name="p1[]" maxlength="2" size="2" />&nbsp;Z1: <input type="text" id="txtZ1" name="p1[]" maxlength="2" size="2" /><br/>
                X2: <input type="text" id="txtX2" name="p2[]" maxlength="2" size="2" />&nbsp;Y2: <input type="text" id="txtY2" name="p2[]" maxlength="2" size="2" />&nbsp;Z2: <input type="text" id="txtZ2" name="p2[]" maxlength="2" size="2" /><br/>
                X3: <input type="text" id="txtX3" name="p3[]" maxlength="2" size="2" />&nbsp;Y3: <input type="text" id="txtY3" name="p3[]" maxlength="2" size="2" />&nbsp;Z3: <input type="text" id="txtZ3" name="p3[]" maxlength="2" size="2" /><br/>
                <input type="button" value="plot" onClick="return plotPath();" />
                <input type="button" value="render" onClick="return animate();" />
            </div>

            <script src="three.min.js" type="text/javascript"></script>
            <script src="Curve.js" type="text/javascript"></script>
            <script src="TubeGeometry.js" type="text/javascript"></script>      
            <script src="Stats.js" type="text/javascript"></script>
            <script src="Detector.js" type="text/javascript"></script>

            <script>

// variables
        var container, stats, form;

        var camera, scene, renderer, splineCamera, cameraHelper, cameraEye;

        var text, plane, tube, tubeMesh, parent;

        var targetRotation = 0;
        var targetRotationOnMouseDown = 0;

        var mouseX = 0;
        var mouseXOnMouseDown = 0;

        var windowHalfX = window.innerWidth / 2;
        var windowHalfY = window.innerHeight / 2;

        var binormal = new THREE.Vector3();
        var normal = new THREE.Vector3();

        var materials = []; 

            function plotPath()
            {           
                var segments = 50;
                var closed = false;
                var debug = true;
                var radiusSegments = 12;
                var tube;
                var points = [];

                for(var i=1; i<4; i++)
                {               
                    x = document.getElementById("txtX"+i).value;
                    y = document.getElementById("txtY"+i).value;
                    z = document.getElementById("txtZ"+i).value;
                    points.push( new THREE.Vector3(x,y,z));
                    extrudePath = new THREE.SplineCurve3(points);

                    tube = new THREE.TubeGeometry(extrudePath, segments, 2, radiusSegments, closed, debug);
                    tube.dynamic = true;

                    tubeMesh = new THREE.Mesh(tube ,new THREE.MeshBasicMaterial({
                        color: 0x000000,side: THREE.DoubleSide,
                        opacity: 0.5,transparent: true,
                        wireframe: true}));
                    tubeMesh.dynamic = true;

                    parent = new THREE.Object3D();
                    parent.position.y = 100;

                    if ( tube.debug ) tubeMesh.add( tube.debug );
                    parent.add( tubeMesh );                 
                    scene.add( tubeMesh );
                }           
                animate();                          
            }

            init();                     
            //animate();

            function init(){

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

                // renderer
                //renderer = new THREE.CanvasRenderer();
                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( 60, window.innerWidth / window.innerHeight, 1, 10000 );
                camera.position.set(30,10,10);
                camera.lookAt(scene.position);
                //camera.up = new THREE.Vector3( 0, 1, 1 );

                // CONTROLS
                //console.log(renderer.domElement);
                controls = new THREE.TrackballControls( this.camera, container );   

                // light            
                scene.add( new THREE.AmbientLight( 0x404040 ) );
                light = new THREE.DirectionalLight( 0xffffff );
                light.position.set( 0, 1, 0 );
                scene.add( light );                 

                // Grid
                geometry = new THREE.Geometry();
                geometry.vertices.push( new THREE.Vector3( - 500, 0, 0 ) );
                geometry.vertices.push( new THREE.Vector3( 500, 0, 0 ) );

                for ( var i = 0; i <= 20; i ++ ) {

                    line = new THREE.Line( geometry, new THREE.LineBasicMaterial( { color: 0x000000, opacity: 0.2 } ) );
                    line.position.z = ( i * 50 ) - 500;
                    scene.add( line );

                    line = new THREE.Line( geometry, new THREE.LineBasicMaterial( { color: 0x000000, opacity: 0.2 } ) );
                    line.position.x = ( i * 50 ) - 500;
                    line.rotation.y = 90 * Math.PI / 180;
                    scene.add( line );
                }

                // projector
                projector = new THREE.Projector();

                // stats
                stats = new Stats();
                stats.domElement.style.position = 'absolute';
                stats.domElement.style.top = '0px';
                //container.appendChild( stats.domElement );

                document.addEventListener( 'mousedown', onDocumentMouseDown, false );
                document.addEventListener( 'mouseover', onDocumentMouseOver, false );
                document.addEventListener( 'touchstart', onDocumentTouchStart, false );
                document.addEventListener( 'touchmove', onDocumentTouchMove, false );               
                window.addEventListener( 'resize', onWindowResize, false );

            }
            /*function draw(x,y,z)
            {           
                points.push( new THREE.Vector3(x,y,z));             
            }*/

            function onWindowResize() {

                camera.left = window.innerWidth / - 2;
                camera.right = window.innerWidth / 2;
                camera.top = window.innerHeight / 2;
                camera.bottom = window.innerHeight / - 2;
                camera.aspect = window.innerWidth / window.innerHeight;
                //camera.updateProjectionMatrix();
                renderer.setSize( window.innerWidth, window.innerHeight );

            }

            function onDocumentMouseDown( event ) {
                event.preventDefault();

                document.addEventListener( 'mousemove', onDocumentMouseMove, false );
                document.addEventListener( 'mouseup', onDocumentMouseUp, false );
                document.addEventListener( 'mouseout', onDocumentMouseOut, false );


                mouseXOnMouseDown = event.clientX - windowHalfX;
                targetRotationOnMouseDown = targetRotation;         
            }

            function onDocumentMouseMove( event ) {

                mouseX = event.clientX - windowHalfX;
                targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.02;

            }

            function onDocumentMouseUp( event ) {

                document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
                document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
                document.removeEventListener( 'mouseout', onDocumentMouseOut, false );

            }

            function onDocumentMouseOut( event ) {

                document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
                document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
                document.removeEventListener( 'mouseout', onDocumentMouseOut, false );

            }

            function onDocumentMouseOver( event ) {

                document.removeEventListener( 'mousemove', onDocumentMouseMove, false );
                document.removeEventListener( 'mouseup', onDocumentMouseUp, false );
                document.removeEventListener( 'mouseout', onDocumentMouseOut, false );

            }

            function onDocumentTouchStart( event ) {

                if ( event.touches.length === 1 ) {

                    event.preventDefault();

                    mouseXOnMouseDown = event.touches[ 0 ].pageX - windowHalfX;
                    targetRotationOnMouseDown = targetRotation;

                }

            }

            function onDocumentTouchMove( event ) {
                if ( event.touches.length === 1 ) {
                    event.preventDefault();
                    mouseX = event.touches[ 0 ].pageX - windowHalfX;
                    targetRotation = targetRotationOnMouseDown + ( mouseX - mouseXOnMouseDown ) * 0.05;
                }
            }

            function animate() {
                requestAnimationFrame( animate );
                tubeMesh.rotation.y += ( targetRotation - tubeMesh.rotation.y ) * 0.15;                         
                camera.updateMatrixWorld();
                renderer.render( scene, camera );                           
                update();
            }

            function update()
            {
                controls.update();
                stats.update();
            }

            function render() {             
                tubeMesh.rotation.y += ( targetRotation - tubeMesh.rotation.y ) * 0.15;         
                camera.updateMatrixWorld();
                renderer.render( scene, camera );
            }
            </script>       


        </body>
    </html>     
4

0 回答 0