/* * SPDX-FileCopyrightText: syuilo and misskey-project * SPDX-License-Identifier: AGPL-3.0-only */ import * as BABYLON from '@babylonjs/core'; import { defineObject, WORLD_SCALE } from '../engine.js'; export const blind = defineObject({ id: 'blind', defaultOptions: { blades: 24, angle: 0, open: 1, }, placement: 'bottom', createInstance: ({ options, loaderResult, meshUpdated }) => { const blade = loaderResult.meshes[0].getChildMeshes().find(m => m.name === 'Blade') as BABYLON.Mesh; blade.rotation = new BABYLON.Vector3(options.angle, 0, 0); let blades = [] as BABYLON.Mesh[]; const applyOpeningState = () => { for (const b of blades) { b.dispose(); } blades = []; for (let i = 0; i < options.blades; i++) { const b = blade.clone(); if (i / options.blades < options.open) { b.position.y -= (i * 4/*cm*/) / WORLD_SCALE; } else { b.position.y -= (((options.blades - 1) * options.open * 4/*cm*/) + (i * 0.3/*cm*/)) / WORLD_SCALE; } blades.push(b); } meshUpdated(); }; const applyAngle = () => { for (const b of [blade, ...blades]) { b.rotation.x = options.angle; b.rotation.x += Math.random() * 0.3 - 0.15; } }; applyOpeningState(); applyAngle(); return { onInited: () => { }, interactions: { adjustBladeRotation: { label: 'Adjust blade rotation', fn: () => { options.angle += Math.PI / 8; if (options.angle >= Math.PI / 2) options.angle = -Math.PI / 2; applyAngle(); }, }, openClose: { label: 'Open/close', fn: () => { options.open -= 0.25; if (options.open < 0) options.open = 1; applyOpeningState(); }, }, }, primaryInteraction: 'openClose', }; }, });