UNPKG

@woosh/meep-engine

Version:

Pure JavaScript game engine. Fully featured and production ready.

63 lines (54 loc) 2.1 kB
import { lerp } from "../../../math/lerp.js"; import Vector3 from "../../Vector3.js"; import { plane3_intersect_plane } from "./plane3_intersect_plane.js"; import { v3_dot } from "../../vec3/v3_dot.js"; import { v3_length } from "../../vec3/v3_length.js"; const v0 = new Vector3(); const v1 = new Vector3(); /** * TODO this produces increasingly incorrect when planes diverge * Interpolate between two planes in linear space * @param {number[]} destination * @param {number} destination_offset * @param {number} a_normal_x * @param {number} a_normal_y * @param {number} a_normal_z * @param {number} a_constant * @param {number} b_normal_x * @param {number} b_normal_y * @param {number} b_normal_z * @param {number} b_constant * @param {number} t */ export function plane3_lerp( destination, destination_offset, a_normal_x, a_normal_y, a_normal_z, a_constant, b_normal_x, b_normal_y, b_normal_z, b_constant, t ) { // compute result normal let normal_x = lerp(a_normal_x, b_normal_x, t); let normal_y = lerp(a_normal_y, b_normal_y, t); let normal_z = lerp(a_normal_z, b_normal_z, t); // normalize the normal vector const normal_length = v3_length(normal_x, normal_y, normal_z); const normal_length_inv = 1 / normal_length; normal_x *= normal_length_inv; normal_y *= normal_length_inv; normal_z *= normal_length_inv; // compute 2-plane intersection const intersection_exists = plane3_intersect_plane(v0, v1, a_normal_x, a_normal_y, a_normal_z, a_constant, b_normal_x, b_normal_y, b_normal_z, b_constant, ); if (!intersection_exists) { // special case, planes are parallel // TODO implement handling } const r_constant = -v3_dot(v0.x, v0.y, v0.z, normal_x, normal_y, normal_z); // write result destination[destination_offset] = normal_x; destination[destination_offset + 1] = normal_y; destination[destination_offset + 2] = normal_z; destination[destination_offset + 3] = r_constant; }