Overte C++ Documentation
ParticleEffectEntityItem.h
1 //
2 // ParticleEffectEntityItem.h
3 // libraries/entities/src
4 //
5 // Created by Jason Rickwald on 3/2/15.
6 //
7 // Distributed under the Apache License, Version 2.0.
8 // See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html
9 //
10 
11 #ifndef hifi_ParticleEffectEntityItem_h
12 #define hifi_ParticleEffectEntityItem_h
13 
14 #include <deque>
15 
16 #include "EntityItem.h"
17 
18 #include "ColorUtils.h"
19 #include "PulsePropertyGroup.h"
20 
21 namespace particle {
22  static const float SCRIPT_MAXIMUM_PI = 3.1416f; // Round up so that reasonable property values work
23  static const float UNINITIALIZED = NAN;
24  static const u8vec3 DEFAULT_COLOR = ENTITY_ITEM_DEFAULT_COLOR;
25  static const vec3 DEFAULT_COLOR_UNINITIALIZED = { UNINITIALIZED, UNINITIALIZED, UNINITIALIZED };
26  static const u8vec3 DEFAULT_COLOR_SPREAD = { 0, 0, 0 };
27  static const float DEFAULT_ALPHA = ENTITY_ITEM_DEFAULT_ALPHA;
28  static const float DEFAULT_ALPHA_SPREAD = 0.0f;
29  static const float DEFAULT_ALPHA_START = UNINITIALIZED;
30  static const float DEFAULT_ALPHA_FINISH = UNINITIALIZED;
31  static const float MINIMUM_ALPHA = 0.0f;
32  static const float MAXIMUM_ALPHA = 1.0f;
33  static const quint32 DEFAULT_MAX_PARTICLES = 1000;
34  static const quint32 MINIMUM_MAX_PARTICLES = 1;
35  static const quint32 MAXIMUM_MAX_PARTICLES = 100000;
36  static const float DEFAULT_LIFESPAN = 3.0f;
37  static const float MINIMUM_LIFESPAN = 0.0f;
38  static const float MAXIMUM_LIFESPAN = 86400.0f; // 1 day
39  static const float DEFAULT_EMIT_RATE = 15.0f;
40  static const float MINIMUM_EMIT_RATE = 0.0f;
41  static const float MAXIMUM_EMIT_RATE = 100000.0f;
42  static const float DEFAULT_EMIT_SPEED = 5.0f;
43  static const float MINIMUM_EMIT_SPEED = -1000.0f;
44  static const float MAXIMUM_EMIT_SPEED = 1000.0f; // Approx mach 3
45  static const float DEFAULT_SPEED_SPREAD = 1.0f;
46  static const quat DEFAULT_EMIT_ORIENTATION = glm::angleAxis(-PI_OVER_TWO, Vectors::UNIT_X); // Vertical
47  static const vec3 DEFAULT_EMIT_DIMENSIONS = Vectors::ZERO; // Emit from point
48  static const float MINIMUM_EMIT_DIMENSION = 0.0f;
49  static const float MAXIMUM_EMIT_DIMENSION = (float)TREE_SCALE;
50  static const float DEFAULT_EMIT_RADIUS_START = 1.0f; // Emit from surface (when emitDimensions > 0)
51  static const float MINIMUM_EMIT_RADIUS_START = 0.0f;
52  static const float MAXIMUM_EMIT_RADIUS_START = 1.0f;
53  static const float MINIMUM_POLAR = 0.0f;
54  static const float MAXIMUM_POLAR = SCRIPT_MAXIMUM_PI;
55  static const float DEFAULT_POLAR_START = 0.0f; // Emit along z-axis
56  static const float DEFAULT_POLAR_FINISH = 0.0f; // ""
57  static const float MINIMUM_AZIMUTH = -SCRIPT_MAXIMUM_PI;
58  static const float MAXIMUM_AZIMUTH = SCRIPT_MAXIMUM_PI;
59  static const float DEFAULT_AZIMUTH_START = -PI; // Emit full circumference (when polarFinish > 0)
60  static const float DEFAULT_AZIMUTH_FINISH = PI; // ""
61  static const vec3 DEFAULT_EMIT_ACCELERATION(0.0f, -9.8f, 0.0f);
62  static const float MINIMUM_EMIT_ACCELERATION = -100.0f; // ~ 10g
63  static const float MAXIMUM_EMIT_ACCELERATION = 100.0f;
64  static const vec3 DEFAULT_ACCELERATION_SPREAD(0.0f, 0.0f, 0.0f);
65  static const float MINIMUM_ACCELERATION_SPREAD = 0.0f;
66  static const float MAXIMUM_ACCELERATION_SPREAD = 100.0f;
67  static const float DEFAULT_PARTICLE_RADIUS = 0.025f;
68  static const float MINIMUM_PARTICLE_RADIUS = 0.0f;
69  static const float MAXIMUM_PARTICLE_RADIUS = (float)TREE_SCALE;
70  static const float DEFAULT_RADIUS_SPREAD = 0.0f;
71  static const float DEFAULT_RADIUS_START = UNINITIALIZED;
72  static const float DEFAULT_RADIUS_FINISH = UNINITIALIZED;
73  static const float DEFAULT_PARTICLE_SPIN = 0.0f;
74  static const float DEFAULT_SPIN_START = UNINITIALIZED;
75  static const float DEFAULT_SPIN_FINISH = UNINITIALIZED;
76  static const float DEFAULT_SPIN_SPREAD = 0.0f;
77  static const float MINIMUM_PARTICLE_SPIN = -2.0f * SCRIPT_MAXIMUM_PI;
78  static const float MAXIMUM_PARTICLE_SPIN = 2.0f * SCRIPT_MAXIMUM_PI;
79  static const QString DEFAULT_TEXTURES = "";
80  static const bool DEFAULT_EMITTER_SHOULD_TRAIL = false;
81  static const bool DEFAULT_ROTATE_WITH_ENTITY = false;
82  static const ShapeType DEFAULT_SHAPE_TYPE = ShapeType::SHAPE_TYPE_ELLIPSOID;
83 
84  template <typename T>
85  struct Range {
86  Range() {}
87  Range(const Range& other) { *this = other; }
88  Range(const T& defaultStart, const T& defaultFinish)
89  : start(defaultStart), finish(defaultFinish) {
90  }
91 
92  Range& operator=(const Range& other) {
93  start = other.start;
94  finish = other.finish;
95  return *this;
96  }
97 
98  T start;
99  T finish;
100  };
101 
102  template <typename T>
103  struct Gradient {
104  Gradient() {}
105  Gradient(const Gradient& other) { *this = other; }
106  Gradient(const T& defaultTarget, const T& defaultSpread)
107  : target(defaultTarget), spread(defaultSpread) {}
108 
109  Gradient& operator=(const Gradient& other) {
110  target = other.target;
111  spread = other.spread;
112  return *this;
113  }
114 
115  T target;
116  T spread;
117  };
118 
119  template <typename T>
120  struct RangeGradient {
121  RangeGradient() {}
122  RangeGradient(const RangeGradient& other) { *this = other; }
123  RangeGradient(const T& defaultValue, const T& defaultStart, const T& defaultFinish, const T& defaultSpread)
124  : range(defaultStart, defaultFinish), gradient(defaultValue, defaultSpread) {
125  }
126 
127  RangeGradient& operator=(const RangeGradient& other) {
128  range = other.range;
129  gradient = other.gradient;
130  return *this;
131  }
132 
133  Range<T> range;
134  Gradient<T> gradient;
135  };
136 
137  struct EmitProperties {
138  float rate { DEFAULT_EMIT_RATE };
139  Gradient<float> speed { DEFAULT_EMIT_SPEED, DEFAULT_SPEED_SPREAD };
140  Gradient<vec3> acceleration { DEFAULT_EMIT_ACCELERATION, DEFAULT_ACCELERATION_SPREAD };
141  glm::quat orientation { DEFAULT_EMIT_ORIENTATION };
142  glm::vec3 dimensions { DEFAULT_EMIT_DIMENSIONS };
143  bool shouldTrail { DEFAULT_EMITTER_SHOULD_TRAIL };
144 
145  EmitProperties() {};
146  EmitProperties(const EmitProperties& other) { *this = other; }
147  EmitProperties& operator=(const EmitProperties& other) {
148  rate = other.rate;
149  speed = other.speed;
150  acceleration = other.acceleration;
151  orientation = other.orientation;
152  dimensions = other.dimensions;
153  shouldTrail = other.shouldTrail;
154  return *this;
155  }
156  };
157 
158  struct Properties {
159  RangeGradient<vec3> color { DEFAULT_COLOR, DEFAULT_COLOR_UNINITIALIZED, DEFAULT_COLOR_UNINITIALIZED, DEFAULT_COLOR_SPREAD };
160  RangeGradient<float> alpha { DEFAULT_ALPHA, DEFAULT_ALPHA_START, DEFAULT_ALPHA_FINISH, DEFAULT_ALPHA_SPREAD };
161  float radiusStart { DEFAULT_EMIT_RADIUS_START };
162  RangeGradient<float> radius { DEFAULT_PARTICLE_RADIUS, DEFAULT_RADIUS_START, DEFAULT_RADIUS_FINISH, DEFAULT_RADIUS_SPREAD };
163  RangeGradient<float> spin { DEFAULT_PARTICLE_SPIN, DEFAULT_SPIN_START, DEFAULT_SPIN_FINISH, DEFAULT_SPIN_SPREAD };
164  bool rotateWithEntity { DEFAULT_ROTATE_WITH_ENTITY };
165  float lifespan { DEFAULT_LIFESPAN };
166  uint32_t maxParticles { DEFAULT_MAX_PARTICLES };
167  EmitProperties emission;
168  Range<float> polar { DEFAULT_POLAR_START, DEFAULT_POLAR_FINISH };
169  Range<float> azimuth { DEFAULT_AZIMUTH_START, DEFAULT_AZIMUTH_FINISH };
170  QString textures;
171 
172 
173  Properties() {};
174  Properties(const Properties& other) { *this = other; }
175  bool valid() const;
176  bool emitting() const;
177  uint64_t emitIntervalUsecs() const;
178 
179  Properties& operator =(const Properties& other) {
180  color = other.color;
181  alpha = other.alpha;
182  radiusStart = other.radiusStart;
183  radius = other.radius;
184  spin = other.spin;
185  rotateWithEntity = other.rotateWithEntity;
186  lifespan = other.lifespan;
187  maxParticles = other.maxParticles;
188  emission = other.emission;
189  polar = other.polar;
190  azimuth = other.azimuth;
191  textures = other.textures;
192  return *this;
193  }
194 
195  vec4 getColorStart() const { return vec4(ColorUtils::sRGBToLinearVec3(toGlm(color.range.start)), alpha.range.start); }
196  vec4 getColorMiddle() const { return vec4(ColorUtils::sRGBToLinearVec3(toGlm(color.gradient.target)), alpha.gradient.target); }
197  vec4 getColorFinish() const { return vec4(ColorUtils::sRGBToLinearVec3(toGlm(color.range.finish)), alpha.range.finish); }
198  vec4 getColorSpread() const { return vec4(ColorUtils::sRGBToLinearVec3(toGlm(color.gradient.spread)), alpha.gradient.spread); }
199  };
200 } // namespace particles
201 
202 
203 bool operator==(const particle::Properties& a, const particle::Properties& b);
204 bool operator!=(const particle::Properties& a, const particle::Properties& b);
205 
206 
207 class ParticleEffectEntityItem : public EntityItem {
208 public:
209  ALLOW_INSTANTIATION // This class can be instantiated
210 
211  static EntityItemPointer factory(const EntityItemID& entityID, const EntityItemProperties& properties);
212 
213  ParticleEffectEntityItem(const EntityItemID& entityItemID);
214 
215  // methods for getting/setting all properties of this entity
216  virtual EntityItemProperties getProperties(const EntityPropertyFlags& desiredProperties, bool allowEmptyDesiredProperties) const override;
217  virtual bool setSubClassProperties(const EntityItemProperties& properties) override;
218 
219  virtual EntityPropertyFlags getEntityProperties(EncodeBitstreamParams& params) const override;
220 
221  virtual void appendSubclassData(OctreePacketData* packetData, EncodeBitstreamParams& params,
222  EntityTreeElementExtraEncodeDataPointer entityTreeElementExtraEncodeData,
223  EntityPropertyFlags& requestedProperties,
224  EntityPropertyFlags& propertyFlags,
225  EntityPropertyFlags& propertiesDidntFit,
226  int& propertyCount,
227  OctreeElement::AppendState& appendState) const override;
228 
229  virtual int readEntitySubclassDataFromBuffer(const unsigned char* data, int bytesLeftToRead,
230  ReadBitstreamToTreeParams& args,
231  EntityPropertyFlags& propertyFlags, bool overwriteLocalData,
232  bool& somethingChanged) override;
233 
234  bool shouldBePhysical() const override { return false; }
235 
236  virtual void debugDump() const override;
237 
238  void setColor(const glm::u8vec3& value);
239  glm::u8vec3 getColor() const;
240 
241  void setColorStart(const vec3& colorStart);
242  vec3 getColorStart() const;
243 
244  void setColorFinish(const vec3& colorFinish);
245  vec3 getColorFinish() const;
246 
247  void setColorSpread(const glm::u8vec3& colorSpread);
248  glm::u8vec3 getColorSpread() const;
249 
250  void setAlpha(float alpha);
251  float getAlpha() const;
252 
253  void setAlphaStart(float alphaStart);
254  float getAlphaStart() const;
255 
256  void setAlphaFinish(float alphaFinish);
257  float getAlphaFinish() const;
258 
259  void setAlphaSpread(float alphaSpread);
260  float getAlphaSpread() const;
261 
262  void setShapeType(ShapeType type) override;
263  virtual ShapeType getShapeType() const override;
264 
265  QString getCompoundShapeURL() const;
266  virtual void setCompoundShapeURL(const QString& url);
267 
268  bool getIsEmitting() const { return _isEmitting; }
269  void setIsEmitting(bool isEmitting);
270 
271  void setMaxParticles(quint32 maxParticles);
272  quint32 getMaxParticles() const;
273 
274  void setLifespan(float lifespan);
275  float getLifespan() const;
276 
277  void setEmitRate(float emitRate);
278  float getEmitRate() const;
279 
280  void setEmitSpeed(float emitSpeed);
281  float getEmitSpeed() const;
282 
283  void setSpeedSpread(float speedSpread);
284  float getSpeedSpread() const;
285 
286  void setEmitOrientation(const glm::quat& emitOrientation);
287  glm::quat getEmitOrientation() const;
288 
289  void setEmitDimensions(const glm::vec3& emitDimensions);
290  glm::vec3 getEmitDimensions() const;
291 
292  void setEmitRadiusStart(float emitRadiusStart);
293  float getEmitRadiusStart() const;
294 
295  void setPolarStart(float polarStart);
296  float getPolarStart() const;
297 
298  void setPolarFinish(float polarFinish);
299  float getPolarFinish() const;
300 
301  void setAzimuthStart(float azimuthStart);
302  float getAzimuthStart() const;
303 
304  void setAzimuthFinish(float azimuthFinish);
305  float getAzimuthFinish() const;
306 
307  void setEmitAcceleration(const glm::vec3& emitAcceleration);
308  glm::vec3 getEmitAcceleration() const;
309 
310  void setAccelerationSpread(const glm::vec3& accelerationSpread);
311  glm::vec3 getAccelerationSpread() const;
312 
313  void setParticleRadius(float particleRadius);
314  float getParticleRadius() const;
315 
316  void setRadiusStart(float radiusStart);
317  float getRadiusStart() const;
318 
319  void setRadiusFinish(float radiusFinish);
320  float getRadiusFinish() const;
321 
322  void setRadiusSpread(float radiusSpread);
323  float getRadiusSpread() const;
324 
325  void setParticleSpin(float particleSpin);
326  float getParticleSpin() const;
327 
328  void setSpinStart(float spinStart);
329  float getSpinStart() const;
330 
331  void setSpinFinish(float spinFinish);
332  float getSpinFinish() const;
333 
334  void setSpinSpread(float spinSpread);
335  float getSpinSpread() const;
336 
337  void setRotateWithEntity(bool rotateWithEntity);
338  bool getRotateWithEntity() const { return _particleProperties.rotateWithEntity; }
339 
340  void computeAndUpdateDimensions();
341 
342  void setTextures(const QString& textures);
343  QString getTextures() const;
344 
345  void setEmitterShouldTrail(bool emitterShouldTrail);
346  bool getEmitterShouldTrail() const { return _particleProperties.emission.shouldTrail; }
347 
348  virtual bool supportsDetailedIntersection() const override { return false; }
349 
350  particle::Properties getParticleProperties() const;
351  PulsePropertyGroup getPulseProperties() const;
352 
353 protected:
354  particle::Properties _particleProperties;
355  PulsePropertyGroup _pulseProperties;
356  bool _isEmitting { true };
357 
358  ShapeType _shapeType{ particle::DEFAULT_SHAPE_TYPE };
359  QString _compoundShapeURL { "" };
360 };
361 
362 #endif // hifi_ParticleEffectEntityItem_h
Definition: EntityItem.h:82
virtual ShapeType getShapeType() const
return preferred shape type (actual physical shape may differ)
Definition: EntityItem.h:390
Abstract ID for editing model items. Used in EntityItem JS API.
Definition: EntityItemID.h:28
Definition: EntityItemProperties.h:106
Handles packing of the data portion of PacketType_OCTREE_DATA messages.
Definition: OctreePacketData.h:93