我现在正在为某些事情而苦苦挣扎。我有一个页面,其中实现了使用带有ToolInterface
的扩展名的查看器,以便捕获鼠标事件。我成功获取了鼠标事件,并且可以检索返回的x,y坐标。
我想做的就是获取该点的世界(x,y,z)坐标。我尝试使用Viewer3D.clientToWorld
函数,但是无论点是否在几何上方,该函数始终返回null
。
我还尝试使用ViewingUtilites.getHitPoint
函数来获取世界(x,y,z)坐标,但它也总是返回null
。我尝试使用事件返回的(x,y)并进行规范化。
所以我的问题是,如何从Forge查看器中的客户端(x,y)坐标中获得世界(x,y,z)坐标?
谢谢!
****编辑:下面添加了代码
MyViewerTool = function (viewer, options) {
Autodesk.Viewing.Extension.call(this, viewer, options);
var _self = this;
var _viewer = viewer;
// ....
_self.tool = null;
function MyViewerTool(viewer, toolName) {
function normalizePoint(screenPoint) {
const viewport = _viewer.navigation.getScreenViewport();
var n = {
x: (screenPoint.x - viewport.left) / viewport.width,
y: (screenPoint.y - viewport.height) / viewport.height
}
return n;
}
this.handleSingleClick = function(event, button) {
console.log(event);
const normalizedPoint = normalizePoint({ x: event.clientX, y: event.clientY });
const worldPoint = _viewer.utilities.getHitPoint(normalizedPoint.x, normalizedPoint.y);
console.log(worldPoint);
return false;
};
// ...
}
// other code that just does the register/load/etc
// this does load in the viewer as I am able to
// set a breakpoint above and I see what looks like
// valid point data.
// my first try used the clientToWorld(...)
// using the same event data passed in handleSingleClick (above).
//
答案 0 :(得分:1)
您可以使用查看器的clientToWorld(x, y, ignoreTransparent)
功能。
示例实现:
let canvas = this.viewer.canvas;
let drag = false;
canvas.addEventListener('mousedown', () => drag = false);
canvas.addEventListener('mousemove', () => drag = true);
canvas.addEventListener('mouseup', (evt) => {
if (!drag) {
var hitTest = this.viewer.clientToWorld(evt.offsetX, evt.offsetY, true);
if (hitTest) {
let x = hitTest.point.x;
let y = hitTest.point.y;
let z = hitTest.point.z;
}
}
});
请注意使用offsetX和offsetY。