In this lab, we will use the bee object created the last week and a height map to create a 3D application.
Ex. 1. In this lab we'll create an environment using a height map and implement a fly-through camera movement with it. The next week, we'll add the bee object to it.
Note: You can see some of the elements we create in this homework in the video demo: https://www.youtube.com/watch?v=Af1f2JPvSIs.
Environment
Open Godot and and create a new project called FlowerRun, of type 3D. Create folders Art, Scripts, and Scenes in the res:// folder.
Save the scene as world_map. Add a 3D Node object for the root. Add the sun and the 3D environment to the scene. Also add a Camera3D object to the scene.
There are online tools for generating height maps such as
https://terraining.ateliernonta.com/
Choose a location in the world to generate a height map, then save it as a png file and as a raw file. You can use that one or one from the following package (also containing textures):
Terrain Plugin
Above the scene editor, click on AssetLib. Click the button Go Online, then enter "Terrain" in the search bar. Among these tools, there is one called Heightmap Terrain. Click on it, then click on Download. You can install it in the resources for this project for now. It will create a folder addons in the project.
Now we need to enable the addon. From the Project menu, open Project Settings, then click on the Plugins tab, and then turn on the Heightmap Terrain.
Create a new object in your scene of type HTerrain and call it Ground. We need to set a folder for it to keep the data in. In the inspector, click on the folder icon next to Data Directory. Right-click in the selection window to create a new folder called t_data and then on the button Select Current Folder.
Terrain Texture
Create a folder textures_raw and add to them the 7 png images from the zip file that contain the textures (bumpT, groundT, grassT, normalT, roughnessT, snowT, waterT). Create another folder called textures.
Then with the terrain object selected, at the bottom where we have texture information, click Import.... In the panel that appears, click the + above Import mode:. This should create Texture 0. Then under Albedo, click Load... and select the groundT image. Then under Bump click Load and select the bumpT image, then the normalT image for the normal, and the roughnessT image for the last square.
Repeat the operation to add the grass as Texture 1, the snow as 3, and the water as 4. Use the same images for the bump, normal, and roughness for all 3 of them. You can experiment on your own with other images for these aspects.
Once the 4 texture images in place, enter res://textures as the import folder, then click Import at the bottom.
To make the textures look a bit better, in the Inspector expand Rendering, then set the Shader to Classic4. Further down, expand Shader Parameters and set the values in U Tile Reduction to 1 in all coordinates.
Height Map
To use the height map, first drag the height map png file that you chose into the folder textures_raw. Then from the Terrain menu (above the scene editor, to the right of View), select Import Maps.... Click on the button next to Heightmap and select the image you just added. Then click Import. You will see how the terrain is generated little by little.
If the terrain area is larger than the map that was just recreated, from the Terrain menu, select Resize, and enter a resolution about half of the existing one. This should limit the terrain to this map.
Camera
Move the camera somewhere above the terrain but not by much and
away from the corner. Also rotate it a little towards the ground along
the red line. Use the preview to get a good view of the ground.
Painting the Terrain
Next to the Terrain menu above the scene, you will see a number of small buttons, used to sculpt or paint the terrain. Click on the Texture Paint button, which is looks like a gray scale paintbrush (4th from the right). You can click on one of the textures at the bottom, adjust the size and opacity of he brush, then paint over the terrain in that texture.
Zoom in on one of the peaks in the editor. Click on the snow texture and and paint the top of these peaks with snow. Repeat the procedure to add some grass to the scene where you think it would look good, as well as water if your map has any areas that look flat. Explore different brush shapes, sizes and strengths, and create a landscape that you like on your terrain.
Zoom in on the camera.
Add a script to the Camera3D object. Add a variable called speed and assign it the value 10 (for now).
Add the following function to the script:
func move_camera(delta:float): var forwardV = Vector3.FORWARD var rightV = Vector3.RIGHT var upV = Vector3.UP var input = Input.get_action_strength("ui_up") position = position + input * forwardV * speed * delta input = Input.get_action_strength("ui_down") position = position - input * forwardV * speed * delta input = Input.get_action_strength("ui_left") position = position - input * rightV * speed * delta input = Input.get_action_strength("ui_right") position = position + input * rightV * speed * delta input = Input.get_action_strength("ui_page_up") position = position + input * upV * speed * delta input = Input.get_action_strength("ui_page_down") position = position - input * upV * speed * delta
Then in the function _process, call this function with delta for parameter. The camera should move in all 6 directions with the arrow keys, WADS, and PageUp, PageDown.
Mouse Movement
Add the following variables to the class:
var mouse_sensitivity := 0.001 var twist_input := 0.0 var pitch_input = 0.0
Add the following function:
func _unhandled_input(event: InputEvent) -> void: if event is InputEventMouseMotion and Input.is_mouse_button_pressed(1) : twist_input = -event.relative.x * mouse_sensitivity pitch_input = -event.relative.y * mouse_sensitivity else: twist_input = 0 pitch_input = 0
Then add the following code to the function _process:
rotate_x(pitch_input) rotate_y(twist_input)
Now the camera should rotate like in Homework 2, but only when you keep the mouse left button clicked. However, it does not move in the direction we want with the arrow keys. Let's fix that.
Move the declaration of the vectors forwardV, rightV, and upV from the function move_camera to the top of the class to make them global. Then add the following to the function _process after the two rotation calls:
forwardV = forwardV.rotated(Vector3(0, 1, 0), twist_input) rightV = rightV.rotated(Vector3(0, 1, 0), twist_input)
Basically, we rotate these vectors around the Y axis at the same time as we rotate the camera and by the same amount. Now the camera should move forward in the direction it is looking.
Ex. 1. Decorations. Add some trees and flowers to the scene using the tree paint functionality of the terrain.
Ex. 2. Reset Button. Create a function
func reset()
that resets the camera to its original position. For this, declare 2 class variables (at the top of the class) called initial_position and initial_rotation. In the function _ready, assign to them the values of position and rotation respectively.
In the function Reset, assign the initial_position back to position and the same for the rotation.
Add a button to the scene with the text "Reset" that calls this function.
Ex. 3. Executable
To create a Windows executable, from the Project menu click on Export. In the new dialog, click on Add at the top, and add Windows and any other platform you want to try. It will likely complain that it cannnot find the template for it. Select to install the necessary templates. This process may take a little while.
Once finished, click on the Windows Desktop version and then on the file icon next to Export Path. This will open the res folder. Right-click to create a new folder called WindowsBuild (or some other name you'll remember) and enter "flyMap" as the file name. Then click Export Project. Once the build is successful, you can close the dialog.
Create a zip of the build folder, and add the script file to it. Also take a screenshot of the running game and add it to the zip file. Make sure that the zip file contains:
Submit the zip file containing the Windows executable and the script file toCanvas, Assignments - Homework 5.