精益对象朝向鼠标threejs

时间:2017-04-04 04:30:46

标签: javascript three.js

我正在尝试使用obj文件重新创建鼠标交互 - 类似于此示例:

http://yungjake.com/usb/

此外,我试图在点击时让它具有动画效果 - 我真的想在鼠标悬停时重新创建鼠标交互的平滑度/物理效果。

到目前为止,我的代码几乎没有关闭,但现在是:

<script src="three.min.js"></script>

    <script src="EffectComposer.js"></script>
    <script src="CopyShader.js"></script>
    <script src="DotScreenShader.js"></script>
    <script src="RGBShiftShader.js"></script>
    <script src="FilmShader.js"></script>
    <script src="FilmPass.js"></script>


    <script src="RenderPass.js"></script>
    <script src="MaskPass.js"></script>
    <script src="ShaderPass.js"></script>

    <script src="Detector.js"></script>
    <script src="OBJLoader.js"></script>


    <script>

        var camera, scene, renderer, composer;
        var object, light;
        var geometry;

        init();
        animate();

        function init() {

            renderer = new THREE.WebGLRenderer();
            renderer.setPixelRatio( window.devicePixelRatio );
            renderer.setSize( window.innerWidth, window.innerHeight );
            document.body.appendChild( renderer.domElement );

            var composer = new THREE.EffectComposer(renderer);

            //

            camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
            camera.position.z = 20;

            scene = new THREE.Scene();
            scene.fog = new THREE.Fog( 0x000000, 1, 1000 );


            //=======================================================================================================================================================
            //=======================================================================================================================================================

                            // BACKGROUND

                            scene.background = new THREE.CubeTextureLoader()
                                    .load( [ 'px.jpg', 'nx.jpg', 'py.jpg', 'ny.jpg', 'pz.jpg', 'nz.jpg' ] );

                            var textureCube = new THREE.CubeTextureLoader()
                                    .load( [ 'px.jpg', 'nx.jpg', 'py.jpg', 'ny.jpg', 'pz.jpg', 'nz.jpg' ] );
                            textureCube.mapping = THREE.CubeRefractionMapping;

                            var manager = new THREE.LoadingManager();
                            manager.onProgress = function ( item, loaded, total ) {
                                console.log( item, loaded, total );
                            };


            scene.add( new THREE.AmbientLight( 0x222222 ) );

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

            // postprocessing

            composer = new THREE.EffectComposer( renderer );

            var effect = new THREE.ShaderPass( THREE.RGBShiftShader );
            effect.uniforms[ 'amount' ].value = .05;
            effect.renderToScreen = true;
            composer.addPass( effect );



            var renderPass = new THREE.RenderPass(scene, camera);
            composer.addPass(renderPass);

            var effectFilm = new THREE.FilmPass( .5, 0, 0, false);
            effectFilm.renderToScreen = true;
            composer.addPass(effectFilm);


            var clock = new THREE.Clock();

            function render() {
                var delta = clock.getDelta();
                composer.render(delta); //parameter must be set with render
                requestAnimationFrame(render);
            }

            render();
            //

            //=======================================================================================================================================================
            //=======================================================================================================================================================


                            // MATERIALS + OBJ LOADER + TEXTURE

                            var texture = new THREE.Texture();

                            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 ) {
                            };

            //=======================================================================================================================================================

                            var material1 = new THREE.MeshBasicMaterial( { color: 0xffffff, envMap: textureCube, refractionRatio: .25 } );

                            var loader = new THREE.ImageLoader( manager );
                            loader.load( 'UV_Grid_Sm.jpg', function ( image ) {
                                texture.image = image;
                                texture.needsUpdate = true;
                            } );

                            var loader = new THREE.OBJLoader( manager );
                            loader.load( 'WorkPlease.obj', function ( object ) {
                                object.traverse( function ( child ) {
                                    if ( child instanceof THREE.Mesh ) {
                                        child.material.map = texture;
                                        child.material = material1;
                                    }
                                } );

                                window.addEventListener('mousemove', function(e){
                                var mouse3D = new THREE.Vector3(
                                        ( event.clientX / window.innerWidth ) * 2 - 1,
                                        - ( event.clientY / window.innerHeight ) * 2 + 1,
                                        2 );

                                object.lookAt(mouse3D);

                                var angle = Math.atan2(mouse3D.y-object.position.y, mouse3D.x-object.position.x);
                                object.rotation.z = angle;
                })
                                scene.add( object );



                                // object.position.y = 0;
                                // //
                                // object.position.x = 0;
                                // //
                                // object.position.z = 0;

                                //object.rotateOnAxis( 50, 50, 50 );

                                // object.center.x = 0;
                                // object.center.y = 0;
                                // object.center.x = 0;
                                //
                                // mesh.position.set( object.center.x, object.center.y, object.center.z );
                                // mesh.geometry.applyMatrix(new THREE.Matrix4().makeTranslation( -object.center.x, -object.center.y, -object.center.z ) );







                            }, onProgress, onError );








            function onDocumentMouseMove(event) {
                mouseX = ( event.clientX - windowHalfX ) * .005;
                mouseY = ( event.clientY - windowHalfY ) * .005;


            }


            window.addEventListener( 'resize', onWindowResize, false );

        }

        function onWindowResize() {

            camera.aspect = window.innerWidth / window.innerHeight;
            camera.updateProjectionMatrix();

            renderer.setSize( window.innerWidth, window.innerHeight );
            //composer.setSize( window.innerWidth, window.innerHeight );

        }

        function animate() {

            requestAnimationFrame( animate );

            // object.rotation.x += 0.005;
            // object.rotation.y += 0.01;


        //  composer.render();

        }

    </script>

</body>

请让我知道我应该更改什么,或者是否有任何教程可以帮助我解决这个问题。我是初学者,所以任何帮助都表示赞赏。

感谢。

0 个答案:

没有答案