Using AlloyTouch to control three.js 3D model

105416-20161207093956632-718807571.gif

As you can see, the above cube rotation, acceleration, deceleration stop all through the AlloyTouch to achieve.

Demo(Mobile)

105416-20161207094015913-505345057.png

Code

<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,    //touch the whole document
        vertical: false,    //monitor horizontal touch
        target: mesh.rotation,  //animate mesh.rotation's y property
        property: "y",      
        factor: 0.08,       //friction coefficient
        moveFactor: 0.2     //touch move friction coefficient
    })
</script>

factor need to continue to debug the best value for the speed and time of inertial motion let go after the best effect.
moveFactor need to continue to debug the best value, is to let the lateral drag distance map to achieve the rotation angle.

If you don't need an inertial motion. For example, the king of glory in the task rotation is not inertia, the finger left the screen will immediately stop moving. Such as:

105416-20161207094025226-19737363.gif

You only need to set the AlloyTouch inertia to false.

Non inertia demo

105416-20161207094038741-1173398716.png

Code

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

    new AlloyTouch({
        touch: document,    
        vertical: false,            
        target: mesh.rotation,  
        property: "y",              
        factor: 0.08,               
        moveFactor: 0.2 ,       
        inertia: false   //disable the inertia animation
    })
</script>

AlloyTouch

Github:https://github.com/AlloyTeam/AlloyTouch
Any question, please let me know:https://github.com/AlloyTeam/AlloyTouch/issues

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值