Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/modelviewer.dev/data/examples.json
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,10 @@
"htmlId": "transformTexturesExample",
"name": "Transform Textures"
},
{
"htmlId": "hideShowMeshVariantsExample",
"name": "Hide/Show Mesh Variants"
},
{
"htmlId": "animatedTexturesExample",
"name": "Animated Textures"
Expand Down
64 changes: 64 additions & 0 deletions packages/modelviewer.dev/examples/scenegraph/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,70 @@ <h2 class="demo-title">Transform textures</h2>
</div>


<div class="sample">
<div id="hideShowMeshVariantsExample" class="demo"></div>
<div class="content">
<div class="wrapper">
<div class="heading">
<h2 class="demo-title">Toggle between Mesh Variants</h2>
<p>If you have a few mesh variants, you can hide and show them by changing textures opacity.</p>
</div>
<example-snippet stamp-to="hideShowMeshVariantsExample" highlight-as="html">
<template>
<model-viewer id="manifold" camera-controls touch-action="pan-y" src="../../shared-assets/models/manifold.glb" ar alt="A 3D model of a stick and ball">
<div class="controls">
<div>Mesh Variants:
<select id="textures">
<option value="ball">ball</option>
<option value="stick">stick</option>
</select>
</div>
</model-viewer>
<script type="module">
const mvTextures = document.querySelector("model-viewer#manifold");
const textures = document.querySelector("#textures");

mvTextures.addEventListener("load", () => {
function updateAlphaValue(texture, alpha) {
const pbr = texture.pbrMetallicRoughness;
const baseColor = pbr.baseColorFactor;
baseColor[3] = alpha;
pbr.setBaseColorFactor(baseColor);
}

textures.addEventListener("input", (event) => {
const selectedValue = event.target.value;
const ball = mvTextures.model.getMaterialByName("ball");
ball.setAlphaMode("BLEND");
const stick = mvTextures.model.getMaterialByName("stick");

const animationDuration = 1000; // 1 second
const startTimestamp = performance.now();
function animate(currentTimestamp) {
const elapsed = currentTimestamp - startTimestamp;

const delta = elapsed / animationDuration;
const alpha = selectedValue === "ball" ? 1 - delta : delta;
updateAlphaValue(stick, alpha);
updateAlphaValue(ball, 1 - alpha);

if (elapsed < animationDuration) {
requestAnimationFrame(animate);
}
}

requestAnimationFrame(animate);
});
});
</script>
</template>
</example-snippet>
</div>
</div>
</div>



<div class="sample">
<div id="animatedTexturesExample" class="demo"></div>
<div class="content">
Expand Down
Binary file added packages/shared-assets/models/manifold.glb
Copy link
Contributor

Choose a reason for hiding this comment

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

I just made exactly this mistake - you need to also add this file to ci-before-deploy.sh or else the public example page won't be able to find it.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

hmm wouldn't this cover it?
shared-assets/models/. \

Copy link
Contributor

Choose a reason for hiding this comment

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

Oh shoot, you're right! Thanks.

Binary file not shown.