@animech-public/playcanvas
Version:
PlayCanvas WebGL game engine
64 lines (60 loc) • 1.78 kB
JavaScript
import { Vec3 } from '../../../core/math/vec3.js';
import { CULLFACE_NONE } from '../../../platform/graphics/constants.js';
import { PlaneGeometry } from '../../../scene/geometry/plane-geometry.js';
import { TriData } from '../tri-data.js';
import { Shape } from './shape.js';
const UPDATE_EPSILON = 1e-6;
class PlaneShape extends Shape {
constructor(device, options = {}) {
super(device, options);
this._cull = CULLFACE_NONE;
this._size = 0.2;
this._gap = 0.1;
this._flipped = new Vec3();
this.triData = [new TriData(new PlaneGeometry())];
this._createPlane();
}
set size(value) {
this._size = value != null ? value : 1;
this._updateTransform();
}
get size() {
return this._size;
}
set gap(value) {
this._gap = value != null ? value : 0;
this._updateTransform();
}
get gap() {
return this._gap;
}
set flipped(value) {
if (this._flipped.distance(value) < UPDATE_EPSILON) {
return;
}
this._flipped.copy(value);
this.entity.setLocalPosition(this._getPosition());
}
get flipped() {
return this._flipped;
}
_getPosition() {
const offset = this._size / 2 + this._gap;
const position = new Vec3(this._flipped.x ? -offset : offset, this._flipped.y ? -offset : offset, this._flipped.z ? -offset : offset);
position[this.axis] = 0;
return position;
}
_createPlane() {
this._createRoot('plane');
this._updateTransform();
// plane
this._addRenderMesh(this.entity, 'plane', this._shading);
}
_updateTransform() {
// intersect/render
this.entity.setLocalPosition(this._getPosition());
this.entity.setLocalEulerAngles(this._rotation);
this.entity.setLocalScale(this._size, this._size, this._size);
}
}
export { PlaneShape };