Skip to content

Commit 48c43a2

Browse files
no message
1 parent da20ca4 commit 48c43a2

File tree

2 files changed

+222
-194
lines changed

2 files changed

+222
-194
lines changed

LSQuickScripts/Example Project/LSQuickScripts Examples/Public/LSQuickScripts/LSQuickScripts.js

Lines changed: 111 additions & 97 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//@ui {"widget":"label", "label":"LSQuickScripts v2.9"}
1+
//@ui {"widget":"label", "label":"LSQuickScripts v2.10"}
22
//@ui {"widget":"label", "label":"By Max van Leeuwen"}
33
//@ui {"widget":"label", "label":"-"}
44
//@ui {"widget":"label", "label":"Place on top of scene ('On Awake')"}
@@ -484,24 +484,26 @@
484484
// -
485485
//
486486
//
487-
// VisualizePositions() : VisualizePositions object
488-
// A class that places cubes on each position in the 'positions' array, given through the update function. Useful for quick visualizations of 3D positions.
487+
// VisualizePoints() : VisualizePoints object
488+
// A class that places a mesh on each point in the given array. Useful for quick visualization of 3D points in your scene.
489+
// For a one-liner, you can pass the array of points as the first optional argument when creating a VisualizePoints(<points>) instance.
489490
//
490-
// Example, showing all properties
491-
//
492-
// var v = new VisualizePositions() // create VisualizePositions instance
493-
// v.scale // (optional) the scale of the cubes (world size, default is 1)
494-
// v.rotation // (optional) the rotation of the cubes (if continuousRotationis set to false)
495-
// v.continuousRotation // (optional) make the cubes rotate around local up, clockwise (boolean, default is true)
496-
// v.material // (optional) the material of the cubes (Asset.Material)
497-
// v.showPositions( [vec3 Array] ) // show cubes on the given positions (array), returns the array of created SceneObjects for further customization
498-
// v.getPositions() // returns currently visualized positions
499-
// v.remove() // clear all created visualizations
491+
// Points can be defined in 3 ways: positions (vec3), Objects ({position:vec3, rotation:quat, scale:vec3}), or transformation matrices (mat4)
492+
// points = [ <vec3> ]
493+
// points = [ {position: <vec3>, rotation: <quat>, scale: <vec3>} ]
494+
// points = [ <transform (mat4)> ]
500495
//
501-
// Example, shorter format
496+
// Example, showing all properties
502497
//
503-
// var positions = [new vec3(0, 0, 0), new vec3(1, 0, 0)] // some world positions to visualize
504-
// new VisualizePositions().showPositions(positions)
498+
// var v = new VisualizePoints(points)) // create instance ('points' argument is optional, this will invoke .show(points) right away)
499+
// v.parent // (optional) SceneObject to parent the points to (default is LSQS' SceneObject)
500+
// v.scale // (optional) scale multiplier for the mesh when created (vec3)
501+
// v.material // (optional) the material on the mesh (Asset.Material)
502+
// v.mesh // (optional) the mesh to show on each point (Asset.RenderMesh, default is a unit box)
503+
// v.maxCount // (optional) maximum amount of points to show, starts cutting off indices at 0 (default is null for unlimited)
504+
// v.show(points) // show an array of points (see different ways to define a point below), returns the array of created SceneObjects for further customization
505+
// v.getTransforms() // get all objects' transform components (<Transform> array)
506+
// v.clear() // destroy all objects
505507
//
506508
//
507509
//
@@ -1778,124 +1780,122 @@ global.wrapFunction = function(originalFunction, newFunction) {
17781780

17791781

17801782

1781-
global.VisualizePositions = function(){
1783+
global.VisualizePoints = function(showPointsOnStart){
17821784
var self = this;
17831785

17841786
/**
1785-
* @type {vec3}
1786-
* @description Size of objects. Default is vec3.one(). */
1787-
this.scale = vec3.one();
1787+
* @type {SceneObject}
1788+
* @description (optional) SceneObject to parent the points to (default is LSQS' SceneObject) */
1789+
this.parent = script.getSceneObject();
17881790

17891791
/**
1790-
* @type {quat}
1791-
* @description Rotation (if continuousRotation is set to false). */
1792-
this.rotation = quat.quatIdentity();
1793-
1794-
/**
1795-
* @type {Boolean}
1796-
* @description If constantly rotating. */
1797-
this.continuousRotation = true;
1792+
* @type {vec3}
1793+
* @description (optional) scale multiplier for the mesh when created (vec3) */
1794+
this.scale;
17981795

17991796
/**
18001797
* @type {Material}
1801-
* @description Material to place on box mesh. */
1798+
* @description (optional) the material on the mesh (Asset.Material) */
18021799
this.material;
18031800

18041801
/**
1805-
* @type {Function}
1806-
* @description Returns currently visualized positions. */
1807-
this.getPositions = function(){
1808-
return allPositions;
1809-
}
1802+
* @type {Asset.RenderMesh}
1803+
* @description (optional) the mesh to show on each point (Asset.RenderMesh, default is a unit box) */
1804+
this.mesh;
1805+
1806+
/**
1807+
* @type {Number}
1808+
* @description (optional) maximum amount of points to show, starts cutting off indices at 0 (default is null for unlimited) */
1809+
this.maxCount;
18101810

18111811
/**
18121812
* @type {Function}
1813-
* @description Call to create objects. */
1814-
this.showPositions = function(positions){
1813+
* @description show an array of points, returns the array of created SceneObjects for further customization */
1814+
this.show = function(allPoints){
18151815
// remove existing
1816-
self.remove();
1816+
self.clear();
1817+
1818+
if(allPoints.length == 0) return;
1819+
var points = [...allPoints]; // make copy of list
1820+
if(self.maxCount != null){
1821+
if(allPoints.length > self.maxCount){
1822+
points = allPoints.slice(-self.maxCount);
1823+
}
1824+
}
1825+
1826+
const pointType = getPointType(points[0]); // get point type (string: vec3, object, mat4)
18171827

18181828
// add new
1819-
for(var i = 0; i < positions.length; i++){
1820-
if(!positions[i]) continue;
1829+
for(var i = 0; i < points.length; i++){
1830+
// get this point (could be vec3, object, or mat4)
1831+
const p = points[i];
18211832

1822-
// create
1823-
var obj = global.scene.createSceneObject("visualizer-cube-" + i.toString());
1833+
// create mesh
1834+
var obj = global.scene.createSceneObject("point " + i.toString());
1835+
obj.setParent(self.parent);
18241836
var rmv = obj.createComponent("Component.RenderMeshVisual");
1825-
rmv.mesh = cube;
1826-
1827-
// material
1837+
rmv.mesh = self.mesh ? self.mesh : cube;
18281838
if(self.material) rmv.addMaterial(self.material);
18291839

1830-
// position, scale
1840+
// set transform
18311841
var trf = obj.getTransform();
1832-
trf.setWorldPosition(positions[i]);
1833-
trf.setWorldScale(self.scale);
1842+
if(pointType == 'vec3'){ // position only
1843+
trf.setWorldPosition(p);
1844+
trf.setWorldRotation(quat.quatIdentity());
1845+
trf.setWorldScale(self.scale ? self.scale : vec3.one());
1846+
}else if(pointType == 'object'){ // position, rotation, scale manual object
1847+
trf.setWorldPosition(p.position);
1848+
trf.setWorldRotation(p.rotation);
1849+
trf.setWorldScale(self.scale ? p.scale.mult(self.scale) : p.scale);
1850+
}else{ // world transform
1851+
trf.setWorldTransform(p);
1852+
if(self.scale) trf.setWorldScale(trf.getWorldScale().mult(self.scale));
1853+
}
18341854

18351855
// register
1836-
objs.push(obj);
1837-
allPositions.push(positions[i]);
1856+
allSceneObjects.push(obj);
1857+
allTransforms.push(trf);
18381858
}
18391859

1840-
// do continuous rotation
1841-
if(self.continuousRotation){
1842-
if(!keepRotatingEvent){ // if no rotating event yet, create new
1843-
function keepRotating(){
1844-
rot = quat.angleAxis(-getTime(), vec3.up());
1845-
for(var i = 0; i < objs.length; i++){
1846-
objs[i].getTransform().setWorldRotation(rot);
1847-
}
1848-
}
1849-
keepRotatingEvent = script.createEvent("UpdateEvent");
1850-
keepRotatingEvent.bind(keepRotating);
1851-
}
1852-
}else{
1853-
// delete existing rotation (if any)
1854-
stopEvents();
1860+
return allSceneObjects;
1861+
};
18551862

1856-
// set rotation
1857-
for(var i = 0; i < objs.length; i++){
1858-
objs[i].getTransform().setWorldRotation(self.rotation);
1859-
}
1860-
}
18611863

1862-
return objs;
1863-
};
18641864

18651865
/**
18661866
* @type {Function}
1867-
* @description Call to clear objects. */
1868-
this.remove = function(){
1869-
for(var i = 0; i < objs.length; i++){
1870-
objs[i].destroy();
1871-
}
1872-
objs = [];
1873-
allPositions = [];
1867+
* @description get all objects' transform components (<Transform> array) */
1868+
this.getTransforms = function(){
1869+
return allTransforms;
18741870
}
18751871

18761872

1877-
// private
1878-
var objs = []; // list of created sceneobjects
1879-
var allPositions = []; // list of created positions
1880-
var keepRotatingEvent; // rotation animation event
1881-
var cube = makeCube(); // get mesh
1882-
var rot = quat.angleAxis(0, vec3.up()); // starting rotation
1883-
1884-
// stops animation event
1885-
function stopEvents(){
1886-
if(keepRotatingEvent){
1887-
script.removeEvent(keepRotatingEvent);
1888-
keepRotatingEvent = null;
1873+
1874+
/**
1875+
* @type {Function}
1876+
* @description destroy all objects */
1877+
this.clear = function(){
1878+
// delete objects
1879+
for(var i = 0; i < allSceneObjects.length; i++){
1880+
allSceneObjects[i].destroy();
18891881
}
1882+
1883+
// reset lists
1884+
allSceneObjects = [];
1885+
allTransforms = [];
18901886
}
18911887

1892-
// generated mesh to be used on created objects
1893-
function makeCube(){
1894-
// cube from MeshBuilder documentation
1888+
1889+
1890+
// private
1891+
var allSceneObjects = [];
1892+
var allTransforms = [];
1893+
const cube = function(){ // generate cube mesh (once on start)
1894+
// simple cube with texture and normals
18951895
var builder = new MeshBuilder([
1896-
{ name: "position", components: 3 },
1897-
{ name: "normal", components: 3, normalized: true },
1898-
{ name: "texture0", components: 2 },
1896+
{name: "position", components: 3},
1897+
{name: "normal", components: 3, normalized: true},
1898+
{name: "texture0", components: 2},
18991899
]);
19001900
builder.topology = MeshTopology.Triangles;
19011901
builder.indexType = MeshIndexType.UInt16;
@@ -1946,8 +1946,22 @@ global.VisualizePositions = function(){
19461946
addQuadIndices(builder, index, index+1, index+2, index+3);
19471947
}
19481948

1949-
// return generated mesh
1949+
// return mesh only
19501950
builder.updateMesh();
19511951
return builder.getMesh();
1952+
}();
1953+
1954+
1955+
1956+
// returns 'vec3', 'object' or 'mat4' depending on type
1957+
function getPointType(p){
1958+
if(p.x != null) return 'vec3';
1959+
if(p.position != null) return 'object';
1960+
return 'mat4';
19521961
}
1962+
1963+
1964+
1965+
// start right away if array given
1966+
if(showPointsOnStart) self.show(showPointsOnStart);
19531967
}

0 commit comments

Comments
 (0)