代码改变世界

Using AlloyTouch to control three.js 3D model

  【当耐特】  阅读(1046)  评论(0编辑  收藏  举报

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

Demo(Mobile)

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:

You only need to set the AlloyTouch inertia to false.

Non inertia demo

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

编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· SQL Server 2025 AI相关能力初探
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
历史上的今天:
2011-12-08 HTML5游戏制作完全指南
点击右上角即可分享
微信分享提示