nPos3D Particle Test
by Admiral Potato
HTML
<script src="//admiralpotato.github.io/js/npos3d/build/npos3d.min.js"></script>
JavaScript
var n = NPos3d;
var s = new n.Scene();
var Something = function(args){
if(this === window){throw 'Please use the `new` keyword when calling a constructor.';}
var t = this;
n.blessWith3DBase(t,args);
t.life = 100;
t.vel = args.vel || [0,0,0];
t.origScale = [t.scale[0],t.scale[1],t.scale[2]];
t.renderAlways = true;
return t;
}
Something.prototype = {
shape:NPos3d.Geom.cube,
update:function(){
var t = this;
var lastPoint = t.shape.points[t.shape.points.length -1];
t.shape.points.push(n.Maths.p3Add(lastPoint,n.Maths.p3Rotate(t.vel,t.rot,[0,1,2])));
t.shape.lines.push([t.shape.points.length - 2, t.shape.points.length - 1]);
t.pos = n.Maths.p3Add(t.pos,t.vel);
t.rot[0] += deg * 5;
t.rot[1] += deg * 5;
t.rot[2] += deg * 5;
var lifeFraction = ( 1 - (t.life / 100) );
var scalar = t.origScale[0] * lifeFraction ;
t.shape.color = 'rgba(255,255,255,' + (1 - lifeFraction) + ')';
t.scale = n.Maths.getP3Scaled(t.origScale, [scalar,scalar,scalar] );
//console.log(t.scale);
t.life -= 1;
if(t.life < 1){
t.destroy();
};
}
};
var makeParticle = function(){
var myCrazyShape = {
points:[
[0,0,0]
],
lines:[
]
}
var myThing = new Something({
pos:[s.mpos.x,s.mpos.y,0],
vel:[
-5 + Math.random() * 10,
-5 + Math.random() * 10,
-5 + Math.random() * 10,
],
rot:[
tau * Math.random(),
tau * Math.random(),
tau * Math.random(),
],
scale:[2,2,2],
shape:myCrazyShape,
renderStyle:'both'
});
s.add(myThing);
}
var cubeSplosion = function(e){
for(var i = 0; i < 10; i += 1){
makeParticle();
}
}
var myThing = new Something({pos:[0,0,0],...