Untitled

 avatar
unknown
plain_text
a year ago
5.1 kB
7
Indexable
'use strict';

const layers = [
    'Layer1',
    'Layer2',
    'Layer3',
    'Layer4',
    'Layer5',
    'Layer6',
    'Layer7',
    'Layer8',
    'Layer9',
    'Layer10'
];

// Manually define the duration for each layer in seconds
const layerDurations = {
    'Layer1': 30,
    'Layer2': 30,
    'Layer3': 30,
    'Layer4': 30,
    'Layer5': 30,
    'Layer6': 30,
    'Layer7': 30,
    'Layer8': 30,
    'Layer9': 30,
    'Layer10': 30
};

let shuffledLayers = [];
let currentIndex = 0;
let currentLayer = null;
let nextLayer = null;
let currentAudio = null;
let nextAudio = null;
let fading = false;

export var scriptProperties = createScriptProperties()
    .addSlider({
        name: 'defaultDuration',
        label: 'Default Display Duration',
        value: 5,
        min: 1,
        max: 20,
        integer: true
    })
    .addSlider({
        name: 'fadeDuration',
        label: 'Fade Duration',
        value: 1,
        min: 0.1,
        max: 5,
        integer: false
    })
    .finish();

// Function to shuffle the array
function shuffleArray(array) {
    for (let i = array.length - 1; i > 0; i--) {
        const j = Math.floor(Math.random() * (i + 1));
        [array[i], array[j]] = [array[j], array[i]];
    }
}

// Function to hide all layers and mute all audio
function hideAllLayers() {
    layers.forEach(layerName => {
        const layer = thisScene.getLayer(layerName);
        const audio = thisScene.getLayer(`${layerName}_audio`);
        if (layer) {
            const video = layer.getVideoTexture();
            if (video) {
                video.pause();
            }
            layer.visible = false;
            layer.alpha = 0;
        }
        if (audio) {
            audio.volume = 0; // Mute audio
            audio.stop();
            audio.visible = false;
        }
    });
}

// Function to preload the next layer and audio
function preloadNextLayer() {
    const nextIndex = (currentIndex + 1) % shuffledLayers.length;
    const nextLayerName = shuffledLayers[nextIndex];
    nextLayer = thisScene.getLayer(nextLayerName);
    nextAudio = thisScene.getLayer(`${nextLayerName}_audio`);
}

// Function to fade out the current layer
function fadeOutLayer(layer, callback) {
    if (!layer) return;
    fading = true;
    const fadeDurationMs = scriptProperties.fadeDuration * 1000;
    const fadeStep = 1 / (fadeDurationMs / 100);

    function fade() {
        layer.alpha -= fadeStep;
        if (layer.alpha > 0) {
            engine.setTimeout(fade, 100);
        } else {
            const video = layer.getVideoTexture();
            if (video) {
                video.pause();
            }
            const audio = thisScene.getLayer(`${layer.name}_audio`);
            if (audio) {
                audio.volume = 0; // Mute audio
                audio.stop();
            }
            layer.alpha = 0;
            layer.visible = false;
            fading = false;
            if (callback) callback();
        }
    }
    fade();
}

// Function to fade in the next layer
function fadeInLayer(layer) {
    if (!layer) return;
    fading = true;
    layer.visible = true;
    const video = layer.getVideoTexture();
    if (video) {
        video.play();
    }
    const audio = thisScene.getLayer(`${layer.name}_audio`);
    if (audio) {
        audio.visible = true;
        audio.volume = 1; // Unmute audio
        audio.play();
    }
    const fadeDurationMs = scriptProperties.fadeDuration * 1000;
    const fadeStep = 1 / (fadeDurationMs / 100);

    function fade() {
        layer.alpha += fadeStep;
        if (layer.alpha < 1) {
            engine.setTimeout(fade, 100);
        } else {
            layer.alpha = 1;
            fading = false;
        }
    }
    fade();
}

// Function to show the next layer
function showNextLayer() {
    if (fading) return;

    if (currentIndex >= shuffledLayers.length) {
        shuffleArray(shuffledLayers);
        currentIndex = 0;
    }

    const layerName = shuffledLayers[currentIndex];
    currentLayer = thisScene.getLayer(layerName);

    if (currentLayer) {
        // Fetch the display duration from the layerDurations object or use the default duration
        const displayDuration = layerDurations[layerName] || scriptProperties.defaultDuration;

        // Preload the next layer and audio
        preloadNextLayer();

        // Fade out the current layer and fade in the next layer
        fadeOutLayer(currentLayer, () => {
            currentLayer = nextLayer;
            currentAudio = nextAudio;
            fadeInLayer(currentLayer);

            // Schedule the next layer to show after the current one
            engine.setTimeout(showNextLayer, displayDuration * 1000);
        });
    }

    currentIndex++;
}

export function init() {
    shuffledLayers = [...layers];
    shuffleArray(shuffledLayers);
    hideAllLayers();
    showNextLayer();
}

export function update(value) {
    return value;
}
Editor is loading...
Leave a Comment