mirror of
https://github.com/lubosz/overte.git
synced 2025-04-08 14:42:19 +02:00
commit
7a5fce4315
16 changed files with 177 additions and 9 deletions
|
@ -1258,6 +1258,7 @@ Application::Application(int& argc, char** argv, QElapsedTimer& startupTimer, bo
|
|||
|
||||
connect(scriptEngines, &ScriptEngines::scriptsReloading, scriptEngines, [this] {
|
||||
getEntities()->reloadEntityScripts();
|
||||
loadAvatarScripts(getMyAvatar()->getScriptUrls());
|
||||
}, Qt::QueuedConnection);
|
||||
|
||||
connect(scriptEngines, &ScriptEngines::scriptLoadError,
|
||||
|
@ -4807,6 +4808,31 @@ void Application::init() {
|
|||
}, Qt::QueuedConnection);
|
||||
}
|
||||
|
||||
void Application::loadAvatarScripts(const QVector<QString>& urls) {
|
||||
auto scriptEngines = DependencyManager::get<ScriptEngines>();
|
||||
auto runningScripts = scriptEngines->getRunningScripts();
|
||||
for (auto url : urls) {
|
||||
int index = runningScripts.indexOf(url);
|
||||
if (index < 0) {
|
||||
auto scriptEnginePointer = scriptEngines->loadScript(url, false);
|
||||
if (scriptEnginePointer) {
|
||||
scriptEnginePointer->setType(ScriptEngine::Type::AVATAR);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Application::unloadAvatarScripts() {
|
||||
auto scriptEngines = DependencyManager::get<ScriptEngines>();
|
||||
auto urls = scriptEngines->getRunningScripts();
|
||||
for (auto url : urls) {
|
||||
auto scriptEngine = scriptEngines->getScriptEngine(url);
|
||||
if (scriptEngine->getType() == ScriptEngine::Type::AVATAR) {
|
||||
scriptEngines->stopScript(url, false);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Application::updateLOD(float deltaTime) const {
|
||||
PerformanceTimer perfTimer("LOD");
|
||||
// adjust it unless we were asked to disable this feature, or if we're currently in throttleRendering mode
|
||||
|
|
|
@ -290,6 +290,9 @@ public:
|
|||
|
||||
void replaceDomainContent(const QString& url);
|
||||
|
||||
void loadAvatarScripts(const QVector<QString>& urls);
|
||||
void unloadAvatarScripts();
|
||||
|
||||
signals:
|
||||
void svoImportRequested(const QString& url);
|
||||
|
||||
|
|
|
@ -156,9 +156,11 @@ bool ModelPackager::zipModel() {
|
|||
|
||||
QByteArray nameField = _mapping.value(NAME_FIELD).toByteArray();
|
||||
tempDir.mkpath(nameField + "/textures");
|
||||
tempDir.mkpath(nameField + "/scripts");
|
||||
QDir fbxDir(tempDir.path() + "/" + nameField);
|
||||
QDir texDir(fbxDir.path() + "/textures");
|
||||
|
||||
QDir scriptDir(fbxDir.path() + "/scripts");
|
||||
|
||||
// Copy textures
|
||||
listTextures();
|
||||
if (!_textures.empty()) {
|
||||
|
@ -166,6 +168,23 @@ bool ModelPackager::zipModel() {
|
|||
_texDir = _modelFile.path() + "/" + texdirField;
|
||||
copyTextures(_texDir, texDir);
|
||||
}
|
||||
|
||||
// Copy scripts
|
||||
QByteArray scriptField = _mapping.value(SCRIPT_FIELD).toByteArray();
|
||||
_mapping.remove(SCRIPT_FIELD);
|
||||
if (scriptField.size() > 1) {
|
||||
tempDir.mkpath(nameField + "/scripts");
|
||||
_scriptDir = _modelFile.path() + "/" + scriptField;
|
||||
QDir wdir = QDir(_scriptDir);
|
||||
_mapping.remove(SCRIPT_FIELD);
|
||||
wdir.setSorting(QDir::Name | QDir::Reversed);
|
||||
auto list = wdir.entryList(QDir::NoDotAndDotDot | QDir::AllEntries);
|
||||
for (auto script : list) {
|
||||
auto sc = tempDir.relativeFilePath(scriptDir.path()) + "/" + QUrl(script).fileName();
|
||||
_mapping.insertMulti(SCRIPT_FIELD, sc);
|
||||
}
|
||||
copyDirectoryContent(wdir, scriptDir);
|
||||
}
|
||||
|
||||
// Copy LODs
|
||||
QVariantHash lodField = _mapping.value(LOD_FIELD).toHash();
|
||||
|
@ -189,7 +208,10 @@ bool ModelPackager::zipModel() {
|
|||
// Correct FST
|
||||
_mapping[FILENAME_FIELD] = tempDir.relativeFilePath(newPath);
|
||||
_mapping[TEXDIR_FIELD] = tempDir.relativeFilePath(texDir.path());
|
||||
|
||||
|
||||
for (auto multi : _mapping.values(SCRIPT_FIELD)) {
|
||||
multi.fromValue(tempDir.relativeFilePath(scriptDir.path()) + multi.toString());
|
||||
}
|
||||
// Copy FST
|
||||
QFile fst(tempDir.path() + "/" + nameField + ".fst");
|
||||
if (fst.open(QIODevice::WriteOnly)) {
|
||||
|
@ -237,7 +259,9 @@ void ModelPackager::populateBasicMapping(QVariantHash& mapping, QString filename
|
|||
if (!mapping.contains(TEXDIR_FIELD)) {
|
||||
mapping.insert(TEXDIR_FIELD, ".");
|
||||
}
|
||||
|
||||
if (!mapping.contains(SCRIPT_FIELD)) {
|
||||
mapping.insert(SCRIPT_FIELD, ".");
|
||||
}
|
||||
// mixamo/autodesk defaults
|
||||
if (!mapping.contains(SCALE_FIELD)) {
|
||||
mapping.insert(SCALE_FIELD, 1.0);
|
||||
|
|
|
@ -37,10 +37,12 @@ private:
|
|||
QFileInfo _fbxInfo;
|
||||
FSTReader::ModelType _modelType;
|
||||
QString _texDir;
|
||||
QString _scriptDir;
|
||||
|
||||
QVariantHash _mapping;
|
||||
std::unique_ptr<FBXGeometry> _geometry;
|
||||
QStringList _textures;
|
||||
QStringList _scripts;
|
||||
};
|
||||
|
||||
|
||||
|
|
|
@ -43,6 +43,9 @@ _geometry(geometry)
|
|||
form->addRow("Texture Directory:", _textureDirectory = new QPushButton());
|
||||
connect(_textureDirectory, SIGNAL(clicked(bool)), SLOT(chooseTextureDirectory()));
|
||||
|
||||
form->addRow("Script Directory:", _scriptDirectory = new QPushButton());
|
||||
connect(_scriptDirectory, SIGNAL(clicked(bool)), SLOT(chooseScriptDirectory()));
|
||||
|
||||
form->addRow("Scale:", _scale = new QDoubleSpinBox());
|
||||
_scale->setMaximum(FLT_MAX);
|
||||
_scale->setSingleStep(0.01);
|
||||
|
@ -100,6 +103,7 @@ QVariantHash ModelPropertiesDialog::getMapping() const {
|
|||
mapping.insert(TYPE_FIELD, getType());
|
||||
mapping.insert(NAME_FIELD, _name->text());
|
||||
mapping.insert(TEXDIR_FIELD, _textureDirectory->text());
|
||||
mapping.insert(SCRIPT_FIELD, _scriptDirectory->text());
|
||||
mapping.insert(SCALE_FIELD, QString::number(_scale->value()));
|
||||
|
||||
// update the joint indices
|
||||
|
@ -157,6 +161,7 @@ void ModelPropertiesDialog::reset() {
|
|||
_name->setText(_originalMapping.value(NAME_FIELD).toString());
|
||||
_textureDirectory->setText(_originalMapping.value(TEXDIR_FIELD).toString());
|
||||
_scale->setValue(_originalMapping.value(SCALE_FIELD).toDouble());
|
||||
_scriptDirectory->setText(_originalMapping.value(SCRIPT_FIELD).toString());
|
||||
|
||||
QVariantHash jointHash = _originalMapping.value(JOINT_FIELD).toHash();
|
||||
|
||||
|
@ -207,6 +212,20 @@ void ModelPropertiesDialog::chooseTextureDirectory() {
|
|||
_textureDirectory->setText(directory.length() == _basePath.length() ? "." : directory.mid(_basePath.length() + 1));
|
||||
}
|
||||
|
||||
void ModelPropertiesDialog::chooseScriptDirectory() {
|
||||
QString directory = QFileDialog::getExistingDirectory(this, "Choose Script Directory",
|
||||
_basePath + "/" + _scriptDirectory->text());
|
||||
if (directory.isEmpty()) {
|
||||
return;
|
||||
}
|
||||
if (!directory.startsWith(_basePath)) {
|
||||
OffscreenUi::asyncWarning(NULL, "Invalid script directory", "Script directory must be child of base path.");
|
||||
return;
|
||||
}
|
||||
_scriptDirectory->setText(directory.length() == _basePath.length() ? "." : directory.mid(_basePath.length() + 1));
|
||||
}
|
||||
|
||||
|
||||
void ModelPropertiesDialog::updatePivotJoint() {
|
||||
_pivotJoint->setEnabled(!_pivotAboutCenter->isChecked());
|
||||
}
|
||||
|
|
|
@ -37,6 +37,7 @@ public:
|
|||
private slots:
|
||||
void reset();
|
||||
void chooseTextureDirectory();
|
||||
void chooseScriptDirectory();
|
||||
void updatePivotJoint();
|
||||
void createNewFreeJoint(const QString& joint = QString());
|
||||
|
||||
|
@ -52,6 +53,7 @@ private:
|
|||
FBXGeometry _geometry;
|
||||
QLineEdit* _name = nullptr;
|
||||
QPushButton* _textureDirectory = nullptr;
|
||||
QPushButton* _scriptDirectory = nullptr;
|
||||
QDoubleSpinBox* _scale = nullptr;
|
||||
QDoubleSpinBox* _translationX = nullptr;
|
||||
QDoubleSpinBox* _translationY = nullptr;
|
||||
|
|
|
@ -121,6 +121,19 @@ MyAvatar::MyAvatar(QThread* thread) :
|
|||
|
||||
_skeletonModel = std::make_shared<MySkeletonModel>(this, nullptr);
|
||||
connect(_skeletonModel.get(), &Model::setURLFinished, this, &Avatar::setModelURLFinished);
|
||||
connect(_skeletonModel.get(), &Model::setURLFinished, this, [this](bool success) {
|
||||
if (success) {
|
||||
qApp->unloadAvatarScripts();
|
||||
_shouldLoadScripts = true;
|
||||
}
|
||||
});
|
||||
connect(_skeletonModel.get(), &Model::rigReady, this, [this]() {
|
||||
if (_shouldLoadScripts) {
|
||||
auto geometry = getSkeletonModel()->getFBXGeometry();
|
||||
qApp->loadAvatarScripts(geometry.scripts);
|
||||
_shouldLoadScripts = false;
|
||||
}
|
||||
});
|
||||
connect(_skeletonModel.get(), &Model::rigReady, this, &Avatar::rigReady);
|
||||
connect(_skeletonModel.get(), &Model::rigReset, this, &Avatar::rigReset);
|
||||
|
||||
|
@ -2839,6 +2852,11 @@ void MyAvatar::setWalkSpeed(float value) {
|
|||
_walkSpeed.set(value);
|
||||
}
|
||||
|
||||
QVector<QString> MyAvatar::getScriptUrls() {
|
||||
QVector<QString> scripts = _skeletonModel->isLoaded() ? _skeletonModel->getFBXGeometry().scripts : QVector<QString>();
|
||||
return scripts;
|
||||
}
|
||||
|
||||
glm::vec3 MyAvatar::getPositionForAudio() {
|
||||
glm::vec3 result;
|
||||
switch (_audioListenerMode) {
|
||||
|
|
|
@ -985,6 +985,8 @@ public:
|
|||
void setWalkSpeed(float value);
|
||||
float getWalkSpeed() const;
|
||||
|
||||
QVector<QString> getScriptUrls();
|
||||
|
||||
public slots:
|
||||
|
||||
/**jsdoc
|
||||
|
@ -1322,7 +1324,6 @@ signals:
|
|||
private slots:
|
||||
void leaveDomain();
|
||||
|
||||
|
||||
protected:
|
||||
virtual void beParentOfChild(SpatiallyNestablePointer newChild) const override;
|
||||
virtual void forgetChild(SpatiallyNestablePointer newChild) const override;
|
||||
|
@ -1564,6 +1565,9 @@ private:
|
|||
// max unscaled forward movement speed
|
||||
ThreadSafeValueCache<float> _walkSpeed { DEFAULT_AVATAR_MAX_WALKING_SPEED };
|
||||
float _walkSpeedScalar { AVATAR_WALK_SPEED_SCALAR };
|
||||
|
||||
// load avatar scripts once when rig is ready
|
||||
bool _shouldLoadScripts { false };
|
||||
};
|
||||
|
||||
QScriptValue audioListenModeToScriptValue(QScriptEngine* engine, const AudioListenerMode& audioListenerMode);
|
||||
|
|
|
@ -298,6 +298,7 @@ public:
|
|||
bool hasSkeletonJoints;
|
||||
|
||||
QVector<FBXMesh> meshes;
|
||||
QVector<QString> scripts;
|
||||
|
||||
QHash<QString, FBXMaterial> materials;
|
||||
|
||||
|
|
|
@ -84,7 +84,7 @@ void FSTReader::writeVariant(QBuffer& buffer, QVariantHash::const_iterator& it)
|
|||
|
||||
QByteArray FSTReader::writeMapping(const QVariantHash& mapping) {
|
||||
static const QStringList PREFERED_ORDER = QStringList() << NAME_FIELD << TYPE_FIELD << SCALE_FIELD << FILENAME_FIELD
|
||||
<< TEXDIR_FIELD << JOINT_FIELD << FREE_JOINT_FIELD
|
||||
<< TEXDIR_FIELD << SCRIPT_FIELD << JOINT_FIELD << FREE_JOINT_FIELD
|
||||
<< BLENDSHAPE_FIELD << JOINT_INDEX_FIELD;
|
||||
QBuffer buffer;
|
||||
buffer.open(QIODevice::WriteOnly);
|
||||
|
@ -92,7 +92,7 @@ QByteArray FSTReader::writeMapping(const QVariantHash& mapping) {
|
|||
for (auto key : PREFERED_ORDER) {
|
||||
auto it = mapping.find(key);
|
||||
if (it != mapping.constEnd()) {
|
||||
if (key == FREE_JOINT_FIELD) { // writeVariant does not handle strings added using insertMulti.
|
||||
if (key == FREE_JOINT_FIELD || key == SCRIPT_FIELD) { // writeVariant does not handle strings added using insertMulti.
|
||||
for (auto multi : mapping.values(key)) {
|
||||
buffer.write(key.toUtf8());
|
||||
buffer.write(" = ");
|
||||
|
@ -187,6 +187,26 @@ FSTReader::ModelType FSTReader::predictModelType(const QVariantHash& mapping) {
|
|||
return ENTITY_MODEL;
|
||||
}
|
||||
|
||||
QVector<QString> FSTReader::getScripts(const QUrl& url, const QVariantHash& mapping) {
|
||||
|
||||
auto fstMapping = mapping.isEmpty() ? downloadMapping(url.toString()) : mapping;
|
||||
QVector<QString> scriptPaths;
|
||||
if (!fstMapping.value(SCRIPT_FIELD).isNull()) {
|
||||
auto scripts = fstMapping.values(SCRIPT_FIELD).toVector();
|
||||
for (auto &script : scripts) {
|
||||
QString scriptPath = script.toString();
|
||||
if (QUrl(scriptPath).isRelative()) {
|
||||
if (scriptPath.at(0) == '/') {
|
||||
scriptPath = scriptPath.right(scriptPath.length() - 1);
|
||||
}
|
||||
scriptPath = url.resolved(QUrl(scriptPath)).toString();
|
||||
}
|
||||
scriptPaths.push_back(scriptPath);
|
||||
}
|
||||
}
|
||||
return scriptPaths;
|
||||
}
|
||||
|
||||
QVariantHash FSTReader::downloadMapping(const QString& url) {
|
||||
QNetworkAccessManager& networkAccessManager = NetworkAccessManager::getInstance();
|
||||
QNetworkRequest networkRequest = QNetworkRequest(url);
|
||||
|
|
|
@ -28,6 +28,7 @@ static const QString TRANSLATION_Z_FIELD = "tz";
|
|||
static const QString JOINT_FIELD = "joint";
|
||||
static const QString FREE_JOINT_FIELD = "freeJoint";
|
||||
static const QString BLENDSHAPE_FIELD = "bs";
|
||||
static const QString SCRIPT_FIELD = "script";
|
||||
|
||||
class FSTReader {
|
||||
public:
|
||||
|
@ -49,6 +50,8 @@ public:
|
|||
/// Predicts the type of model by examining the mapping
|
||||
static ModelType predictModelType(const QVariantHash& mapping);
|
||||
|
||||
static QVector<QString> getScripts(const QUrl& fstUrl, const QVariantHash& mapping = QVariantHash());
|
||||
|
||||
static QString getNameFromType(ModelType modelType);
|
||||
static FSTReader::ModelType getTypeFromName(const QString& name);
|
||||
static QVariantHash downloadMapping(const QString& url);
|
||||
|
|
|
@ -66,6 +66,7 @@ void GeometryMappingResource::downloadFinished(const QByteArray& data) {
|
|||
auto mapping = FSTReader::readMapping(data);
|
||||
|
||||
QString filename = mapping.value("filename").toString();
|
||||
|
||||
if (filename.isNull()) {
|
||||
qCDebug(modelnetworking) << "Mapping file" << _url << "has no \"filename\" field";
|
||||
finishedLoading(false);
|
||||
|
@ -82,6 +83,14 @@ void GeometryMappingResource::downloadFinished(const QByteArray& data) {
|
|||
_textureBaseUrl = url.resolved(QUrl("."));
|
||||
}
|
||||
|
||||
auto scripts = FSTReader::getScripts(_url, mapping);
|
||||
if (scripts.size() > 0) {
|
||||
mapping.remove(SCRIPT_FIELD);
|
||||
for (auto &scriptPath : scripts) {
|
||||
mapping.insertMulti(SCRIPT_FIELD, scriptPath);
|
||||
}
|
||||
}
|
||||
|
||||
auto animGraphVariant = mapping.value("animGraphUrl");
|
||||
if (animGraphVariant.isValid()) {
|
||||
QUrl fstUrl(animGraphVariant.toString());
|
||||
|
@ -209,6 +218,14 @@ void GeometryReader::run() {
|
|||
throw QString("unsupported format");
|
||||
}
|
||||
|
||||
// Add scripts to fbxgeometry
|
||||
if (!_mapping.value(SCRIPT_FIELD).isNull()) {
|
||||
QVariantList scripts = _mapping.values(SCRIPT_FIELD);
|
||||
for (auto &script : scripts) {
|
||||
fbxGeometry->scripts.push_back(script.toString());
|
||||
}
|
||||
}
|
||||
|
||||
// Ensure the resource has not been deleted
|
||||
auto resource = _resource.toStrongRef();
|
||||
if (!resource) {
|
||||
|
|
|
@ -180,6 +180,21 @@ ScriptEngine::ScriptEngine(Context context, const QString& scriptContents, const
|
|||
// don't delete `ScriptEngines` until all `ScriptEngine`s are gone
|
||||
_scriptEngines(DependencyManager::get<ScriptEngines>())
|
||||
{
|
||||
switch (_context) {
|
||||
case Context::CLIENT_SCRIPT:
|
||||
_type = Type::CLIENT;
|
||||
break;
|
||||
case Context::ENTITY_CLIENT_SCRIPT:
|
||||
_type = Type::ENTITY_CLIENT;
|
||||
break;
|
||||
case Context::ENTITY_SERVER_SCRIPT:
|
||||
_type = Type::ENTITY_SERVER;
|
||||
break;
|
||||
case Context::AGENT_SCRIPT:
|
||||
_type = Type::AGENT;
|
||||
break;
|
||||
}
|
||||
|
||||
connect(this, &QScriptEngine::signalHandlerException, this, [this](const QScriptValue& exception) {
|
||||
if (hasUncaughtException()) {
|
||||
// the engine's uncaughtException() seems to produce much better stack traces here
|
||||
|
|
|
@ -103,6 +103,14 @@ public:
|
|||
AGENT_SCRIPT
|
||||
};
|
||||
|
||||
enum Type {
|
||||
CLIENT,
|
||||
ENTITY_CLIENT,
|
||||
ENTITY_SERVER,
|
||||
AGENT,
|
||||
AVATAR
|
||||
};
|
||||
|
||||
static int processLevelMaxRetries;
|
||||
ScriptEngine(Context context, const QString& scriptContents = NO_SCRIPT, const QString& fileNameString = QString("about:ScriptEngine"));
|
||||
~ScriptEngine();
|
||||
|
@ -493,6 +501,9 @@ public:
|
|||
*/
|
||||
Q_INVOKABLE QUuid generateUUID() { return QUuid::createUuid(); }
|
||||
|
||||
void setType(Type type) { _type = type; };
|
||||
Type getType() { return _type; };
|
||||
|
||||
bool isFinished() const { return _isFinished; } // used by Application and ScriptWidget
|
||||
bool isRunning() const { return _isRunning; } // used by ScriptWidget
|
||||
|
||||
|
@ -724,6 +735,7 @@ protected:
|
|||
void callWithEnvironment(const EntityItemID& entityID, const QUrl& sandboxURL, QScriptValue function, QScriptValue thisObject, QScriptValueList args);
|
||||
|
||||
Context _context;
|
||||
Type _type;
|
||||
QString _scriptContents;
|
||||
QString _parentURL;
|
||||
std::atomic<bool> _isFinished { false };
|
||||
|
|
|
@ -427,11 +427,13 @@ bool ScriptEngines::stopScript(const QString& rawScriptURL, bool restart) {
|
|||
if (_scriptEnginesHash.contains(scriptURL)) {
|
||||
ScriptEnginePointer scriptEngine = _scriptEnginesHash[scriptURL];
|
||||
if (restart) {
|
||||
bool isUserLoaded = scriptEngine->isUserLoaded();
|
||||
ScriptEngine::Type type = scriptEngine->getType();
|
||||
auto scriptCache = DependencyManager::get<ScriptCache>();
|
||||
scriptCache->deleteScript(scriptURL);
|
||||
connect(scriptEngine.data(), &ScriptEngine::finished,
|
||||
this, [this](QString scriptName, ScriptEnginePointer engine) {
|
||||
reloadScript(scriptName);
|
||||
this, [this, isUserLoaded, type](QString scriptName, ScriptEnginePointer engine) {
|
||||
reloadScript(scriptName, isUserLoaded)->setType(type);
|
||||
});
|
||||
}
|
||||
scriptEngine->stop();
|
||||
|
|
|
@ -259,7 +259,7 @@ protected slots:
|
|||
protected:
|
||||
friend class ScriptEngine;
|
||||
|
||||
void reloadScript(const QString& scriptName) { loadScript(scriptName, true, false, false, true); }
|
||||
ScriptEnginePointer reloadScript(const QString& scriptName, bool isUserLoaded = true) { return loadScript(scriptName, isUserLoaded, false, false, true); }
|
||||
void removeScriptEngine(ScriptEnginePointer);
|
||||
void onScriptEngineLoaded(const QString& scriptFilename);
|
||||
void onScriptEngineError(const QString& scriptFilename);
|
||||
|
|
Loading…
Reference in a new issue