playcanvas
Version:
Open-source WebGL/WebGPU 3D engine for the web
105 lines (104 loc) • 3.3 kB
JavaScript
import { Quat } from "../../../core/math/quat.js";
import { Vec3 } from "../../../core/math/vec3.js";
import { Entity } from "../../../framework/entity.js";
import { ConeGeometry } from "../../../scene/geometry/cone-geometry.js";
import { CylinderGeometry } from "../../../scene/geometry/cylinder-geometry.js";
import { Mesh } from "../../../scene/mesh.js";
import { TriData } from "../tri-data.js";
import { Shape } from "./shape.js";
const tmpV1 = new Vec3();
const tmpV2 = new Vec3();
const tmpQ1 = new Quat();
class ArrowShape extends Shape {
_gap = 0;
_lineThickness = 0.02;
_lineLength = 0.5;
_arrowThickness = 0.12;
_arrowLength = 0.18;
_tolerance = 0.1;
_head;
_line;
constructor(device, args = {}) {
super(device, "arrow", args);
this._gap = args.gap ?? this._gap;
this._lineThickness = args.lineThickness ?? this._lineThickness;
this._lineLength = args.lineLength ?? this._lineLength;
this._arrowThickness = args.arrowThickness ?? this._arrowThickness;
this._arrowLength = args.arrowLength ?? this._arrowLength;
this._tolerance = args.tolerance ?? this._tolerance;
this.triData = [
new TriData(new ConeGeometry()),
new TriData(new CylinderGeometry(), 1)
];
this._head = new Entity(`head:${this.axis}`);
this.entity.addChild(this._head);
this._createRenderComponent(this._head, [
Mesh.fromGeometry(this.device, new ConeGeometry())
]);
this._line = new Entity(`line:${this.axis}`);
this.entity.addChild(this._line);
this._createRenderComponent(this._line, [
Mesh.fromGeometry(this.device, new CylinderGeometry())
]);
this._update();
}
set gap(value) {
this._gap = value ?? this._gap;
this._update();
}
get gap() {
return this._gap;
}
set lineThickness(value) {
this._lineThickness = value ?? this._lineThickness;
this._update();
}
get lineThickness() {
return this._lineThickness;
}
set lineLength(value) {
this._lineLength = value ?? this._lineLength;
this._update();
}
get lineLength() {
return this._lineLength;
}
set arrowThickness(value) {
this._arrowThickness = value ?? this._arrowThickness;
this._update();
}
get arrowThickness() {
return this._arrowThickness;
}
set arrowLength(value) {
this._arrowLength = value ?? this._arrowLength;
this._update();
}
get arrowLength() {
return this._arrowLength;
}
set tolerance(value) {
this._tolerance = value;
this._update();
}
get tolerance() {
return this._tolerance;
}
_update() {
tmpV1.set(0, this._gap + this._arrowLength * 0.5 + this._lineLength, 0);
tmpQ1.set(0, 0, 0, 1);
tmpV2.set(this._arrowThickness, this._arrowLength, this._arrowThickness);
this.triData[0].setTransform(tmpV1, tmpQ1, tmpV2);
tmpV1.set(0, this._gap + this._lineLength * 0.5, 0);
tmpQ1.set(0, 0, 0, 1);
tmpV2.set(this._lineThickness + this._tolerance, this._lineLength, this._lineThickness + this._tolerance);
this.triData[1].setTransform(tmpV1, tmpQ1, tmpV2);
this._head.setLocalPosition(0, this._gap + this._arrowLength * 0.5 + this._lineLength, 0);
this._head.setLocalScale(this._arrowThickness, this._arrowLength, this._arrowThickness);
this._line.setLocalPosition(0, this._gap + this._lineLength * 0.5, 0);
this._line.setLocalScale(this._lineThickness, this._lineLength, this._lineThickness);
}
}
export {
ArrowShape
};