import { InheritType } from '../const';
import { Utils } from '../common/Utils';
import { MathUtils } from '../math/MathUtils';
import { BaseTickableObject3D } from './BaseTickableObject3D';
/**
* @class EffectGroundObject
* The effect ground object.
* @memberof THING
* @extends THING.BaseTickableObject3D
* @public
*/
export class EffectGroundObject extends BaseTickableObject3D {
/**
* The effect ground object.
* @param {Object} param The initial parameters.
*/
constructor(param = {}) {
super(param);
this._initParam(param);
}
/**
* Get type.
* @returns {String}
*/
getType() {
return 'EffectGroundObject';
}
_initParam(param) {
param.color = param.color && Utils.parseColor(param.color);
if (param.flowColor) {
param.flowColor = Utils.parseColor(param.flowColor);
param.flowColor.length = 3;
}
}
hasResource() {
return true;
}
/**
* When load reosurce.
* @param {Object} options The options to load.
* @param {Function} resolve The promise resolve callback function.
* @param {Function} reject The promise reject callback function.
* @private
*/
onLoadRenderableResource(options, resolve, reject) {
const node = Utils.createObject('EffectGroundObjectNode', { external: { uScene: this.node._uScene, param: options }});
this.level.config.ignoreStyle = true; // skip level outline
this._node = node;
this.body.setNode(node);
this.bounding.inheritType = InheritType.Jump;
this.updateGround(options.parent);
resolve();
}
onUpdate(deltaTime) {
if (this._node && !this._node.groundReflect) {
this._node.update(deltaTime);
}
}
/**
* Update ground parent object and calculation position.
* @param {Object3D} target Calculate and update the position and scale of effect ground based on the bounding box of the target.
* @public
* @example
* const building = app.query('.Building')[0];
* effectGround.updateGround(building);
*/
updateGround(target) {
if (target) {
this.parent = target;
const bbx = target.getOBB(true, false);
const radius = bbx.halfSize[1] + 0.2 - this._node.groundClearance;
const rDis = MathUtils.scaleVector(target.up, radius);
let pos = MathUtils.subVector(bbx.center, rDis);
this.position = [pos[0], pos[1], pos[2]];
let scaleFactor = MathUtils.scaleVector([bbx.radius * 2, 1, bbx.radius * 2], this._node.sizeFactor); // use box's diameter
this.scale = [scaleFactor[0], scaleFactor[1], scaleFactor[2]];
this.angles = target.angles;
}
else if (!target) {
this.position = [0, this._node.groundClearance, 0];
this.scale = [this._node.sizeFactor, 1, this._node.sizeFactor];
}
}
/**
* Set whether the effect ground is reflective.
* @param {Boolean}
* @public
*/
set groundReflect(value) {
this._node.groundReflect = value;
if (this.groundReflect) {
this.updateGround(this.parent);
}
}
/**
* get whether the effect ground is reflective.
* @type {Boolean}
* @public
*/
get groundReflect() {
return this._node.groundReflect;
}
/**
* Set size factor.
* @param {Number}
* @public
*/
set sizeFactor(value) {
this._node.sizeFactor = value;
this.updateGround(this.parent);
}
/**
* Get size factor.
* @type {Number}
* @public
*/
get sizeFactor() {
return this._node.sizeFactor;
}
/**
* Set ground image url.
* @param {String}
* @public
*/
set imageUrl(value) {
this._url = this._node.imageUrl = value;
}
/**
* Get ground image url.
* @type {String}
* @public
*/
get imageUrl() {
return this._node.imageUrl;
}
/**
* Set mask image url.
* @param {String}
* @public
*/
set maskUrl(value) {
this._node.maskUrl = value;
}
/**
* Get mask image url.
* @type {String}
* @public
*/
get maskUrl() {
return this._node.maskUrl;
}
/**
* Set opacity.
* @param {Number}
* @public
*/
set opacityValue(value) {
this._node.opacityValue = value;
}
/**
* Get opacity.
* @type {Number}
* @public
*/
get opacityValue() {
return this._node.opacityValue;
}
/**
* Set glow intensity factor.
* @param {Number}
* @public
*/
set glowFactorValue(value) {
this._node.glowFactorValue = value;
}
/**
* Get glow intensity factor.
* @type {Number}
* @public
*/
get glowFactorValue() {
return this._node.glowFactorValue;
}
/**
* Set texture UV repeat factor.
* @param {Number}
* @public
*/
set repeatFactorValue(value) {
this._node.repeatFactorValue = value;
}
/**
* Get texture UV repeat factor.
* @type {Number}
* @public
*/
get repeatFactorValue() {
return this._node.repeatFactorValue;
}
/**
* Set texture repeat factor on the U Axis.
* @param {Number}
* @public
*/
set repeatFactorValueX(value) {
this._node.repeatFactorValueX = value;
}
/**
* Get texture repeat factor on the U Axis.
* @type {Number}
* @public
*/
get repeatFactorValueX() {
return this._node.repeatFactorValueX;
}
/**
* Set texture repeat factor on the V Axis.
* @param {Number}
* @public
*/
set repeatFactorValueY(value) {
this._node.repeatFactorValueY = value;
}
/**
* Get texture repeat factor on the V Axis.
* @type {Number}
* @public
*/
get repeatFactorValueY() {
return this._node.repeatFactorValueY;
}
/**
* Set ground color.
* @param {String}
* @public
*/
set groundColorValue(value) {
const colorArray = Utils.parseColor(value);
this._node.groundColorValue = colorArray;
}
/**
* Get ground color.
* @type {String}
* @public
*/
get groundColorValue() {
const value = Utils.toColorHexString(this._node.groundColorValue);
return value;
}
/**
* Set flow color.
* @param {String}
* @public
*/
set flowColorValue(value) {
const colorArray = Utils.parseColor(value);
colorArray.length = 3;
this._node.flowColorValue = colorArray;
}
/**
* Get flow color.
* @type {String}
* @public
*/
get flowColorValue() {
const value = Utils.toColorHexString(this._node.flowColorValue);
return value;
}
/**
* Set flow animation speed.
* @param {Number}
* @public
*/
set animationSpeed(value) {
this._node.animationSpeed = value;
}
/**
* Get flow animation speed.
* @type {Number}
* @public
*/
get animationSpeed() {
return this._node.animationSpeed;
}
/**
* Set ground clearance.
* @param {Number}
* @public
*/
set groundClearance(value) {
this._node.groundClearance = value;
this.updateGround(this.parent);
}
/**
* Get ground clearance.
* @type {Number}
* @public
*/
get groundClearance() {
return this._node.groundClearance;
}
dispose() {
this._node.dispose();
}
copy(source) {
this._node.copy(source);
}
}