Skip to content

Commit

Permalink
fix mesh contains point if there is no index buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
GoodBoyDigital committed Apr 30, 2024
1 parent ff67a1d commit 2d8241f
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 20 deletions.
42 changes: 42 additions & 0 deletions src/maths/point/pointInTriangle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/**
* Check if a point is inside a triangle.
* @param px - x coordinate of the point
* @param py - y coordinate of the point
* @param x1 - x coordinate of the first vertex of the triangle
* @param y1 - y coordinate of the first vertex of the triangle
* @param x2 - x coordinate of the second vertex of the triangle
* @param y2 - y coordinate of the second vertex of the triangle
* @param x3 - x coordinate of the third vertex of the triangle
* @param y3 - y coordinate of the third vertex of the triangle
* @returns `true` if the point is inside the triangle, `false` otherwise
*/
export function pointInTriangle(
px: number, py: number,
x1: number, y1: number,
x2: number, y2: number,
x3: number, y3: number
)
{
// Calculate vectors from point p to each vertex of the triangle
const v2x = x3 - x1;
const v2y = y3 - y1;
const v1x = x2 - x1;
const v1y = y2 - y1;
const v0x = px - x1;
const v0y = py - y1;

// Compute dot products
const dot00 = (v2x * v2x) + (v2y * v2y);
const dot01 = (v2x * v1x) + (v2y * v1y);
const dot02 = (v2x * v0x) + (v2y * v0y);
const dot11 = (v1x * v1x) + (v1y * v1y);
const dot12 = (v1x * v0x) + (v1y * v0y);

// Calculate barycentric coordinates
const invDenom = 1 / ((dot00 * dot11) - (dot01 * dot01));
const u = ((dot11 * dot02) - (dot01 * dot12)) * invDenom;
const v = ((dot00 * dot12) - (dot01 * dot02)) * invDenom;

// Check if point is in triangle
return (u >= 0) && (v >= 0) && (u + v < 1);
}
66 changes: 46 additions & 20 deletions src/scene/mesh/shared/Mesh.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Polygon } from '../../../maths/shapes/Polygon';
import { pointInTriangle } from '../../../maths/point/pointInTriangle';
import { Geometry } from '../../../rendering/renderers/shared/geometry/Geometry';
import { State } from '../../../rendering/renderers/shared/state/State';
import { Texture } from '../../../rendering/renderers/shared/texture/Texture';
Expand All @@ -15,8 +15,6 @@ import type { Bounds } from '../../container/bounds/Bounds';
import type { ContainerOptions } from '../../container/Container';
import type { DestroyOptions } from '../../container/destroyTypes';

const tempPolygon = new Polygon();

export interface TextureShader extends Shader
{
texture: Texture;
Expand Down Expand Up @@ -280,27 +278,55 @@ export class Mesh<

const vertices = this.geometry.getBuffer('aPosition').data;

const points = tempPolygon.points;
const indices = this.geometry.getIndex().data;
const len = indices.length;
const step = this.geometry.topology === 'triangle-strip' ? 3 : 1;

for (let i = 0; i + 2 < len; i += step)
if (this.geometry.getIndex())
{
const ind0 = indices[i] * 2;
const ind1 = indices[i + 1] * 2;
const ind2 = indices[i + 2] * 2;

points[0] = vertices[ind0];
points[1] = vertices[ind0 + 1];
points[2] = vertices[ind1];
points[3] = vertices[ind1 + 1];
points[4] = vertices[ind2];
points[5] = vertices[ind2 + 1];

if (tempPolygon.contains(x, y))
const indices = this.geometry.getIndex().data;
const len = indices.length;

for (let i = 0; i + 2 < len; i += step)
{
const ind0 = indices[i] * 2;
const ind1 = indices[i + 1] * 2;
const ind2 = indices[i + 2] * 2;

if (pointInTriangle(
x, y,
vertices[ind0],
vertices[ind0 + 1],
vertices[ind1],
vertices[ind1 + 1],
vertices[ind2],
vertices[ind2 + 1],
))
{
return true;
}
}
}
else
{
const len = vertices.length / 2; // Each vertex has 2 coordinates, x and y

for (let i = 0; i + 2 < len; i += step)
{
return true;
const ind0 = i * 2;
const ind1 = (i + 1) * 2;
const ind2 = (i + 2) * 2;

if (pointInTriangle(
x, y,
vertices[ind0],
vertices[ind0 + 1],
vertices[ind1],
vertices[ind1 + 1],
vertices[ind2],
vertices[ind2 + 1],
))
{
return true;
}
}
}

Expand Down

0 comments on commit 2d8241f

Please sign in to comment.