Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ArrayMesh final triangle has incorrect vertex normals applied #58645

Closed
funIntentions opened this issue Mar 1, 2022 · 2 comments
Closed

ArrayMesh final triangle has incorrect vertex normals applied #58645

funIntentions opened this issue Mar 1, 2022 · 2 comments

Comments

@funIntentions
Copy link

Godot version

v3.4.stable.official [206ba70]

System information

Windows 10, GLES3, NVIDIA GeForce RTX 3060/PCIe/SSE2

Issue description

When creating an ArrayMesh from an array of vertices and an array of vertex normals, it appears that the last triangle (last three vertices in the vertex array) can sometimes receive vertex normals that are different from the vertex normals specified in the array provided.

When the normals are incorrect, the vertex blending will look wrong for the problematic triangle.
image
The triangle facing the camera has incorrect vertex normals.

The issue is very noticeable if the MeshInstance is given a Spatial Material that has the vertex grow setting enabled (Parameters -> Grow). The vertices will grow in the direction of their normals and for the triangle with incorrect normals, it will skew it such that holes appear in the mesh.
image
This Mesh has a Spatial Material with Grow enabled with a Grow Amount of 0.5.

I suspect it's an issue with the last triangle because if the triangles are rearranged in the vertex and normal arrays, the problematic triangle will change to the new final one. A hacky workaround I've found is adding a final triangle with all the vertices and normals set to (0, 0, 0). In the sample code I've provided, I've added some comments on that.

Steps to reproduce

Here's some sample code that when attached to a MeshInstance will create a Mesh that has this issue. The sample project that I've attached below has a simple scene with this setup.

As I mentioned in the description, if the MeshInstance Node has a Spatial Material with the Grow setting enabled (Parameters -> Grow), the issue becomes very visible with holes in the mesh appearing. In the attached project, I've enabled the Grow setting and I've set the Grow Amount to 0.5.

image

extends MeshInstance

func _ready():
	var mesh_array: ArrayMesh = ArrayMesh.new()
	mesh = mesh_array

	var vertices = [
		Vector3(5, 5.5, 3), 
		Vector3(5, 6, 2.5), 
		Vector3(4.5, 6, 3), 

		Vector3(5, 6, 2.5), 
		Vector3(5, 5.5, 3), 
		Vector3(5.5, 6, 3), 

		Vector3(5, 6, 2.5), 
		Vector3(5, 6.5, 3), 
		Vector3(4.5, 6, 3), 

		Vector3(5, 6, 2.5), 
		Vector3(5.5, 6, 3), 
		Vector3(5, 6.5, 3), 

		Vector3(5, 5.5, 3), 
		Vector3(4.5, 6, 3), 
		Vector3(5, 6, 3.5), 

		Vector3(5.5, 6, 3), 
		Vector3(5, 5.5, 3), 
		Vector3(5, 6, 3.5), 

		Vector3(4.5, 6, 3), 
		Vector3(5, 6.5, 3), 
		Vector3(5, 6, 3.5), 

		Vector3(5.5, 6, 3), 
		Vector3(5, 6, 3.5), 
		Vector3(5, 6.5, 3),

		# Uncomment these vertices and the corresponding normals and see that the triangle that
		# was previously rendering incorrectly, is now rendering correctly.
		#Vector3(0, 0, 0),
		#Vector3(0, 0, 0),
		#Vector3(0, 0, 0),
	];

	var normals = [
		Vector3(0, -1, 0),
		Vector3(0, 0, -1),
		Vector3(-1, 0, 0), 

		Vector3(0, 0, -1),
		Vector3(0, -1, 0),
		Vector3(1, 0, 0),

		Vector3(0, 0, -1),
		Vector3(0, 1, 0),
		Vector3(-1, 0, 0),

		Vector3(0, 0, -1),
		Vector3(1, 0, 0),
		Vector3(0, 1, 0),

		Vector3(0, -1, 0),
		Vector3(-1, 0, 0),
		Vector3(0, 0, 1),

		Vector3(1, 0, 0),
		Vector3(0, -1, 0),
		Vector3(0, 0, 1),

		Vector3(-1, 0, 0),
		Vector3(0, 1, 0),
		Vector3(0, 0, 1),

		# These vertex normals aren't being applied correctly to their corresponding vertices.
		# It appears to be an issue that sometimes occurs for the final set of triangle normals.
		# If another triangle is added, the issue with this triangle will often disappear. 
		Vector3(1, 0, 0),
		Vector3(0, 0, 1),
		Vector3(0, 1, 0),
		
		# Uncomment these normals and the corresponding vertices and see that the triangle that
		# was previously rendering incorrectly, is now rendering correctly.
		#Vector3(0, 0, 0),
		#Vector3(0, 0, 0),
		#Vector3(0, 0, 0),
	];

	var arrays = []
	arrays.resize(Mesh.ARRAY_MAX)
	arrays[Mesh.ARRAY_VERTEX] = PoolVector3Array(vertices)
	arrays[Mesh.ARRAY_NORMAL] = PoolVector3Array(normals)

	mesh.clear_surfaces()
	mesh.clear_blend_shapes()
	mesh.add_surface_from_arrays(Mesh.PRIMITIVE_TRIANGLES, arrays)

Minimal reproduction project

incorrect-vertex-normals.zip

@clayjohn
Copy link
Member

clayjohn commented Mar 1, 2022

This looks like a duplicate of #52399 which was fixed a few days after the release of version 3.4 and so didn't make it in until 3.4.1. Can you try again in the latest stable (3.4.3 at the time of writing)?

Here is the download link https://downloads.tuxfamily.org/godotengine/3.4.3/ (the website is down for maintenance right now, so you'll have to get it right from the tuxfamily site)

@funIntentions
Copy link
Author

Thank you!! I tried it on 3.4.3 and it is fixed. 👍

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants