axmol/extensions/Particle3D/PU/CCPUParticleSystem3D.h

435 lines
14 KiB
C
Raw Normal View History

2015-02-11 18:14:22 +08:00
/****************************************************************************
2015-03-02 16:05:26 +08:00
Copyright (C) 2013 Henry van Merode. All rights reserved.
Copyright (c) 2015 Chukong Technologies Inc.
2015-02-11 18:14:22 +08:00
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#ifndef __CC_PU_PARTICLE_SYSTEM_3D_H__
#define __CC_PU_PARTICLE_SYSTEM_3D_H__
#include "2d/CCNode.h"
#include "base/CCProtocols.h"
#include "math/CCMath.h"
2015-03-02 16:05:26 +08:00
#include "extensions/Particle3D/CCParticleSystem3D.h"
2015-02-11 18:14:22 +08:00
#include <vector>
#include <map>
NS_CC_BEGIN
/**
* 3d particle system
*/
2015-03-05 13:24:06 +08:00
class PUListener;
class PUObserver;
class PUBehaviour;
class PUEmitter;
class PUAffector;
2015-02-11 18:14:22 +08:00
class Particle3DRender;
2015-03-02 13:07:32 +08:00
enum PUComponentType
{
CT_PARTICLE,
CT_SYSTEM,
CT_TECHNIQUE,
CT_EMITTER,
CT_AFFECTOR,
CT_OBSERVER
};
2015-02-11 18:14:22 +08:00
struct CC_DLL PUParticle3D : public Particle3D
{
static float DEFAULT_TTL;
static float DEFAULT_MASS;
PUParticle3D();
2015-03-02 13:07:32 +08:00
virtual ~PUParticle3D();
2015-03-05 13:24:06 +08:00
typedef std::vector<PUBehaviour*> ParticleBehaviourList;
2015-03-02 13:07:32 +08:00
enum ParticleType
{
PT_VISUAL,
PT_TECHNIQUE,
PT_EMITTER,
PT_AFFECTOR,
PT_SYSTEM,
};
2015-02-11 18:14:22 +08:00
/** Enumeration which lists a number of reserved event flags. Although custom flags can be used to
indicate that a certain condition occurs, the first number of flags may not be used as custom flags.
*/
enum ReservedParticleEventFlags
{
PEF_EXPIRED = 1<<0,
PEF_EMITTED = 1<<1,
PEF_COLLIDED = 1<<2
};
2015-03-02 13:07:32 +08:00
Ref *particleEntityPtr;
2015-03-05 13:24:06 +08:00
PUEmitter* parentEmitter;
2015-02-11 18:14:22 +08:00
2015-03-02 13:07:32 +08:00
Ref *visualData;
ParticleType particleType;
2015-02-11 18:14:22 +08:00
// Values that are assigned as soon as the particle is emitted (non-transformed)
2015-03-02 13:07:32 +08:00
//Vec3 positionInWorld;
2015-02-11 18:14:22 +08:00
Vec3 originalPosition;
Vec3 latestPosition;
// Direction (and speed)
Vec3 direction;
Vec3 originalDirection;
float originalDirectionLength; // Length of the direction that has been set
float originalVelocity;
float originalScaledDirectionLength; // Length of the direction after multiplication with the velocity
/** The rotation axis is used in combination with orientation. Because the rotation axis is part
of the particle itself, it can be changed independently. */
Vec3 rotationAxis;
/** Current and original colour */
Vec4 originalColor;
/** The zRotationSpeed is used in combination with zRotation and defines tha actual rotationspeed
in 2D. */
float zRotationSpeed; //radian
2015-03-02 13:07:32 +08:00
//Quaternion orientationInWorld;
2015-02-11 18:14:22 +08:00
/*@remarks
The orientation of the particle is only visible if the Particle Renderer - such as the Box renderer -
supports orientation.
*/
Quaternion originalOrientation;
/** The rotation is used in combination with orientation. Because the rotation speed is part
of the particle itself, it can be changed independently. */
float rotationSpeed;
/** Radius of the particle, to be used for inter-particle collision and such.
*/
float radius;
2015-03-02 13:07:32 +08:00
ParticleBehaviourList behaviours;
void copyBehaviours(const ParticleBehaviourList &list);
2015-02-11 18:14:22 +08:00
float calculateVelocity() const;
/** Set own dimensions
*/
void setOwnDimensions(float newWidth, float newHeight, float newDepth);
void calculateBoundingSphereRadius();
void initForEmission();
2015-03-02 13:07:32 +08:00
void initForExpiration(float timeElapsed);
void process(float timeElapsed);
2015-02-11 18:14:22 +08:00
/** Does this particle have it's own dimensions? */
bool ownDimensions;
/** Sets the event flags.
*/
void setEventFlags(unsigned int flags) { eventFlags = flags; }
2015-02-11 18:14:22 +08:00
/** As setEventFlags, except the flags passed as parameters are appended to the
existing flags on this object.
*/
void addEventFlags(unsigned int flags) { eventFlags |= flags; }
2015-02-11 18:14:22 +08:00
/** The flags passed as parameters are removed from the existing flags.
*/
void removeEventFlags(unsigned int flags) { eventFlags &= ~flags; }
2015-02-11 18:14:22 +08:00
/** Return the event flags.
*/
unsigned int getEventFlags() const { return eventFlags; }
2015-02-11 18:14:22 +08:00
/** Determines whether it has certain flags set.
*/
bool hasEventFlags(unsigned int flags) const { return (eventFlags & flags) != 0; }
2015-02-11 18:14:22 +08:00
unsigned int eventFlags;
bool isFreezed(void) const
{
return freezed;
}
//-----------------------------------------------------------------------
void setFreezed(bool fzd)
{
freezed = fzd;
}
bool freezed;
// Time to live, number of seconds left of particles natural life
float timeToLive;
// Total Time to live, number of seconds of particles natural life
float totalTimeToLive;
// The timeFraction is calculated every update. It is used in other observers, affectors, etc. so it is
// better to calculate it once at the Particle level.
float timeFraction;
/* Mass of a particle.
@remarks
In case of simulations where mass of a particle is needed (i.e. exploding particles of different
mass) this attribute can be used.
*/
float mass;
/** Animation attributes
*/
float textureAnimationTimeStep;
float textureAnimationTimeStepCount;
unsigned short textureCoordsCurrent;
bool textureAnimationDirectionUp;
float depthInView;//depth in camera view
float zRotation; //zRotation is used to rotate the particle in 2D (around the Z-axis) (radian)
2015-03-02 13:07:32 +08:00
//float widthInWorld;
//float heightInWorld;
//float depthInWorld;
2015-02-11 18:14:22 +08:00
};
class CC_DLL PUParticleSystem3D : public ParticleSystem3D
{
public:
2015-03-02 13:07:32 +08:00
typedef std::map<std::string, ParticlePool> ParticlePoolMap;
2015-02-11 18:14:22 +08:00
static const float DEFAULT_WIDTH;
static const float DEFAULT_HEIGHT;
static const float DEFAULT_DEPTH;
2015-03-02 13:07:32 +08:00
static const unsigned int DEFAULT_PARTICLE_QUOTA;
static const unsigned int DEFAULT_EMITTED_EMITTER_QUOTA;
static const unsigned int DEFAULT_EMITTED_SYSTEM_QUOTA;
2015-02-11 18:14:22 +08:00
static const float DEFAULT_MAX_VELOCITY;
static PUParticleSystem3D* create();
static PUParticleSystem3D* create(const std::string &filePath);
static PUParticleSystem3D* create(const std::string &filePath, const std::string &materialPath);
2015-03-02 13:07:32 +08:00
virtual void draw(Renderer *renderer, const Mat4 &transform, uint32_t flags) override;
2015-02-11 18:14:22 +08:00
virtual void update(float delta) override;
2015-03-02 13:07:32 +08:00
void forceUpdate(float delta);
2015-02-11 18:14:22 +08:00
/**
* particle system play control
*/
2015-02-12 16:09:08 +08:00
virtual void startParticleSystem() override;
2015-02-11 18:14:22 +08:00
/**
* stop particle
*/
2015-02-12 16:09:08 +08:00
virtual void stopParticleSystem() override;
2015-02-11 18:14:22 +08:00
/**
* pause particle
*/
2015-02-12 16:09:08 +08:00
virtual void pauseParticleSystem() override;
2015-02-11 18:14:22 +08:00
/**
* resume particle
*/
2015-02-12 16:09:08 +08:00
virtual void resumeParticleSystem() override;
2015-02-11 18:14:22 +08:00
virtual int getAliveParticleCount() const override;
2015-03-02 13:07:32 +08:00
2015-03-06 10:44:56 +08:00
/**
* Returns the velocity scale, defined in the particle system, but passed to the technique for convenience.
*/
2015-02-11 18:14:22 +08:00
float getParticleSystemScaleVelocity() const;
void setParticleSystemScaleVelocity(float scaleVelocity) { _particleSystemScaleVelocity = scaleVelocity; }
2015-03-06 10:44:56 +08:00
/**
* If the orientation of the particle system has been changed since the last update, the passed vector is rotated accordingly.
*/
2015-02-11 18:14:22 +08:00
void rotationOffset(Vec3& pos);
float getTimeElapsedSinceStart() const { return _timeElapsedSinceStart; }
2015-02-11 18:14:22 +08:00
2015-03-06 10:44:56 +08:00
/**
* default particle width
*/
float getDefaultWidth() const;
2015-02-11 18:14:22 +08:00
void setDefaultWidth(const float width);
/**
2015-03-06 10:44:56 +08:00
* default particle height
*/
float getDefaultHeight() const;
2015-02-11 18:14:22 +08:00
void setDefaultHeight(const float height);
/**
2015-03-06 10:44:56 +08:00
* default particle depth
*/
float getDefaultDepth() const;
2015-02-11 18:14:22 +08:00
void setDefaultDepth(const float depth);
Vec3 getDerivedPosition();
Quaternion getDerivedOrientation();
Vec3 getDerivedScale();
2015-03-06 10:44:56 +08:00
/**
* Return the maximum velocity a particle can have, even if the velocity of the particle has been set higher (either by initialisation of the particle or by means of an affector).
*/
2015-02-11 18:14:22 +08:00
float getMaxVelocity() const;
2015-03-06 10:44:56 +08:00
/**
* Set the maximum velocity a particle can have.
*/
2015-02-11 18:14:22 +08:00
void setMaxVelocity(float maxVelocity);
void setMaterialName(const std::string &name) { _matName = name; };
2016-02-15 23:25:01 +08:00
const std::string& getMaterialName() const { return _matName; };
2015-02-11 18:14:22 +08:00
2015-03-02 13:07:32 +08:00
/** Forces emission of particles.
2016-07-03 23:42:10 +08:00
* @remarks The number of requested particles are the exact number that are emitted. No down-scaling is applied.
2015-03-06 10:44:56 +08:00
*/
2015-03-05 13:24:06 +08:00
void forceEmission(PUEmitter* emitter, unsigned requested);
2015-03-02 13:07:32 +08:00
2015-03-06 10:44:56 +08:00
/**
2015-02-11 18:14:22 +08:00
* add particle affector
*/
2015-03-05 13:24:06 +08:00
void addEmitter(PUEmitter* emitter);
2015-03-02 13:07:32 +08:00
2015-03-05 13:24:06 +08:00
PUAffector* getAffector(const std::string &name);
PUEmitter* getEmitter(const std::string &name);
2015-03-02 13:07:32 +08:00
void removeAllEmitter();
2015-03-05 13:24:06 +08:00
void addListener(PUListener *listener);
void removeListener(PUListener *listener);
2015-03-02 13:07:32 +08:00
void removeAllListener();
2015-03-05 13:24:06 +08:00
void addObserver(PUObserver *observer);
PUObserver* getObserver(const std::string &name);
2015-03-02 13:07:32 +08:00
void removerAllObserver();
2015-03-05 13:24:06 +08:00
void addBehaviourTemplate(PUBehaviour *behaviour);
2015-03-02 13:07:32 +08:00
void removeAllBehaviourTemplate();
bool isMarkedForEmission() const {return _isMarkedForEmission;};
void setMarkedForEmission(bool isMarked) {_isMarkedForEmission = isMarked;};
void clearAllParticles();
unsigned int getEmittedEmitterQuota() const { return _emittedEmitterQuota; };
void setEmittedEmitterQuota(unsigned int quota) { _emittedEmitterQuota = quota; };
unsigned int getEmittedSystemQuota() const { return _emittedSystemQuota; };
void setEmittedSystemQuota(unsigned int quota) { _emittedSystemQuota = quota; };
PUParticleSystem3D* getParentParticleSystem()const { return _parentParticleSystem; };
const ParticlePoolMap& getEmittedEmitterParticlePool() const { return _emittedEmitterParticlePool; };
const ParticlePoolMap& getEmittedSystemParticlePool() const { return _emittedSystemParticlePool; };
bool makeParticleLocal(PUParticle3D* particle);
void calulateRotationOffset(void);
virtual PUParticleSystem3D* clone();
virtual void copyAttributesTo(PUParticleSystem3D* system);
2015-02-11 18:14:22 +08:00
2015-11-18 14:34:14 +08:00
bool initSystem(const std::string &filePath);
2015-02-11 18:14:22 +08:00
CC_CONSTRUCTOR_ACCESS:
PUParticleSystem3D();
virtual ~PUParticleSystem3D();
bool initWithFilePath(const std::string &filePath);
bool initWithFilePathAndMaterialPath (const std::string &filePath, const std::string &materialPath);
2015-02-11 18:14:22 +08:00
protected:
void prepared();
void unPrepared();
void preUpdator(float elapsedTime);
void updator(float elapsedTime);
void postUpdator(float elapsedTime);
void emitParticles(float elapsedTime);
2015-03-05 13:24:06 +08:00
void executeEmitParticles(PUEmitter* emitter, unsigned requested, float elapsedTime);
void emitParticles(ParticlePool &pool, PUEmitter* emitter, unsigned requested, float elapsedTime);
2015-03-02 13:07:32 +08:00
void processParticle(ParticlePool &pool, bool &firstActiveParticle, bool &firstParticle, float elapsedTime);
void processMotion(PUParticle3D* particle, float timeElapsed, const Vec3 &scl, bool firstParticle);
2015-03-02 13:07:32 +08:00
void notifyRescaled(const Vec3 &scl);
void initParticleForEmission(PUParticle3D* particle);
void initParticleForExpiration(PUParticle3D* particle, float timeElapsed);
2015-03-02 17:47:26 +08:00
void forceStopParticleSystem();
2015-02-11 18:14:22 +08:00
inline bool isExpired(PUParticle3D* particle, float timeElapsed);
static void convertToUnixStylePath(std::string &path);
protected:
2015-03-06 10:44:56 +08:00
std::vector<PUEmitter*> _emitters;
std::vector<PUObserver *> _observers;
ParticlePoolMap _emittedEmitterParticlePool;
ParticlePoolMap _emittedSystemParticlePool;
2015-03-02 13:07:32 +08:00
2015-03-06 10:44:56 +08:00
unsigned int _emittedEmitterQuota;
unsigned int _emittedSystemQuota;
2015-03-02 13:07:32 +08:00
//internal
PUParticle3D::ParticleBehaviourList _behaviourTemplates;
2015-03-06 10:44:56 +08:00
std::vector<PUListener *> _listeners;
2015-02-11 18:14:22 +08:00
2015-03-06 10:44:56 +08:00
bool _prepared;
bool _poolPrepared;
2015-02-11 18:14:22 +08:00
2015-03-06 10:44:56 +08:00
float _particleSystemScaleVelocity;
float _timeElapsedSinceStart;
2015-02-11 18:14:22 +08:00
2015-03-06 10:44:56 +08:00
Quaternion _rotationOffset; //Rotation offset between 2 updates.
2015-02-11 18:14:22 +08:00
2015-03-06 10:44:56 +08:00
Vec3 _rotationCentre; //The rotation centre.
2015-02-11 18:14:22 +08:00
2015-03-06 10:44:56 +08:00
float _defaultWidth; //Default width of each visual particle.
2015-02-11 18:14:22 +08:00
2015-03-06 10:44:56 +08:00
float _defaultHeight; //Default height of each visual particle.
float _defaultDepth; //Default depth of each visual particle.
2015-02-11 18:14:22 +08:00
2015-03-06 10:44:56 +08:00
float _maxVelocity; //Attributes that limit the velocity of the particles in this technique.
bool _maxVelocitySet;
2015-02-11 18:14:22 +08:00
2015-03-06 10:44:56 +08:00
std::string _matName;//material name
2015-03-02 13:07:32 +08:00
2015-03-06 10:44:56 +08:00
bool _isMarkedForEmission;
2015-03-02 13:07:32 +08:00
2015-03-06 10:44:56 +08:00
Vec3 _latestPositionDiff;
Vec3 _latestPosition;// Keep latest position
2015-03-02 13:07:32 +08:00
2015-03-06 10:44:56 +08:00
Quaternion _latestOrientation;
2015-03-02 13:07:32 +08:00
2015-03-06 10:44:56 +08:00
PUParticleSystem3D * _parentParticleSystem;
2015-02-11 18:14:22 +08:00
};
NS_CC_END
#endif