Aligning a scene to a surface normal

Godot Version

4.2.1-stable

Question

I’m trying to align a scene so that the Vector3(0,1,0) is redefined as a surface normal.

I have this code:

this_decal.global_transform.basis.y = normal
this_decal.global_transform.basis.x = -this_decal.global_transform.basis.z.cross(normal)
this_decal.global_transform.basis = this_decal.global_transform.basis.orthonormalized()

It causes weird distortions at some angles. The most noticeable one is if the normal is Vector3(0,0,1) or Vector3(0,0,-1). In those cases, the scale will be set to zero for some reason.

I should note that I am not a professional with transformation matrices. I only needed to take 1 semester of it in at university and they failed to cover practical applications of them.

How do I fix this? If I force set the scale to 1, the resulting decal is still not properly rotated.

The code was taken from this.

I’ve tried using look_at() but it also doesn’t produce the intended behavior.

Solution:

func align_up(node_basis, normal): #use: node3d.global_transform.basis = Helper.align_up(node3d.global_transform.basis, normal)
	node_basis.y = normal
	var potential_z = -node_basis.x.cross(normal)
	var potential_x = -node_basis.z.cross(normal)
	if potential_z.length() > potential_x.length():
		node_basis.x = potential_z
	else:
		node_basis.x = potential_x
	node_basis.z = node_basis.x.cross(node_basis.y)
	node_basis = node_basis.orthonormalized()
	
	return node_basis
1 Like

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.