-
-
Notifications
You must be signed in to change notification settings - Fork 925
feat: enhance gl-matrix types #6460
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
using patched version of gl-matrix see bigmistqke/gl-matrix@9267a9e
| } | ||
|
|
||
| private get currentTransform(): ITransform { | ||
| private get currentTransform(): VerticalPerspectiveTransform | MercatorTransform { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why was this changed?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will revert this. It's a more narrow type, but it's also a bit irrelevant to this PR.
| public get inverseProjectionMatrix(): mat4<Float64Array> { return this.currentTransform.inverseProjectionMatrix; } | ||
|
|
||
| public get cameraPosition(): vec3 { return this.currentTransform.cameraPosition; } | ||
| public get cameraPosition(): vec3<Float64Array | Vec3Tuple> { return this.currentTransform.cameraPosition; } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like an odd typescript definition...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I agree, this looks bug prone. All the 'ugly' types are values that switch type during their lifetime, which the previous typings had hidden away from us.
| } | ||
|
|
||
| calculateFogMatrix(unwrappedTileID: UnwrappedTileID): mat4 { | ||
| calculateFogMatrix(unwrappedTileID: UnwrappedTileID): mat4<Float64Array> | mat4<Float32Array> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This looks a bad return value... Is it really the case here?
| return Frustum.fromInvProjectionMatrix(this._invViewProjMatrix, this.worldSize); | ||
| } | ||
| getClippingPlane(): vec4 | null { | ||
| getClippingPlane(): null { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm guessing the return value here was meant for the interface implementation...?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I will revert this!
|
Is be more interested to see the typescript charges in gl-matrix that solves all these issues. |
The patch is generated from a build of the last commit of https://github.com/bigmistqke/gl-matrix/tree/feat-improve-typings
I am not completely following here.
I agree, it's a horrible name, but sadly it is what |
This is in regards to the possible solution of the gl-matrix types. Maybe allowing the matrix operators to receive more general types by adding an overload to the methods definition can solve some of the issues here. |
I think C# also can hold n-tuples - not just 2-tuples? https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/value-tuples "You can define tuples with an arbitrary large number of elements:" |
|
True, but I think the original implementation was only |
Continuing from issue #6316 in which I proposed for
MapLibre'sgl-matrixtypes to reflect more their actual types. The original usecase for this type enhancement was a bug in an earlier version of chrome where aFloat64ArrayfromMapLibrethat was typed as aFloat32Arraywas passed to WebGL, which caused an error as earlier WebGL versions only accepted aFloat32Array.Introduction
Earlier versions of
gl-matrixhad as typeTuple | Float32Array.MapLibreusesFloat64Arrayin several spots, but these were cast toFloat32Arrayto appease the squiggly typelords.The most recent version of
gl-matrixfixed this issue slightly by widening its types toTuple | IndexedCollection<number>whereIndexedCollection extends Iterable<number>.This removed the need for casting to
Float32Array, but there is still room for improvement:gl-matrixdoes not make use of any generics, this means that all of its methods returnvec3and the like, widening the types of its input..create()and a handful of other methods create a default array-type which can be set withimport {glMatrix} from 'gl-matrix'; glMatrix.ARRAY_TYPE = .... They also returnvec3and the like.Tuple | ...because it wants to communicate that only certain indices are valid and typescript has no alternative for a tuple forTypedArrays.Changes
I patched
gl-matrixin the following ways (see https://github.com/bigmistqke/gl-matrix/tree/feat-improve-typings):TypedArrayTupleto simulate tuple-like typings forTypedArraysand otherIterables, see ts playgroundVec3Result<T>and the like,Vec3ResultisVec3TupleifTis an array andTypedArrayTuple<Kind, Vec3Tuple['length']>if it's notFloat32Array(although this is not 100% correct as it falls back toArrayifFloat32Arrayis not available in the runtime the code is executed)declare module 'gl-matrix' { interface Overrides { arrayType: Float64Array; }}it is typed asFloat64ArrayThis allowed me to remove a bunch of type-casts and highlight methods/properties that have multiple types throughout their lifetimes.
Conclusion
I am not completely sure of how I refactored the base types from
to
which isn't super useful as we do lose some type-information when doing
position: vec3<Float64Array> = new Float64Array(3)as this just becomesFloat64Array. I could eithervec3<Float64Array>toTypedArrayTuple<Float64Array, 3>, but this would cause a bunch of type-errors in existing code and the only solution would be to cast as typescript natively does not capture its lengthvec3and the like:position: Float64Array = new Float64Array(3)Ideally I want to upstream this to
gl-matrix, but I first wanted to get some feedback from you guys.