UNPKG

playcanvas

Version:

Open-source WebGL/WebGPU 3D engine for the web

71 lines (70 loc) 1.84 kB
import { Plane } from "./plane.js"; class Frustum { planes = []; constructor() { for (let i = 0; i < 6; i++) { this.planes[i] = new Plane(); } } clone() { const cstr = this.constructor; return new cstr().copy(this); } copy(src) { for (let i = 0; i < 6; i++) { this.planes[i].copy(src.planes[i]); } return this; } setFromMat4(matrix) { const d = matrix.data; const m00 = d[0], m01 = d[1], m02 = d[2], m03 = d[3]; const m10 = d[4], m11 = d[5], m12 = d[6], m13 = d[7]; const m20 = d[8], m21 = d[9], m22 = d[10], m23 = d[11]; const m30 = d[12], m31 = d[13], m32 = d[14], m33 = d[15]; const planes = this.planes; planes[0].set(m03 - m00, m13 - m10, m23 - m20, m33 - m30).normalize(); planes[1].set(m03 + m00, m13 + m10, m23 + m20, m33 + m30).normalize(); planes[2].set(m03 + m01, m13 + m11, m23 + m21, m33 + m31).normalize(); planes[3].set(m03 - m01, m13 - m11, m23 - m21, m33 - m31).normalize(); planes[4].set(m03 - m02, m13 - m12, m23 - m22, m33 - m32).normalize(); planes[5].set(m03 + m02, m13 + m12, m23 + m22, m33 + m32).normalize(); } containsPoint(point) { for (let p = 0; p < 6; p++) { const { normal, distance } = this.planes[p]; if (normal.dot(point) + distance <= 0) { return false; } } return true; } add(other) { const planes = this.planes; const otherPlanes = other.planes; for (let p = 0; p < 6; p++) { if (otherPlanes[p].distance > planes[p].distance) { planes[p].copy(otherPlanes[p]); } } return this; } containsSphere(sphere) { const { center, radius } = sphere; let c = 0; for (let p = 0; p < 6; p++) { const { normal, distance } = this.planes[p]; const d = normal.dot(center) + distance; if (d <= -radius) { return 0; } if (d > radius) { c++; } } return c === 6 ? 2 : 1; } } export { Frustum };