google / google/model-viewer

Support models lacking the "materials" property or primitive/material association

Open
#3,279 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
TypeScript
Stars
8.2k
Forks
947
PR merge metrics
No merged PRs in 30d

Description

### Description
I have point cloud scans that yield models without any materials once converted to GLTF/GLB.
These models visually load, but then, break the code at a few places. This brought me to investigate further the situation.
As far as I can tell, I have encountered 2 situations where the code breaks with these unconventional files.

#### Case 1: File with no "materials" property in the GLTF

![Screen Shot 2022-03-16 at 9 49 06 PM](https://user-images.githubusercontent.com/8660478/158720560-229806bc-afe3-427f-bc37-ae4b04a899bf.png)

In `model-viewer/src/features/scene-graph/model.ts`
![Screen Shot 2022-03-16 at 9 49 44 PM](https://user-images.githubusercontent.com/8660478/158720559-6a3ace17-9ba1-4ab9-8142-3ad7cb48f663.png)

**Suggestion**: Don't use the `!` syntax in `gltf.materials!.entries()` and do a null check instead `gltf.materials?.entries() ?? []`

#### Case 2: File with the "materials" property in the GLTF but no material associated in the mesh primitives

![Screen Shot 2022-03-16 at 9 55 48 PM](https://user-images.githubusercontent.com/8660478/158721121-43f28d52-df62-4485-b5f5-ca511712afc4.png)

In `model-viewer/src/features/scene-graph/nodes/primitive-node.ts`
![Screen Shot 2022-03-16 at 9 55 16 PM](https://user-images.githubusercontent.com/8660478/158721122-6b9a8baa-93bb-4cfa-9666-e49a63248e4e.png)

**Suggestion**: Simple null check `materialMappings?.materials != null`

This is another case where I seem to be the only one having to deal with bizarre GLTF structures 😆 . I will open a PR for this if that is ok with you guys.

### Version
- model-viewer: v1.10.1

### Browser Affected
- [x] Chrome, version: latest
- [ ] Edge
- [ ] Firefox
- [ ] IE
- [ ] Safari

### OS
- [ ] Android
- [ ] iOS
- [ ] Linux
- [x] MacOS
- [ ] Windows

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.