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

Allow to use CSG without nodes #46258

Closed
wants to merge 1 commit into from

Conversation

trollodel
Copy link
Contributor

@trollodel trollodel commented Feb 20, 2021

Helps godotengine/godot-proposals#182, godotengine/godot-proposals#1920, #39706

This PR exposes to scripting a SurfaceTool like workflow for CSG, and migrate CSG nodes to use that workflow.
In particular, this PR exposes a bunch of CSG shapes (resources) similar to PrimitiveMesh, where each type holds a specific shape (Box, Cylinder, Sphere, ...); and a dedicated class for doing CSG operations called CSGTool.

Here is a GDScript example of CSGTool:

var ct := CSGTool.new()
var box := CSGBoxShape3D.new()
var box2 := CSGBoxShape3D.new()
box2.size = Vector3(1, 1, 2)
  
ct.add_primitive(box)
ct.add_primitive(box2, CSGTool.OPERATION_SUBTRACTION, Transform().translated(Vector3(0,0,1)))
mesh = ct.commit(null, true)

And the result:
image

Changes in the code side:

  • Move CSGBrush creation from CSG nodes to CSG shapes.
  • Cache the CSGBrush into the shape instance. The cache will be invalidated when at least one property changes. This should speed up CSG operation a bit.
  • Now CSGPrimitive3D subclasses holds a CSG shape, instead of storing fields directly in the class.
  • CSG operations in nodes are now done using CSGTool, that handle also mesh, collision shape, tangents, and AABB generation.
  • Make CSGTool::Operation compatible with CSGBrushOperation::Operation.
  • Move material and smooth_faces to CSGPrimitive3D.
  • Add aliases for CSGTool::Operation, CSGPrimitiveShape3D::Mode and CSGPrimitiveShape3D::PathRotation in the nodes counterparts for compat.
  • Use less memallocs for CSGBrush.

TODO:

  • Create a dedicated resource for CSGPolygon and expose CSGPolygon3D again.
  • Add documentation.
  • Make icons for CSG shapes (they are the same as the node ones, except that they are yellow)
  • Fill documentation (I'll do that at the end to write it only once).
  • Check if the rebase of Fix multiple issues with CSGPolygon #49313 was successful.

@Xrayez
Copy link
Contributor

Xrayez commented Feb 21, 2021

If I understand correctly, CSGTool added in this PR is the class responsible for core boolean functionality in 3D.

I've been working on implementing an equivalent feature in 2D in goostengine/goost#39. I've basically added PolyBoolean2D class and a bunch of nodes which use it (which looks more or less like suggested CSGTool), and handles polygon boolean operations in 2D, as any 2D shape can be represented via polygons alone.

Since all boolean operations on 2D shapes are done via polygons, it doesn't make much sense to create additional resources for those, because it's just easier to expose polygon generation methods for this (like GoostGeometry2D.cirlce()). But for 3D, I think this is justified.

So, I think it makes sense to expose CSG operations this way (or another), even though I haven't used CSG features in Godot (yet got greatly inspired by the existing node approach).

@trollodel
Copy link
Contributor Author

Currently, the PR doesn't break compat (except for enums, but I'll fix it).

I thought about restructuring CSG, too. But I prefer to keep CSG resources to be consistent with Mesh.

Since all boolean operations on 2D shapes are done via polygons

Sure, I can get rid of those resources and use meshes directly. The issues with this approach are:

  • Currently, Godot doesn't have CSGTorus and CSGPolygon Mesh equivalents (implementing those as mesh is not impossible, however).
  • It'll make per-face materials, which CSG supports under the hood, more difficult to handle, since the user needs to put every face in a dedicated mesh surface (and Mesh surface API are cumbersome to use right now).

I'll write a proposal where I'll explain better how I'd change CSG to make it friendlier with the current mesh-based procedural geometry.

@fire
Copy link
Member

fire commented Feb 24, 2021

I approve the idea of using CSG without nodes, haven't looked into the details of the pr yet, but wanted to say that.

@BastiaanOlij
Copy link
Contributor

Cool! I have to find some time to do a proper code review but I agree with the principle idea.

@aaronfranke
Copy link
Member

@trollodel The CI checks are failing because the documentation needs to be updated.

Add CSG shape resources.
Add CSGTool for CSG operations.
Make CSG nodes using CSG resources and CSGTool.
@trollodel
Copy link
Contributor Author

I don't need this PR anymore as I switched to another approach for procedural content. So, I don't work on this anymore.
If anyone wants, feel free to take over this PR. Note that this PR doesn't have an associated proposal, so it may be worth opening one based on the issues listed in the OP.

@trollodel trollodel closed this Jul 1, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants