|
| 1 | +--- |
| 2 | +title: Embedding 3D model in Quarto |
| 3 | +description: Interact with 3D models in a Quarto website |
| 4 | +author: "Jeevith Hegde" |
| 5 | +date: 2025-11-28 19:03 +0100 |
| 6 | +format: html |
| 7 | +toc: true |
| 8 | +lang: en |
| 9 | +jupyter: python3 |
| 10 | +resources: |
| 11 | + - DamagedHelmet.glb |
| 12 | + - spruit_sunrise_1k_HDR.hdr |
| 13 | + - poster.webp |
| 14 | +include-in-header: |
| 15 | + - text: | |
| 16 | + <script type="module" src="https://ajax.googleapis.com/ajax/libs/model-viewer/3.4.0/model-viewer.min.js"></script> |
| 17 | +ipynb-shell-interactivity: all |
| 18 | +execute: |
| 19 | + echo: false |
| 20 | +categories: ["3D-rendering", "model-viewer"] |
| 21 | +tags: ["3D visualization"] |
| 22 | +comments: |
| 23 | + giscus: |
| 24 | + repo: jeev20/jeev20.github.io |
| 25 | +--- |
| 26 | + |
| 27 | +A couple of weeks back, I found about [Model viewer](https://modelviewer.dev/). Model viewer can render 3D models in a web browser with minimal syntax. I found it to be fascinating because of its ease of use and possible usecases. Not to forget it is also open-source! |
| 28 | + |
| 29 | +However, I had some challenges getting the `html` code to retrieve the 3D model and other resources in Quarto. |
| 30 | + |
| 31 | +## Front matter edits |
| 32 | +I had to add the below keys and values in the front matter of the this quarto post. |
| 33 | + |
| 34 | +### Alternative 1 - filenames |
| 35 | +```{.yaml} |
| 36 | +resources: |
| 37 | + - DamagedHelmet.glb |
| 38 | + - spruit_sunrise_1k_HDR.hdr |
| 39 | + - poster.webp |
| 40 | +include-in-header: |
| 41 | + - text: | |
| 42 | + <script type="module" src="https://ajax.googleapis.com/ajax/libs/model-viewer/3.4.0/model-viewer.min.js"></script> |
| 43 | +``` |
| 44 | +### Alternative 2 - wildcards |
| 45 | +```{.yaml} |
| 46 | +resources: |
| 47 | + - "*.glb" |
| 48 | + - "*.hdr" |
| 49 | + - "*.webp" |
| 50 | +include-in-header: |
| 51 | + - text: | |
| 52 | + <script type="module" src="https://ajax.googleapis.com/ajax/libs/model-viewer/3.4.0/model-viewer.min.js"></script> |
| 53 | +``` |
| 54 | + |
| 55 | +### Alternative 3 - folder |
| 56 | +All resources will have to be in the `model_assets` folder where the `.qmd` file of the post resides. |
| 57 | +```{.yaml} |
| 58 | +resources: |
| 59 | + - "model_assets/" |
| 60 | +include-in-header: |
| 61 | + - text: | |
| 62 | + <script type="module" src="https://ajax.googleapis.com/ajax/libs/model-viewer/3.4.0/model-viewer.min.js"></script> |
| 63 | +``` |
| 64 | + |
| 65 | +### Alternative 4 - copy all resources |
| 66 | +```{.yaml} |
| 67 | +resources: |
| 68 | + - "*" |
| 69 | +include-in-header: |
| 70 | + - text: | |
| 71 | + <script type="module" src="https://ajax.googleapis.com/ajax/libs/model-viewer/3.4.0/model-viewer.min.js"></script> |
| 72 | +``` |
| 73 | + |
| 74 | + |
| 75 | +## HTML code |
| 76 | +Now we connect the resources to the keys in `<model-viewer>` tag. The below code should be of `{=html}` type in markdown code block. |
| 77 | +```{.html} |
| 78 | +<model-viewer |
| 79 | + src="DamagedHelmet.glb" |
| 80 | + alt="A 3D model of a helmet" |
| 81 | + auto-rotate |
| 82 | + camera-controls |
| 83 | + environment-image="spruit_sunrise_1k_HDR.hdr" |
| 84 | + skybox-image="spruit_sunrise_1k_HDR.hdr" |
| 85 | + style="width: 100%; height: 500px; background-color: #f0f0f0;"> |
| 86 | +</model-viewer> |
| 87 | +``` |
| 88 | + |
| 89 | + |
| 90 | +## Rendered model |
| 91 | + |
| 92 | +This took a while to realize but was worth the headache! I did get some help from Gemini Pro 3 on how to use the resources tag in the front matter. |
| 93 | +```{=html} |
| 94 | +<model-viewer |
| 95 | + src="DamagedHelmet.glb" |
| 96 | + alt="A 3D model of a helmet" |
| 97 | + auto-rotate |
| 98 | + camera-controls |
| 99 | + environment-image="spruit_sunrise_1k_HDR.hdr" |
| 100 | + skybox-image="spruit_sunrise_1k_HDR.hdr" |
| 101 | + style="width: 100%; height: 500px; background-color: #f0f0f0;" |
| 102 | +> |
| 103 | +</model-viewer> |
| 104 | +
|
| 105 | +``` |
| 106 | + |
| 107 | + |
| 108 | +# References |
| 109 | +* [Model viewer](https://modelviewer.dev/) |
| 110 | +* [Model Viewer Editor](https://modelviewer.dev/editor/) |
| 111 | + |
| 112 | + |
0 commit comments