threejs修改滚轮移动的缩放倍数

参考https://www.cnblogs.com/zjf-1992/p/6146486.html:

这篇博客里实现了“鼠标上下滑轮实现放大缩小”的效果:

//监听鼠标滚动事件
canvas.addEventListener('mousewheel', mousewheel, false);

(1)搜索本项目已经使用的OrbitControls.js(控制鼠标行为)中的“mousewheel”关键字,得知其监听
“function onMouseWheel(event)方法”:

scope.domElement.addEventListener( 'wheel', onMouseWheel, false );

(2)进入 function onMouseWheel(event):

	function onMouseWheel( event ) {
     
		if ( scope.enabled === false || scope.enableZoom === false || ( state !== STATE.NONE && state !== STATE.ROTATE ) ) return;
		event.preventDefault();
		event.stopPropagation();
		scope.dispatchEvent( startEvent );
		handleMouseWheel( event );
		scope.dispatchEvent( endEvent );
	}

(3)得知其调用相关方法 handleMouseWheel(event),进入该方法:

	function handleMouseWheel( event ) {
     
		// console.log( 'handleMouseWheel' );
		if ( event.deltaY < 0 ) {
     
			dollyOut( getZoomScale() );
		} else if ( event.deltaY > 0 ) {
     
			dollyIn( getZoomScale() );
		}
		scope.update();
	}

(4)结合threeJS使用的PerspectiveCamera的zoom属性,是“获取或者设置摄像机的缩放倍数,其默认值为1”,猜想dollyOut和dollyIn传入的参数getZoomScale()就是需要修改的缩放倍数,于是进入getZoomScale()函数:

	function getZoomScale() {
     
		return Math.pow( 0.95, scope.zoomSpeed );
	}

(5)搜索zoomSpeed查到 this.zoomSpeed的默认值是1.0,修改该值即可。

...
	// This option actually enables dollying in and out; left as "zoom" for backwards compatibility.
	// Set to false to disable zooming
	this.enableZoom = true;
	this.zoomSpeed = 1.0;

	// Set to false to disable rotating
	this.enableRotate = true;
	this.rotateSpeed = 1.0;
...

总结:
(1)直接修改threejs的PerspectiveCamera的zoom属性,是没有效果滴。
(2)本文记录了探索修改源码的过程;源码写的很清晰,修改起来就很快,很值得学习。

你可能感兴趣的:(javascript)