Skip to content
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

Move WEBGL_CONSTANTS and THREE_TO_WEBGL constants out from GLTFExporter and GLTFLoader #11978

Closed
wants to merge 8 commits into from

Conversation

fernandojsg
Copy link
Collaborator

Not so happy with the name THREEToWebGL.js and WebGLToTHREE any suggestions? :)
I believe most of the mappings from WebGLUtils (https:/mrdoob/three.js/blob/dev/src/renderers/webgl/WebGLUtils.js#L13-L35 ...) could be replaced by this mappings constants as I find it more handy to use than need to create a WebGLUtils instance with a renderer and extensions. Maybe in another PR we could just reuse the constants mapping on the WebGLUtils function and just leave the code related to extensions there?

@takahirox
Copy link
Collaborator

The variables are glTF specific, correct?
I'm feeling like the variable names (of file names) should indicate they're for glTF.
So add prefix GLTF like GLTF_WEBGL_CONSTANTS?

@fernandojsg
Copy link
Collaborator Author

fernandojsg commented Aug 18, 2017

@takahirox these are not gltf specific, these are generic WEBGL constants (https://developer.mozilla.org/en-US/docs/Web/API/WebGL_API/Constants) that can be used in other places not just in the gltf loader.
And the other files are just conversions between THREEjs constants to and from WEBGL constants.

@takahirox
Copy link
Collaborator

takahirox commented Aug 18, 2017

Oops, I didn't know that they're equal to gl.*, thanks!

@fernandojsg
Copy link
Collaborator Author

Yes, the point was to avoid sending a renderer's context to the exporter (As discussed in #11964)

@takahirox
Copy link
Collaborator

I see I see, that makes sense!

@takahirox takahirox mentioned this pull request Aug 19, 2017
87 tasks
src/Three.js Outdated
@@ -156,5 +156,8 @@ export { Curve } from './extras/core/Curve.js';
export { ShapeUtils } from './extras/ShapeUtils.js';
export { SceneUtils } from './extras/SceneUtils.js';
export { WebGLUtils } from './renderers/webgl/WebGLUtils.js';
export { WEBGL_CONSTANTS } from './renderers/webgl/WebGLConstants.js';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I would keep this as:

export { WebGLConstants } from './renderers/webgl/WebGLConstants.js';

Also, I think I would add a fromGL() and toGL() methods that does the THREEToWebGL and WebGLToTHREE` conversions. Just to keep everything in one file.

Copy link
Collaborator Author

@fernandojsg fernandojsg Aug 21, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mrdoob Do you mean including and exporting fromGL, toGL and WebGLConstants from WebGLConstants.js?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If that's the case, maybe rename the export to constants so you could use it as:

THREE.WebGLConstants.constants.POINTS
THREE.WebGLConstants.fromGL(gl.POINTS)
THREE.WebGLConstants.toGL(THREE.FrontSide)

Copy link
Owner

@mrdoob mrdoob Aug 21, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like this API style better.

var constants = new THREE.WebGLConstants();
constants.POINTS;
constants.fromGL(gl.POINTS);
constants.toGL(THREE.FrontSide);

But I'm not 100%. Maybe fromGL() and toGL() could go on WebGLUtils instead.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't really like the need to create a new instance just to query a constant. I would prefer to access it directly:

THREE.WebGLConstants.POINTS;

The same for the fromGL and toGL that I believe too that they should be part of WebGLUtils

THREE.WebGLUtils.fromGL(gl.POINTS)
THREE.WebGLUtils.toGL(THREE.FrontSide)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mrdoob I've updated WebGLUtils and WebGLConstants (still WIP) but to show a possible implementation of this last option

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Eventually convert and toGL will become the same function. In fact right now with the current modifications convert doesn't need a gl context anymore just the extensions dependency.

@fernandojsg fernandojsg force-pushed the moveConstants branch 3 times, most recently from c23d6f5 to afe46fc Compare August 21, 2017 23:26
@fernandojsg
Copy link
Collaborator Author

@mrdoob I've updated the WebGLUtils to export toGL and fromGL. I've replaced convert by toGL in the rest of the code as it was just duplicated code and the need to create a new instance and the dependencies from a gl context and extensions are not needed anymore as we're just exposing constants that we've in the WebGLConstants file.
I didn't change yet the GLTFLoader/Exporter until you'd validate the code.

Taking a look at constants.js, I believe I could export the WebGLConstants in a similar way instead of just exporting the whole object if you prefer so.

@takahirox
Copy link
Collaborator

takahirox commented Aug 23, 2017

Will this PR be merged soon?
I'm making PRs using constants.
I'll wait for this PR being merged if it'll be soon.

@fernandojsg fernandojsg force-pushed the moveConstants branch 4 times, most recently from eb02143 to 7cfdced Compare August 23, 2017 22:40
@fernandojsg
Copy link
Collaborator Author

Rebased!
I export WebGLUtils object that contains the toGL and fromGL functions that are basically just an alias to access to the mapping dictionaries.
I believe it's more intuitive and easy to use this way instead of creating a new instance of the object everytime you need to use, the same way as we don't do var math = new Math(); math.clamp(x) every time we need to use a math function.
@mrdoob friendly bump 👋 👼

@fernandojsg fernandojsg force-pushed the moveConstants branch 2 times, most recently from 42771a7 to 7cfdced Compare August 23, 2017 22:47
@fernandojsg
Copy link
Collaborator Author

fernandojsg commented Aug 24, 2017

I'm not sure why the lgtm tests are failing they were ok I did a rebase and they seem to be broken since then ¯_(ツ)_/¯

@takahirox
Copy link
Collaborator

lgtm tests are fine now 😄

@fernandojsg
Copy link
Collaborator Author

@takahirox yep, I just rebased it and pushed it again :)

@fernandojsg fernandojsg force-pushed the moveConstants branch 2 times, most recently from ab3400c to a8ac02d Compare September 4, 2017 17:56
@takahirox
Copy link
Collaborator

When will we merge this PR?

minFilter: THREE_TO_WEBGL[ map.minFilter ],
wrapS: THREE_TO_WEBGL[ map.wrapS ],
wrapT: THREE_TO_WEBGL[ map.wrapT ]
magFilter: THREE.WebGLUtils.toGL[ map.magFilter ],
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

toGL is a function 😅

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed 👼

@@ -1381,7 +1264,7 @@ THREE.GLTFLoader = ( function () {
return parser.getDependency( 'bufferView', accessor.bufferView ).then( function ( bufferView ) {

var itemSize = WEBGL_TYPE_SIZES[ accessor.type ];
var TypedArray = WEBGL_COMPONENT_TYPES[ accessor.componentType ];
var TypedArray = THREE.WEBGL_TO_THREE[ accessor.componentType ];
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wouldn't it be better to use the same approach everywhere? THREE.WebGLUtils.fromGL()...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@@ -31,7 +31,7 @@ import { Vector3 } from '../math/Vector3.js';
import { WebGLClipping } from './webgl/WebGLClipping.js';
import { Frustum } from '../math/Frustum.js';
import { Vector4 } from '../math/Vector4.js';
import { WebGLUtils } from './webgl/WebGLUtils.js';
import { WebGLUtils } from './webgl/WebGLUtils';
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets keep WebGLUtils.js.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

//------------------------------------------------------------------------------
var THREE_TO_WEBGL = {

[ Constants.NearestFilter ]: WebGLConstants.NEAREST,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Was not aware of this [ variable ] thing. Is it well supported?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's ES6 computed literals. I thought gulp would do the transform itself but it doesn't seems so :\

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The ugly but compatible version is to put the value of the constant as is, or try to use a gulp pluging to convert it to its literal value when creating the bundle. @mrdoob thoughts?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My fault, this is es2015 and it's supported in all the browsers, but on firefox we need to add extra parenthesis: [ (value) ]. I'll fix it

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually, on the latest version of nightly it works as expected :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@mrdoob my fault, I was testing it incorrectly on firefox O:) it's working as expected in all the browsers, also tested on mobile (android & ios).

@fernandojsg fernandojsg force-pushed the moveConstants branch 2 times, most recently from 9f60f23 to f9fb91b Compare October 20, 2017 09:41
@mrdoob
Copy link
Owner

mrdoob commented Feb 14, 2020

Closing this for now.

@mrdoob mrdoob closed this Feb 14, 2020
@fernandojsg fernandojsg deleted the moveConstants branch February 16, 2020 19:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants