📄 particlesystem.java
字号:
else
for (int x = 0; x < 3; x++)
t.set(x, vertices[x]);
t.calculateCenter();
t.calculateNormal();
// turn the triangle corners into vector offsets from center
for (int x = 0; x < 3; x++) {
vertices[x].subtract(t.getCenter(), vertices[x]);
t.set(x, vertices[x]);
}
p.setTriangleModel(t);
psGeom.localToWorld(t.getCenter(), p.getPosition());
p.getPosition().multLocal(getInvScale());
} else if (getEmitType() == EmitType.Geometry) {
if (getGeometry() != null && getGeometry() instanceof TriMesh)
((TriMesh) getGeometry()).randomPointOnTriangles(p
.getPosition(), workVect3);
else if (getGeometry() != null)
getGeometry().randomVertex(p.getPosition());
p.getPosition().multLocal(getInvScale());
} else {
switch (getEmitType()) {
case Line:
getLine().random(p.getPosition());
break;
case Rectangle:
getRectangle().random(p.getPosition());
break;
case Ring:
getRing().random(p.getPosition());
break;
case Point:
default:
p.getPosition().set(originOffset);
break;
}
emitterTransform.multPoint(p.getPosition());
}
}
public boolean isCameraFacing() {
return cameraFacing;
}
public void setCameraFacing(boolean cameraFacing) {
this.cameraFacing = cameraFacing;
}
public boolean isVelocityAligned() {
return velocityAligned;
}
public void setVelocityAligned(boolean velocityAligned) {
this.velocityAligned = velocityAligned;
}
public Particle getParticle(int i) {
return particles[i];
}
public boolean isActive() {
return controller.isActive();
}
public void setSpeed(float f) {
controller.setSpeed(f);
}
public void setRepeatType(int type) {
controller.setRepeatType(type);
}
public void setControlFlow(boolean b) {
controller.setControlFlow(b);
}
public Vector3f getOriginCenter() {
return originCenter;
}
public Vector3f getUpVector() {
return upVector;
}
public void setUpVector(Vector3f upVector) {
this.upVector = upVector;
}
public Vector3f getLeftVector() {
return leftVector;
}
public void setLeftVector(Vector3f leftVector) {
this.leftVector = leftVector;
}
public boolean isRotateWithScene() {
return rotateWithScene;
}
public void setRotateWithScene(boolean rotate) {
this.rotateWithScene = rotate;
}
public void resetParticleVelocity(int i) {
getRandomVelocity(particles[i].getVelocity());
}
/**
* Returns a random angle between the min and max angles.
*
* @return the random angle.
*/
public float getRandomAngle() {
return getMinimumAngle() + FastMath.nextRandomFloat()
* (getMaximumAngle() - getMinimumAngle());
}
/**
* generate a random lifespan between the min and max lifespan of the
* particle system.
*
* @return the generated lifespan value
*/
public float getRandomLifeSpan() {
return getMinimumLifeTime()
+ ((getMaximumLifeTime() - getMinimumLifeTime()) * FastMath
.nextRandomFloat());
}
/**
* Generate a random velocity within the parameters of max angle and the
* rotation matrix.
*
* @param pSpeed
* a vector to store the results in.
*/
protected Vector3f getRandomVelocity(Vector3f pSpeed) {
float randDir = FastMath.TWO_PI * FastMath.nextRandomFloat();
float randAngle = getRandomAngle();
if (pSpeed == null)
pSpeed = new Vector3f();
pSpeed.x = FastMath.cos(randDir) * FastMath.sin(randAngle);
pSpeed.y = FastMath.cos(randAngle);
pSpeed.z = FastMath.sin(randDir) * FastMath.sin(randAngle);
rotateVectorSpeed(pSpeed);
pSpeed.multLocal(getInitialVelocity());
return pSpeed;
}
/**
* Apply the rotation matrix to a given vector representing a particle
* velocity.
*
* @param pSpeed
* the velocity vector to be modified.
*/
protected void rotateVectorSpeed(Vector3f pSpeed) {
float x = pSpeed.x, y = pSpeed.y, z = pSpeed.z;
pSpeed.x = -1
* ((rotMatrix.m00 * x) + (rotMatrix.m10 * y) + (rotMatrix.m20 * z));
pSpeed.y = (getRotMatrix().m01 * x) + (rotMatrix.m11 * y)
+ (rotMatrix.m21 * z);
pSpeed.z = -1
* ((rotMatrix.m02 * x) + (rotMatrix.m12 * y) + (rotMatrix.m22 * z));
}
public void warmUp(int iterations) {
if (controller != null) {
controller.warmUp(iterations);
}
}
public int getNumParticles() {
return numParticles;
}
public void setNumParticles(int numParticles) {
this.numParticles = numParticles;
}
public float getReleaseVariance() {
if (controller != null) {
return controller.getReleaseVariance();
}
return 0;
}
public void setReleaseVariance(float var) {
if (controller != null) {
controller.setReleaseVariance(var);
}
}
public ParticleAppearanceRamp getRamp() {
return ramp;
}
public void setRamp(ParticleAppearanceRamp ramp) {
if (ramp == null) {
logger.warning("Can not set a null ParticleAppearanceRamp.");
return;
}
this.ramp = ramp;
}
public TexAnimation getTexAnimation() {
return texAnimation;
}
public void setTexAnimation(TexAnimation texAnimation) {
if (texAnimation == null) {
logger.warning("Can not set a null TexAnimation.");
return;
}
this.texAnimation = texAnimation;
}
/**
* @return true if the particles are already in world coordinate space
* (default). When true, scene-graph transforms will only affect the
* emission of particles, not particles that are already living.
*/
public boolean isParticlesInWorldCoords() {
return particlesInWorldCoords;
}
public void setParticlesInWorldCoords(boolean particlesInWorldCoords) {
this.particlesInWorldCoords = particlesInWorldCoords;
}
/**
* Changes the number of particles in this particle mesh.
*
* @param count
* the desired number of particles to change to.
*/
public void recreate(int count) {
numParticles = count;
initializeParticles(numParticles);
}
@Override
public void updateWorldBound() {
; // ignore this since we want it to happen only when we say it can
// happen due to world vectors not being used
}
public void updateWorldBoundManually() {
super.updateWorldBound();
}
public void updateGeometricState(float time, boolean initiator) {
super.updateGeometricState(time, initiator);
if (isRotateWithScene()) {
if (emitType == EmitType.Geometry && getGeometry() != null) {
getGeometry().getWorldRotation().mult(emissionDirection,
worldEmit);
} else {
worldRotation.mult(emissionDirection, worldEmit);
}
} else
worldEmit.set(emissionDirection);
if (particlesInWorldCoords) {
emitterTransform.set(worldRotation, worldTranslation
.divide(worldScale));
originCenter.set(worldTranslation).addLocal(originOffset);
getWorldTranslation().set(0, 0, 0);
getWorldRotation().set(0, 0, 0, 1);
} else {
originCenter.set(originOffset);
}
}
public void write(JMEExporter e) throws IOException {
detachAllChildren();
super.write(e);
OutputCapsule capsule = e.getCapsule(this);
capsule.write(emitType, "emitType", EmitType.Point);
capsule.write(particleType, "particleType", ParticleType.Quad);
capsule.write(psLine, "psLine", null);
capsule.write(psRect, "psRect", null);
capsule.write(psRing, "psRing", null);
capsule.write(psGeom, "psGeom", null);
capsule.write(startSize, "startSize", DEFAULT_START_SIZE);
capsule.write(endSize, "endSize", DEFAULT_END_SIZE);
capsule.write(startColor, "startColor", DEFAULT_START_COLOR);
capsule.write(endColor, "endColor", DEFAULT_END_COLOR);
capsule.write(startSpin, "startSpin", 0);
capsule.write(endSpin, "endSpin", 0);
capsule.write(startMass, "startMass", 0);
capsule.write(endMass, "endMass", 0);
capsule.write(startTexIndex, "startTexIndex", 0);
capsule.write(texQuantity, "texQuantity", 1);
capsule.write(initialVelocity, "initialVelocity", 1);
capsule.write(minimumLifeTime, "minimumLifeTime", DEFAULT_MIN_LIFE);
capsule.write(maximumLifeTime, "maximumLifeTime", DEFAULT_MAX_LIFE);
capsule.write(minimumAngle, "minimumAngle", 0);
capsule.write(maximumAngle, "maximumAngle", DEFAULT_MAX_ANGLE);
capsule.write(emissionDirection, "emissionDirection", Vector3f.UNIT_Y);
capsule.write(worldEmit, "worldEmit", Vector3f.ZERO);
capsule.write(upVector, "upVector", Vector3f.UNIT_Y);
capsule.write(leftVector, "leftVector", new Vector3f(-1, 0, 0));
capsule.write(numParticles, "numParticles", 0);
capsule.write(particleOrientation, "particleOrientation", 0);
capsule.write(rotateWithScene, "rotateWithScene", false);
capsule.write(geometryCoordinates, "geometryCoordinates", null);
capsule.write(appearanceColors, "appearanceColors", null);
capsule.write(releaseRate, "releaseRate", numParticles);
capsule.write(originCenter, "originCenter", Vector3f.ZERO);
capsule.write(originOffset, "originOffset", Vector3f.ZERO);
capsule.write(controller, "controller", null);
capsule.write(cameraFacing, "cameraFacing", true);
capsule.write(velocityAligned, "velocityAligned", false);
capsule.write(particlesInWorldCoords, "particlesInWorldCoords", true);
capsule.write(ramp, "ramp", new ParticleAppearanceRamp());
capsule.write(texAnimation, "texAnimation", new TexAnimation());
}
public void read(JMEImporter e) throws IOException {
detachAllChildren();
super.read(e);
InputCapsule capsule = e.getCapsule(this);
emitType = capsule.readEnum("emitType", EmitType.class, EmitType.Point);
particleType = capsule.readEnum("particleType", ParticleType.class,
ParticleType.Quad);
psLine = (Line) capsule.readSavable("psLine", null);
psRect = (Rectangle) capsule.readSavable("psRect", null);
psRing = (Ring) capsule.readSavable("psRing", null);
psGeom = (Geometry) capsule.readSavable("psGeom", null);
startSize = capsule.readFloat("startSize", DEFAULT_START_SIZE);
endSize = capsule.readFloat("endSize", DEFAULT_END_SIZE);
startColor = (ColorRGBA) capsule.readSavable("startColor",
DEFAULT_START_COLOR.clone());
endColor = (ColorRGBA) capsule.readSavable("endColor",
DEFAULT_END_COLOR.clone());
startSpin = capsule.readFloat("startSpin", 0);
endSpin = capsule.readFloat("endSpin", 0);
startMass = capsule.readFloat("startMass", 0);
endMass = capsule.readFloat("endMass", 0);
startTexIndex = capsule.readInt("startTexIndex", 0);
texQuantity = capsule.readInt("texQuantity", 1);
initialVelocity = capsule.readFloat("initialVelocity", 1);
minimumLifeTime = capsule
.readFloat("minimumLifeTime", DEFAULT_MIN_LIFE);
maximumLifeTime = capsule
.readFloat("maximumLifeTime", DEFAULT_MAX_LIFE);
minimumAngle = capsule.readFloat("minimumAngle", 0);
maximumAngle = capsule.readFloat("maximumAngle", DEFAULT_MAX_ANGLE);
emissionDirection = (Vector3f) capsule.readSavable("emissionDirection",
Vector3f.UNIT_Y.clone());
worldEmit = (Vector3f) capsule.readSavable("worldEmit", Vector3f.ZERO
.clone());
upVector = (Vector3f) capsule.readSavable("upVector", Vector3f.UNIT_Y
.clone());
leftVector = (Vector3f) capsule.readSavable("leftVector", new Vector3f(
-1, 0, 0));
numParticles = capsule.readInt("numParticles", 0);
rotateWithScene = capsule.readBoolean("rotateWithScene", false);
geometryCoordinates = capsule.readFloatBuffer("geometryCoordinates",
null);
appearanceColors = capsule.readFloatBuffer("appearanceColors", null);
releaseRate = capsule.readInt("releaseRate", numParticles);
particleOrientation = capsule.readFloat("particleOrientation", 0);
originCenter = (Vector3f) capsule.readSavable("originCenter",
new Vector3f());
originOffset = (Vector3f) capsule.readSavable("originOffset",
new Vector3f());
controller = (ParticleController) capsule.readSavable("controller",
null);
cameraFacing = capsule.readBoolean("cameraFacing", true);
velocityAligned = capsule.readBoolean("velocityAligned", false);
particlesInWorldCoords = capsule.readBoolean("particlesInWorldCoords", true);
ramp = (ParticleAppearanceRamp) capsule.readSavable("ramp",
new ParticleAppearanceRamp());
texAnimation = (TexAnimation) capsule.readSavable("texAnimation",
new TexAnimation());
invScale = new Vector3f();
upXemit = new Vector3f();
absUpVector = new Vector3f();
abUpMinUp = new Vector3f();
rotMatrix = new Matrix3f();
initializeParticles(numParticles);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -