MCPcopy
hub / github.com/spritejs/spritejs / slerp

Function slerp

docs/demo/spritejs.esm.js:4334–4377  ·  view source on GitHub ↗

* Performs a spherical linear interpolation between two quat * * @param {quat} out the receiving quaternion * @param {ReadonlyQuat} a the first operand * @param {ReadonlyQuat} b the second operand * @param {Number} t interpolation amount, in the range [0-1], between the two inputs * @ret

(out, a, b, t)

Source from the content-addressed store, hash-verified

4332 */
4333
4334function slerp(out, a, b, t) {
4335 // benchmarks:
4336 // http://jsperf.com/quaternion-slerp-implementations
4337 var ax = a[0],
4338 ay = a[1],
4339 az = a[2],
4340 aw = a[3];
4341 var bx = b[0],
4342 by = b[1],
4343 bz = b[2],
4344 bw = b[3];
4345 var omega, cosom, sinom, scale0, scale1; // calc cosine
4346
4347 cosom = ax * bx + ay * by + az * bz + aw * bw; // adjust signs (if necessary)
4348
4349 if (cosom < 0.0) {
4350 cosom = -cosom;
4351 bx = -bx;
4352 by = -by;
4353 bz = -bz;
4354 bw = -bw;
4355 } // calculate coefficients
4356
4357
4358 if (1.0 - cosom > _common_js__WEBPACK_IMPORTED_MODULE_0__["EPSILON"]) {
4359 // standard case (slerp)
4360 omega = Math.acos(cosom);
4361 sinom = Math.sin(omega);
4362 scale0 = Math.sin((1.0 - t) * omega) / sinom;
4363 scale1 = Math.sin(t * omega) / sinom;
4364 } else {
4365 // "from" and "to" quaternions are very close
4366 // ... so we can do a linear interpolation
4367 scale0 = 1.0 - t;
4368 scale1 = t;
4369 } // calculate final values
4370
4371
4372 out[0] = scale0 * ax + scale1 * bx;
4373 out[1] = scale0 * ay + scale1 * by;
4374 out[2] = scale0 * az + scale1 * bz;
4375 out[3] = scale0 * aw + scale1 * bw;
4376 return out;
4377}
4378/**
4379 * Generates a random unit quaternion
4380 *

Callers 1

spritejs.esm.jsFile · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected