UltimateZoneBuilder/Build/UDBScript/Scripts/Examples/Geometry/jittervertices.js
biwa 5eb438e3ba
Add map scripting capabilities to UDB (#656)
Added map scripting capabilities to UDB (#656). Documentation at https://biwa.github.io/udbscript-docs/
2021-11-28 14:00:24 +01:00

41 lines
No EOL
976 B
JavaScript

`#name Jitter Vertices`;
`#description Jitters the selected vertices. If no vertices are selected all vertices in the map are jittered. Does not make sure that the resulting geometry is still valid.`;
`#scriptoptions
min
{
description = "Minimum jitter";
default = 0;
type = 1; // Integer
}
max
{
description = "Maximum jitter";
default = 16;
type = 1; // Integer
}
`;
// Gets a random value between min and max, then randomly make it negative
function getRandomValue(min, max)
{
return (Math.floor(Math.random() * (max-min)) + min) * (Math.random() < 0.5 ? 1 : -1);
}
// Get selected vertices
let vertices = Map.getSelectedVertices();
// No vertices selected? Get all vertices!
if(vertices.length == 0)
vertices = Map.getVertices();
// Jitter each vertex
vertices.forEach(v => {
v.position += [
getRandomValue(ScriptOptions.min, ScriptOptions.max),
getRandomValue(ScriptOptions.min, ScriptOptions.max),
];
});