From 3bd11978aaa33de3799809e52747855b10b4bbab Mon Sep 17 00:00:00 2001 From: howard-stearns Date: Tue, 7 Feb 2017 15:59:38 -0800 Subject: [PATCH] low-hanging fruit on pal performance --- scripts/system/pal.js | 45 +++++++++++++++++++++++-------------------- 1 file changed, 24 insertions(+), 21 deletions(-) diff --git a/scripts/system/pal.js b/scripts/system/pal.js index 9e9c49b1a0..7e451a39fb 100644 --- a/scripts/system/pal.js +++ b/scripts/system/pal.js @@ -11,6 +11,8 @@ // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html // +(function() { // BEGIN LOCAL_SCOPE + // hardcoding these as it appears we cannot traverse the originalTextures in overlays??? Maybe I've missed // something, will revisit as this is sorta horrible. const UNSELECTED_TEXTURES = {"idle-D": Script.resolvePath("./assets/models/Avatar-Overlay-v1.fbx/Avatar-Overlay-v1.fbm/avatar-overlay-idle.png"), @@ -27,7 +29,7 @@ const UNSELECTED_COLOR = { red: 0x1F, green: 0xC6, blue: 0xA6}; const SELECTED_COLOR = {red: 0xF3, green: 0x91, blue: 0x29}; const HOVER_COLOR = {red: 0xD0, green: 0xD0, blue: 0xD0}; // almost white for now -(function() { // BEGIN LOCAL_SCOPE +var conserveResources = true; Script.include("/~/system/libraries/controllers.js"); @@ -265,15 +267,16 @@ pal.fromQml.connect(function (message) { // messages are {method, params}, like function addAvatarNode(id) { var selected = ExtendedOverlay.isSelected(id); return new ExtendedOverlay(id, "sphere", { - drawInFront: true, - solid: true, - alpha: 0.8, - color: color(selected, false, 0.0), - ignoreRayIntersection: false}, selected, true); + drawInFront: true, + solid: true, + alpha: 0.8, + color: color(selected, false, 0.0), + ignoreRayIntersection: false}, selected, !conserveResources); } function populateUserList(selectData) { - var data = []; - AvatarList.getAvatarIdentifiers().sort().forEach(function (id) { // sorting the identifiers is just an aid for debugging + var data = [], avatars = AvatarList.getAvatarIdentifiers(); + conserveResources = avatars.length > 20; + avatars.forEach(function (id) { // sorting the identifiers is just an aid for debugging var avatar = AvatarList.getAvatar(id); var avatarPalDatum = { displayName: avatar.sessionDisplayName, @@ -498,6 +501,7 @@ if (Settings.getValue("HUDUIEnabled")) { }); } var isWired = false; +var audioTimer; function off() { if (isWired) { // It is not ok to disconnect these twice, hence guard. Script.update.disconnect(updateOverlays); @@ -505,6 +509,7 @@ function off() { Controller.mouseMoveEvent.disconnect(handleMouseMoveEvent); isWired = false; } + if (audioTimer) { Script.clearInterval(audioTimer); } triggerMapping.disable(); // It's ok if we disable twice. triggerPressMapping.disable(); // see above removeOverlays(); @@ -521,7 +526,7 @@ function onClicked() { Controller.mouseMoveEvent.connect(handleMouseMoveEvent); triggerMapping.enable(); triggerPressMapping.enable(); - createAudioInterval(); + audioTimer = createAudioInterval(AUDIO_LEVEL_UPDATE_INTERVAL_MS / (conserveResources ? 3 : 1)); } else { off(); } @@ -591,21 +596,19 @@ function getAudioLevel(id) { return audioLevel; } -function createAudioInterval() { +function createAudioInterval(interval) { // we will update the audioLevels periodically // TODO: tune for efficiency - expecially with large numbers of avatars return Script.setInterval(function () { - if (pal.visible) { - var param = {}; - AvatarList.getAvatarIdentifiers().forEach(function (id) { - var level = getAudioLevel(id); - // qml didn't like an object with null/empty string for a key, so... - var userId = id || 0; - param[userId] = level; - }); - pal.sendToQml({method: 'updateAudioLevel', params: param}); - } - }, AUDIO_LEVEL_UPDATE_INTERVAL_MS); + var param = {}; + AvatarList.getAvatarIdentifiers().forEach(function (id) { + var level = getAudioLevel(id); + // qml didn't like an object with null/empty string for a key, so... + var userId = id || 0; + param[userId] = level; + }); + pal.sendToQml({method: 'updateAudioLevel', params: param}); + }, interval); } function avatarDisconnected(nodeID) {