UNPKG

@babylonjs/gui

Version:

Babylon.js GUI module =====================

98 lines 3.82 kB
import { Tools } from "@babylonjs/core/Misc/tools.js"; import { TmpVectors, Vector3 } from "@babylonjs/core/Maths/math.vector.js"; import { VolumeBasedPanel } from "./volumeBasedPanel.js"; import { Container3D } from "./container3D.js"; /** * Class used to create a container panel where items get randomized planar mapping */ export class ScatterPanel extends VolumeBasedPanel { constructor() { super(...arguments); this._iteration = 100.0; } /** * Gets or sets the number of iteration to use to scatter the controls (100 by default) */ get iteration() { return this._iteration; } set iteration(value) { if (this._iteration === value) { return; } this._iteration = value; Tools.SetImmediate(() => { this._arrangeChildren(); }); } _mapGridNode(control, nodePosition) { const mesh = control.mesh; const newPos = this._scatterMapping(nodePosition); if (!mesh) { return; } switch (this.orientation) { case Container3D.FACEORIGIN_ORIENTATION: case Container3D.FACEFORWARD_ORIENTATION: mesh.lookAt(new Vector3(0, 0, 1)); break; case Container3D.FACEFORWARDREVERSED_ORIENTATION: case Container3D.FACEORIGINREVERSED_ORIENTATION: mesh.lookAt(new Vector3(0, 0, -1)); break; } control.position = newPos; } _scatterMapping(source) { source.x = (1.0 - Math.random() * 2.0) * this._cellWidth; source.y = (1.0 - Math.random() * 2.0) * this._cellHeight; return source; } _finalProcessing() { const meshes = []; for (const child of this._children) { if (!child.mesh) { continue; } meshes.push(child.mesh); } for (let count = 0; count < this._iteration; count++) { meshes.sort((a, b) => { const distance1 = a.position.lengthSquared(); const distance2 = b.position.lengthSquared(); if (distance1 < distance2) { return 1; } else if (distance1 > distance2) { return -1; } return 0; }); const radiusPaddingSquared = Math.pow(this.margin, 2.0); const cellSize = Math.max(this._cellWidth, this._cellHeight); const difference2D = TmpVectors.Vector2[0]; const difference = TmpVectors.Vector3[0]; for (let i = 0; i < meshes.length - 1; i++) { for (let j = i + 1; j < meshes.length; j++) { if (i != j) { meshes[j].position.subtractToRef(meshes[i].position, difference); // Ignore Z axis difference2D.x = difference.x; difference2D.y = difference.y; const combinedRadius = cellSize; let distance = difference2D.lengthSquared() - radiusPaddingSquared; const minSeparation = Math.min(distance, radiusPaddingSquared); distance -= minSeparation; if (distance < Math.pow(combinedRadius, 2.0)) { difference2D.normalize(); difference.scaleInPlace((combinedRadius - Math.sqrt(distance)) * 0.5); meshes[j].position.addInPlace(difference); meshes[i].position.subtractInPlace(difference); } } } } } } } //# sourceMappingURL=scatterPanel.js.map