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

CSGMesh GetAabb() / GetTransformedAabb() returns zero #38273

Open
chhjel opened this issue Apr 27, 2020 · 3 comments
Open

CSGMesh GetAabb() / GetTransformedAabb() returns zero #38273

chhjel opened this issue Apr 27, 2020 · 3 comments

Comments

@chhjel
Copy link

chhjel commented Apr 27, 2020

Godot version:
3.2.2.beta1

OS/device including version:
Windows 10 Pro (10.0.18362)

Issue description:
CSGMesh methods GetAabb() and GetTransformedAabb() returns Vector3.Zero.
csgMesh.Mesh.GetAabb() seems to return the correct value.

Steps to reproduce:

  1. Create a CSGMesh node.
  2. Add any mesh to it.
  3. Call GetAabb() or GetTransformedAabb() on the CSGMesh node.
  4. Zero-value is returned from the methods.
@chhjel chhjel changed the title CSGMesh GetAabb returns zero CSGMesh GetAabb() / GetTransformedAabb() returns zero Apr 27, 2020
@madmiraal
Copy link
Contributor

madmiraal commented Apr 29, 2020

This happens with all CSG shapes, not just the CSGMesh.

Update: This appears to be a timing issue, because it only happens when calling the functions in _ready(). When calling them in the _process() loop, for example, they have been calculated and are available.

@mrushyendra
Copy link
Contributor

To calculate the AABB for a CSG Shape, it looks like _update_shape() needs to be called, which will in turn create the CSGBrush and calculate the bounding box.

if (n) {
AABB aabb;
for (int i = 0; i < n->faces.size(); i++) {
for (int j = 0; j < 3; j++) {
if (i == 0 && j == 0) {
aabb.position = n->faces[i].vertices[j];
} else {
aabb.expand_to(n->faces[i].vertices[j]);
}
}
}
node_aabb = aabb;
} else {

Currently, it seems like _update_shape() is not called in the CSG shape's constructor, which is why calling get_aabb() in _ready() returns a zero vector. I tried adding a call to update_shape() from the constructor, but this alone does not seem sufficient. This is because properties like the width, height, etc. are only updated afterwards, which means that the AABB calculated in _ready() is the default, and not the latest one updated with values from the editor.

@hoontee
Copy link
Contributor

hoontee commented Aug 7, 2020

CSGShapes update in a deferred call in order to prevent each individual child shape from updating upon entering the scene tree.
Since this is intentional, the only solution is to use call_deferred:

func _ready():
	call_deferred("deferred")

func deferred():
	print(csg_shape.get_aabb())

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

4 participants