AlloyTouch與three.js 3D模型交互

如你所見,上面的cube的旋轉、加速、減速中止都是經過AlloyTouch去實現的。git

演示

代碼

<script src="asset/three.js"></script>
<script src="../../alloy_touch.js"></script>

<script>
    var camera = new THREE.PerspectiveCamera( 70, window.innerWidth / window.innerHeight, 1, 1000 );
    camera.position.z = 500;

    var scene = new THREE.Scene();

    var texture = new THREE.TextureLoader().load( 'asset/crate.gif' );
    //幾何體
    var geometry = new THREE.BoxBufferGeometry( 200, 200, 200 );
    //材質
    var material = new THREE.MeshBasicMaterial( { map: texture } );

    var mesh = new THREE.Mesh( geometry, material );
    //添加到舞臺
    scene.add( mesh );

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

    function animate() {
        requestAnimationFrame( animate );
        renderer.render( scene, camera );
    }

    animate();

    new AlloyTouch({
        touch: document,    //觸摸整個文檔
        vertical: false,            //監聽橫向觸摸
        target: mesh.rotation,    //運動 mesh.rotation
        property: "y",                //被運動的屬性 y
        factor: 0.08,                //運動期間的摩擦力
        moveFactor: 0.2        //拖拽期間的摩擦力
    })
</script>複製代碼

factor須要本身不斷去調試出最佳的值,讓鬆手以後的慣性運動的速率和時間達到最佳的效果。
moveFactor須要本身不斷去調試出最佳的值,就是讓橫向拖拽的距離映射到旋轉的角度上達到最跟手的效果。github

若是,不須要慣性運動。好比像王者榮耀裏的任務旋轉就是沒有慣性的,手指離開屏幕就會立馬中止運動。如:app

你只須要在new AlloyTouch設置inertia爲false即可。dom

無慣性演示

無慣性代碼

<script src="asset/three.js"></script>
<script src="../../alloy_touch.js"></script>
<script>
      ...
      ...
    ...
    animate();

    new AlloyTouch({
        touch: document,    //觸摸整個文檔
        vertical: false,            //監聽橫向觸摸
        target: mesh.rotation,    //運動 mesh.rotation
        property: "y",                //被運動的屬性 y
        factor: 0.08,                //運動期間的摩擦力
        moveFactor: 0.2 ,        //拖拽期間的摩擦力
        inertia: false        //禁止慣性運動
    })
</script>複製代碼

開始AlloyTouch吧

Github地址:github.com/AlloyTeam/A…
歡迎issues:github.com/AlloyTeam/A…ui

相關文章
相關標籤/搜索