BallControls.js

2018-08-29  本文已影响0人  浪巅

var BallControls = function(camera, domElement) {

    var scope = this;

    scope.enabled = true;

    scope.arrBall = [];

    scope.arrTarget = []

    var clock = new THREE.Clock();

    var rect = domElement.getBoundingClientRect();

    var pointerVector = new THREE.Vector2();

    // var gplane;

    // Ball throwing mechanics

    var startTouchTime

    var endTouchTime

    var startTouch

    var endTouch

    var selectBall = null;

    ray = new THREE.Raycaster();

    domElement.addEventListener("mousedown", onMouseDown, false );

    domElement.addEventListener("mousemove", onMouseMove, false );

    domElement.addEventListener("mouseup", onMouseUp, false );

    // this.dispose = function () {

    //  window.removeEventListener( 'mousedown', onMouseDown, false );

    //  window.removeEventListener( 'mousemove', onMouseMove, false );

    //  window.removeEventListener( 'mouseup', onMouseUp, false );

    // };

    function onMouseDown(e){

        if ( scope.enabled === false ) return;       

        clock.getDelta();

        // Find mesh from a ray

        var entity = intersectObjects(e.clientX, e.clientY, scope.arrBall)

        // var pos = entity.point;

        if(/*pos &&*/ entity){

            selectBall = entity.object

            scope.dispatchEvent( { type: 'selectBall' } );

            event.stopPropagation();

            // Set the movement plane

            // setScreenPerpCenter(pos,camera);

            // var idx = arrBall.indexOf(entity.object);

            // if(idx !== -1){

            //    // addMouseConstraint(pos.x,pos.y,pos.z,bodies[idx]);

            // }

        }

    }

    function onMouseMove(e){

        if ( scope.enabled === false ) return;

        if (!selectBall) return;

        event.stopPropagation();

        // Move and project on the plane

        // if (gplane) {

        //    var pos = projectOntoPlane(e.clientX,e.clientY,gplane,camera);

        //    if(pos){

        //    }

        // }

    }

    function onMouseUp(e) {

        if ( scope.enabled === false ) return;

        if (!selectBall) return;

        var delta = clock.getDelta();

        console.log(delta)

        throwBall(e, delta)

        event.stopPropagation();

    }

    function throwBall(e, delta) {

        var entity = intersectObjects(e.clientX, e.clientY, scope.arrTarget)

        if(entity){

            scope.dispatchEvent( { type: 'throwBall' ,

                                  ball: selectBall ,

                                  to: entity.point ,

                                  delta: delta

            });

        }

        selectBall = null;

    }

    function intersectObjects( clientX, clientY, objects ) {

        var x = ( clientX - rect.left ) / rect.width;

        var y = ( clientY - rect.top ) / rect.height;

        pointerVector.set( ( x * 2 ) - 1, - ( y * 2 ) + 1 );

        ray.setFromCamera( pointerVector, camera );

        var intersections = ray.intersectObjects( objects, false );

        return intersections[ 0 ] ? intersections[ 0 ] : false;

    }

}

BallControls.prototype = Object.create( THREE.EventDispatcher.prototype );

BallControls.prototype.constructor = BallControls;

上一篇下一篇

猜你喜欢

热点阅读