EN | RU
< Back | Index | Next > |
---|---|---|
02. Mouse | Tutorials | 04. Node selection |
In this tutorial we are going to display spheres.
Estimated completion time: 10 minutes.
Let's display single sphere:
local nodes = main.application.nodes
local sphere = nodes:createSphere("sphere", 1)
local root = nodes:node("root")
root:addChild(sphere)
Let's see into API we introduced:
main.application.nodes
instance hosts scene nodes (not necessarily visible) ofscene.Node
classmain.application.nodes:createSphere()
method- accepts name of the sphere and its radius
- returns a new instance of
scene.Node
represented as a spherelocal sphere = main.application.nodes:createSphere("sphere", 1)
main.application.nodes:node()
method- accepts name of the node to find among nodes hosted by
main.application.nodes
instance - returns either an instance of
scene.Node
that already exists, ornil
otherwiselocal node = main.application.nodes:node("sphere") if (node) then print("Node exists") else print("Node does not exist") end
- accepts name of the node to find among nodes hosted by
scene
namespace hosts scene graph management functionalityscene.Node:addChild()
method- accepts an instance of
scene.Node
to attach it as a child nodelocal parent = main.application.nodes:node("parent") local child = main.application.nodes:node("child") parent:addChild(child)
- accepts an instance of
Notes:
- scene node names should be unique
root
node is created byogs
and serves as the root for visible scene nodes- only nodes that have
root
at the beginning of their scene graph hierarchy are visible
Let's display another sphere to the right of currently displayed sphere.
Add the following code:
local sphereRight = nodes:createSphere("another", 1)
root:addChild(sphereRight)
sphereRight.position = {2, 0, 0}
Let's see into API we introduced:
scene.Node.position
property- accepts array of position components in XYZ format
node.position = {2, 0, 0}
- returns array of position components in XYZ format
local pos = node.position print("Node position:", pos[1], pos[2], pos[3])
- accepts array of position components in XYZ format
You probably noticed two strange things by now:
sphereRight
is actually at the left- you only see part of the sphere
These issues arise because we let camera decide its position for itself. Let's see current camera position and rotation.
Add the following code:
local camera = main.application.camera
local pos = camera.position
print("Camera position:", pos[1], pos[2], pos[3])
local rot = camera.rotation
print("Camera rotation:", rot[1], rot[2], rot[3])
Let's see into API we introduced:
main.application.camera.position
property- accepts array of position components in XYZ format
main.application.camera.position = {0, -30, 0}
- returns array of position components in XYZ format
local pos = main.application.camera.position print("Camera position:", pos[1], pos[2], pos[3])
- accepts array of position components in XYZ format
main.application.camera.rotation
property- accepts array of rotation components in XYZ degrees
main.application.camera.rotation = {90, 0, 0}
- returns array of rotation components in XYZ degrees
local rot = main.application.camera.rotation print("Camera rotation:", rot[1], rot[2], rot[3])
- accepts array of rotation components in XYZ degrees
You should see something like this in the debug console:
Camera position: 0.000000 3.974028 0.000000
Camera rotation: 90.000000 0.000000 180.000000
Quite an interesting camera orientation, right?
You might be thinking that setting camera rotation to 0, 0, 0
would place
sphereRight
to the right. However, rotation alone is not enough to make
scene look good. Instead, we need the following camera orientation:
- position:
0, -30, 0
- rotation:
90, 0, 0
To be honest, we came up with these values by experimenting, we don't have any logical explanation here for you. Sorry.
Now, let's display both spheres so that we can see them clearly. Here's complete code:
local nodes = main.application.nodes
local sphere = nodes:createSphere("sphere", 1)
local root = nodes:node("root")
root:addChild(sphere)
local sphereRight = nodes:createSphere("another", 1)
root:addChild(sphereRight)
sphereRight.position = {2, 0, 0}
local camera = main.application.camera
camera.position = {0, -30, 0}
camera.rotation = {90, 0, 0}
Now sphereRight
is finally to the right of the original sphere.
You have successfully created spherical scene nodes, positioned one of them and then oriented the camera to have a better look at the scene.
< Back | Index | Next > |
---|---|---|
02. Mouse | Tutorials | 04. Node selection |