2D 缩放到 webgl 中的点

Luc*_*ozo 5 javascript webgl gl-matrix regl

我正在尝试使用 WebGL (更具体地说,是 regl)创建 2D 图形可视化。通过我当前的实现,我已经可以看到力布局应用于每个节点,这很好。当我尝试相对于当前鼠标位置进行缩放时,问题就出现了。根据我的研究,要实现这种行为,需要按以下顺序应用矩阵变换:

translate(nodePosition, mousePosition)
scale(scaleFactor)
translate(nodePosition, -mousePosition)
Run Code Online (Sandbox Code Playgroud)

因此,每次wheel触发事件时,都会重新计算鼠标位置,并使用新的鼠标位置信息更新变换矩阵。目前的行为很奇怪,我似乎不明白出了什么问题。这是一个活生生的例子

在此输入图像描述

显然,如果我将鼠标固定在初始位置进行放大和缩小,一切都会正常工作。但是,如果我移动鼠标并尝试将焦点集中在另一个节点上,则会失败。

检索鼠标位置的函数是:

const getMousePosition = (event) => {
    var canvas = event.currentTarget
    var rect = canvas.getBoundingClientRect()
    var x = event.clientX - rect.left
    var y = event.clientY - rect.top
    var projection = mat3.create()
    var pos = vec2.fromValues(x,y)
    // this converts the mouse coordinates from 
    // pixel space to WebGL clipspace
    mat3.projection(projection, canvas.clientWidth, canvas.clientHeight)
    vec2.transformMat3(pos, pos, projection)
    return(pos)
}
Run Code Online (Sandbox Code Playgroud)

事件wheel监听回调:

var zoomFactor = 1.0
var mouse = vec2.fromValues(0.0, 0.0)
options.canvas.addEventListener("wheel", (event) => {
    event.preventDefault()
    mouse = getMousePosition(event)
    var direction = event.deltaY < 0 ? 1 : -1
    zoomFactor = 1 + direction * 0.1
    updateTransform()
})
Run Code Online (Sandbox Code Playgroud)

以及更新变换的函数:

var transform = mat3.create()
function updateTransform() {
    var negativeMouse = vec2.create()
    vec2.negate(negativeMouse, mouse)
    mat3.translate(transform, transform, mouse)
    mat3.scale(transform, transform, [zoomFactor, zoomFactor])
    mat3.translate(transform, transform, negativeMouse)
}
Run Code Online (Sandbox Code Playgroud)

transform矩阵在顶点着色器中作为统一矩阵提供:

  precision highp float;
  attribute vec2 position;

  uniform mat3 transform;

  uniform float stageWidth;
  uniform float stageHeight;

  vec2 normalizeCoords(vec2 position) {
    float x = (position[0]+ (stageWidth  / 2.0));
    float y = (position[1]+ (stageHeight / 2.0));

    return vec2(
        2.0 * ((x / stageWidth ) - 0.5),
      -(2.0 * ((y / stageHeight) - 0.5))
    );
  }

  void main () {
    gl_PointSize = 7.0;
    vec3 final = transform * vec3(normalizeCoords(position), 1);
    gl_Position = vec4(final.xy, 0, 1);
  }
Run Code Online (Sandbox Code Playgroud)

其中,position是保存节点位置的属性。

到目前为止我已经尝试过:

  • 我已经尝试更改转换的顺序。结果更奇怪。
  • 当我独立应用平移或缩放时,一切看起来都很好。

这是我第一次与不常见的 SVG/canvas 东西进行交互。解决方案可能很明显,但我真的不知道该去哪里寻找。我究竟做错了什么?

更新 06/11/2018

我遵循@Johan的建议并在现场演示中实现了它。虽然解释颇有说服力,但结果却不太符合我的预期。反转变换以获取模型空间中的鼠标位置的想法对我来说很有意义,但我的直觉(可能是错误的)表明直接在屏幕空间上应用变换也应该可行。为什么我不能将节点和鼠标都投影到屏幕空间中并直接在那里应用变换?

更新 07/11/2018

经过一番挣扎后,我决定采取不同的方法,并根据我的用例调整此答案中的解决方案。尽管缩放功能按预期工作(还添加了平移),但我仍然相信存在根本不依赖 d3-zoom 的解决方案。也许隔离视图矩阵并独立控制它以实现预期的行为,如评论中所建议的。要查看我当前的解决方案,请检查下面我的答案。

Luc*_*ozo 2

好吧,在原始方法失败后,我设法使这个解决方案适用于我的用例。

现在的功能updateTransform是:

var transform = mat3.create();
function updateTransform(x, y, scale) {
    mat3.projection(transform, options.canvas.width, options.canvas.height);
    mat3.translate(transform, transform, [x,y]);
    mat3.scale(transform, transform, [scale,scale]);
    mat3.translate(transform, transform, [
      options.canvas.width / 2,
      options.canvas.height / 2
    ]);
    mat3.scale(transform, transform, [
      options.canvas.width / 2,
      options.canvas.height / 2
    ]);
    mat3.scale(transform, transform, [1, -1]);
}
Run Code Online (Sandbox Code Playgroud)

并由 d3-zoom 调用:

import { zoom as d3Zoom } from "d3-zoom";
import { select } from "d3-selection";

var zoom = d3Zoom();

d3Event = () => require("d3-selection").event;

select(options.canvas)
      .call(zoom.on("zoom", () => {
          var t = d3Event().transform
          updateTransform(t.x, t.y, t.k)
       }));
Run Code Online (Sandbox Code Playgroud)

是该解决方案的现场演示。

在此输入图像描述