src/viewer/scene/geometry/builders/buildBoxLinesGeometry.js
import {utils} from '../../utils.js';
/**
* @desc Creates a box-shaped lines {@link Geometry}.
*
* ## Usage
*
* In the example below we'll create a {@link Mesh} with a box-shaped {@link ReadableGeometry} that has lines primitives.
*
* [[Run this example](http://xeokit.github.io/xeokit-sdk/examples/#geometry_builders_buildBoxLinesGeometry)]
*
* ````javascript
* import {Viewer, Mesh, buildBoxLinesGeometry, ReadableGeometry, PhongMaterial} from "xeokit-sdk.es.js";
*
* const viewer = new Viewer({
* canvasId: "myCanvas"
* });
*
* viewer.scene.camera.eye = [0, 0, 5];
* viewer.scene.camera.look = [0, 0, 0];
* viewer.scene.camera.up = [0, 1, 0];
*
* new Mesh(viewer.scene, {
* geometry: new ReadableGeometry(viewer.scene, buildBoxLinesGeometry({
* center: [0,0,0],
* xSize: 1, // Half-size on each axis
* ySize: 1,
* zSize: 1
* }),
* material: new PhongMaterial(viewer.scene, {
* emissive: [0,1,0]
* })
* });
* ````
*
* @function buildBoxLinesGeometry
* @param {*} [cfg] Configs
* @param {String} [cfg.id] Optional ID, unique among all components in the parent {@link Scene}, generated automatically when omitted.
* @param {Number[]} [cfg.center] 3D point indicating the center position.
* @param {Number} [cfg.xSize=1.0] Half-size on the X-axis.
* @param {Number} [cfg.ySize=1.0] Half-size on the Y-axis.
* @param {Number} [cfg.zSize=1.0] Half-size on the Z-axis.
* @returns {Object} Configuration for a {@link Geometry} subtype.
*/
function buildBoxLinesGeometry(cfg = {}) {
let xSize = cfg.xSize || 1;
if (xSize < 0) {
console.error("negative xSize not allowed - will invert");
xSize *= -1;
}
let ySize = cfg.ySize || 1;
if (ySize < 0) {
console.error("negative ySize not allowed - will invert");
ySize *= -1;
}
let zSize = cfg.zSize || 1;
if (zSize < 0) {
console.error("negative zSize not allowed - will invert");
zSize *= -1;
}
const center = cfg.center;
const centerX = center ? center[0] : 0;
const centerY = center ? center[1] : 0;
const centerZ = center ? center[2] : 0;
const xmin = -xSize + centerX;
const ymin = -ySize + centerY;
const zmin = -zSize + centerZ;
const xmax = xSize + centerX;
const ymax = ySize + centerY;
const zmax = zSize + centerZ;
return utils.apply(cfg, {
primitive: "lines",
positions: [
xmin, ymin, zmin,
xmin, ymin, zmax,
xmin, ymax, zmin,
xmin, ymax, zmax,
xmax, ymin, zmin,
xmax, ymin, zmax,
xmax, ymax, zmin,
xmax, ymax, zmax
],
indices: [
0, 1,
1, 3,
3, 2,
2, 0,
4, 5,
5, 7,
7, 6,
6, 4,
0, 4,
1, 5,
2, 6,
3, 7
]
});
}
export {buildBoxLinesGeometry};