Skip to content

Commit d4c3aed

Browse files
jbousquieRaananW
authored andcommitted
Added SPS per particle visibility + fix config (BabylonJS#1147)
* Added SPS per particle visibility + fix config * commit missing
1 parent a5b86c1 commit d4c3aed

File tree

3 files changed

+125
-91
lines changed

3 files changed

+125
-91
lines changed

Tools/Gulp/config.json

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -43,18 +43,18 @@
4343
"../../src/Collisions/babylon.collider.js",
4444
"../../src/Collisions/babylon.collisionCoordinator.js",
4545
"../../src/Cameras/babylon.camera.js",
46-
"../../src/cameras/babylon.camerainputsmanager.js",
47-
"../../src/cameras/inputs/babylon.freecamera.input.mouse.js",
48-
"../../src/cameras/inputs/babylon.freecamera.input.keyboard.js",
49-
"../../src/cameras/inputs/babylon.freecamera.input.touch.js",
50-
"../../src/cameras/inputs/babylon.freecamera.input.deviceorientation.js",
51-
"../../src/cameras/inputs/babylon.freecamera.input.vrdeviceorientation.js",
52-
"../../src/cameras/inputs/babylon.freecamera.input.gamepad.js",
53-
"../../src/cameras/inputs/babylon.arcrotatecamera.input.keyboard.js",
54-
"../../src/cameras/inputs/babylon.arcrotatecamera.input.mousewheel.js",
55-
"../../src/cameras/inputs/babylon.arcrotatecamera.input.pointers.js",
56-
"../../src/cameras/inputs/babylon.arcrotatecamera.input.gamepad.js",
57-
"../../src/cameras/inputs/babylon.arcrotatecamera.input.vrdeviceorientation.js",
46+
"../../src/Cameras/babylon.camerainputsmanager.js",
47+
"../../src/Cameras/inputs/babylon.freecamera.input.mouse.js",
48+
"../../src/Cameras/inputs/babylon.freecamera.input.keyboard.js",
49+
"../../src/Cameras/inputs/babylon.freecamera.input.touch.js",
50+
"../../src/Cameras/inputs/babylon.freecamera.input.deviceorientation.js",
51+
"../../src/Cameras/inputs/babylon.freecamera.input.vrdeviceorientation.js",
52+
"../../src/Cameras/inputs/babylon.freecamera.input.gamepad.js",
53+
"../../src/Cameras/inputs/babylon.arcrotatecamera.input.keyboard.js",
54+
"../../src/Cameras/inputs/babylon.arcrotatecamera.input.mousewheel.js",
55+
"../../src/Cameras/inputs/babylon.arcrotatecamera.input.pointers.js",
56+
"../../src/Cameras/inputs/babylon.arcrotatecamera.input.gamepad.js",
57+
"../../src/Cameras/inputs/babylon.arcrotatecamera.input.vrdeviceorientation.js",
5858
"../../src/Cameras/babylon.targetCamera.js",
5959
"../../src/Cameras/babylon.freeCamera.js",
6060
"../../src/Cameras/babylon.freeCameraInputsManager.js",

src/Particles/babylon.solidParticle.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ module BABYLON {
1010
public uvs = new Vector4(0, 0, 1, 1); // uvs
1111
public velocity = Vector3.Zero(); // velocity
1212
public alive = true; // alive
13+
public isVisible = true; // visibility
1314
public _pos: number; // index of this particle in the global "positions" array
1415
public _model: ModelShape; // model shape reference
1516
public shapeId: number; // model shape id

src/Particles/babylon.solidParticleSystem.ts

Lines changed: 112 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,7 @@
532532
var colorIndex = 0;
533533
var uvidx = 0;
534534
var uvIndex = 0;
535+
var pt = 0;
535536

536537
if (this._computeBoundingBox) {
537538
Vector3.FromFloatsToRef(Number.MAX_VALUE, Number.MAX_VALUE, Number.MAX_VALUE, this._minimum);
@@ -547,105 +548,137 @@
547548

548549
// call to custom user function to update the particle properties
549550
this.updateParticle(this._particle);
551+
552+
if (this._particle.isVisible) {
550553

551-
// particle rotation matrix
552-
if (this.billboard) {
553-
this._particle.rotation.x = 0.0;
554-
this._particle.rotation.y = 0.0;
555-
}
556-
if (this._computeParticleRotation) {
557-
if (this._particle.rotationQuaternion) {
558-
this._quaternion.copyFrom(this._particle.rotationQuaternion);
559-
} else {
560-
this._yaw = this._particle.rotation.y;
561-
this._pitch = this._particle.rotation.x;
562-
this._roll = this._particle.rotation.z;
563-
this._quaternionRotationYPR();
554+
// particle rotation matrix
555+
if (this.billboard) {
556+
this._particle.rotation.x = 0.0;
557+
this._particle.rotation.y = 0.0;
564558
}
565-
this._quaternionToRotationMatrix();
566-
}
567-
568-
for (var pt = 0; pt < this._shape.length; pt++) {
569-
idx = index + pt * 3;
570-
colidx = colorIndex + pt * 4;
571-
uvidx = uvIndex + pt * 2;
572-
573-
this._vertex.x = this._shape[pt].x;
574-
this._vertex.y = this._shape[pt].y;
575-
this._vertex.z = this._shape[pt].z;
576-
577-
if (this._computeParticleVertex) {
578-
this.updateParticleVertex(this._particle, this._vertex, pt);
559+
if (this._computeParticleRotation) {
560+
if (this._particle.rotationQuaternion) {
561+
this._quaternion.copyFrom(this._particle.rotationQuaternion);
562+
} else {
563+
this._yaw = this._particle.rotation.y;
564+
this._pitch = this._particle.rotation.x;
565+
this._roll = this._particle.rotation.z;
566+
this._quaternionRotationYPR();
567+
}
568+
this._quaternionToRotationMatrix();
579569
}
580570

581-
// positions
582-
this._vertex.x *= this._particle.scaling.x;
583-
this._vertex.y *= this._particle.scaling.y;
584-
this._vertex.z *= this._particle.scaling.z;
585-
586-
this._w = (this._vertex.x * this._rotMatrix.m[3]) + (this._vertex.y * this._rotMatrix.m[7]) + (this._vertex.z * this._rotMatrix.m[11]) + this._rotMatrix.m[15];
587-
this._rotated.x = ((this._vertex.x * this._rotMatrix.m[0]) + (this._vertex.y * this._rotMatrix.m[4]) + (this._vertex.z * this._rotMatrix.m[8]) + this._rotMatrix.m[12]) / this._w;
588-
this._rotated.y = ((this._vertex.x * this._rotMatrix.m[1]) + (this._vertex.y * this._rotMatrix.m[5]) + (this._vertex.z * this._rotMatrix.m[9]) + this._rotMatrix.m[13]) / this._w;
589-
this._rotated.z = ((this._vertex.x * this._rotMatrix.m[2]) + (this._vertex.y * this._rotMatrix.m[6]) + (this._vertex.z * this._rotMatrix.m[10]) + this._rotMatrix.m[14]) / this._w;
571+
// particle vertex loop
572+
for (pt = 0; pt < this._shape.length; pt++) {
573+
idx = index + pt * 3;
574+
colidx = colorIndex + pt * 4;
575+
uvidx = uvIndex + pt * 2;
590576

591-
this._positions32[idx] = this._particle.position.x + this._cam_axisX.x * this._rotated.x + this._cam_axisY.x * this._rotated.y + this._cam_axisZ.x * this._rotated.z;
592-
this._positions32[idx + 1] = this._particle.position.y + this._cam_axisX.y * this._rotated.x + this._cam_axisY.y * this._rotated.y + this._cam_axisZ.y * this._rotated.z;
593-
this._positions32[idx + 2] = this._particle.position.z + this._cam_axisX.z * this._rotated.x + this._cam_axisY.z * this._rotated.y + this._cam_axisZ.z * this._rotated.z;
577+
this._vertex.x = this._shape[pt].x;
578+
this._vertex.y = this._shape[pt].y;
579+
this._vertex.z = this._shape[pt].z;
594580

595-
if (this._computeBoundingBox) {
596-
if (this._positions32[idx] < this._minimum.x) {
597-
this._minimum.x = this._positions32[idx];
598-
}
599-
if (this._positions32[idx] > this._maximum.x) {
600-
this._maximum.x = this._positions32[idx];
601-
}
602-
if (this._positions32[idx + 1] < this._minimum.y) {
603-
this._minimum.y = this._positions32[idx + 1];
604-
}
605-
if (this._positions32[idx + 1] > this._maximum.y) {
606-
this._maximum.y = this._positions32[idx + 1];
581+
if (this._computeParticleVertex) {
582+
this.updateParticleVertex(this._particle, this._vertex, pt);
607583
}
608-
if (this._positions32[idx + 2] < this._minimum.z) {
609-
this._minimum.z = this._positions32[idx + 2];
610-
}
611-
if (this._positions32[idx + 2] > this._maximum.z) {
612-
this._maximum.z = this._positions32[idx + 2];
584+
585+
// positions
586+
this._vertex.x *= this._particle.scaling.x;
587+
this._vertex.y *= this._particle.scaling.y;
588+
this._vertex.z *= this._particle.scaling.z;
589+
590+
this._w = (this._vertex.x * this._rotMatrix.m[3]) + (this._vertex.y * this._rotMatrix.m[7]) + (this._vertex.z * this._rotMatrix.m[11]) + this._rotMatrix.m[15];
591+
this._rotated.x = ((this._vertex.x * this._rotMatrix.m[0]) + (this._vertex.y * this._rotMatrix.m[4]) + (this._vertex.z * this._rotMatrix.m[8]) + this._rotMatrix.m[12]) / this._w;
592+
this._rotated.y = ((this._vertex.x * this._rotMatrix.m[1]) + (this._vertex.y * this._rotMatrix.m[5]) + (this._vertex.z * this._rotMatrix.m[9]) + this._rotMatrix.m[13]) / this._w;
593+
this._rotated.z = ((this._vertex.x * this._rotMatrix.m[2]) + (this._vertex.y * this._rotMatrix.m[6]) + (this._vertex.z * this._rotMatrix.m[10]) + this._rotMatrix.m[14]) / this._w;
594+
595+
this._positions32[idx] = this._particle.position.x + this._cam_axisX.x * this._rotated.x + this._cam_axisY.x * this._rotated.y + this._cam_axisZ.x * this._rotated.z;
596+
this._positions32[idx + 1] = this._particle.position.y + this._cam_axisX.y * this._rotated.x + this._cam_axisY.y * this._rotated.y + this._cam_axisZ.y * this._rotated.z;
597+
this._positions32[idx + 2] = this._particle.position.z + this._cam_axisX.z * this._rotated.x + this._cam_axisY.z * this._rotated.y + this._cam_axisZ.z * this._rotated.z;
598+
599+
if (this._computeBoundingBox) {
600+
if (this._positions32[idx] < this._minimum.x) {
601+
this._minimum.x = this._positions32[idx];
602+
}
603+
if (this._positions32[idx] > this._maximum.x) {
604+
this._maximum.x = this._positions32[idx];
605+
}
606+
if (this._positions32[idx + 1] < this._minimum.y) {
607+
this._minimum.y = this._positions32[idx + 1];
608+
}
609+
if (this._positions32[idx + 1] > this._maximum.y) {
610+
this._maximum.y = this._positions32[idx + 1];
611+
}
612+
if (this._positions32[idx + 2] < this._minimum.z) {
613+
this._minimum.z = this._positions32[idx + 2];
614+
}
615+
if (this._positions32[idx + 2] > this._maximum.z) {
616+
this._maximum.z = this._positions32[idx + 2];
617+
}
613618
}
614-
}
615619

616-
// normals : if the particles can't be morphed then just rotate the normals
617-
if (!this._computeParticleVertex && !this.billboard) {
618-
this._normal.x = this._fixedNormal32[idx];
619-
this._normal.y = this._fixedNormal32[idx + 1];
620-
this._normal.z = this._fixedNormal32[idx + 2];
620+
// normals : if the particles can't be morphed then just rotate the normals, what if much more faster than ComputeNormals()
621+
if (!this._computeParticleVertex && !this.billboard) {
622+
this._normal.x = this._fixedNormal32[idx];
623+
this._normal.y = this._fixedNormal32[idx + 1];
624+
this._normal.z = this._fixedNormal32[idx + 2];
621625

622-
this._w = (this._normal.x * this._rotMatrix.m[3]) + (this._normal.y * this._rotMatrix.m[7]) + (this._normal.z * this._rotMatrix.m[11]) + this._rotMatrix.m[15];
623-
this._rotated.x = ((this._normal.x * this._rotMatrix.m[0]) + (this._normal.y * this._rotMatrix.m[4]) + (this._normal.z * this._rotMatrix.m[8]) + this._rotMatrix.m[12]) / this._w;
624-
this._rotated.y = ((this._normal.x * this._rotMatrix.m[1]) + (this._normal.y * this._rotMatrix.m[5]) + (this._normal.z * this._rotMatrix.m[9]) + this._rotMatrix.m[13]) / this._w;
625-
this._rotated.z = ((this._normal.x * this._rotMatrix.m[2]) + (this._normal.y * this._rotMatrix.m[6]) + (this._normal.z * this._rotMatrix.m[10]) + this._rotMatrix.m[14]) / this._w;
626+
this._w = (this._normal.x * this._rotMatrix.m[3]) + (this._normal.y * this._rotMatrix.m[7]) + (this._normal.z * this._rotMatrix.m[11]) + this._rotMatrix.m[15];
627+
this._rotated.x = ((this._normal.x * this._rotMatrix.m[0]) + (this._normal.y * this._rotMatrix.m[4]) + (this._normal.z * this._rotMatrix.m[8]) + this._rotMatrix.m[12]) / this._w;
628+
this._rotated.y = ((this._normal.x * this._rotMatrix.m[1]) + (this._normal.y * this._rotMatrix.m[5]) + (this._normal.z * this._rotMatrix.m[9]) + this._rotMatrix.m[13]) / this._w;
629+
this._rotated.z = ((this._normal.x * this._rotMatrix.m[2]) + (this._normal.y * this._rotMatrix.m[6]) + (this._normal.z * this._rotMatrix.m[10]) + this._rotMatrix.m[14]) / this._w;
626630

627-
this._normals32[idx] = this._cam_axisX.x * this._rotated.x + this._cam_axisY.x * this._rotated.y + this._cam_axisZ.x * this._rotated.z;
628-
this._normals32[idx + 1] = this._cam_axisX.y * this._rotated.x + this._cam_axisY.y * this._rotated.y + this._cam_axisZ.y * this._rotated.z;
629-
this._normals32[idx + 2] = this._cam_axisX.z * this._rotated.x + this._cam_axisY.z * this._rotated.y + this._cam_axisZ.z * this._rotated.z;
630-
}
631+
this._normals32[idx] = this._cam_axisX.x * this._rotated.x + this._cam_axisY.x * this._rotated.y + this._cam_axisZ.x * this._rotated.z;
632+
this._normals32[idx + 1] = this._cam_axisX.y * this._rotated.x + this._cam_axisY.y * this._rotated.y + this._cam_axisZ.y * this._rotated.z;
633+
this._normals32[idx + 2] = this._cam_axisX.z * this._rotated.x + this._cam_axisY.z * this._rotated.y + this._cam_axisZ.z * this._rotated.z;
634+
}
631635

632-
if (this._computeParticleColor) {
633-
this._colors32[colidx] = this._particle.color.r;
634-
this._colors32[colidx + 1] = this._particle.color.g;
635-
this._colors32[colidx + 2] = this._particle.color.b;
636-
this._colors32[colidx + 3] = this._particle.color.a;
636+
if (this._computeParticleColor) {
637+
this._colors32[colidx] = this._particle.color.r;
638+
this._colors32[colidx + 1] = this._particle.color.g;
639+
this._colors32[colidx + 2] = this._particle.color.b;
640+
this._colors32[colidx + 3] = this._particle.color.a;
641+
}
642+
643+
if (this._computeParticleTexture) {
644+
this._uvs32[uvidx] = this._shapeUV[pt * 2] * (this._particle.uvs.z - this._particle.uvs.x) + this._particle.uvs.x;
645+
this._uvs32[uvidx + 1] = this._shapeUV[pt * 2 + 1] * (this._particle.uvs.w - this._particle.uvs.y) + this._particle.uvs.y;
646+
}
637647
}
638648

639-
if (this._computeParticleTexture) {
640-
this._uvs32[uvidx] = this._shapeUV[pt * 2] * (this._particle.uvs.z - this._particle.uvs.x) + this._particle.uvs.x;
641-
this._uvs32[uvidx + 1] = this._shapeUV[pt * 2 + 1] * (this._particle.uvs.w - this._particle.uvs.y) + this._particle.uvs.y;
649+
}
650+
// particle not visible : scaled to zero and positioned to the camera position
651+
else {
652+
for (pt = 0; pt < this._shape.length; pt++) {
653+
idx = index + pt * 3;
654+
colidx = colorIndex + pt * 4;
655+
uvidx = uvIndex + pt * 2;
656+
this._positions32[idx] = this._camera.position.x;
657+
this._positions32[idx + 1] = this._camera.position.y;
658+
this._positions32[idx + 2] = this._camera.position.z;
659+
this._normals32[idx] = 0.0;
660+
this._normals32[idx + 1] = 0.0;
661+
this._normals32[idx + 2] = 0.0;
662+
if (this._computeParticleColor) {
663+
this._colors32[colidx] = this._particle.color.r;
664+
this._colors32[colidx + 1] = this._particle.color.g;
665+
this._colors32[colidx + 2] = this._particle.color.b;
666+
this._colors32[colidx + 3] = this._particle.color.a;
667+
}
668+
if (this._computeParticleTexture) {
669+
this._uvs32[uvidx] = this._shapeUV[pt * 2] * (this._particle.uvs.z - this._particle.uvs.x) + this._particle.uvs.x;
670+
this._uvs32[uvidx + 1] = this._shapeUV[pt * 2 + 1] * (this._particle.uvs.w - this._particle.uvs.y) + this._particle.uvs.y;
671+
}
642672
}
643673
}
674+
675+
// increment indexes for the next particle
644676
index = idx + 3;
645677
colorIndex = colidx + 4;
646678
uvIndex = uvidx + 2;
647679
}
648680

681+
// if the VBO must be updated
649682
if (update) {
650683
if (this._computeParticleColor) {
651684
this.mesh.updateVerticesData(VertexBuffer.ColorKind, this._colors32, false, false);
@@ -656,7 +689,7 @@
656689
this.mesh.updateVerticesData(VertexBuffer.PositionKind, this._positions32, false, false);
657690
if (!this.mesh.areNormalsFrozen) {
658691
if (this._computeParticleVertex || this.billboard) {
659-
// recompute the normals only if the particles can be morphed, update then the normal reference array
692+
// recompute the normals only if the particles can be morphed, update then also the normal reference array _fixedNormal32[]
660693
VertexData.ComputeNormals(this._positions32, this._indices, this._normals32);
661694
for (var i = 0; i < this._normals32.length; i++) {
662695
this._fixedNormal32[i] = this._normals32[i];

0 commit comments

Comments
 (0)