Skip to content

Commit

Permalink
test(particles.cloudPoint): add tests for intersectsMesh function (#1…
Browse files Browse the repository at this point in the history
  • Loading branch information
Dok11 authored Sep 19, 2022
1 parent ef0d2e1 commit d0d52bd
Show file tree
Hide file tree
Showing 3 changed files with 317 additions and 37 deletions.
38 changes: 19 additions & 19 deletions packages/dev/core/src/Particles/cloudPoint.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,29 +149,29 @@ export class CloudPoint {
if (!target.hasBoundingInfo) {
return false;
}
isSphere = isSphere ? isSphere : false;

if (!this._pcs.mesh) {
throw new Error("Point Cloud System doesnt contain the Mesh");
}

if (isSphere) {
return target.getBoundingInfo().boundingSphere.intersectsPoint(this.position.add(this._pcs.mesh.position));
} else {
let maxX = 0;
let minX = 0;
let maxY = 0;
let minY = 0;
let maxZ = 0;
let minZ = 0;
maxX = target.getBoundingInfo().boundingBox.maximumWorld.x;
minX = target.getBoundingInfo().boundingBox.minimumWorld.x;
maxY = target.getBoundingInfo().boundingBox.maximumWorld.y;
minY = target.getBoundingInfo().boundingBox.minimumWorld.y;
maxZ = target.getBoundingInfo().boundingBox.maximumWorld.z;
minZ = target.getBoundingInfo().boundingBox.minimumWorld.z;

const x = this.position.x + this._pcs.mesh.position.x;
const y = this.position.y + this._pcs.mesh.position.y;
const z = this.position.z + this._pcs.mesh.position.z;
return minX <= x && x <= maxX && minY <= y && y <= maxY && minZ <= z && z <= maxZ;
}

const bbox = target.getBoundingInfo().boundingBox;

const maxX = bbox.maximumWorld.x;
const minX = bbox.minimumWorld.x;
const maxY = bbox.maximumWorld.y;
const minY = bbox.minimumWorld.y;
const maxZ = bbox.maximumWorld.z;
const minZ = bbox.minimumWorld.z;

const x = this.position.x + this._pcs.mesh.position.x;
const y = this.position.y + this._pcs.mesh.position.y;
const z = this.position.z + this._pcs.mesh.position.z;

return minX <= x && x <= maxX && minY <= y && y <= maxY && minZ <= z && z <= maxZ;
}

/**
Expand Down
46 changes: 28 additions & 18 deletions packages/dev/core/src/Particles/pointsCloudSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export class PointsCloudSystem implements IDisposable {
/**
* The PCS mesh. It's a standard BJS Mesh, so all the methods from the Mesh class are available.
*/
public mesh: Mesh;
public mesh?: Mesh;
/**
* This empty object is intended to store some PCS specific or temporary values in order to lower the Garbage Collector activity.
* Please read :
Expand Down Expand Up @@ -765,15 +765,15 @@ export class PointsCloudSystem implements IDisposable {
Matrix.IdentityToRef(rotMatrix);
let idx = 0; // current index of the particle

if (this.mesh.isFacetDataEnabled) {
if (this.mesh?.isFacetDataEnabled) {
this._computeBoundingBox = true;
}

end = end >= this.nbParticles ? this.nbParticles - 1 : end;
if (this._computeBoundingBox) {
if (start != 0 || end != this.nbParticles - 1) {
// only some particles are updated, then use the current existing BBox basis. Note : it can only increase.
const boundingInfo = this.mesh.getBoundingInfo();
const boundingInfo = this.mesh?.getBoundingInfo();
if (boundingInfo) {
minimum.copyFrom(boundingInfo.minimum);
maximum.copyFrom(boundingInfo.maximum);
Expand Down Expand Up @@ -907,21 +907,23 @@ export class PointsCloudSystem implements IDisposable {
}

// if the VBO must be updated
if (update) {
if (this._computeParticleColor) {
mesh.updateVerticesData(VertexBuffer.ColorKind, colors32, false, false);
}
if (this._computeParticleTexture) {
mesh.updateVerticesData(VertexBuffer.UVKind, uvs32, false, false);
if (mesh) {
if (update) {
if (this._computeParticleColor) {
mesh.updateVerticesData(VertexBuffer.ColorKind, colors32, false, false);
}
if (this._computeParticleTexture) {
mesh.updateVerticesData(VertexBuffer.UVKind, uvs32, false, false);
}
mesh.updateVerticesData(VertexBuffer.PositionKind, positions32, false, false);
}
mesh.updateVerticesData(VertexBuffer.PositionKind, positions32, false, false);
}

if (this._computeBoundingBox) {
if (mesh.hasBoundingInfo) {
mesh.getBoundingInfo().reConstruct(minimum, maximum, mesh._worldMatrix);
} else {
mesh.buildBoundingInfo(minimum, maximum, mesh._worldMatrix);
if (this._computeBoundingBox) {
if (mesh.hasBoundingInfo) {
mesh.getBoundingInfo().reConstruct(minimum, maximum, mesh._worldMatrix);
} else {
mesh.buildBoundingInfo(minimum, maximum, mesh._worldMatrix);
}
}
}
this.afterUpdateParticles(start, end, update);
Expand All @@ -932,7 +934,7 @@ export class PointsCloudSystem implements IDisposable {
* Disposes the PCS.
*/
public dispose(): void {
this.mesh.dispose();
this.mesh?.dispose();
this.vars = null;
// drop references to internal big arrays for the GC
(<any>this._positions) = null;
Expand All @@ -953,7 +955,7 @@ export class PointsCloudSystem implements IDisposable {
*/
public refreshVisibleSize(): PointsCloudSystem {
if (!this._isVisibilityBoxLocked) {
this.mesh.refreshBoundingInfo();
this.mesh?.refreshBoundingInfo();
}
return this;
}
Expand All @@ -965,6 +967,10 @@ export class PointsCloudSystem implements IDisposable {
* doc :
*/
public setVisibilityBox(size: number): void {
if (!this.mesh) {
return;
}

const vis = size / 2;
this.mesh.buildBoundingInfo(new Vector3(-vis, -vis, -vis), new Vector3(vis, vis, vis));
}
Expand All @@ -982,6 +988,10 @@ export class PointsCloudSystem implements IDisposable {
* doc :
*/
public set isAlwaysVisible(val: boolean) {
if (!this.mesh) {
return;
}

this._alwaysVisible = val;
this.mesh.alwaysSelectAsActiveMesh = val;
}
Expand Down
Loading

0 comments on commit d0d52bd

Please sign in to comment.