Home | Features | Roadmap | Learn | Documentation | |||||
Latest version: Tramway SDK 0.0.9 Github Quick links Home Get Started |
Basic scriptingIn order to follow this guide, you might need the following:
Reminder about Lua syntaxAnyway, here's a small reminder of how Lua works.
-- this is a comment
What does the init script do?
First, let's go through the
print("\n\nHello! [...]\n")
This part just prints some text to the console.
-- Retitling the main window.
This part sets the title of the window, which is opened automatically on program start. It also sets the size of the window.
-- Setting up the global lighting.
This section will set up the scene lighting. All of these functions accept
a
-- Move the camera a bit away from the origin.
By default, all new 3D models get created at the origin, which is the
coordinates
-- Setting up a light so that you can see something.
Since the previous bit of code set the directional light's color to zero, it won't contribute to the scene's illumination, so here we create a light, so that you can actually see the teapot.
-- Adding a teapot to the scene.
Finally, we add the 3D model of the teapot to the scene.
-- This vector here will contain teapot euler angle rotation in radians.
In order to rotate the teapot, we need to set up its initial rotation. -- This function will be called every tick. tram.event.AddListener(tram.event.TICK, function() if tram.ui.PollKeyboardKey(tram.ui.KEY_LEFT) or tram.ui.PollKeyboardKey(tram.ui.KEY_A) then teapot_modifier = teapot_modifier - tram.math.vec3(0.01, 0.0, 0.0) end if tram.ui.PollKeyboardKey(tram.ui.KEY_RIGHT) or tram.ui.PollKeyboardKey(tram.ui.KEY_D) then teapot_modifier = teapot_modifier + tram.math.vec3(0.01, 0.0, 0.0) end if tram.ui.PollKeyboardKey(tram.ui.KEY_UP) or tram.ui.PollKeyboardKey(tram.ui.KEY_W) then teapot_modifier = teapot_modifier - tram.math.vec3(0.01, 0.0, 0.0) end if tram.ui.PollKeyboardKey(tram.ui.KEY_DOWN) or tram.ui.PollKeyboardKey(tram.ui.KEY_S) then teapot_modifier = teapot_modifier + tram.math.vec3(0.01, 0.0, 0.0) end teapot:SetRotation(tram.math.quat(teapot_modifier)) end) We need to execute the rotation logic during runtime. A good way to do it is to add an Event Listener and set it to listen to the Tick event, which is emitted at regular intervals. The code for the logic itself will check if the arrow keys are being pressed, and if they are, it will modify the rotation of the teapot. After all of the keys are checked and a new rotation is computed, it is assigned to the teapot's 3D model.
Since all rotations use quaternions, we can pass the
Teapot coloring.The default white teapot color is rather uninteresting. Let's change it to something more interesting.
Add this line of code somewhere after the
teapot:SetColor(tram.render.COLOR_PINK)
All of the color definitions are just The teapot has been colored pink. ExerciseLook up the hex or integer color values for your favorite color. Convert them into the range from 0.0 to 1.0 for each color channel and assign them to the teapot.
At this point, you are probably very proud of your teapot coloring skills.
Press the Let's try doing something more interesting with the colors. Place these lines of code somewhere in the update function:
local r = math.sin(tram.GetTickTime())
The teapot fades from white to black and then back to white. We can make this even more interesting. Let's add a different phase shift to each of the color channels.
local r = math.sin(tram.GetTickTime() + 1.0)
The teapot cycles through all of the colors. ExerciseNow that you can dynamically change a vec3 for colors, how about dynamically changing the teapot's position and rotation? ExerciseMaybe try changing the color of the scene's light. ExerciseThe code for controlling the teapot's rotation is already set up. Maybe add the option to not only be able to change the rotation of the teapot, but also its position?
|
|
|||
|