Comments (11)
I was thinking about this on my way in to work this morning. :) Completely agree on all points. I'm sure someone somewhere will gripe about needing to type a bit more, but it would bring a lot of consistency to the use of the library, further enforce best practices, and be faster to boot! Can't really see a downside!
from gl-matrix.
One way to avoid too much extra typing would be to make the names like vec3
, mat4
, etc. directly into the creation functions for the relevant objects. Since in Javascript, functions can still have arbitrary properties assigned, nothing else would change. So:
eye4 = mat4.identity(mat4());
w = vec3.add(u, v, vec3());
// ... or alternately
vec3.add(u, v, u);
from gl-matrix.
If you do make the change so that dest
is always required, I would also change it so that the creation function never takes an argument. So under the scheme suggested above, replace..
v = mat3.create(u);
with
v = mat3.set(u, mat3());
or to keep the current creator function name
v = mat3.set(u, mat3.create());
or maybe that change would just seem unnecessarily obnoxious for library consumers.
from gl-matrix.
I don't see a problem with mat3()
as a shortcut to mat3.create
. I'd lean towards keeping one as an alias for the other, rather than replacing one with the other.
As for constructor arguments, I like being able to initialize it with a value. I do this in dozens of places, particularly during app initialization: position = vec3.create([0, 0, -5]);
. The create
function is one (the only?) function I wouldn't mind keeping the optional argument to, although I'd be just as happy to initialize it without having to build a junk array in the process, like so: position = vec3.create(0, 0, -5);
.
from gl-matrix.
For the record, there'd be no performance loss for turning each namespace into a function. http://jsperf.com/access-function-properties-vs-object-properties
from gl-matrix.
I'd lean towards keeping one as an alias for the other
Oh absolutely. To do otherwise would be a really mean break in backwards-compat.
Actually, maybe make functions like mat3()
take no arguments, and then mat3.create(foo)
could be used for initializing with a value.
Or as you say, the “bare” functions could be instead aliases for the current createFrom
functions, but with arguments optional. So you could have: position = vec3(0, 0, -5)
, or zeros = mat3()
.
from gl-matrix.
Here’s what the homepage examples could look like
// creating a perspective matrix
var persp = mat4.perspective(45, 4/3, 1, 100, mat4());
// performing multiple transforms on a matrix
var modelView = mat4.identity(mat4()); // Start with identity
mat4.translate(modelView, [0, 0, -10], modelView); // Translate back 10 units
mat4.rotate(modelView, Math.PI/2, [0, 1, 0], modelView); // Rotate 90 degrees around the Y axis
mat4.scale(modelView, [2, 2, 2], modelView); // Scale by 200%
// matrices can be passed directly to WebGL
gl.uniformMatrix4fv(modelViewUniform, false, modelView);
// updating a destination matrix ... modelViewPersp = modelView * persp
var modelViewPersp = mat4.multiply(modelView, persp, mat4());
// transforming a point
var cameraPos = vec3(0, 0, 0);
mat4.multiplyVec3(modelView, cameraPos, cameraPos); // result written into cameraPos
var newPos = mat4.multiplyVec3(modelView, cameraPos, vec3());
from gl-matrix.
By the way, the last couple lines of example on the current front page save the output of mat4.multiplyVec3(...)
into the two regular Javascript arrays, instead of into whatever type is specified to be a MatrixArray. Is that intentional?
from gl-matrix.
Yes, that was absolutely intentional. glMatrix works with any array-like object that contains enough elements to represent the given type. So a JS array of 16 values can be used as a 4x4 matrix just as easily as one that you get from mat4.create();
from gl-matrix.
Okay. Probably worth saying so explicitly in the text around the example. So (still using this proposed new API with required destination), something like:
// Transforming a point. Note that regular arrays of Javascript numbers
// can be used as the "destination" for all functions in this API.
var cameraPos = [0, 0, 0],
newPos = [0, 0, 0];
mat4.multiplyVec3(modelView, cameraPos, cameraPos); // result written into cameraPos
mat4.multiplyVec3(modelView, cameraPos, newPos); // result written into newPos
from gl-matrix.
This request is being addressed in the 2.0 revision, described in #43.
from gl-matrix.
Related Issues (20)
- Published version 3.4.3 does not contain type definitions HOT 1
- Missing frustumZO method HOT 1
- How to pass data to webgl? HOT 2
- vector equals fails if one vector is all Infinity
- Breaking change: 3.4.3 no longer has exports in package.json HOT 2
- Math.hypot is really slow HOT 4
- vec*.round(out, a) is asymmetric
- Should targetTo preserve scaling? HOT 2
- Matrix-Vector multiplication HOT 1
- glMatrix v4.0 - Request for feedback HOT 64
- Some return types don't include `null` HOT 1
- fromQuat2 allocates a vector on every call
- Functions not the same across mat4, mat3, mat2
- TypeScript types: suggest omitting Float32Array type
- `Array` vs `Float32Array`: Performance measurements (again)
- Bug: `lookAt` and `targetTo` will not work if `position` and `target` are vertical
- [BUG] build error HOT 1
- Inconsistent order or arguments HOT 1
- No Explanation on how to access functions after upgrade
- IndexedCollection type not available in releases HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from gl-matrix.