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

TS: Added undefined as .defines member #20356

Merged
merged 1 commit into from
Sep 24, 2020
Merged

TS: Added undefined as .defines member #20356

merged 1 commit into from
Sep 24, 2020

Conversation

ycw
Copy link
Contributor

@ycw ycw commented Sep 15, 2020

@@ -139,7 +139,7 @@ export class Material extends EventDispatcher {
* The pairs are defined in both vertex and fragment shaders. Default is undefined.
* @default undefined
*/
defines: { [key: string]: any };
defines: undefined | { [key: string]: any };
Copy link
Collaborator

@Mugen87 Mugen87 Sep 16, 2020

Choose a reason for hiding this comment

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

This should be safe since all materials which actually set defines like MeshStandardMaterial overwrite the type declaration in their own TS file with:

defines: { [key: string]: any };

So property access in context of those materials should not break.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It would be better modeling the type exactly for that .js unless the item is internal.
for this case Material is exported (not internal), one can class MyMaterial extends Material {} in TS.

this.defines['x'] = 0; // passed typecheck, fail at runtime.

Copy link
Collaborator

@Mugen87 Mugen87 Sep 18, 2020

Choose a reason for hiding this comment

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

How about defining defines in Material.js like so:

this.defines = {};

In this way, it would be never undefined. An we could delete all type declarations of defines in derived materials (e.g. ShaderMaterial).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

How about defining defines in Material.js like so:

this.defines = {};

In this way, it would be never undefined. An we could delete all type declarations of defines in derived materials (e.g. ShaderMaterial).

This will make those derived materials (point, sprite, shadow..) bloated.

Copy link
Collaborator

Choose a reason for hiding this comment

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

What do you mean with "bloated"? Do you mind explaining in more detail?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What do you mean with "bloated"? Do you mind explaining in more detail?

After adding .defines = {} to Material.js; it is fine from shadermaterial POV as it needs this property.
But for those derived materials like pointsmaterial, spritematerial, shadowmaterial etc,
they own a .defines (an empty js object) which is never be used.

Copy link
Collaborator

@Mugen87 Mugen87 Sep 18, 2020

Choose a reason for hiding this comment

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

But this also true for other common material properties. E.g. vertexColors or flatShading is not use by all materials.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But this also true for other common material properties. E.g. vertexColors or flatShading is not use by all materials.

I've no idea why those props are existing there.

@mrdoob mrdoob added this to the r121 milestone Sep 24, 2020
@mrdoob mrdoob merged commit bf67ea6 into mrdoob:dev Sep 24, 2020
@mrdoob
Copy link
Owner

mrdoob commented Sep 24, 2020

Thanks!

@ycw ycw deleted the patch-1 branch September 25, 2020 20:29
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