Godot Version
Godot 4.3
Question
Hey all,
I am fairly new to Godot, but I am an experienced programmer in general. I am really enjoying Godot so far. I have been working through the “catlikecoding” hex map tutorials that were written for Unity (link: Unity Hex Map Tutorials), and I have been implementing them in Godot as a way to learn.
So far it’s been going well. I’ve implemented the first 3 hex map tutorials in Godot. Everything is working great, but I am having a weird issue with lighting and I don’t know enough to fix it on my own. Basically, to summarize the issue: everything in my scene is shaded weird.
Here is how the scene should look (this is the image taken straight from catlikecoding’s tutorials):
Here is how my scene actually looks (ignore the fact that my hex map is 7x7 instead of 6x6):
I know that it is a lighting issue. When I use Godot’s “debug drawing mode” to turn off lighting effects, this is what I see:
Additionally, when I use the “debug drawing mode” to only look at lighting, this is what I see:
With regard to how the scene is set up, it is pretty simple. I just have the hex grid and then I have an omnidirectional light placed above the hex grid:
To create each of the hexes in the scene, I am generating them procedurally (as is done in the catlikecoding tutorial). To generate the meshes, I use Godot’s SurfaceTool. Code is below:
func _create_mesh () -> void:
#Get an instance of the surface tool
var surface_tool = SurfaceTool.new();
#Begin creating the mesh
surface_tool.begin(Mesh.PRIMITIVE_TRIANGLES);
#Iterate over each of the 6 directions from the center of the hex
for i in range(0, 6):
#Form the mesh for this direction of the hex
_triangulate_hex(surface_tool, i)
#Generate the normals for the mesh
surface_tool.generate_normals()
#Generate the tangents for the mesh
surface_tool.generate_tangents()
#Commit the mesh
visualization.mesh = surface_tool.commit()
#Create the collision object for the mesh
visualization.create_trimesh_collision()
#Set the material for the mesh
visualization.material_override = hex_shader_material
The function _triangulate_hex is too complex to put in this forum post, but the entirety of my code is viewable on Github at the following repository: GitHub - davepruitt/hex_map_godot: Hexagonal-based maps in Godot
However, if we drill down deep enough into _triangulate_hex, eventually all vertices are added to the hex’s mesh using the following function:
func _add_triangle (st: SurfaceTool, v1: Vector3, v2: Vector3, v3: Vector3, c1: Color, c2: Color, c3: Color) -> void:
#Set the color for the vertex, and then add the vertex
st.set_color(c1)
st.add_vertex(v1)
#Set the color for the vertex, and then add the vertex
st.set_color(c2)
st.add_vertex(v2)
#Set the color for the vertex, and then add the vertex
st.set_color(c3)
st.add_vertex(v3)
The hex_shader_material that is used is a very simple ShaderMaterial object. The shader itself looks like this right now:
shader_type spatial;
void vertex() {
// Called for every vertex the material is visible on.
}
void fragment() {
ALBEDO = vec3(COLOR.r, COLOR.g, COLOR.b);
}
I believe that should be all the most pertinent pieces of information and code, but once again, all of my code is viewable on Github, and I have followed the catlikecoding tutorials very closely if you are already familiar with those tutorials.
Any ideas on what is going on with my lighting/shading? Any ideas on how to fix it?
Thanks for any help you can provide!!!