Skip to content

Matrix4.lookAt doesn't overwrite 7 of the 16 matrix elements, such as translation #30274

@finnbear

Description

@finnbear

Description

The documentation of Matrix4.lookAt(eye, target, up) says:

Constructs a rotation matrix, looking from eye towards target oriented by the up vector.

However, the implementation only overwrites 9 of the 16 elements:

		te[ 0 ] = _x.x; te[ 4 ] = _y.x; te[ 8 ] = _z.x;
		te[ 1 ] = _x.y; te[ 5 ] = _y.y; te[ 9 ] = _z.y;
		te[ 2 ] = _x.z; te[ 6 ] = _y.z; te[ 10 ] = _z.z;

As a result, the return value is not necessarily (just) a rotation matrix. I was trying to avoid allocating by re-using the same Matrix4 for multiple types of calculations. The current behavior caused some confusion, especially as it is contrary to how every other constructor that I checked works including, notably, setFromMatrix3.

Reproduction steps

Run the code and look at the output.

Code

import * as THREE from 'three';

console.log(new THREE.Matrix4().makeTranslation(42, 42, 42).lookAt(new THREE.Vector3(0, 0, 1), new THREE.Vector3(0, 0, 0), new THREE.Vector3(0, 1, 0)));

Live example

Paste the code into https://playcode.io/three

Screenshots

image

Workaround

matrix4.identity().lookAt(...)

Alternatives

  • The documentation could be updated to mention that not all elements are overwritten.
  • (Breaking change/bad idea) use the existing translation instead of the eye argument.

Version

r172

Device

Desktop

Browser

Chrome

OS

Linux

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions