toji / gl-matrix

Javascript Matrix and Vector library for High Performance WebGL apps
glmatrix.net
MIT License
5.4k stars 725 forks source link

Should targetTo preserve scaling? #450

Closed bchirlsbiodigital closed 2 years ago

bchirlsbiodigital commented 2 years ago

Consider the case where you want to point an object in a 3D scene towards another object and the "eye" object has a scale (either its own or inherited from a parent). It seems to me that the targetTo operation should effectively only change the orientation of that object's matrix, without affecting either the translation or scale.

Is this a reasonable assumption? I could see where this might get complicated if the scale is non-uniform.

targetTo sets the scaling back to [1, 1, 1] even if there is already a scaling factor in the source matrix.

Sample code:

const {
  mat4,
  vec3,
  quat
} = require("gl-matrix");

const originalMatrix = mat4.create();
mat4.fromRotationTranslationScale(
    originalMatrix,
    [0, 0, 0, 1], // quaternion
    [1, -2, 3], // translation
    [2, 2, 2] // scale
);

const targetedMatrix = mat4.create();
mat4.targetTo(
    targetedMatrix,
    [1, -2, 3], // eye
    [5, 6, 7], // target
    [0, 1, 0] // up
);

const translation = mat4.getTranslation([], targetedMatrix);
const quaternion = mat4.getRotation([], targetedMatrix);
const scale = mat4.getScaling([], targetedMatrix);

console.log({translation}); // [1, -2, 3]
console.log({quaternion}); // [-0.1759... 0.8204... -0.4247... -0.3398...]
console.log({scale}); // [0.9999..., 0.9999..., 1.0000...] shouldn't it be ~[2, 2, 2]?

Live test here: https://runkit.com/bchirlsbiodigital/62bb0ec0ff1ecd0008afb295

magcius commented 2 years ago

You never used originalMatrix, so I don't know where targetedMatrix would get the scaling factor from...

bchirlsbiodigital commented 2 years ago

Ugh, you're right!