Skip to content

Commit

Permalink
Added example to toggle between mesh variants (#4528)
Browse files Browse the repository at this point in the history
* Added example to toggle between mesh variants

* Fixed the alt

* Addressed some of the comments including changing updateAlpha and making options declerative

* simplify code and address all comments
  • Loading branch information
samaneh-kazemi authored Oct 27, 2023
1 parent 81a5542 commit 741a18a
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 0 deletions.
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
Binary file not shown.

0 comments on commit 741a18a

Please sign in to comment.