当前位置:   article > 正文

Laya实现单指旋转,双指缩放与平移_laya 双指缩放

laya 双指缩放

export default class MobileModelScript extends Laya.Script3D{// ES6类的继承,继承父类的属性和方法。继承实质:先创建父类的实例对象this,然后再用子类的构造函数修改this;
	constructor(){
		// super作为对象使用的话可以用来调用父类中原型上的方法(不能调用实例的方法属性-constructor中的),作为函数使用代表父类的构造函数
		super();// 表示父类的构造函数,调用父类的constructor,用来新建父类的this对象;子类必须在constructor方法中调用super()方法,因为子类没有自己的this对象,而是继承父类的this对象,然后对其进行加工;如果不调用super,子类就得不到this对象;
		this.scene = null;
		this.lastPosition = new Laya.Vector2(0, 0);
		this.distance = 0.0;
		this.disFirstTouch = new Laya.Vector2(0, 0);
		this.disLastTouch = new Laya.Vector2(0, 0);
		this.isTwoTouch = false;
		this.first = true;
		this.twoFirst = true;
		this.rotate = new Laya.Vector3(0,0,0);
		this.sprite3DSacle = new Laya.Vector3(0,0,0);
		//模型默认自转,当用户单击屏幕时取消自转,双击屏幕重新按照初始角度,初始位置和当前缩放值旋转
		this.modelRotate = true;
		this.clicked = 1;
		this.clickedTime = {
			'timeA':'',
			'timeB':''
		}
	}
	
    /**
     * 第一次执行update之前执行,只会执行一次
     */
	onStart(){
		this.scene =  this.owner.parent;
		// 存储模型的初始旋转角度
		this.firstRotate = new Laya.Quaternion(this.owner.transform.rotation.x,this.owner.transform.rotation.y,this.owner.transform.rotation.z,this.owner.transform.rotation.w);
		// 存储模型初始位置
		this.firstPosition = new Laya.Vector3(this.owner.transform.position.x,this.owner.transform.position.y,this.owner.transform.position.z);
		// Laya.stage.on(Laya.Event.MOUSE_OUT,this,this.onMouseOut);
		Laya.stage.on(Laya.Event.MOUSE_UP,this,this.onMouseUp);
	}

    /**
     * 每帧更新时执行
     */
	onUpdate(){
		var mod = this.owner;
		if (this.modelRotate) {
			//自动旋转
			mod.transform.rotate(new Laya.Vector3(0,0.5,0),false,false);
		}
		// 获取触摸点个数
		let touchCount = this.scene.input.touchCount();
		if (1 === touchCount){
			//判断是否为两指触控,撤去一根手指后引发的touchCount===1
			if(this.isTwoTouch){
				return;
			}
			//获取当前的触控点,数量为1
			let touch = this.scene.input.getTouch(0);// 获取触摸点,参数代表索引
			//是否为新一次触碰,并未发生移动
			if (this.first){
				//获取触碰点的位置
				this.lastPosition.x = touch._position.x;
				this.lastPosition.y = touch._position.y;
				this.first = false;
			}
			else{
				//移动触碰点
                let deltaY =  this.lastPosition.y - touch._position.y;
				let deltaX = touch._position.x - this.lastPosition.x;
				this.lastPosition.x = touch._position.x;
				this.lastPosition.y = touch._position.y;
				//根据移动的距离进行旋转
				this.rotate.setValue(1 * deltaY /2, 1 * deltaX / 2, 0);
				mod.transform.rotate(this.rotate,false,false);
			}
		}
		else if (2 === touchCount){
			this.isTwoTouch = true;
			//获取两个触碰点
			let touch = this.scene.input.getTouch(0);
			let touch2 = this.scene.input.getTouch(1);
			//是否为新一次触碰,并未发生移动
			if (this.twoFirst){
				//获取触碰点的位置
				this.disFirstTouch.x = touch.position.x - touch2.position.x;
				this.disFirstTouch.y = touch.position.y - touch2.position.y;
				this.distance = Laya.Vector2.scalarLength(this.disFirstTouch);// 计算标量长度
				// console.log('First Distance',this.distance);
				this.sprite3DSacle = mod.transform.scale;

				this.touchAFirstX = touch.position.x;
				this.touchAFirstY = touch.position.y;
				this.touchBFirstX = touch2.position.x;
				this.touchBFirstY = touch2.position.y;

				this.twoFirst = false;
			}
			else{
				// 缩放
				this.disLastTouch.x = touch.position.x - touch2.position.x;
				this.disLastTouch.y = touch.position.y - touch2.position.y;
				let distance2 = Laya.Vector2.scalarLength(this.disLastTouch);
				//根据移动的距离进行缩放
				let factor =  0.001 * (distance2 - this.distance);
				this.sprite3DSacle.x += factor;
				this.sprite3DSacle.y += factor;
				this.sprite3DSacle.z += factor;
				this.ctrolScale();
				mod.transform.scale = this.sprite3DSacle;
				this.distance = distance2;

				// 移动
				this.touchALastX = touch.position.x;
				this.touchALastY = touch.position.y;
				this.touchBLastX = touch2.position.x;
				this.touchBLastY = touch2.position.y;

				let centerFirst = new Laya.Point((this.touchAFirstX + this.touchBFirstX) / 2, (this.touchAFirstY + this.touchBFirstY) / 2); 
				let centerLast = new Laya.Point((this.touchALastX + this.touchBLastX) / 2, (this.touchALastY + this.touchBLastY) / 2);

				let moveX = (centerFirst.x - centerLast.x)
				let moveY = (centerFirst.y - centerLast.y);
				mod.transform.translate(new Laya.Vector3(moveX,moveY,0),false);

				this.touchAFirstX = touch.position.x;
				this.touchAFirstY = touch.position.y;
				this.touchBFirstX = touch2.position.x;
				this.touchBFirstY = touch2.position.y;
			}	
		}
		else if (0 === touchCount){
			this.first = true;
			this.twoFirst = true;
			this.lastPosition.x = 0;
			this.lastPosition.y = 0;
			this.isTwoTouch = false;
		}
	}

	ctrolScale(){
        this.sprite3DSacle.x = this.sprite3DSacle.x < 0.75 ? 0.75 : (this.sprite3DSacle.x > 2.5 ? 2.5 : this.sprite3DSacle.x);
        this.sprite3DSacle.y = this.sprite3DSacle.y < 0.75 ? 0.75 : (this.sprite3DSacle.y > 2.5 ? 2.5 : this.sprite3DSacle.y);
        this.sprite3DSacle.z = this.sprite3DSacle.z < 0.75 ? 0.75 : (this.sprite3DSacle.z > 2.5 ? 2.5 : this.sprite3DSacle.z);
	}

    /**
     * 鼠标弹起时执行
     */
	onMouseUp() {	
		if (this.clicked == 1) {
			this.clicked++;
			// 单击事件 模型停止自动旋转
			this.modelRotate = false;
		} else if (this.clicked ==  2) {
			this.clickedTime.timeA = new Date();
			this.clicked++;
		} else if(this.clicked == 3) {
			this.clickedTime.timeB = new Date();
			let gap = Math.abs(this.clickedTime.timeA - this.clickedTime.timeB);
			if (gap < 400 && gap > 100){
				this.clicked = 1;
				// 双击事件 模型恢复初始角度和初始位置并重新开始自动旋转
				this.owner.transform.rotation = this.firstRotate;
				this.owner.transform.position = this.firstPosition;
				this.modelRotate = true;
			} else {
				this.clickedTime.timeA = new Date();
			}
		}
	}
}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/214618
推荐阅读
  

闽ICP备14008679号