pex-gl / pex-math

Array-based vector, quaternion and matrix math with utils for PEX.
MIT License
14 stars 2 forks source link

Add mat2d, mat3.fromMat2d and mat4.fromTranslationRotationScale #22

Closed dmnsgn closed 5 years ago

dmnsgn commented 5 years ago

Fix #20

vorg commented 5 years ago

Most rotations in pex-renderer are Quaternions. In pex-math we do have old OpenGL axis + angle rotations too. What's the use case for mat3.fromTranslationRotationScale(t, s, r)? What's the axis of the rotation?

Mat3 is capable of representing 3d rotations and mat3.fromQuat exists so we could align with mat4 and accept quat in both cases. Currently in this PR it's confusing to have 2 functions with the same name but different types.

dmnsgn commented 5 years ago

I updated the first comment with usage. mat2d should be faster for texture transform as offset and scale are array[2] and rotation is in radians. I removed mat3.fromTranslationRotationScale as it will be confusing and not needed anymore with the addition of mat2d.

vorg commented 5 years ago
  1. How is it faster? Because we don't use quaternions for such a simple rotation?
  2. I think mat23 is a better name because we don't have mat3d
  3. Here is a crazy idea. From the KHR_texture_transform specs.

This is equivalent to Unity's Material#SetTextureOffset and Material#SetTextureScale, or Three.js's Texture#offset and Texture#repeat. UV rotation is not widely supported as of today, but is included here for forward compatibility.

I can't think of an use case where texture rotation would be useful. That simplifies data to vec2 offset and vec2 scale which... can be sent together as one vec4 and no need for mat23. Hmm?

dmnsgn commented 5 years ago
  1. How is it faster? Because we don't use quaternions for such a simple rotation?

And because it is 6 and not 9 elements

  1. I think mat23 is a better name because we don't have mat3d

I took it from glmatrix. What about mat2x3 instead to match OpenGL and WebGL 2?

  1. Here is a crazy idea. From the KHR_texture_transform specs.

This is equivalent to Unity's Material#SetTextureOffset and Material#SetTextureScale, or Three.js's Texture#offset and Texture#repeat. UV rotation is not widely supported as of today, but is included here for forward compatibility.

I can't think of an use case where texture rotation would be useful. That simplifies data to vec2 offset and vec2 scale which... can be sent together as one vec4 and no need for mat23. Hmm?

Imagine an atlas of 4 rectangular textures 300x130:

Altlas: 512x512

Columns layout:
- 300 + [82] + 130
- 300 + [82] + 130
- 300 + [212]

Row layout:
130 +
130 +
130 +
[122]
___________
| / / /  / |
| / / /  / | 
| / / /    | 
___________
dmnsgn commented 5 years ago

Cool, I updated yesterday, it's ready to be merged.

vorg commented 5 years ago

Published as pex-math@next aka pex-math@3.0.0-2