附件中就是测试工程,Ray和鼠标之间有偏差~
测试主要是因为:
Laya.stage.scaleMode = Laya.Stage.SCALE_SHOWALL;
这个缩放模式引起的,这种模式下有什么特别要注意的地方么,构造Ray的时候~
import Camera = Laya.Camera;
import PhasorSpriter3D = Laya.PhasorSpriter3D;
import Ray = Laya.Ray;
import RenderState = Laya.RenderState;
import Script = Laya.Script;
import Vector2 = Laya.Vector2;
import Vector3 = Laya.Vector3;
import Vector4 = Laya.Vector4;
import WebGLContext = Laya.WebGLContext;
class SceneScript extends Script {
private _originPosition: Vector3 = new Vector3(0, 0, 0);
private _phasorSpriter3D: PhasorSpriter3D;
private _color: Vector4 = new Vector4(1, 0, 0, 1);
private _point: Vector2 = new Vector2();
private _camera: Camera;
private _ray: Ray = new Ray(new Vector3(0, 0, 0), new Vector3(0, 0, 0));
public _start(state: RenderState): void {
super._start(state);
this._phasorSpriter3D = new PhasorSpriter3D();
this._camera = this._owner.getChildByName("Camera") as Camera;
}
public _postRenderUpdate(state: RenderState): void {
super._update(state);
this._point.elements[0] = Laya.stage.mouseX;
this._point.elements[1] = Laya.stage.mouseY;
this._camera.viewportPointToRay(this._point, this._ray);
this._phasorSpriter3D.begin(WebGLContext.LINES, this._camera);
// 绘出射线
this._phasorSpriter3D.line(
this._ray.origin,
this._color,
this._originPosition,
this._color);
this._phasorSpriter3D.end();
}
}
// 程序入口
class GameMain {
constructor() {
Laya3D.init(1334, 768, true, true);
Laya.stage.alignV = Laya.Stage.ALIGN_MIDDLE;
Laya.stage.alignH = Laya.Stage.ALIGN_CENTER;
Laya.stage.scaleMode = Laya.Stage.SCALE_SHOWALL;
Laya.stage.bgColor = "#232628";
Laya.Stat.show();
let camera: Laya.Camera = new Laya.Camera();
camera.name = "Camera";
camera.transform.position = new Laya.Vector3(
-6.87492561340332, 3.95000958442688, 1.06434166431427);
camera.transform.rotation = new Laya.Quaternion(
-0.19387270510196686,
-0.6291995048522949,
-0.16616787016391754,
0.7341046333312988);
let scene: Laya.Scene = new Laya.Scene();
scene.addChild(camera);
scene.addScript(SceneScript);
Laya.stage.addChild(scene);
}
}
new GameMain();