/*
用法:
//初始化照相机
var camera = this.newScene.addChild(new Laya.Camera(0, 0.1, 100)) as Laya.Camera;
camera.transform.rotationEuler = new Laya.Vector3(-32, -75, 0);
camera.transform.position =new Laya.Vector3(-8.2, 5.3, 2.2);
var modelViewer = camera.addComponent(ModelViewer);
modelViewer.AroundPos = plane.transform.position;
*/
export default class ModelViewer extends Laya.Script {
// Text m_debugTip;
public canRotation_X: boolean = true;
public canRotation_Y: boolean = true;
public canScale: boolean = true;
/// <summary>
/// Around center.
/// </summary>
//public target: Transform3D;
public AroundPos: Vector3 = new Vector3();
/// <summary>
/// Settings of mouse button, pointer and scrollwheel.
/// </summary>
public mouseSettings: MouseSettings = new MouseSettings(0, 1, 0.3);
/// <summary>
/// Range limit of angle.
/// </summary>
public angleRange: MyRange = new MyRange(5, 90);
/// <summary>
/// Range limit of distance.
/// </summary>
public distanceRange: MyRange = new MyRange(1, 10);
/// <summary>
/// Damper for move and rotate.
/// </summary>
///[Range(0, 10)]
public damper: number = 5;
/// <summary>
/// Camera current angls.
/// </summary>
public CurrentAngles: Vector3 = new Vector3();
public CurrentAnglesTemp: Vector3 = new Vector3();
/// <summary>
/// Current distance from camera to target.
/// </summary>
public CurrentDistance: number;
/// <summary>
/// Camera target angls.
/// </summary>
protected targetAngles: Vector3 = new Vector3();
/// <summary>
/// Target distance from camera to target.
/// </summary>
protected targetDistance: number;
//protected camera: Laya.Camera;
constructor() {
super();
}
public transform: Transform3D;
public get TouchCount(): number {
if (Laya.TouchManager.I._event.touches == null) {
return 0;
} else {
return this.myevent.touches.length;
}
}
public get Touchs(): Array<any> {
return this.myevent.touches
}
public get GetAxisX(): number {
return Laya.stage.mouseX - this.lastMouseX;
}
public get GetAxisY(): number {
return Laya.stage.mouseY - this.lastMouseY;
}
public get deltaTime(): number {
return Laya.timer.delta / 1000;
}
public deltaWheel: number = 0;
public FORWORD: Vector3 = new Vector3();
public get GetForward(): Vector3 {
this.transform.getForward(this.FORWORD);
return this.FORWORD
}
protected lastMouseX: number = 0;
protected lastMouseY: number = 0;
protected mouseRunning: boolean = false;
protected Clamp(num, min, max): number {
if (num < min) {
return min;
} else if (num > max) {
return max;
} else {
return num;
}
}
protected LerpVector3(min: Vector3, max: Vector3, t: number): Vector3 {
var vec: Vector3 = new Vector3();
vec.x = min.x + t * (max.x - min.x);
vec.y = min.y + t * (max.y - min.y);
vec.z = min.z + t * (max.z - min.z);
return vec;
}
protected LerpNum(min: number, max: number, t: number): number {
return min + t * (max - min);
}
protected AroundByMobileInput(): void {
if (this.TouchCount == 1) {
console.log(this.targetAngles);
this.targetAngles.y -= this.GetAxisX * this.mouseSettings.pointerSensitivity;
this.targetAngles.x += this.GetAxisY * this.mouseSettings.pointerSensitivity;
//Range.
this.targetAngles.y = this.Clamp(this.targetAngles.y, this.angleRange.min, this.angleRange.max);
//Mouse pointer.
this.m_IsSingleFinger = true;
}
//Mouse scrollwheel.
if (this.canScale) {
if (this.TouchCount > 1) {
//计算出当前两点触摸点的位置
if (this.m_IsSingleFinger) {
this.oldPosition1 = new Vector3(this.Touchs[0].stageX, this.Touchs[0].stageY);
this.oldPosition2 = new Vector3(this.Touchs[1].stageX, this.Touchs[1].stageY);
}
var tempPosition1 = new Vector3(this.Touchs[0].stageX, this.Touchs[0].stageY);
var tempPosition2 = new Vector3(this.Touchs[1].stageX, this.Touchs[1].stageY);
var currentTouchDistance = Vector3.distance(tempPosition1, tempPosition2);
var lastTouchDistance = Vector3.distance(this.oldPosition1, this.oldPosition2);
/// <summary>
/// Range form min to max.
/// </summary>
class MyRange {
/// <summary>
/// Min value of range.
/// </summary>
public min: number;
/// <summary>
/// Max value of range.
/// </summary>
public max: number;
/// <summary>
/// Constructor.
/// </summary>
/// <param name="min">Min value of range.</param>
/// <param name="max">Max value of range.</param>
constructor(min: number, max: number) {
this.min = min;
this.max = max;
}
}
/// <summary>
/// Rectangle area on plane.
/// </summary>
class PlaneArea {
/// <summary>
/// Center of area.
/// </summary>
public center: Transform3D;
/// <summary>
/// Width of area.
/// </summary>
public width: number;
/// <summary>
/// Length of area.
/// </summary>
public length: number;
3 个回复
ebaww
赞同来自: liwenhua
183*****755
赞同来自:
赞同来自: