Source: components/CameraBoundingComponent.js

import { CameraFrustum } from '@uino/base-thing';
import { MathUtils } from '../math/MathUtils';
import { BoundingComponent } from './BoundingComponent';

const __ = {
	private: Symbol('private'),
}

let _mat4_cameraMatrixWorld = MathUtils.createMat4();
let _mat4_projectionMatrix = MathUtils.createMat4();

/**
 * @class CameraBoundingComponent
 * The camera bounding box component.
 * @memberof THING
 * @extends THING.BoundingComponent
 */
class CameraBoundingComponent extends BoundingComponent {

	/**
	 * The bounding of camera, it can work with light shadow effect system.
	 */
	constructor() {
		super();

		this[__.private] = {};
		let _private = this[__.private];

		_private.distanceFactor = 2;

		_private.frustum = new CameraFrustum();
	}

	/**
	 * Get/Set distance factor of bounding sphere, it can affects light's shadow range.
	 * @type {Number}
	 * @private
	 */
	get distanceFactor() {
		let _private = this[__.private];

		return _private.distanceFactor;
	}
	set distanceFactor(value) {
		let _private = this[__.private];

		_private.distanceFactor = value;
	}

	/**
	 * Get/Set light sphere info.
	 * @type {LightSphereInfo}
	 * @private
	 */
	get lightSphere() {
		let _private = this[__.private];

		let camera = this.object;
		let cameraNode = camera.node;

		cameraNode.getMatrixWorld(_mat4_cameraMatrixWorld);
		cameraNode.getProjectionMatrix(_mat4_projectionMatrix);

		let distance = camera.distance * _private.distanceFactor;

		_private.frustum.setFromProjectionMatrix(_mat4_projectionMatrix, distance);
		let boundingSphere = _private.frustum.getBoundingSphere(_mat4_cameraMatrixWorld);

		return {
			center: boundingSphere.center,
			radius: boundingSphere.radius,
		};
	}

	// #endregion

}

export { CameraBoundingComponent }