1

我正在尝试通过单击按钮来启用\禁用三个.js 场景中的对象。我在按钮的 Onclick() 中创建了一个函数来渲染对象并且它可以工作。但是如何重置场景并禁用对象?

<!DOCTYPE html>
        <meta name="viewport" content="width=device-width, user-scalable=no, minimum-scale=1.0, maximum-scale=1.0">
        <!-- three.js library -->
        <script src="js/three.js"></script>
        <!-- ar.js -->
        <script src="js/ar.js"></script>
        <script src="loaders/OBJLoader.js"></script>
        <script src="loaders/MTLLoader.js"></script>
        <script src="loaders/DDSLoader.js"></script>
    <script>THREEx.ArToolkitContext.baseURL = '../'</script>

        <body style='margin : 0px; overflow: hidden; font-family: Monospace;'><div style='position: absolute; top: 10px; width:100%; text-align: center; z-index: 1;'>
        </div><script>
            //////////////////////////////////////////////////////////////////////////////////
            //      Init
            //////////////////////////////////////////////////////////////////////////////////

            // init renderer
            var renderer    = new THREE.WebGLRenderer({
                antialias: true,
                alpha: true
            });
            renderer.setClearColor(new THREE.Color('lightgrey'), 0)
            renderer.setSize( 640, 480 );
            renderer.domElement.style.position = 'absolute'
            renderer.domElement.style.top = '0px'
            renderer.domElement.style.left = '0px'
            document.body.appendChild( renderer.domElement );

            // array of functions for the rendering loop
            var onRenderFcts= [];

            // init scene and camera
            var scene   = new THREE.Scene();
            scene = new THREE.Scene();
                        var ambientLight = new THREE.AmbientLight( 0xcccccc, 0.5 );
                        scene.add( ambientLight );
                        var pointLight = new THREE.PointLight( 0xffffff, 0.6 );



            //////////////////////////////////////////////////////////////////////////////////
            //      Initialize a basic camera
            //////////////////////////////////////////////////////////////////////////////////

            // Create a camera
            var camera = new THREE.Camera();
            camera.add( pointLight );
            scene.add(camera);

            ////////////////////////////////////////////////////////////////////////////////
            //          handle arToolkitSource
            ////////////////////////////////////////////////////////////////////////////////

            var arToolkitSource = new THREEx.ArToolkitSource({
                // to read from the webcam 
                // sourceType : 'webcam',

                // // to read from an image
                sourceType : 'image',
                sourceUrl : THREEx.ArToolkitContext.baseURL + '../data/images/prova.jpg',       

                // to read from a video
                // sourceType : 'video',
                // sourceUrl : THREEx.ArToolkitContext.baseURL + '../data/videos/headtracking.mp4',     
            })

            arToolkitSource.init(function onReady(){
                onResize()
            })

            // handle resize
            window.addEventListener('resize', function(){
                onResize()
            })
            function onResize(){
                arToolkitSource.onResize()  
                arToolkitSource.copySizeTo(renderer.domElement) 
                if( arToolkitContext.arController !== null ){
                    arToolkitSource.copySizeTo(arToolkitContext.arController.canvas)    
                }   
            }
            ////////////////////////////////////////////////////////////////////////////////
            //          initialize arToolkitContext
            ////////////////////////////////////////////////////////////////////////////////


            // create atToolkitContext
            var arToolkitContext = new THREEx.ArToolkitContext({
                cameraParametersUrl: THREEx.ArToolkitContext.baseURL + '../data/data/camera_para.dat',
                detectionMode: 'mono',
            })
            // initialize it
            arToolkitContext.init(function onCompleted(){
                // copy projection matrix to camera
                camera.projectionMatrix.copy( arToolkitContext.getProjectionMatrix() );
            })

            // update artoolkit on every frame
            onRenderFcts.push(function(){
                if( arToolkitSource.ready === false )   return

                arToolkitContext.update( arToolkitSource.domElement )

                // update scene.visible if the marker is seen
                scene.visible = camera.visible
            })

            ////////////////////////////////////////////////////////////////////////////////
            //          Create a ArMarkerControls
            ////////////////////////////////////////////////////////////////////////////////

            // init controls for camera
            var markerControls = new THREEx.ArMarkerControls(arToolkitContext, camera, {
                type : 'pattern',
                patternUrl : THREEx.ArToolkitContext.baseURL + '../data/data/patt.hiro',
                // patternUrl : THREEx.ArToolkitContext.baseURL + '../data/data/patt.kanji',
                // as we controls the camera, set changeMatrixMode: 'cameraTransformMatrix'
                changeMatrixMode: 'cameraTransformMatrix'
            })
            // as we do changeMatrixMode: 'cameraTransformMatrix', start with invisible scene
            scene.visible = false

            //////////////////////////////////////////////////////////////////////////////////
            //      add an object in the scene
            //////////////////////////////////////////////////////////////////////////////////


                var onProgress = function ( xhr ) {
                            if ( xhr.lengthComputable ) {
                                var percentComplete = xhr.loaded / xhr.total * 100;
                                console.log( Math.round( percentComplete, 2 ) + '% downloaded' );
                            }
                        };

                                var onError = function ( xhr ) { };
                        THREE.Loader.Handlers.add( /\.dds$/i, new THREE.DDSLoader() );

                var mtlLoader = new THREE.MTLLoader();
                        mtlLoader.setPath( 'obj/' );
                        mtlLoader.load( 'futurama.mtl', function( materials ) {
                            materials.preload();
                            var objLoader = new THREE.OBJLoader();
                            objLoader.setMaterials( materials );
                            objLoader.setPath( 'obj/' );
                            objLoader.load( 'futurama.obj', function ( object ) {
                                scene.add( object );
                            }, onProgress );
                        });




            //////////////////////////////////////////////////////////////////////////////////
            //      render the whole thing on the page
            //////////////////////////////////////////////////////////////////////////////////

            // render the scene
            onRenderFcts.push(function(){
                renderer.render( scene, camera );

            })

            // run the rendering loop
            var lastTimeMsec= null
            requestAnimationFrame(function animate(nowMsec){
                // keep looping
                requestAnimationFrame( animate );
                // measure time
                lastTimeMsec    = lastTimeMsec || nowMsec-1000/60
                var deltaMsec   = Math.min(200, nowMsec - lastTimeMsec)
                lastTimeMsec    = nowMsec
                // call each update function
                onRenderFcts.forEach(function(onRenderFct){
                    onRenderFct(deltaMsec/1000, nowMsec/1000)
                })
            })
        </script></body>

我正在尝试启用和禁用 futurama.obj,我想用一个按钮来完成,我可以将渲染放入一个函数中,当我点击按钮时调用它,但我不知道如何删除它.

4

0 回答 0