mirror of
https://github.com/JulianGro/overte.git
synced 2025-04-17 22:08:27 +02:00
add script to the particle wire and disk format
This commit is contained in:
parent
d65479406c
commit
542019a491
6 changed files with 120 additions and 119 deletions
|
@ -26,20 +26,6 @@ Agent::Agent(const unsigned char* dataBuffer, int numBytes) :
|
|||
{
|
||||
}
|
||||
|
||||
QScriptValue vec3toScriptValue(QScriptEngine *engine, const glm::vec3 &vec3) {
|
||||
QScriptValue obj = engine->newObject();
|
||||
obj.setProperty("x", vec3.x);
|
||||
obj.setProperty("y", vec3.y);
|
||||
obj.setProperty("z", vec3.z);
|
||||
return obj;
|
||||
}
|
||||
|
||||
void vec3FromScriptValue(const QScriptValue &object, glm::vec3 &vec3) {
|
||||
vec3.x = object.property("x").toVariant().toFloat();
|
||||
vec3.y = object.property("y").toVariant().toFloat();
|
||||
vec3.z = object.property("z").toVariant().toFloat();
|
||||
}
|
||||
|
||||
void Agent::processDatagram(const QByteArray& dataByteArray, const HifiSockAddr& senderSockAddr) {
|
||||
if (dataByteArray[0] == PACKET_TYPE_JURISDICTION) {
|
||||
int headerBytes = numBytesForPacketHeader((const unsigned char*) dataByteArray.constData());
|
||||
|
@ -91,7 +77,7 @@ void Agent::run() {
|
|||
QScriptEngine engine;
|
||||
|
||||
// register meta-type for glm::vec3 conversions
|
||||
qScriptRegisterMetaType(&engine, vec3toScriptValue, vec3FromScriptValue);
|
||||
registerMetaTypes(&engine);
|
||||
|
||||
QScriptValue agentValue = engine.newQObject(this);
|
||||
engine.globalObject().setProperty("Agent", agentValue);
|
||||
|
|
|
@ -10,6 +10,7 @@
|
|||
#include <QtScript/QScriptEngine>
|
||||
#include <QtCore/QObject>
|
||||
|
||||
#include <RegisteredMetaTypes.h>
|
||||
#include <SharedUtil.h> // usecTimestampNow()
|
||||
|
||||
#include "Particle.h"
|
||||
|
@ -17,19 +18,22 @@
|
|||
uint32_t Particle::_nextID = 0;
|
||||
|
||||
|
||||
Particle::Particle(glm::vec3 position, float radius, rgbColor color, glm::vec3 velocity, float damping, glm::vec3 gravity) {
|
||||
init(position, radius, color, velocity, damping, gravity);
|
||||
Particle::Particle(glm::vec3 position, float radius, rgbColor color, glm::vec3 velocity,
|
||||
float damping, glm::vec3 gravity, QString updateScript) {
|
||||
|
||||
init(position, radius, color, velocity, damping, gravity, updateScript);
|
||||
}
|
||||
|
||||
Particle::Particle() {
|
||||
rgbColor noColor = { 0, 0, 0 };
|
||||
init(glm::vec3(0,0,0), 0, noColor, glm::vec3(0,0,0), DEFAULT_DAMPING, DEFAULT_GRAVITY);
|
||||
init(glm::vec3(0,0,0), 0, noColor, glm::vec3(0,0,0), DEFAULT_DAMPING, DEFAULT_GRAVITY, DEFAULT_SCRIPT);
|
||||
}
|
||||
|
||||
Particle::~Particle() {
|
||||
}
|
||||
|
||||
void Particle::init(glm::vec3 position, float radius, rgbColor color, glm::vec3 velocity, float damping, glm::vec3 gravity) {
|
||||
void Particle::init(glm::vec3 position, float radius, rgbColor color, glm::vec3 velocity,
|
||||
float damping, glm::vec3 gravity, QString updateScript) {
|
||||
_id = _nextID;
|
||||
_nextID++;
|
||||
_lastUpdated = usecTimestampNow();
|
||||
|
@ -40,6 +44,7 @@ void Particle::init(glm::vec3 position, float radius, rgbColor color, glm::vec3
|
|||
_velocity = velocity;
|
||||
_damping = damping;
|
||||
_gravity = gravity;
|
||||
_updateScript = updateScript;
|
||||
}
|
||||
|
||||
|
||||
|
@ -70,6 +75,13 @@ bool Particle::appendParticleData(OctreePacketData* packetData) const {
|
|||
if (success) {
|
||||
success = packetData->appendValue(getDamping());
|
||||
}
|
||||
if (success) {
|
||||
uint16_t scriptLength = _updateScript.size() + 1; // include NULL
|
||||
success = packetData->appendValue(scriptLength);
|
||||
if (success) {
|
||||
success = packetData->appendRawData((const unsigned char*)qPrintable(_updateScript), scriptLength);
|
||||
}
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
|
@ -88,36 +100,52 @@ int Particle::readParticleDataFromBuffer(const unsigned char* data, int bytesLef
|
|||
// id
|
||||
memcpy(&_id, dataAt, sizeof(_id));
|
||||
dataAt += sizeof(_id);
|
||||
bytesRead += sizeof(_id);
|
||||
|
||||
// lastupdated
|
||||
memcpy(&_lastUpdated, dataAt, sizeof(_lastUpdated));
|
||||
dataAt += sizeof(_lastUpdated);
|
||||
bytesRead += sizeof(_lastUpdated);
|
||||
|
||||
// radius
|
||||
memcpy(&_radius, dataAt, sizeof(_radius));
|
||||
dataAt += sizeof(_radius);
|
||||
bytesRead += sizeof(_radius);
|
||||
|
||||
// position
|
||||
memcpy(&_position, dataAt, sizeof(_position));
|
||||
dataAt += sizeof(_position);
|
||||
bytesRead += sizeof(_position);
|
||||
|
||||
// color
|
||||
memcpy(_color, dataAt, sizeof(_color));
|
||||
dataAt += sizeof(_color);
|
||||
bytesRead += sizeof(_color);
|
||||
|
||||
// velocity
|
||||
memcpy(&_velocity, dataAt, sizeof(_velocity));
|
||||
dataAt += sizeof(_velocity);
|
||||
bytesRead += sizeof(_velocity);
|
||||
|
||||
// gravity
|
||||
memcpy(&_gravity, dataAt, sizeof(_gravity));
|
||||
dataAt += sizeof(_gravity);
|
||||
bytesRead += sizeof(_gravity);
|
||||
|
||||
// damping
|
||||
memcpy(&_damping, dataAt, sizeof(_damping));
|
||||
dataAt += sizeof(_damping);
|
||||
|
||||
bytesRead = expectedBytes();
|
||||
bytesRead += sizeof(_damping);
|
||||
|
||||
// script
|
||||
uint16_t scriptLength;
|
||||
memcpy(&scriptLength, dataAt, sizeof(scriptLength));
|
||||
dataAt += sizeof(scriptLength);
|
||||
bytesRead += sizeof(scriptLength);
|
||||
QString tempString((const char*)dataAt);
|
||||
_updateScript = tempString;
|
||||
dataAt += scriptLength;
|
||||
bytesRead += scriptLength;
|
||||
}
|
||||
return bytesRead;
|
||||
}
|
||||
|
@ -156,6 +184,26 @@ Particle Particle::fromEditPacket(unsigned char* data, int length, int& processe
|
|||
dataAt += sizeof(newParticle._velocity);
|
||||
processedBytes += sizeof(newParticle._velocity);
|
||||
|
||||
// gravity
|
||||
memcpy(&newParticle._gravity, dataAt, sizeof(newParticle._gravity));
|
||||
dataAt += sizeof(newParticle._gravity);
|
||||
processedBytes += sizeof(newParticle._gravity);
|
||||
|
||||
// damping
|
||||
memcpy(&newParticle._damping, dataAt, sizeof(newParticle._damping));
|
||||
dataAt += sizeof(newParticle._damping);
|
||||
processedBytes += sizeof(newParticle._damping);
|
||||
|
||||
// script
|
||||
uint16_t scriptLength;
|
||||
memcpy(&scriptLength, dataAt, sizeof(scriptLength));
|
||||
dataAt += sizeof(scriptLength);
|
||||
processedBytes += sizeof(scriptLength);
|
||||
QString tempString((const char*)dataAt);
|
||||
newParticle._updateScript = tempString;
|
||||
dataAt += scriptLength;
|
||||
processedBytes += scriptLength;
|
||||
|
||||
return newParticle;
|
||||
}
|
||||
|
||||
|
@ -205,6 +253,25 @@ bool Particle::encodeParticleEditMessageDetails(PACKET_TYPE command, int count,
|
|||
memcpy(copyAt, &details[i].velocity, sizeof(details[i].velocity));
|
||||
copyAt += sizeof(details[i].velocity);
|
||||
sizeOut += sizeof(details[i].velocity);
|
||||
|
||||
// gravity
|
||||
memcpy(copyAt, &details[i].gravity, sizeof(details[i].gravity));
|
||||
copyAt += sizeof(details[i].gravity);
|
||||
sizeOut += sizeof(details[i].gravity);
|
||||
|
||||
// damping
|
||||
memcpy(copyAt, &details[i].damping, sizeof(details[i].damping));
|
||||
copyAt += sizeof(details[i].damping);
|
||||
sizeOut += sizeof(details[i].damping);
|
||||
|
||||
// script
|
||||
uint16_t scriptLength = details[i].updateScript.size() + 1;
|
||||
memcpy(copyAt, &scriptLength, sizeof(scriptLength));
|
||||
copyAt += sizeof(scriptLength);
|
||||
sizeOut += sizeof(scriptLength);
|
||||
memcpy(copyAt, qPrintable(details[i].updateScript), scriptLength);
|
||||
copyAt += scriptLength;
|
||||
sizeOut += scriptLength;
|
||||
}
|
||||
// cleanup
|
||||
delete[] octcode;
|
||||
|
@ -245,79 +312,27 @@ void Particle::update() {
|
|||
_lastUpdated = now;
|
||||
}
|
||||
|
||||
Q_DECLARE_METATYPE(glm::vec3);
|
||||
Q_DECLARE_METATYPE(xColor);
|
||||
|
||||
QScriptValue Particle::vec3toScriptValue(QScriptEngine *engine, const glm::vec3 &vec3) {
|
||||
QScriptValue obj = engine->newObject();
|
||||
obj.setProperty("x", vec3.x);
|
||||
obj.setProperty("y", vec3.y);
|
||||
obj.setProperty("z", vec3.z);
|
||||
return obj;
|
||||
}
|
||||
|
||||
void Particle::vec3FromScriptValue(const QScriptValue &object, glm::vec3 &vec3) {
|
||||
vec3.x = object.property("x").toVariant().toFloat();
|
||||
vec3.y = object.property("y").toVariant().toFloat();
|
||||
vec3.z = object.property("z").toVariant().toFloat();
|
||||
}
|
||||
|
||||
QScriptValue Particle::xColorToScriptValue(QScriptEngine *engine, const xColor& color) {
|
||||
QScriptValue obj = engine->newObject();
|
||||
obj.setProperty("red", color.red);
|
||||
obj.setProperty("green", color.green);
|
||||
obj.setProperty("blue", color.blue);
|
||||
return obj;
|
||||
}
|
||||
|
||||
void Particle::xColorFromScriptValue(const QScriptValue &object, xColor& color) {
|
||||
color.red = object.property("red").toVariant().toInt();
|
||||
color.green = object.property("green").toVariant().toInt();
|
||||
color.blue = object.property("blue").toVariant().toInt();
|
||||
}
|
||||
|
||||
void Particle::runScript() {
|
||||
QString scriptContents(""
|
||||
//"var myGravity = ((Math.random() * 20)-10) / TREE_SCALE;\n"
|
||||
//"var myGravityVector = { x: myGravity, y: myGravity, z: myGravity };\n"
|
||||
//"Particle.setGravity(myGravityVector);\n"
|
||||
//"print(\"hello world\\n\");\n"
|
||||
//"var position = Particle.getPosition();\n"
|
||||
//"print(\"position.x=\" + position.x + \"\\n\");\n"
|
||||
//"position.x = position.x * 0.9;\n"
|
||||
//"Particle.setPosition(position);\n"
|
||||
//"print(\"position.x=\" + position.x + \"\\n\");\n"
|
||||
//"var color = Particle.getColor();\n"
|
||||
//"color.red = color.red - 1;\n"
|
||||
//"if (color.red < 10) { color.red = 255; }\n"
|
||||
//"Particle.setColor(color);\n"
|
||||
//"print(\"position=\" + position.x + \",\" + position.y + \",\" + position.z + \"\\n\");\n"
|
||||
);
|
||||
QScriptEngine engine;
|
||||
if (!_updateScript.isEmpty()) {
|
||||
QScriptEngine engine;
|
||||
|
||||
// register meta-type for glm::vec3 and rgbColor conversions
|
||||
qScriptRegisterMetaType(&engine, vec3toScriptValue, vec3FromScriptValue);
|
||||
qScriptRegisterMetaType(&engine, xColorToScriptValue, xColorFromScriptValue);
|
||||
// register meta-type for glm::vec3 and rgbColor conversions
|
||||
registerMetaTypes(&engine);
|
||||
|
||||
ParticleScriptObject particleScriptable(this);
|
||||
QScriptValue particleValue = engine.newQObject(&particleScriptable);
|
||||
engine.globalObject().setProperty("Particle", particleValue);
|
||||
ParticleScriptObject particleScriptable(this);
|
||||
QScriptValue particleValue = engine.newQObject(&particleScriptable);
|
||||
engine.globalObject().setProperty("Particle", particleValue);
|
||||
|
||||
QScriptValue treeScaleValue = engine.newVariant(QVariant(TREE_SCALE));
|
||||
engine.globalObject().setProperty("TREE_SCALE", TREE_SCALE);
|
||||
|
||||
//QScriptValue voxelScripterValue = engine.newQObject(&_voxelScriptingInterface);
|
||||
//engine.globalObject().setProperty("Voxels", voxelScripterValue);
|
||||
|
||||
//QScriptValue particleScripterValue = engine.newQObject(&_particleScriptingInterface);
|
||||
//engine.globalObject().setProperty("Particles", particleScripterValue);
|
||||
//qDebug() << "Downloaded script:" << _updateScript << "\n";
|
||||
QScriptValue result = engine.evaluate(_updateScript);
|
||||
//qDebug() << "Evaluated script.\n";
|
||||
|
||||
QScriptValue treeScaleValue = engine.newVariant(QVariant(TREE_SCALE));
|
||||
engine.globalObject().setProperty("TREE_SCALE", TREE_SCALE);
|
||||
|
||||
//qDebug() << "Downloaded script:" << scriptContents << "\n";
|
||||
QScriptValue result = engine.evaluate(scriptContents);
|
||||
//qDebug() << "Evaluated script.\n";
|
||||
|
||||
if (engine.hasUncaughtException()) {
|
||||
int line = engine.uncaughtExceptionLineNumber();
|
||||
qDebug() << "Uncaught exception at line" << line << ":" << result.toString() << "\n";
|
||||
if (engine.hasUncaughtException()) {
|
||||
int line = engine.uncaughtExceptionLineNumber();
|
||||
qDebug() << "Uncaught exception at line" << line << ":" << result.toString() << "\n";
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -25,23 +25,28 @@ public:
|
|||
float radius;
|
||||
rgbColor color;
|
||||
glm::vec3 velocity;
|
||||
glm::vec3 gravity;
|
||||
float damping;
|
||||
QString updateScript;
|
||||
};
|
||||
|
||||
const float DEFAULT_DAMPING = 0.99f;
|
||||
const glm::vec3 DEFAULT_GRAVITY(0, (-9.8f / TREE_SCALE), 0);
|
||||
const QString DEFAULT_SCRIPT("");
|
||||
|
||||
class Particle {
|
||||
|
||||
public:
|
||||
Particle();
|
||||
Particle(glm::vec3 position, float radius, rgbColor color, glm::vec3 velocity,
|
||||
float damping = DEFAULT_DAMPING, glm::vec3 gravity = DEFAULT_GRAVITY);
|
||||
float damping = DEFAULT_DAMPING, glm::vec3 gravity = DEFAULT_GRAVITY, QString updateScript = DEFAULT_SCRIPT);
|
||||
|
||||
/// creates an NEW particle from an PACKET_TYPE_PARTICLE_ADD edit data buffer
|
||||
static Particle fromEditPacket(unsigned char* data, int length, int& processedBytes);
|
||||
|
||||
virtual ~Particle();
|
||||
virtual void init(glm::vec3 position, float radius, rgbColor color, glm::vec3 velocity, float damping, glm::vec3 gravity);
|
||||
virtual void init(glm::vec3 position, float radius, rgbColor color, glm::vec3 velocity,
|
||||
float damping, glm::vec3 gravity, QString updateScript);
|
||||
|
||||
const glm::vec3& getPosition() const { return _position; }
|
||||
const rgbColor& getColor() const { return _color; }
|
||||
|
@ -53,6 +58,7 @@ public:
|
|||
uint64_t getLastUpdated() const { return _lastUpdated; }
|
||||
uint32_t getID() const { return _id; }
|
||||
bool getShouldDie() const { return _shouldDie; }
|
||||
QString getUpdateScript() const { return _updateScript; }
|
||||
|
||||
void setPosition(const glm::vec3& value) { _position = value; }
|
||||
void setVelocity(const glm::vec3& value) { _velocity = value; }
|
||||
|
@ -66,6 +72,7 @@ public:
|
|||
void setGravity(const glm::vec3& value) { _gravity = value; }
|
||||
void setDamping(float value) { _damping = value; }
|
||||
void setShouldDie(bool shouldDie) { _shouldDie = shouldDie; }
|
||||
void setUpdateScript(QString updateScript) { _updateScript = updateScript; }
|
||||
|
||||
bool appendParticleData(OctreePacketData* packetData) const;
|
||||
int readParticleDataFromBuffer(const unsigned char* data, int bytesLeftToRead, ReadBitstreamToTreeParams& args);
|
||||
|
@ -75,14 +82,14 @@ public:
|
|||
unsigned char* bufferOut, int sizeIn, int& sizeOut);
|
||||
|
||||
void update();
|
||||
void runScript();
|
||||
|
||||
protected:
|
||||
void runScript();
|
||||
static QScriptValue vec3toScriptValue(QScriptEngine *engine, const glm::vec3 &vec3);
|
||||
static void vec3FromScriptValue(const QScriptValue &object, glm::vec3 &vec3);
|
||||
static QScriptValue xColorToScriptValue(QScriptEngine *engine, const xColor& color);
|
||||
static void xColorFromScriptValue(const QScriptValue &object, xColor& color);
|
||||
|
||||
protected:
|
||||
glm::vec3 _position;
|
||||
rgbColor _color;
|
||||
float _radius;
|
||||
|
@ -91,9 +98,9 @@ protected:
|
|||
uint32_t _id;
|
||||
static uint32_t _nextID;
|
||||
bool _shouldDie;
|
||||
|
||||
glm::vec3 _gravity;
|
||||
float _damping;
|
||||
QString _updateScript;
|
||||
};
|
||||
|
||||
class ParticleScriptObject : public QObject {
|
||||
|
|
|
@ -19,17 +19,12 @@ void ParticleScriptingInterface::queueParticleAdd(PACKET_TYPE addPacketType, Par
|
|||
_particlePacketSender.queueParticleEditMessages(addPacketType, 1, &addParticleDetails);
|
||||
}
|
||||
|
||||
void ParticleScriptingInterface::queueParticleAdd(float x, float y, float z, float scale, uchar red, uchar green, uchar blue,
|
||||
float vx, float vy, float vz) {
|
||||
void ParticleScriptingInterface::queueParticleAdd(glm::vec3 position, float radius,
|
||||
xColor color, glm::vec3 velocity, glm::vec3 gravity, float damping, QString updateScript) {
|
||||
|
||||
// setup a ParticleDetail struct with the data
|
||||
ParticleDetail addParticleDetail = { glm::vec3(x, y, z), scale, {red, green, blue} , glm::vec3(vx, vy, vz) };
|
||||
ParticleDetail addParticleDetail = { position, radius, {color.red, color.green, color.blue }, velocity, gravity, damping, updateScript };
|
||||
|
||||
// queue the packet
|
||||
queueParticleAdd(PACKET_TYPE_PARTICLE_ADD, addParticleDetail);
|
||||
}
|
||||
|
||||
void ParticleScriptingInterface::queueParticleDelete(float x, float y, float z, float scale) {
|
||||
// not yet supported
|
||||
}
|
||||
|
||||
|
|
|
@ -24,23 +24,9 @@ public:
|
|||
JurisdictionListener* getJurisdictionListener() { return &_jurisdictionListener; }
|
||||
public slots:
|
||||
/// queues the creation of a Particle which will be sent by calling process on the PacketSender
|
||||
/// \param x the x-coordinate of the particle (in VS space)
|
||||
/// \param y the y-coordinate of the particle (in VS space)
|
||||
/// \param z the z-coordinate of the particle (in VS space)
|
||||
/// \param scale the scale of the particle (in VS space)
|
||||
/// \param red the R value for RGB color of particle
|
||||
/// \param green the G value for RGB color of particle
|
||||
/// \param blue the B value for RGB color of particle
|
||||
void queueParticleAdd(float x, float y, float z, float scale, uchar red, uchar green, uchar blue,
|
||||
float vx, float vy, float vz);
|
||||
void queueParticleAdd(glm::vec3 position, float radius,
|
||||
xColor color, glm::vec3 velocity, glm::vec3 gravity, float damping, QString updateScript);
|
||||
|
||||
/// queues the deletion of a particle, sent by calling process on the PacketSender
|
||||
/// \param x the x-coordinate of the particle (in VS space)
|
||||
/// \param y the y-coordinate of the particle (in VS space)
|
||||
/// \param z the z-coordinate of the particle (in VS space)
|
||||
/// \param scale the scale of the particle (in VS space)
|
||||
void queueParticleDelete(float x, float y, float z, float scale);
|
||||
|
||||
/// Set the desired max packet size in bytes that should be created
|
||||
void setMaxPacketSize(int maxPacketSize) { return _particlePacketSender.setMaxPacketSize(maxPacketSize); }
|
||||
|
||||
|
|
|
@ -11,6 +11,18 @@
|
|||
#ifndef hifi_RegisteredMetaTypes_h
|
||||
#define hifi_RegisteredMetaTypes_h
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
#include <QtScript/QScriptEngine>
|
||||
|
||||
#include "SharedUtil.h"
|
||||
|
||||
Q_DECLARE_METATYPE(glm::vec3)
|
||||
Q_DECLARE_METATYPE(xColor)
|
||||
|
||||
void registerMetaTypes(QScriptEngine* engine);
|
||||
QScriptValue vec3toScriptValue(QScriptEngine* engine, const glm::vec3 &vec3);
|
||||
void vec3FromScriptValue(const QScriptValue &object, glm::vec3 &vec3);
|
||||
QScriptValue xColorToScriptValue(QScriptEngine* engine, const xColor& color);
|
||||
void xColorFromScriptValue(const QScriptValue &object, xColor& color);
|
||||
|
||||
#endif
|
||||
|
|
Loading…
Reference in a new issue