layaair3.0 非正交相机下,鼠标坐标转成世界坐标
时间: 2024-01-10 21:04:45 浏览: 124
在LayaAir 3.0中,非正交相机下,鼠标坐标转成世界坐标的方法如下:
```typescript
// 获取鼠标在Canvas区域的坐标
var point = new Laya.Point(Laya.MouseManager.instance.mouseX, Laya.MouseManager.instance.mouseY);
// 将鼠标坐标转换为相机空间坐标
var cameraPos = new Laya.Vector3();
camera.convertScreenCoordToOrthographicCoord(point, cameraPos);
// 将相机空间坐标转换为世界坐标
var worldPos = new Laya.Vector3();
camera.convertCoordToWorldCoord(cameraPos, worldPos);
```
其中,`camera` 为非正交相机实例,可以通过 `Laya.stage.addChild()` 方法添加到舞台中。
需要注意的是,非正交相机下的坐标转换需要先将屏幕坐标转换为相机空间坐标,再将相机空间坐标转换为世界坐标。
相关问题
layaair3.0 鼠标坐标转成世界坐标
在LayaAir 3.0中,可以通过`Camera`组件的`convertScreenCoordToOrthographicCoord`方法将屏幕坐标转换为世界坐标。具体实现如下:
```typescript
// 获取到场景中的相机
var camera = scene.getChildByName("Main Camera");
// 获取到鼠标在屏幕上的位置
var mouseX = Laya.MouseManager.instance.mouseX;
var mouseY = Laya.MouseManager.instance.mouseY;
// 将屏幕坐标转换为世界坐标
var point = new Laya.Vector3();
camera.convertScreenCoordToOrthographicCoord(new Laya.Vector3(mouseX, mouseY), point);
console.log("世界坐标:", point.x, point.y);
```
需要注意的是,`convertScreenCoordToOrthographicCoord`方法需要传入一个`Vector3`类型的屏幕坐标和一个`Vector3`类型的世界坐标,方法会将屏幕坐标转换为以相机为中心的正交投影下的世界坐标。
阅读全文
相关推荐














