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

Add support for material variants #4528

Merged
merged 23 commits into from
Aug 17, 2022
Merged

Conversation

GSterbrant
Copy link
Contributor

@GSterbrant GSterbrant commented Aug 10, 2022

Description

Adds support for KHR_materials_variants as well as provides an API for changing the material. Also fixes the default material index ownership, having them live in the container instead of on the mesh.

image

image

Fixes #3647

API changes

  • Adds getMaterialVariants to GlbContainerResource. This API fetches the list of variants present in the GLB.
  • Adds applyMaterialVariant(name, entity) to GlbContainerResource. This API applies a material variant to all mesh instances in the render component of an entity.
  • Adds applyMaterialVariantInstances(name, instances) to GlbContainerResource. This API applies a material variant to a set of mesh instances, using the GlbContainerResource as the source of the variants, their mappings, and the materials.

@mvaligursky
Copy link
Contributor

Could you please add an overview of the new public API to the PR description.

@GSterbrant GSterbrant added the area: graphics Graphics related issue label Aug 10, 2022
}

// reset material variants on entity
_resetMaterialVariant(entity) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I think I'd get rid of these _reset functions, as its the same code as the function above apart from the material selection - just make a small change to that function to handle it and remove the code duplication?

The API lives entirely in the GLB container, allowing the user to get the list of variants and apply it to a mesh.
The variants are parsed, and the data for the variants are stored on the resource level of the container, thus avoiding any intrusion into any engine types.
…rnally, as well as debug warn if the variant can't be found.
Also provide documentation clarifying that the name is optional, and if not provided resets the material variants.
const meshVariants = this.data.meshVariants[instance.mesh.id];
if (meshVariants) {
instance.material = this.data.materials[meshVariants[variant]];
Debug.assert(instance.material);
Copy link
Contributor

Choose a reason for hiding this comment

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

if you could move the assert outside of if/else, so that it validates both cases.

Copy link
Contributor

@mvaligursky mvaligursky left a comment

Choose a reason for hiding this comment

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

approved with a single small comment.

@GSterbrant GSterbrant force-pushed the gsterbrant_materials_variants branch from 4c970f1 to 10c8224 Compare August 17, 2022 09:32
@GSterbrant GSterbrant merged commit c280f01 into main Aug 17, 2022
@GSterbrant GSterbrant deleted the gsterbrant_materials_variants branch August 17, 2022 09:40
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area: graphics Graphics related issue
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support glTF extension: KHR_materials_variants
3 participants