UNPKG

@babylonjs/core

Version:

Getting started? Play directly with the Babylon.js API using our [playground](https://playground.babylonjs.com/). It also contains a lot of samples to learn how to use it.

156 lines 5.13 kB
import { Matrix, Quaternion, Vector3 } from "../../../Maths/math.vector.js"; import { _SpatialAudioSubNode } from "../../abstractAudio/subNodes/spatialAudioSubNode.js"; import { _SpatialAudioDefaults } from "../../abstractAudio/subProperties/abstractSpatialAudio.js"; const TmpMatrix = Matrix.Zero(); const TmpQuaternion = new Quaternion(); const TmpVector = Vector3.Zero(); function d2r(degrees) { return (degrees * Math.PI) / 180; } function r2d(radians) { return (radians * 180) / Math.PI; } /** @internal */ export async function _CreateSpatialAudioSubNodeAsync(engine) { return new _SpatialWebAudioSubNode(engine); } /** @internal */ export class _SpatialWebAudioSubNode extends _SpatialAudioSubNode { /** @internal */ constructor(engine) { super(engine); this._lastPosition = Vector3.Zero(); this._lastRotation = Vector3.Zero(); this._lastRotationQuaternion = new Quaternion(); /** @internal */ this.position = _SpatialAudioDefaults.position.clone(); /** @internal */ this.rotation = _SpatialAudioDefaults.rotation.clone(); /** @internal */ this.rotationQuaternion = _SpatialAudioDefaults.rotationQuaternion.clone(); this.node = new PannerNode(engine._audioContext); } /** @internal */ get coneInnerAngle() { return d2r(this.node.coneInnerAngle); } set coneInnerAngle(value) { this.node.coneInnerAngle = r2d(value); } /** @internal */ get coneOuterAngle() { return d2r(this.node.coneOuterAngle); } set coneOuterAngle(value) { this.node.coneOuterAngle = r2d(value); } /** @internal */ get coneOuterVolume() { return this.node.coneOuterGain; } set coneOuterVolume(value) { this.node.coneOuterGain = value; } /** @internal */ get distanceModel() { return this.node.distanceModel; } set distanceModel(value) { this.node.distanceModel = value; // Wiggle the max distance to make the change take effect. const maxDistance = this.node.maxDistance; this.node.maxDistance = maxDistance + 0.001; this.node.maxDistance = maxDistance; } /** @internal */ get minDistance() { return this.node.refDistance; } set minDistance(value) { this.node.refDistance = value; } /** @internal */ get maxDistance() { return this.node.maxDistance; } set maxDistance(value) { this.node.maxDistance = value; } /** @internal */ get panningModel() { return this.node.panningModel; } set panningModel(value) { this.node.panningModel = value; } /** @internal */ get rolloffFactor() { return this.node.rolloffFactor; } set rolloffFactor(value) { this.node.rolloffFactor = value; } /** @internal */ get _inNode() { return this.node; } /** @internal */ get _outNode() { return this.node; } /** @internal */ _updatePosition() { if (this._lastPosition.equalsWithEpsilon(this.position)) { return; } this.engine._setAudioParam(this.node.positionX, this.position.x); this.engine._setAudioParam(this.node.positionY, this.position.y); this.engine._setAudioParam(this.node.positionZ, this.position.z); this._lastPosition.copyFrom(this.position); } /** @internal */ _updateRotation() { if (!this._lastRotationQuaternion.equalsWithEpsilon(this.rotationQuaternion)) { TmpQuaternion.copyFrom(this.rotationQuaternion); this._lastRotationQuaternion.copyFrom(this.rotationQuaternion); } else if (!this._lastRotation.equalsWithEpsilon(this.rotation)) { Quaternion.FromEulerAnglesToRef(this.rotation.x, this.rotation.y, this.rotation.z, TmpQuaternion); this._lastRotation.copyFrom(this.rotation); } else { return; } Matrix.FromQuaternionToRef(TmpQuaternion, TmpMatrix); Vector3.TransformNormalToRef(Vector3.RightReadOnly, TmpMatrix, TmpVector); this.engine._setAudioParam(this.node.orientationX, TmpVector.x); this.engine._setAudioParam(this.node.orientationY, TmpVector.y); this.engine._setAudioParam(this.node.orientationZ, TmpVector.z); } _connect(node) { const connected = super._connect(node); if (!connected) { return false; } // If the wrapped node is not available now, it will be connected later by the subgraph. if (node._inNode) { this.node.connect(node._inNode); } return true; } _disconnect(node) { const disconnected = super._disconnect(node); if (!disconnected) { return false; } if (node._inNode) { this.node.disconnect(node._inNode); } return true; } /** @internal */ getClassName() { return "_SpatialWebAudioSubNode"; } } //# sourceMappingURL=spatialWebAudioSubNode.js.map