AlloyTouch与three.js 3D模型交互
2016-12-07 09:41 【当耐特】 阅读(2335) 评论(0) 编辑 收藏 举报如你所见,上面的cube的旋转、加速、减速停止都是通过AlloyTouch去实现的。
演示
代码
<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需要自己不断去调试出最佳的值,就是让横向拖拽的距离映射到旋转的角度上达到最跟手的效果。
如果,不需要惯性运动。比如像王者荣耀里的任务旋转就是没有惯性的,手指离开屏幕就会立马停止运动。如:
你只需要在new AlloyTouch设置inertia为false便可。
无惯性演示
无惯性代码
<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地址:https://github.com/AlloyTeam/AlloyTouch
欢迎issues:https://github.com/AlloyTeam/AlloyTouch/issues
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 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-07 怎么把CanvasLoading插件嵌入你的游戏