How to use the gl-matrix.mat4.translate function in gl-matrix

To help you get started, we’ve selected a few gl-matrix examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github Kitware / vtk-js / Sources / Interaction / Manipulators / MouseCameraTrackballRollManipulator / index.js View on Github external
);

    const { center } = model;
    mat4.identity(transform);
    centerNeg[0] = -center[0];
    centerNeg[1] = -center[1];
    centerNeg[2] = -center[2];

    // Translate to center
    mat4.translate(transform, transform, center);

    // roll
    mat4.rotate(transform, transform, vtkMath.radiansFromDegrees(angle), axis);

    // Translate back
    mat4.translate(transform, transform, centerNeg);

    // Apply transformation to camera position, focal point, and view up
    vec3.transformMat4(newCamPos, cameraPos, transform);
    vec3.transformMat4(newFp, cameraFp, transform);

    direction[0] = viewUp[0] + cameraPos[0];
    direction[1] = viewUp[1] + cameraPos[1];
    direction[2] = viewUp[2] + cameraPos[2];
    vec3.transformMat4(newViewUp, direction, transform);

    camera.setPosition(newCamPos[0], newCamPos[1], newCamPos[2]);
    camera.setFocalPoint(newFp[0], newFp[1], newFp[2]);
    camera.setViewUp(
      newViewUp[0] - newCamPos[0],
      newViewUp[1] - newCamPos[1],
      newViewUp[2] - newCamPos[2]
github mattdesl / canvas-sketch / examples / experimental / webgl-2d.js View on Github external
// Merge in defaults
      params = Object.assign({}, opt, params);

      // Reset to model
      mat4.identity(model);

      const scale = expandVector(params.scale, 1);

      // Reposition mesh
      if (params.position) mat4.translate(model, model, expandVector(params.position));

      // Align to origin (center or corner mode)
      if (params.unitShape && params.center !== false) {
        vec3.multiply(tmpOrigin, scale, centerOrigin);
        mat4.translate(model, model, tmpOrigin);
      }

      // Apply rotation
      if (params.rotation) {
        const hasPivot = Boolean(params.pivot);
        if (hasPivot) {
          // Determine real pivot point
          const pivot = expandVector(params.pivot);

          // Cancel Z component in pivoting
          pivot[2] = 0;

          vec3.multiply(tmpPivot, bounds.size, scale);
          vec3.multiply(tmpPivot, tmpPivot, pivot);
          vec3.negate(tmpPivotNegated, tmpPivot);
github uber / deck.gl / src / viewport / old-viewport.js View on Github external
// PROJECTION MATRIX: PROJECTS FROM CAMERA SPACE TO CLIPSPACE
    /* eslint-disable no-inline-comments */
    this.projectionMatrix = mat4.perspective(
      this._createMat4(),
      2 * Math.atan((this.height / 2) / this.altitude), // fov in radians
      this.width / this.height,                         // aspect ratio
      0.2,                                              // near plane
      this.farZ * 2                                     // far plane
    );
    /* eslint-enable no-inline-comments */

    // VIEW MATRIX: PROJECTS FROM VIRTUAL PIXELS TO CAMERA SPACE
    const vm = this._createMat4();

    // Move camera to altitude
    mat4.translate(vm, vm, [0, 0, -this.altitude]);

    // After the rotateX, z values are in pixel units. Convert them to
    // altitude units. 1 altitude unit = the screen height.
    mat4.scale(vm, vm, [1, -1, 1 / this.height]);

    // Rotate by bearing, and then by pitch (which tilts the view)
    mat4.rotateX(vm, vm, this.pitchRadians);
    mat4.rotateZ(vm, vm, -this.bearingRadians);

    this.viewMatrix = this._createMat4();
    mat4.translate(this.viewMatrix, vm, [-this.centerX, -this.centerY, 0]);

    const vpm = this._createMat4();
    mat4.multiply(vpm, vpm, this.projectionMatrix);
    mat4.multiply(vpm, vpm, this.viewMatrix);
github magcius / noclip.website / src / DarkSouls / render.ts View on Github external
function modelMatrixFromPart(m: mat4, part: Part): void {
    const modelScale = 100;

    // Game uses +x = left convention for some reason.
    mat4.scale(m, m, [-modelScale, modelScale, modelScale]);

    mat4.translate(m, m, part.translation);
    mat4.rotateX(m, m, part.rotation[0] * MathConstants.DEG_TO_RAD);
    mat4.rotateY(m, m, part.rotation[1] * MathConstants.DEG_TO_RAD);
    mat4.rotateZ(m, m, part.rotation[2] * MathConstants.DEG_TO_RAD);
    mat4.scale(m, m, part.scale);
}
github stephomi / sculptgl / src / drawables / Selection.js View on Github external
mat4.identity(_TMP_MAT);
    mat4.translate(_TMP_MAT, _TMP_MAT, vec3.transformMat4(_TMP_VEC, picking.getIntersectionPoint(), mesh.getMatrix()));
    mat4.rotate(_TMP_MAT, _TMP_MAT, rad, _TMP_AXIS);

    mat4.mul(_TMP_MATPV, camera.getProjection(), camera.getView());

    // circle mvp
    mat4.scale(this._cacheCircleMVP, _TMP_MAT, vec3.set(_TMP_VEC, worldRadius, worldRadius, worldRadius));
    mat4.mul(this._cacheCircleMVP, _TMP_MATPV, this._cacheCircleMVP);
    // dot mvp
    mat4.scale(this._cacheDotMVP, _TMP_MAT, vec3.set(_TMP_VEC, constRadius, constRadius, constRadius));
    mat4.mul(this._cacheDotMVP, _TMP_MATPV, this._cacheDotMVP);
    // symmetry mvp
    vec3.transformMat4(_TMP_VEC, pickingSym.getIntersectionPoint(), mesh.getMatrix());
    mat4.identity(_TMP_MAT);
    mat4.translate(_TMP_MAT, _TMP_MAT, _TMP_VEC);
    mat4.rotate(_TMP_MAT, _TMP_MAT, rad, _TMP_AXIS);

    mat4.scale(_TMP_MAT, _TMP_MAT, vec3.set(_TMP_VEC, constRadius, constRadius, constRadius));
    mat4.mul(this._cacheDotSymMVP, _TMP_MATPV, _TMP_MAT);
  }
github mattdesl / canvas-sketch / examples / experimental / webgl-2d.js View on Github external
// Apply rotation
      if (params.rotation) {
        const hasPivot = Boolean(params.pivot);
        if (hasPivot) {
          // Determine real pivot point
          const pivot = expandVector(params.pivot);

          // Cancel Z component in pivoting
          pivot[2] = 0;

          vec3.multiply(tmpPivot, bounds.size, scale);
          vec3.multiply(tmpPivot, tmpPivot, pivot);
          vec3.negate(tmpPivotNegated, tmpPivot);

          // Move to pivot point
          mat4.translate(model, model, tmpPivot);
        }

        // Apply rotation
        mat4.rotateZ(model, model, params.rotation);

        // Negate pivot
        if (hasPivot) {
          mat4.translate(model, model, tmpPivotNegated);
        }
      }

      // Apply scale
      mat4.scale(model, model, scale);

      // Apply user transforms
      if (params.transform) mat4.multiply(model, model, params.transform);
github Kitware / vtk-js / Sources / Interaction / Manipulators / MouseCameraTrackballRotateManipulator / index.js View on Github external
);

    // Elevation
    vtkMath.cross(camera.getDirectionOfProjection(), viewUp, v2);
    mat4.rotate(
      trans,
      trans,
      vtkMath.radiansFromDegrees(((-360.0 * dy) / size[1]) * rotationFactor),
      v2
    );

    // Translate back
    centerNeg[0] = -center[0];
    centerNeg[1] = -center[1];
    centerNeg[2] = -center[2];
    mat4.translate(trans, trans, centerNeg);

    // Apply transformation to camera position, focal point, and view up
    vec3.transformMat4(newCamPos, cameraPos, trans);
    vec3.transformMat4(newFp, cameraFp, trans);
    direction[0] = viewUp[0] + cameraPos[0];
    direction[1] = viewUp[1] + cameraPos[1];
    direction[2] = viewUp[2] + cameraPos[2];
    vec3.transformMat4(newViewUp, direction, trans);

    camera.setPosition(newCamPos[0], newCamPos[1], newCamPos[2]);
    camera.setFocalPoint(newFp[0], newFp[1], newFp[2]);
    camera.setViewUp(
      newViewUp[0] - newCamPos[0],
      newViewUp[1] - newCamPos[1],
      newViewUp[2] - newCamPos[2]
    );
github yoo2001818 / webglue / client-test / newRenderMinimal.js View on Github external
let instancedData = renderer.geometries.create({
  attributes: {
    aInstPos: range(100).map(() => [
      Math.random() * 50 - 25, Math.random() * 50 - 25, Math.random() * 50 - 25
    ])
  },
  instanced: {
    aInstPos: 1
  }
});
let boxes = renderer.geometries.create([box, instancedData]);

let projMat = mat4.create();
mat4.perspective(projMat, Math.PI / 180 * 70, 800/600, 0.1, 60);
let viewMat = mat4.create();
mat4.translate(viewMat, viewMat, new Float32Array([0, 0, -5]));

let model1Mat = mat4.create();
let model2Mat = mat4.create();
let uvMat = mat4.create();

mat4.translate(model2Mat, model2Mat, new Float32Array([0, 2, 2]));
mat4.translate(model1Mat, model1Mat, new Float32Array([0, 0, 1]));

let model1Normal = mat3.create();
let model2Normal = mat3.create();
mat3.normalFromMat4(model2Normal, model2Mat);

mat4.translate(uvMat, uvMat, new Float32Array([0, 0, 2]));

let prevTime = -1;
let timer = 0;
github DeviateFish / ingress-model-viewer / src / mesh / spherical-portal-link.js View on Github external
function buildMatrix(s, e, radius) {
  var mat = mat4.create();
  mat4.rotateY(mat, mat, s[1]);
  mat4.rotateZ(mat, mat, s[0] - Math.PI / 2);
  mat4.rotateY(mat, mat, -getBearing(s, e));
  mat4.translate(mat, mat, [0, radius, 0]);
  return mat;
}
github pex-gl / pex-math / benchmark / bench-gl-matrix.js View on Github external
bench('mat4/translate', function (b) {
  var mat4 = glMat4.create()

  b.start()
  glMat4.translate(mat4, mat4, [4, 2, 9])
  b.end()
})