mesh_generation_with_heightmap_and_shaders.rst 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. .. _doc_mesh_generation_with_heightmap_and_shaders:
  2. Mesh generation with heightmap and shaders
  3. ==========================================
  4. Introduction
  5. ------------
  6. This tutorial will help you to use Godot shaders to deform a plane
  7. mesh so it appears like a basic terrain. Remember that this solution
  8. has pros and cons.
  9. Pros:
  10. - Pretty easy to do.
  11. - This approach allows computation of LOD terrains.
  12. - The heightmap can be used in Godot to create a normal map.
  13. Cons:
  14. - The Vertex Shader can't re-compute normals of the faces. Thus, if
  15. your mesh is not static, this method will **not** work with shaded
  16. materials.
  17. - This tutorial uses a plane mesh imported from Blender to Godot
  18. Engine. Godot is able to create meshes as well.
  19. See this tutorial as an introduction, not a method that you should
  20. employ in your games, except if you intend to do LOD. Otherwise, this is
  21. probably not the best way.
  22. However, let's first create a heightmap,or a 2D representation of the terrain.
  23. To do this, I'll use GIMP, but you can use any image editor you like.
  24. The heightmap
  25. -------------
  26. We will use a few functions of GIMP image editor to produce a simple
  27. heightmap. Start GIMP and create a square image of 512x512 pixels.
  28. .. image:: /img/1_GIMP_createImage512.png
  29. You are now in front of a new, blank, square image.
  30. .. image:: /img/2_GIMP.png
  31. Then, use a filter to render some clouds on this new image.
  32. .. image:: /img/3_GIMP_FilterRenderClouds.png
  33. Parameter this filter to whatever you want. A white pixel corresponds
  34. to the highest point of the heightmap, a black pixel corresponds to
  35. the lowest one. So, darker regions are valleys and brighter are
  36. mountains. If you want, you can check "tileable" to render a heightmap
  37. that can be cloned and tiled close together with another one. X and Y
  38. size don't matter a lot as long as they are big enough to provide a
  39. decent ground. A value of 4.0 or 5.0 for both is nice. Click on the
  40. "New Seed" button to roll a dice and GIMP will create a new random
  41. heightmap. Once you are happy with the result, click "OK".
  42. .. image:: /img/4_GIMP_Clouds.png
  43. You can continue to edit your image if you wish. For our example,
  44. let's keep the heightmap as is, and let's export it to a PNG file, say
  45. "heightmap.png". Save it in your Godot project folder.
  46. The plane mesh
  47. --------------
  48. Now, we will need a plane mesh to import in Godot. Let's run Blender.
  49. .. image:: /img/5_Blender.png
  50. Remove the start cube mesh, then add a new plane to the scene.
  51. .. image:: /img/6_Blender_CreatePlane.png
  52. Zoom a bit, then switch to Edit mode (Tab key) and in the Tools
  53. buttongroup at the left, hit "Subdivide" 5 or 6 times.
  54. .. image:: /img/7_Blender_subdivided.png
  55. Your mesh is now subdivided, which means we added vertices to the
  56. plane mesh that we will later be able to move. Job's not finished yet:
  57. in order to texture this mesh a proper UV map is necessary. Currently,
  58. the default UV map contains only the 4 corner vertices we had at the
  59. beginning. However, we now have more, and we want to be able to
  60. texture over the whole mesh correctly.
  61. If all the vertices of your mesh are not selected, select them all
  62. (hit "A"). They must appear orange, not black. Then, in the
  63. Shading/UVs button group to the left, click the "Unwrap" button (or
  64. simply hit "U") and select "Smart UV Project". Keep the default
  65. options and hit "Ok".
  66. .. image:: /img/8_Blender_UVSmart.png
  67. Now, we need to switch our view to "UV/Image editor".
  68. .. image:: /img/9_Blender_UV_editor.png
  69. Select all the vertices again ("A") then in the UV menu, select
  70. "Export UV Layout".
  71. .. image:: /img/10_Blender_exportUV.png
  72. Export the layout as a PNG file. Name it "plane.png" and save it in
  73. your Godot project folder. Now, let's export our mesh as an OBJ file.
  74. Top of the screen, click "File/Export/Wavefront (obj)". Save your
  75. object as "plane.obj" in your Godot project folder.
  76. Shader magic
  77. ------------
  78. Let's now open Godot Editor.
  79. Create a new project in the folder you previously created and name it
  80. what you want.
  81. .. image:: /img/11_Godot.png
  82. In our default scene (3D), create a root node "Spatial". Next, import
  83. the mesh OBJ file. Click "Import", choose "3D Mesh" and select your
  84. plane.obj file, set the target path as "/" (or wherever you want in
  85. your project folder).
  86. .. image:: /img/12_Godot_ImportMesh.png
  87. I like to check "Normals" in the import pop-up so the import will also
  88. consider faces normals, which can be useful (even if we don't use them
  89. in this tutorial). Your mesh is now displayed in the FileSystem in
  90. "res://".
  91. .. image:: /img/13_Godot_ImportPopup.png
  92. Create a MeshInstance node. In the Inspector, load the mesh we just
  93. imported. Select "plane.msh" and hit ok.
  94. .. image:: /img/14_Godot_LoadMesh.png
  95. Great! Our plane is now rendered in the 3D view.
  96. .. image:: /img/15_Godot_MeshPlaneRendered.png
  97. It is time to add some shader stuff. In the Inspector, in the
  98. "Material Override" line, add a "New ShaderMaterial". Edit it by
  99. clicking the ">" button just right to it.
  100. .. image:: /img/16_Godot_ShaderMaterial.png
  101. You have two ways to create a shader: by code (MaterialShader), or
  102. using a shader graph (MaterialShaderGraph). The second one is a bit
  103. more visual, but we will not cover it for now. Create a "New
  104. MaterialShader".
  105. .. image:: /img/17_Godot_newMaterialShader.png
  106. Edit it by clicking the ">" button just right to it. The Shaders
  107. editor opens.
  108. .. image:: /img/18_Godot_ShaderEditorOpened.png
  109. The Vertex tab is for the Vertex shader, and the Fragment tab is for
  110. the Fragment shader. No need to explain what both of them do, right?
  111. If so, head to the :ref:`doc_shading_language` page. Else, let's start with the
  112. Fragment shader. This one is used to texture the plane using an image.
  113. For this example, we will texture it with the heightmap image itself,
  114. so we'll actually see mountains as brighter regions and canyons as
  115. darker regions. Use this code:
  116. ::
  117. uniform texture source;
  118. uniform color col;
  119. DIFFUSE = col.rgb * tex(source,UV).rgb;
  120. This shader is very simple (it actually comes from the :ref:`doc_shading_language` page).
  121. What it basically does is take 2 parameters that we have to provide from
  122. outside the shader ("uniform"):
  123. - the texture file
  124. - a color
  125. Then, we multiply every pixel of the image given by
  126. ``tex(source, UV).rgb`` by the color defined ``col`` and we set it to
  127. DIFFUSE variable, which is the rendered color. Remember that the
  128. ``UV`` variable is a shader variable that returns the 2D position of
  129. the pixel in the texture image, according to the vertex we are
  130. currently dealing with. That is the use of the UV Layout we made
  131. before. The color ``col`` is actually not necessary to display the
  132. texture, but it is interesting to play and see how it does, right?
  133. However, the plane is displayed black! This is because we didn't set
  134. the texture file and the color to use.
  135. .. image:: /img/19_Godot_BlackPlane.png
  136. In the Inspector, click the "Previous" button to get back to the
  137. ShaderMaterial. This is where you want to set the texture and the
  138. color. In "Source", click "Load" and select the texture file
  139. "heightmap.png". But the mesh is still black! This is because our
  140. Fragment shader multiplies each pixel value of the texture by the
  141. ``col`` parameter. However, this color is currently set to black
  142. (0,0,0), and as you know, 0\*x = 0 ;) . Just change the ``col``
  143. parameter to another color to see your texture appear:
  144. .. image:: /img/20_Godot_TexturedPlane.png
  145. Good. Now, the Vertex Shader.
  146. The Vertex Shader is the first shader to be executed by the pipeline. It
  147. deals with vertices.
  148. Click the "Vertex" tab to switch, and paste this code:
  149. ::
  150. uniform texture source;
  151. uniform float height_range;
  152. vec2 xz = SRC_VERTEX.xz;
  153. float h = tex(source, UV).g * height_range;
  154. VERTEX = vec3(xz.x, h, xz.y);
  155. VERTEX = MODELVIEW_MATRIX * VERTEX;
  156. This shader uses two "uniform" parameters. The ``source`` parameter is
  157. already set for the fragment shader. Thus, the same image will be used
  158. in this shader as the heightmap. The ``height_range`` parameter is a
  159. parameter that we will use to increase the height effect.
  160. At line 3, we save the x and z position of the SRC_VERTEX, because we
  161. do not want them to change : the plane must remain square. Remember
  162. that Y axis corresponds to the "altitude", which is the only one we
  163. want to change with the heightmap.
  164. At line 4, we compute an ``h`` variable by multiplying the pixel value
  165. at the UV position and the ``height_range``. As the heightmap is a
  166. greyscale image, all r, g and b channels contain the same value. I
  167. used ``g``, but any of r, g and b have the same effect.
  168. At line 5, we set the current vertex' position at (xz.x, h, xz.y)
  169. position. Concerning xz.y remember that its type is "vec2". Thus, its
  170. components are x and y. The y component simply contains the z position
  171. we set at line 3.
  172. Finally, at line 6, we multiply the vertex by the model/view matrix in
  173. order to set its position according to camera position. If you try to
  174. comment this line, you'll see that the mesh behaves weird as you move
  175. and rotate the camera.
  176. That's all good, but our plane remains flat. This is because the
  177. ``height_range`` value is 0. Increase this value to observe the mesh
  178. distort and take to form of the terrain we set before:
  179. .. image:: /img/21_Godot_Fini.png