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

Restore initial material #2764

Merged
6 commits merged into from
Sep 9, 2021
Merged

Restore initial material #2764

6 commits merged into from
Sep 9, 2021

Conversation

ghost
Copy link

@ghost ghost commented Sep 7, 2021

Allows passing 'null' to enableVariant() API in order to switch the material back its initial setting.

@ghost ghost requested a review from elalish September 7, 2021 20:12
this[$defaultMaterialIdx] = materialRef.index;
} else {
console.error(
`Primitive (${mesh.name}) missing default material reference.`);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is this possible? If no material was specified, we should have created a default material and then reference that, right? If it's just an assert, that's fine, I'm just curious.

Copy link
Author

Choose a reason for hiding this comment

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

Correct the default material works and I don't think it would be possible for this to be null, at least not with todays codebase.

}
expect(materials.find((material: MeshStandardMaterial) => {
return material.name === 'purple';
})).to.not.be.null;
});

test('Primitive switches to default material', async () => {
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is good, but it's not a public method. Let's also include a more end-to-end version that sets it via the public variantName API. And let's also update the documentation on that.

Copy link
Author

Choose a reason for hiding this comment

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

Added a test using the variantName API, a bit tricky to verify without being able to access a gltf-primitive mapping on the model, have a look, updating the docs next...

Adds a 'variant-applied' event to give the all-done signal
@ghost ghost requested a review from elalish September 8, 2021 02:15
@@ -47,7 +47,7 @@ interface SceneExportOptions {

export interface SceneGraphInterface {
readonly model?: Model;
variantName: string|undefined;
variantName: string|null|undefined;
Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think you need undefined here anymore. You also need to change its type in the @property declaration below; probably best to initialize it to null.

Copy link
Author

Choose a reason for hiding this comment

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

Done.

@@ -149,8 +149,10 @@ export const SceneGraphMixin = <T extends Constructor<ModelViewerElementBase>>(
return;
}

await this[$model]![$switchVariant](variantName!);
await this[$model]![$switchVariant](
variantName! == 'null' ? null : variantName!);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do you need the !s after variantName? Also == 'null' seems pretty weird? Can you just pass variantName in as is?

Copy link
Author

Choose a reason for hiding this comment

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

lol, yeah

Copy link
Author

Choose a reason for hiding this comment

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

passing variantName as is, it's possible, this API responds to the actual value being null though, not a magical variant-name 'null' that's why this is here. Also if you assign the value null to the property variantName Lit apparently converts that into a string, don't know why, that's why I'm checking for a string at all.

Copy link
Author

Choose a reason for hiding this comment

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

Just works with strings now.

Updates docs, adds a 'reset' option to the Variant switching example.
@ghost ghost requested a review from elalish September 8, 2021 20:16
Copy link
Collaborator

@elalish elalish left a comment

Choose a reason for hiding this comment

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

Looks good, just a couple minor things.

{
"name": "variantName",
"htmlName": "variantName",
"description": "Corresponding property of the attribute <code>variant-name</code>.",
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is not necessary (you'll notice none of our other attributes have property definitions, since all HTML attributes always have corresponding properties). That said, I'm open to changing the overall docs template to make that more clear.

Copy link
Author

Choose a reason for hiding this comment

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

Wasn't sure if this was omitted on purpose or not, omitting it does assume that anyone can work out the fact that variant-name corresponds to a property called variantName.

@@ -867,8 +867,8 @@
"Attributes": [
{
"name": "variant-name",
"htmlName": "variantName",
"description": "Selects a model variant by name.",
"htmlName": "variant-name",
Copy link
Collaborator

Choose a reason for hiding this comment

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

Let's stick with our conventions here. Also, might be good to add that the default is null and options include variant name string or null, in the default json.

Copy link
Author

@ghost ghost Sep 8, 2021

Choose a reason for hiding this comment

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

Is the "htmlName" supposed to be visible somewhere on the documentation page? I don't see it.

Copy link
Author

Choose a reason for hiding this comment

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

Done.

let defaultIndex = 0;
// Patches the first primitive with variant-info and a default
// material index of 0.
// let defaultIndex = 0;
Copy link
Collaborator

Choose a reason for hiding this comment

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

remove?

Copy link
Author

Choose a reason for hiding this comment

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

done.

@@ -149,8 +149,10 @@ export const SceneGraphMixin = <T extends Constructor<ModelViewerElementBase>>(
return;
}

await this[$model]![$switchVariant](variantName!);
await this[$model]![$switchVariant](
variantName == 'null' ? null : variantName!);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Did you see my whole comment on this line?

Copy link
Author

Choose a reason for hiding this comment

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

Updated that comment.

element.variantName = null;
await waitForEvent(element, 'variant-applied');
expect((primitiveNode!.mesh.material as MeshStandardMaterial).name)
.equal('purple');
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍

@ghost ghost requested a review from elalish September 8, 2021 21:39
Copy link
Collaborator

@elalish elalish left a comment

Choose a reason for hiding this comment

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

Thanks!

@ghost ghost merged commit 7e6c920 into master Sep 9, 2021
@elalish elalish deleted the restore-initial-material branch July 29, 2022 21:45
This pull request was closed.
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.

2 participants