mirror of
https://github.com/yeicor-3d/yet-another-cad-viewer.git
synced 2025-12-20 14:37:03 +01:00
add support for custom model opacities
This commit is contained in:
@@ -5,6 +5,7 @@ import {
|
|||||||
VExpansionPanel,
|
VExpansionPanel,
|
||||||
VExpansionPanelText,
|
VExpansionPanelText,
|
||||||
VExpansionPanelTitle,
|
VExpansionPanelTitle,
|
||||||
|
VSlider,
|
||||||
VSpacer,
|
VSpacer,
|
||||||
VTooltip
|
VTooltip
|
||||||
} from "vuetify/lib/components";
|
} from "vuetify/lib/components";
|
||||||
@@ -12,9 +13,8 @@ import {extrasNameKey} from "../misc/gltf";
|
|||||||
import {Document, Mesh} from "@gltf-transform/core";
|
import {Document, Mesh} from "@gltf-transform/core";
|
||||||
import {watch} from "vue";
|
import {watch} from "vue";
|
||||||
import type ModelViewerWrapper from "../viewer/ModelViewerWrapper.vue";
|
import type ModelViewerWrapper from "../viewer/ModelViewerWrapper.vue";
|
||||||
import {mdiDelete, mdiRectangle, mdiRectangleOutline, mdiVectorRectangle} from '@mdi/js'
|
import {mdiCircleOpacity, mdiDelete, mdiRectangle, mdiRectangleOutline, mdiVectorRectangle} from '@mdi/js'
|
||||||
import SvgIcon from '@jamescoyle/vue-icon/lib/svg-icon.vue';
|
import SvgIcon from '@jamescoyle/vue-icon/lib/svg-icon.vue';
|
||||||
import type {ModelViewerElement, RGBA} from '@google/model-viewer';
|
|
||||||
|
|
||||||
const props = defineProps<{ mesh: Mesh, viewer: InstanceType<typeof ModelViewerWrapper> | null, document: Document }>();
|
const props = defineProps<{ mesh: Mesh, viewer: InstanceType<typeof ModelViewerWrapper> | null, document: Document }>();
|
||||||
const emit = defineEmits<{ remove: [] }>()
|
const emit = defineEmits<{ remove: [] }>()
|
||||||
@@ -26,22 +26,17 @@ let edgeCount = props.mesh.listPrimitives().filter(p => p.getMode() === WebGL2Re
|
|||||||
let vertexCount = props.mesh.listPrimitives().filter(p => p.getMode() === WebGL2RenderingContext.POINTS).length
|
let vertexCount = props.mesh.listPrimitives().filter(p => p.getMode() === WebGL2RenderingContext.POINTS).length
|
||||||
|
|
||||||
const enabledFeatures = defineModel<Array<number>>("enabledFeatures", {default: [0, 1, 2]});
|
const enabledFeatures = defineModel<Array<number>>("enabledFeatures", {default: [0, 1, 2]});
|
||||||
|
const opacity = defineModel<number>("opacity", {default: 1});
|
||||||
let hasListener = false;
|
|
||||||
|
|
||||||
function onEnabledFeaturesChange(newEnabledFeatures: Array<number>) {
|
function onEnabledFeaturesChange(newEnabledFeatures: Array<number>) {
|
||||||
//console.log('Enabled features may have changed', newEnabledFeatures)
|
//console.log('Enabled features may have changed', newEnabledFeatures)
|
||||||
let scene = props.viewer?.scene;
|
let scene = props.viewer?.scene;
|
||||||
let elem = props.viewer?.elem;
|
let sceneModel = (scene as any)?._model;
|
||||||
if (!scene || !scene._model || !elem) return;
|
if (!scene || !sceneModel) return;
|
||||||
if (!hasListener) { // Make sure we listen for reloads and re-apply enabled features
|
|
||||||
elem.addEventListener('load', () => onEnabledFeaturesChange(enabledFeatures.value));
|
|
||||||
hasListener = true;
|
|
||||||
}
|
|
||||||
// Iterate all primitives of the mesh and set their visibility based on the enabled features
|
// Iterate all primitives of the mesh and set their visibility based on the enabled features
|
||||||
// Use the scene graph instead of the document to avoid reloading the same model, at the cost
|
// Use the scene graph instead of the document to avoid reloading the same model, at the cost
|
||||||
// of not actually removing the primitives from the scene graph
|
// of not actually removing the primitives from the scene graph
|
||||||
scene._model.traverse((child) => {
|
sceneModel.traverse((child) => {
|
||||||
if (child.userData[extrasNameKey] === modelName) {
|
if (child.userData[extrasNameKey] === modelName) {
|
||||||
let childIsFace = child.type == 'Mesh' || child.type == 'SkinnedMesh'
|
let childIsFace = child.type == 'Mesh' || child.type == 'SkinnedMesh'
|
||||||
let childIsEdge = child.type == 'Line'
|
let childIsEdge = child.type == 'Line'
|
||||||
@@ -56,8 +51,37 @@ function onEnabledFeaturesChange(newEnabledFeatures: Array<number>) {
|
|||||||
});
|
});
|
||||||
scene.queueRender()
|
scene.queueRender()
|
||||||
}
|
}
|
||||||
|
|
||||||
watch(enabledFeatures, onEnabledFeaturesChange);
|
watch(enabledFeatures, onEnabledFeaturesChange);
|
||||||
|
|
||||||
|
function onOpacityChange(newOpacity: number) {
|
||||||
|
let scene = props.viewer?.scene;
|
||||||
|
let sceneModel = (scene as any)?._model;
|
||||||
|
if (!scene || !sceneModel) return;
|
||||||
|
// Iterate all primitives of the mesh and set their opacity based on the enabled features
|
||||||
|
// Use the scene graph instead of the document to avoid reloading the same model, at the cost
|
||||||
|
// of not actually removing the primitives from the scene graph
|
||||||
|
console.log('Opacity may have changed', newOpacity)
|
||||||
|
sceneModel.traverse((child) => {
|
||||||
|
if (child.userData[extrasNameKey] === modelName) {
|
||||||
|
if (child.material) {
|
||||||
|
console.log('Setting opacity of', child)
|
||||||
|
child.material.transparent = newOpacity < 1;
|
||||||
|
child.material.opacity = newOpacity;
|
||||||
|
child.material.needsUpdate = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
});
|
||||||
|
scene.queueRender()
|
||||||
|
}
|
||||||
|
watch(opacity, onOpacityChange);
|
||||||
|
|
||||||
|
// props.viewer.elem is already available as a model has been loaded...
|
||||||
|
props.viewer.elem.addEventListener('load', () => {
|
||||||
|
// Enabled features may have been reset after a reload
|
||||||
|
onEnabledFeaturesChange(enabledFeatures.value)
|
||||||
|
// Opacity may have been reset after a reload
|
||||||
|
onOpacityChange(opacity.value)
|
||||||
|
});
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<template>
|
<template>
|
||||||
@@ -85,7 +109,11 @@ watch(enabledFeatures, onEnabledFeaturesChange);
|
|||||||
</v-btn>
|
</v-btn>
|
||||||
</v-expansion-panel-title>
|
</v-expansion-panel-title>
|
||||||
<v-expansion-panel-text>
|
<v-expansion-panel-text>
|
||||||
TODO: Settings...
|
<v-slider v-model="opacity" hide-details min="0" max="1" :step="0.1">
|
||||||
|
<template v-slot:prepend>
|
||||||
|
<svg-icon type="mdi" :path="mdiCircleOpacity"></svg-icon>
|
||||||
|
</template>
|
||||||
|
</v-slider>
|
||||||
</v-expansion-panel-text>
|
</v-expansion-panel-text>
|
||||||
</v-expansion-panel>
|
</v-expansion-panel>
|
||||||
</template>
|
</template>
|
||||||
@@ -97,6 +125,10 @@ watch(enabledFeatures, onEnabledFeaturesChange);
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* More compact accordions */
|
/* More compact accordions */
|
||||||
|
.v-expansion-panel {
|
||||||
|
margin-top: 0 !important;
|
||||||
|
}
|
||||||
|
|
||||||
.v-expansion-panel-title {
|
.v-expansion-panel-title {
|
||||||
padding: 0;
|
padding: 0;
|
||||||
}
|
}
|
||||||
@@ -123,6 +155,9 @@ watch(enabledFeatures, onEnabledFeaturesChange);
|
|||||||
</style>
|
</style>
|
||||||
|
|
||||||
<style>
|
<style>
|
||||||
|
.v-expansion-panel-text__wrapper {
|
||||||
|
padding: 0 !important;
|
||||||
|
}
|
||||||
.hide-this-icon {
|
.hide-this-icon {
|
||||||
display: none !important;
|
display: none !important;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,7 +5,6 @@ import Loading from "../misc/Loading.vue";
|
|||||||
import {Document, Mesh} from "@gltf-transform/core";
|
import {Document, Mesh} from "@gltf-transform/core";
|
||||||
import {extrasNameKey} from "../misc/gltf";
|
import {extrasNameKey} from "../misc/gltf";
|
||||||
import Model from "./Model.vue";
|
import Model from "./Model.vue";
|
||||||
import {watch, ref} from "vue";
|
|
||||||
|
|
||||||
const props = defineProps<{ viewer: InstanceType<typeof ModelViewerWrapper> | null, document: Document }>();
|
const props = defineProps<{ viewer: InstanceType<typeof ModelViewerWrapper> | null, document: Document }>();
|
||||||
const emit = defineEmits<{ remove: [string] }>()
|
const emit = defineEmits<{ remove: [string] }>()
|
||||||
|
|||||||
@@ -73,17 +73,16 @@ function toggleProjection() {
|
|||||||
toggleProjectionText.value = wasPerspectiveCamera ? 'ORTHO' : 'PERSP';
|
toggleProjectionText.value = wasPerspectiveCamera ? 'ORTHO' : 'PERSP';
|
||||||
}
|
}
|
||||||
|
|
||||||
function centerCamera() {
|
async function centerCamera() {
|
||||||
let viewerEl: ModelViewerElement = props.viewer?.elem;
|
let viewerEl: ModelViewerElement = props.viewer.elem;
|
||||||
if (!viewerEl) return;
|
await viewerEl.updateFraming();
|
||||||
viewerEl.updateFraming();
|
viewerEl.zoom(3);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
async function downloadSceneGlb() {
|
async function downloadSceneGlb() {
|
||||||
let viewerEl: ModelViewerElement = props.viewer?.elem;
|
let viewerEl: ModelViewerElement = props.viewer.elem;
|
||||||
if (!viewerEl) return;
|
const glTF = await viewerEl.exportScene({onlyVisible: true, binary: true});
|
||||||
const glTF = await viewerEl.exportScene();
|
|
||||||
const file = new File([glTF], "export.glb");
|
const file = new File([glTF], "export.glb");
|
||||||
const link = document.createElement("a");
|
const link = document.createElement("a");
|
||||||
link.download = file.name;
|
link.download = file.name;
|
||||||
|
|||||||
Reference in New Issue
Block a user