MicroCity

< 4.2 Operation System Table of Contents 4.4 Discrete Event Simulation >

4.3 Scene and Object

The scene library is designed to control the scene canvas and the objects within it.

Scene

This part of the functions include setting up the scene, adding lookup objects, and coordinate transformation tools.

scene.setenv (opttab)

Set scene according to options in the opttab. No return. The list of options:

Here is a sample code for setting the camera projection and background color:

scene.setenv({camtype='ortho', bgcolor='black'})

scene.addobj (type [, opttab])

Add an object to (0, 0, 0) of the scene with specific type and options. Return the object. The type includes:

The list of options in opttab:

Here is are some samples for adding objects to the scene:

scene.setenv({grid='plane'})                                                 --set a plane grid
scene.addobj("points", {vertices={0,0,0, 5,5,5}, size=5})                    --add two points with size of 5
scene.addobj("polyline", {vertices={0,0,0, -2,3,5, 4,6,7}, color='blue'})    --add a blue polyline
scene.addobj("polygon", {vertices={-1,-1,0, -1,1,0, 1,1,0, 1,-1,0}, size=0}) --add a 2D polygon
scene.addobj("label", {text="hello", size=5})                                --add a label
scene.render()                                                               --render the scene

scene.getobj (name)

Searches through the scene, and returns the first with a matching name. Camera and selected object can be found with “camera” and “seleted”.

scene.tovector (radius, phi, theta)

Return x, y, z from conversion of a spherical coordinates.

scene.topolar (x, y, z)

Return a spherical coordinates: radius, phi, theta from a vector.

scene.render ([ms])

Render the scene and check the browser like os.getready and return the same. The interval (milliseconds) between renders can be controlled via the ms parameter.

Object

This part of functions control the objects in the scene. Note that a call obj:func(args) is syntactic sugar for scene.func(obj, args).

obj:getpos ()

Return the position: x, y, z of the object.

obj:setpos (x, y, z)

Set the position: x, y, z of the object. If the object is a orthographic camera, the z is also used as the zoom value of the camera.

obj:getrot ()

Return the rotation(in radians): x, y, z of the object.

obj:setrot (x, y, z)

Set the rotation(in radians): x, y, z of the object.

obj:getscale ()

Return the scale: x, y, z of the object.

obj:setscale (x, y, z)

Set the scale: x, y, z of the object.

obj:getchildren ()

Get all child objects and put them in a table as the return value.

obj:setchildren (childtab)

Set all objects in the table childtab as children for the object.

obj:getparent ()

Return the parent of the object.

obj:setparent ([parent])

Set the parent of the object. The object can have no parent.

obj:getmat ()

Return the material information table of the object. The list of information:

obj:setmat (opttab)

Set the material of the object from the options in opttab. The list of options:

obj:getvertices ()

Return the array of vertices and the array of indices (if any) of the object’s geometry.

obj:setvertices (vertices [, indices])

Set the vertices (array) and indices (array, if any) of the object’s geometry.

obj:getdata ()

Return a table which stores custom data of the object. Please refer to 2.2 Searching for Countries for sample codes.

obj:setdata (datatab)

Write the (key, value) in the datatab to the custom data field of the object.

obj:tojson (fpath)

Write the object to a Threejs JSON file of the fpath.

obj:delete ()

Delete the object and all its children recursively.

< 4.2 Operation System Table of Contents 4.4 Discrete Event Simulation >