@openhps/core
Version:
Open Hybrid Positioning System - Core component
87 lines (82 loc) • 3.69 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.rotate = exports.default = void 0;
var _TempNode = _interopRequireDefault(require("../core/TempNode.js"));
var _TSLBase = require("../tsl/TSLBase.js");
var _MathNode = require("../math/MathNode.js");
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
/**
* Applies a rotation to the given position node.
*
* @augments TempNode
*/
class RotateNode extends _TempNode.default {
static get type() {
return 'RotateNode';
}
/**
* Constructs a new rotate node.
*
* @param {Node} positionNode - The position node.
* @param {Node} rotationNode - Represents the rotation that is applied to the position node. Depending
* on whether the position data are 2D or 3D, the rotation is expressed a single float value or an Euler value.
*/
constructor(positionNode, rotationNode) {
super();
/**
* The position node.
*
* @type {Node}
*/
this.positionNode = positionNode;
/**
* Represents the rotation that is applied to the position node.
* Depending on whether the position data are 2D or 3D, the rotation is expressed a single float value or an Euler value.
*
* @type {Node}
*/
this.rotationNode = rotationNode;
}
/**
* The type of the {@link RotateNode#positionNode} defines the node's type.
*
* @param {NodeBuilder} builder - The current node builder.
* @return {string} The node's type.
*/
getNodeType(builder) {
return this.positionNode.getNodeType(builder);
}
setup(builder) {
const {
rotationNode,
positionNode
} = this;
const nodeType = this.getNodeType(builder);
if (nodeType === 'vec2') {
const cosAngle = rotationNode.cos();
const sinAngle = rotationNode.sin();
const rotationMatrix = (0, _TSLBase.mat2)(cosAngle, sinAngle, sinAngle.negate(), cosAngle);
return rotationMatrix.mul(positionNode);
} else {
const rotation = rotationNode;
const rotationXMatrix = (0, _TSLBase.mat4)((0, _TSLBase.vec4)(1.0, 0.0, 0.0, 0.0), (0, _TSLBase.vec4)(0.0, (0, _MathNode.cos)(rotation.x), (0, _MathNode.sin)(rotation.x).negate(), 0.0), (0, _TSLBase.vec4)(0.0, (0, _MathNode.sin)(rotation.x), (0, _MathNode.cos)(rotation.x), 0.0), (0, _TSLBase.vec4)(0.0, 0.0, 0.0, 1.0));
const rotationYMatrix = (0, _TSLBase.mat4)((0, _TSLBase.vec4)((0, _MathNode.cos)(rotation.y), 0.0, (0, _MathNode.sin)(rotation.y), 0.0), (0, _TSLBase.vec4)(0.0, 1.0, 0.0, 0.0), (0, _TSLBase.vec4)((0, _MathNode.sin)(rotation.y).negate(), 0.0, (0, _MathNode.cos)(rotation.y), 0.0), (0, _TSLBase.vec4)(0.0, 0.0, 0.0, 1.0));
const rotationZMatrix = (0, _TSLBase.mat4)((0, _TSLBase.vec4)((0, _MathNode.cos)(rotation.z), (0, _MathNode.sin)(rotation.z).negate(), 0.0, 0.0), (0, _TSLBase.vec4)((0, _MathNode.sin)(rotation.z), (0, _MathNode.cos)(rotation.z), 0.0, 0.0), (0, _TSLBase.vec4)(0.0, 0.0, 1.0, 0.0), (0, _TSLBase.vec4)(0.0, 0.0, 0.0, 1.0));
return rotationXMatrix.mul(rotationYMatrix).mul(rotationZMatrix).mul((0, _TSLBase.vec4)(positionNode, 1.0)).xyz;
}
}
}
var _default = exports.default = RotateNode;
/**
* TSL function for creating a rotate node.
*
* @tsl
* @function
* @param {Node} positionNode - The position node.
* @param {Node} rotationNode - Represents the rotation that is applied to the position node. Depending
* on whether the position data are 2D or 3D, the rotation is expressed a single float value or an Euler value.
* @returns {RotateNode}
*/
const rotate = exports.rotate = /*@__PURE__*/(0, _TSLBase.nodeProxy)(RotateNode).setParameterLength(2);