# Tri

Class · category: Math

Source: https://github.com/playcanvas/engine/blob/970d89f6d3bc6667f4e1f88153abd6e7e82ed52c/src/core/shape/tri.js#L32

A triangle defined by three [Vec3](https://api.playcanvas.com/engine/classes/Vec3.md) vectors.

The vertices are [v0](https://api.playcanvas.com/engine/classes/Tri.md#v0), [v1](https://api.playcanvas.com/engine/classes/Tri.md#v1) and [v2](https://api.playcanvas.com/engine/classes/Tri.md#v2); the constructor and [set](https://api.playcanvas.com/engine/classes/Tri.md#set) copy the
vectors they are given. [intersectsRay](https://api.playcanvas.com/engine/classes/Tri.md#intersectsray) tests a [Ray](https://api.playcanvas.com/engine/classes/Ray.md) whose direction is normalized
against either face of the triangle and writes the hit point into an optional vector. It is the
building block for precise picking against mesh geometry.

**Example**

```ts
const tri = new Tri(new Vec3(0, 0, 0), new Vec3(1, 0, 0), new Vec3(0, 1, 0));
const hit = new Vec3();
if (tri.intersectsRay(ray, hit)) {
    console.log(`Hit the triangle at ${hit}`);
}
```

## Constructors

### constructor

```ts
new Tri(v0?: Vec3, v1?: Vec3, v2?: Vec3)
```

Creates a new Tri object.

**Parameters**

- `v0` ([`Vec3`](https://api.playcanvas.com/engine/classes/Vec3.md), optional, default `Vec3.ZERO`): The first 3-dimensional vector.
- `v1` ([`Vec3`](https://api.playcanvas.com/engine/classes/Vec3.md), optional, default `Vec3.ZERO`): The second 3-dimensional vector.
- `v2` ([`Vec3`](https://api.playcanvas.com/engine/classes/Vec3.md), optional, default `Vec3.ZERO`): The third 3-dimensional vector.

**Example**

```ts
const v0 = new Vec3(1, 0, 0);
const v1 = new Vec3(0, 1, 0);
const v2 = new Vec3(2, 2, 1);
const t = new Tri(v0, v1, v2);
```

## Properties

### v0

```ts
readonly v0: Vec3
```

The first 3-dimensional vector of the triangle.

### v1

```ts
readonly v1: Vec3
```

The second 3-dimensional vector of the triangle.

### v2

```ts
readonly v2: Vec3
```

The third 3-dimensional vector of the triangle.

## Methods

### intersectsRay

```ts
intersectsRay(ray: Ray, point?: Vec3): boolean
```

Test if a ray intersects with the triangle.

**Parameters**

- `ray` ([`Ray`](https://api.playcanvas.com/engine/classes/Ray.md)): Ray to test against (direction must be normalized).
- `point` ([`Vec3`](https://api.playcanvas.com/engine/classes/Vec3.md), optional): If there is an intersection, the intersection point will be copied
  into here.

**Returns** `boolean`: True if there is an intersection.

### set

```ts
set(v0: Vec3, v1: Vec3, v2: Vec3): Tri
```

Sets the specified triangle to the supplied 3-dimensional vectors.

**Parameters**

- `v0` ([`Vec3`](https://api.playcanvas.com/engine/classes/Vec3.md)): The value set on the first 3-dimensional vector of the triangle.
- `v1` ([`Vec3`](https://api.playcanvas.com/engine/classes/Vec3.md)): The value set on the second 3-dimensional vector of the triangle.
- `v2` ([`Vec3`](https://api.playcanvas.com/engine/classes/Vec3.md)): The value set on the third 3-dimensional vector of the triangle.

**Returns** [`Tri`](https://api.playcanvas.com/engine/classes/Tri.md): Self for chaining.

**Example**

```ts
const t = new Tri(Vec3.UP, Vec3.RIGHT, Vec3.BACK);
const v0 = new Vec3(1, 0, 0);
const v1 = new Vec3(0, 1, 0);
const v2 = new Vec3(2, 2, 1);
t.set(v0, v1, v2);

// Outputs [[1, 0, 0], [0, 1, 0], [2, 2, 1]]
console.log("The result of the triangle set is: " + t.toString());
```

### toString

```ts
toString(): string
```

Converts the specified triangle to string form.

**Returns** `string`: The triangle in string form.

**Example**

```ts
const t = new Tri(Vec3.UP, Vec3.RIGHT, Vec3.BACK);
// Outputs [[0, 1, 0], [1, 0, 0], [0, 0, 1]]
console.log(t.toString());
```
